void sort_scores(float *scores,int f,int l) { int i,last; if(f>=l) return; swap_scores(scores,f,(f+l)/2); last=f; for(i=f+1;i<=l;i++) { if(scores[i]>scores[f]) swap_scores(scores,++last,i); } swap_scores(scores,f,last); sort_scores(scores,f,last-1); sort_scores(scores,last+1,l); }
void set_hscore(int points) { clear_keybuf(); char name[10]; FONT *fonte = (FONT*)fnt_datafile[COMICSANS].dat; rect(screen, SCREEN_W/3.23, SCREEN_H/2.13, SCREEN_W/1.46, SCREEN_H/1.89, WHITE); /// Draws the rectangle for the user input the name textout_ex(screen, fonte, "Your name: ", SCREEN_W/6.3, SCREEN_H/2.1, WHITE, -1); read_string(name, SCREEN_W/3, SCREEN_H/2, 10); /// get the player's name strcat(name, " "); ///Add some trash in the end of the name, so we can point it out later strcpy(score_table[10].player_name, name); score_table[10].player_score = points; /// Inserts the score in the last position of the SCORE_TABLE sort_scores(); /// Since we add another player in the last position of the score_table, we should sort it to get its proper position show_hscore_list(name); /// Show highscore list pointing out name return; }
//!Calculate the ending cell Viterbi scores void simpleTrellis::calcNthEndViterbi(size_t n) { size_t stateSize=hmm->state_size(); size_t seqSize=seq->getLength(); std::vector<scores>* temp_nth = new (std::nothrow) std::vector<scores>; //Calculate the ending viterbi scores for(size_t previousSt=0; previousSt<stateSize; previousSt++) { double trans = getEndingTransition(previousSt); double finalViterbiScore=trans+trell[seqSize-1][previousSt].viti; //std::cout << "Final Viterbi Score:" << finalViterbiScore << std::endl; if (finalViterbiScore>-INFINITY) { if (finalViterbiScore>ending.viti) { ending.viti=finalViterbiScore; ending.ptr=previousSt; } } //Calculate the nth viterbi scores for(size_t i = 0; i<trell[seqSize-1][previousSt].nth_viterbi_scores->size(); ++i) { scores temp_score; temp_score.viterbi_score = trans + (*trell[seqSize-1][previousSt].nth_viterbi_scores)[i].viterbi_score; temp_score.traceback_state = previousSt; temp_score.traceback_state_score = i; temp_nth->push_back(temp_score); //std::cout << "Temp Score:" << temp_score.viterbi_score << std::endl; } } //Sort the top scores sort_scores(*temp_nth); //Resize the list to nth top if (temp_nth->size()>n) { temp_nth->resize(n); } //Assign the Nth to ending cell ending.nth_viterbi_scores = temp_nth; return; }
static void calc_median_limits(float *scores,sint n,float *ll, float *ul) { sint i; float t,q1,q3; float *sortedscores; if(n==0) { (*ul)=(*ll)=0; } else { sortedscores=ckalloc((n+1)*sizeof(float)); for (i=0;i<n;i++) sortedscores[i]=scores[i]; sort_scores(sortedscores,0,n-1); t = n/4.0 + 0.5; if(t - (int)t == 0.5) { q3=(sortedscores[(int)t]+sortedscores[(int)t+1])/2.0; q1=(sortedscores[n-(int)t]+sortedscores[n-(int)t-1])/2.0; } else if(t - (int)t > 0.5) { q3=sortedscores[(int)t+1]; q1=sortedscores[n-(int)t-1]; } else { q3=sortedscores[(int)t]; q1=sortedscores[n-(int)t]; } if (n<4) { (*ul)=sortedscores[0]; (*ll)=sortedscores[n-1]; } else { (*ul)=q3+(q3-q1)/2.0; (*ll)=q1-(q3-q1); } } ckfree(sortedscores); }
int main (int argc, char **argv) { int c; bool running_suid; void *lockstate; char *scorefile; char *nl; const char *prefix, *user_prefix = NULL; struct stat buf; struct score_entry *scores; struct score_entry newscore; bool reverse = false; ptrdiff_t scorecount, scorealloc; ptrdiff_t max_scores = MAX_SCORES; srand (time (0)); while ((c = getopt (argc, argv, "hrm:d:")) != -1) switch (c) { case 'h': usage (EXIT_SUCCESS); break; case 'd': user_prefix = optarg; break; case 'r': reverse = 1; break; case 'm': { intmax_t m = strtoimax (optarg, 0, 10); if (m < 0) usage (EXIT_FAILURE); max_scores = min (m, MAX_SCORES); } break; default: usage (EXIT_FAILURE); } if (argc - optind != 3) usage (EXIT_FAILURE); running_suid = (getuid () != geteuid ()); prefix = get_prefix (running_suid, user_prefix); scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2); if (!scorefile) lose_syserr ("Couldn't allocate score file"); strcpy (scorefile, prefix); strcat (scorefile, "/"); strcat (scorefile, argv[optind]); newscore.score = strtoimax (argv[optind + 1], 0, 10); newscore.data = argv[optind + 2]; if (strlen (newscore.data) > MAX_DATA_LEN) newscore.data[MAX_DATA_LEN] = '\0'; nl = strchr (newscore.data, '\n'); if (nl) *nl = '\0'; newscore.username = get_user_id (); if (! newscore.username) lose_syserr ("Couldn't determine user id"); if (stat (scorefile, &buf) < 0) lose_syserr ("Failed to access scores file"); if (lock_file (scorefile, &lockstate) < 0) lose_syserr ("Failed to lock scores file"); if (read_scores (scorefile, &scores, &scorecount, &scorealloc) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to read scores file"); } if (push_score (&scores, &scorecount, &scorealloc, &newscore) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to add score"); } sort_scores (scores, scorecount, reverse); /* Limit the number of scores. If we're using reverse sorting, then also increment the beginning of the array, to skip over the *smallest* scores. Otherwise, just decrementing the number of scores suffices, since the smallest is at the end. */ if (scorecount > max_scores) { if (reverse) scores += scorecount - max_scores; scorecount = max_scores; } if (write_scores (scorefile, scores, scorecount) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to write scores file"); } if (unlock_file (scorefile, lockstate) < 0) lose_syserr ("Failed to unlock scores file"); exit (EXIT_SUCCESS); }
//!Calculate viterbi score for give cell from previous cell void simpleTrellis::calcNthViterbi(size_t n) { //For all the possible states at this position for(currentStatesIterator = currentStates->begin(); currentStatesIterator!=currentStates->end(); currentStatesIterator++) { //Get pointer to current state currentStatePtr = (*currentStatesIterator); currentState = currentStatePtr->getIterator(); //Calculate emission for the current cell double emm; //If emission is not yet calculated for the cell if (!trell[sequencePosition][currentState].emmCalculated) { //Calculate emission emm=currentStatePtr->get_emission(*seq, sequencePosition); //Check for external definition if (externalDefinitionSet) { emm+=seq->getWeight(sequencePosition, currentState); } //Store emission value in cell trell[sequencePosition][currentState].emmCalculated=true; trell[sequencePosition][currentState].emm=emm; } else { //emm was previously calculated //Get previously calculated emm value; emm = trell[sequencePosition][currentState].emm; } std::vector<scores>* temp_scores = new std::vector<scores>; //Store all nth_viterbi scores for current cell std::vector<state*>::iterator it; //Get the possible transition to this state for(it=currentStatePtr->getFromBegin(); it!=currentStatePtr->getFromEnd(); it++) { //Get pointer to previous state previousStatePtr=(*it); previousState=previousStatePtr->getIterator(); //Only need to calculate transition if the viterbi > -INFINITY if (trell[sequencePosition-1][previousState].viti!=-INFINITY) { //Calculate transition double trans; //If transition is already calculated if (trell[sequencePosition][currentState].trans.count(previousStatePtr)) { trans = trell[sequencePosition][currentState].trans[previousStatePtr]; } else { trans = getTransition(); trell[sequencePosition][currentState].trans[previousStatePtr]=trans; } #ifdef VERBOSE std::cout << "Position:\t" << sequencePosition <<std::endl; std::cout << "State:\t" << currentState <<std::endl; std::cout << "Previous State:\t"<<previousState << std::endl; std::cout << "Transition:\t" << trans <<std::endl; std::cout << "Emission:\t" << emm << std::endl; std::cout <<std::endl; #endif //Calculate viterbi value for cell double viterbiValue = emm + trans + trell[sequencePosition-1][previousState].viti; //If current viterbi value > -Infinity then it's possible to transition from this state to next //Set the next possible states if(viterbiValue>-INFINITY) { _nextStates->insert(currentStatePtr->getToBegin(),currentStatePtr->getToEnd()); setViterbi(viterbiValue); } for(size_t nth_iterator=0; nth_iterator<trell[sequencePosition-1][previousState].nth_viterbi_scores->size(); ++nth_iterator) { scores tmp; tmp.viterbi_score = emm + trans + (*trell[sequencePosition-1][previousState].nth_viterbi_scores)[nth_iterator].viterbi_score; tmp.traceback_state = previousState; tmp.traceback_state_score = nth_iterator; temp_scores->push_back(tmp); } } } //Sort scores and assign to current cell sort_scores(*temp_scores); if (temp_scores->size()>n) { temp_scores->resize(n); } trell[sequencePosition][currentState].nth_viterbi_scores = temp_scores; } return; }
int main (int argc, char **argv) { int c; bool running_suid, running_sgid; void *lockstate; char *scorefile; char *end, *nl, *user, *data; const char *prefix, *user_prefix = NULL; struct score_entry *scores; struct score_entry newscore; bool reverse = false; ptrdiff_t scorecount, scorealloc; ptrdiff_t max_scores = MAX_SCORES; srand (time (0)); while ((c = getopt (argc, argv, "hrm:d:")) != -1) switch (c) { case 'h': usage (EXIT_SUCCESS); break; case 'd': user_prefix = optarg; break; case 'r': reverse = 1; break; case 'm': { intmax_t m = strtoimax (optarg, &end, 10); if (optarg == end || *end || m < 0) usage (EXIT_FAILURE); max_scores = min (m, MAX_SCORES); } break; default: usage (EXIT_FAILURE); } if (argc - optind != 3) usage (EXIT_FAILURE); running_suid = (getuid () != geteuid ()); running_sgid = (getgid () != getegid ()); if (running_suid && running_sgid) lose ("This program can run either suid or sgid, but not both."); prefix = get_prefix (running_suid || running_sgid, user_prefix); scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2); if (!scorefile) lose_syserr ("Couldn't allocate score file"); char *z = stpcpy (scorefile, prefix); *z++ = '/'; strcpy (z, argv[optind]); newscore.score = normalize_integer (argv[optind + 1]); if (! newscore.score) { fprintf (stderr, "%s: Invalid score\n", argv[optind + 1]); return EXIT_FAILURE; } user = get_user_id (); if (! user) lose_syserr ("Couldn't determine user id"); data = argv[optind + 2]; if (strlen (data) > MAX_DATA_LEN) data[MAX_DATA_LEN] = '\0'; nl = strchr (data, '\n'); if (nl) *nl = '\0'; newscore.user_data = malloc (strlen (user) + 1 + strlen (data) + 1); if (! newscore.user_data || sprintf (newscore.user_data, "%s %s", user, data) < 0) lose_syserr ("Memory exhausted"); if (lock_file (scorefile, &lockstate) < 0) lose_syserr ("Failed to lock scores file"); if (read_scores (scorefile, &scores, &scorecount, &scorealloc) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to read scores file"); } if (push_score (&scores, &scorecount, &scorealloc, &newscore) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to add score"); } sort_scores (scores, scorecount, reverse); /* Limit the number of scores. If we're using reverse sorting, then also increment the beginning of the array, to skip over the *smallest* scores. Otherwise, just decrementing the number of scores suffices, since the smallest is at the end. */ if (scorecount > max_scores) { if (reverse) scores += scorecount - max_scores; scorecount = max_scores; } if (write_scores (scorefile, running_sgid ? 0664 : 0644, scores, scorecount) < 0) { unlock_file (scorefile, lockstate); lose_syserr ("Failed to write scores file"); } if (unlock_file (scorefile, lockstate) < 0) lose_syserr ("Failed to unlock scores file"); exit (EXIT_SUCCESS); }
int main(int argc, char **argv) { FILE *ofd,*ifd; ALN mult_aln; OPT opt; char infile[FILENAMELEN+1]; char outfile[FILENAMELEN+1]; int nseqs; int i,j,l,n,ires; int err,ix,ntot; float min_nn,nn; float tmp; Boolean eof,found; if(argc!=3 && argc!=7 && argc!=8) { usage(argv[0]); return 0; } strcpy(infile,argv[1]); strcpy(outfile,argv[2]); /* open the matrix file */ verbose=FALSE; if(argc==3) { get_default_matrix(); go=0.0; ge=0.1; egap=0.0; } else { if(argc==8) verbose=TRUE; if((ifd=fopen(argv[3],"r"))==NULL) { fprintf(stderr,"Cannot open matrix file [%s]",argv[3]); return 0; } err=readmatrix(ifd); if(err<=0) { fprintf(stderr,"Error: bad matrix in %s\n",argv[3]); return 0; } go=atof(argv[4]); ge=atof(argv[5]); egap=atof(argv[6]); } init_options(&opt); (*opt.alnout_opt).output_clustal=FALSE; (*opt.alnout_opt).output_relacs=TRUE; /* read in the sequences */ seq_input(infile,opt.explicit_type,FALSE,&mult_aln); if(mult_aln.nseqs<=0) { error("No sequences in %s\n",infile); exit(1); } nseqs=mult_aln.nseqs; /* remove the gaps */ seqlength=0; useqlen_array=(int *)ckalloc((nseqs+1)*sizeof(int)); for(i=0;i<nseqs;i++) { if(mult_aln.seqs[i].len>seqlength) seqlength=mult_aln.seqs[i].len; l=0; for(j=0;j<mult_aln.seqs[i].len;j++) if(isalpha(mult_aln.seqs[i].data[j])) { l++; } useqlen_array[i]=l; } maxlen=0; for(i=0;i<nseqs;i++) if(useqlen_array[i]>maxlen) maxlen=useqlen_array[i]; minlen=10000; for(i=0;i<nseqs;i++) if(useqlen_array[i]<minlen) minlen=useqlen_array[i]; /* remove any column score data that already exists in the input file */ /*if (mult_aln.ncol_scores==1) ckfree(mult_aln.col_score[0].data);*/ ix=mult_aln.ncol_scores; mult_aln.col_score[ix].data=(sint *)ckalloc((seqlength+1)*sizeof(sint)); mult_aln.ncol_scores=ix+1; /* calculate some simple statistics */ pcid=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) pcid[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); for(i=0;i<nseqs;i++) { for(j=i+1;j<nseqs;j++) { pcid[j][i]=pcid[i][j]=pcidentity(mult_aln,i,j); } } /* find the nearest neighbor for each sequence */ min_nn=1.0; for(i=0;i<nseqs;i++) { nn=0.0; for(j=0;j<nseqs;j++) { if(i!=j && pcid[i][j]>nn) nn=pcid[i][j]; } if(nn<min_nn) min_nn=nn; } seqweight=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) seqweight[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); for(i=0;i<nseqs;i++) for(j=i;j<nseqs;j++) { seqweight[j][i]=seqweight[i][j]=1.0-pcid[i][j]; } fragment=(Boolean *)ckalloc((nseqs+1)*sizeof(Boolean)); /* calculate pairwise alignment scores using k-tuple scores */ qpw_id=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) qpw_id[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); for(i=0,n=0;i<nseqs;i++) for(j=i+1;j<nseqs;j++) { qpw_id[i][j]=100.0*pcid[i][j]; if(qpw_id[i][j]<60) { qpw_id[i][j]=show_pair(mult_aln,i,j); } if(qpw_id[i][j]>40) { tmp=(float)useqlen_array[i]/(float)useqlen_array[j]; if(tmp<0.8) fragment[i]=TRUE; else if(tmp>1.25) fragment[j]=TRUE; } n++; } /*if(verbose) for(i=0;i<nseqs;i++) if(fragment[i]) fprintf(stdout,"%s fragment %s\n",argv[1],names[i]);*/ /* calculate sequence groups and keep first sequence in each group for processing */ use_seq=(int *)ckalloc((nseqs+1)*sizeof(int)); for(i=0;i<nseqs;i++) use_seq[i]=2; query=0; seqgroup=(int *)ckalloc((nseqs+1)*sizeof(int)); groupseed=(int *)ckalloc((nseqs+1)*sizeof(int)); calc_groups(query,0.7,nseqs,pcid,seqgroup,groupseed); if(ngroups<=0) { fprintf(stderr,"Error: problem with sequence grouping\n"); exit(1); } for(j=0;j<nseqs;j++) use_seq[j]=(-1); for(i=0;i<ngroups;i++) { j=groupseed[i]; use_seq[j]=2; } for(i=0;i<nseqs;i++) ckfree(pcid[i]); ckfree(pcid); ckfree(groupseed); ckfree(seqgroup); qpw=(float *)ckalloc((ngroups*ngroups+1)*sizeof(float)); for(i=0,n=0;i<nseqs;i++) if(use_seq[i]>1) for(j=i+1;j<nseqs;j++) if(use_seq[j]>1) qpw[n++]=qpw_id[i][j]; for(i=0;i<nseqs;i++) ckfree(qpw_id[i]); ckfree(qpw_id); /* sort the pairwise k-tuple scores into ascending order */ sort_scores(qpw,0,n-1); /* calculate the scores for the gaps */ gop=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) gop[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); gep=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) gep[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); egp=(float **)ckalloc((nseqs+1)*sizeof(float *)); for(i=0;i<nseqs;i++) egp[i]=(float *)ckalloc((nseqs+1)*sizeof(float)); for(i=0;i<nseqs;i++) if(use_seq[i]>1) for(j=i+1;j<nseqs;j++) if(use_seq[j]>1) score_gaps(mult_aln,i,j); calc_md(&mult_aln,ix); for(i=0,ntot=0;i<nseqs;i++) if(use_seq[i]>1) { ntot++; } tmp=set_mdcutoff(ntot,q1); normd_rs/=tmp; tmp=1.0; norm_md/=tmp; mult_aln.alnscore=norm_md; mult_aln.validalnscore=TRUE; if(!verbose) { fprintf(stdout,"%s\t%.3f\n",argv[1],norm_md); /*fprintf(stdout,"%.3f\n",norm_md);*/ } else { /*fprintf(stdout,"%s %.3f %.3f %.3f %.3f %.3f %.3f %d %d %d\n", argv[1],norm_md,normd_rs,col,max_colscore,gap_extscore*0.1,q1,nseqs,minlen,maxlen);*/ fprintf(stdout,"FILE %s\n",argv[1]); fprintf(stdout,"norMD %.3f\n",norm_md); /*fprintf(stdout,"norMD_of %.3f\n",norm_md); fprintf(stdout,"norMD_rs %.3f\n",normd_rs);*/ fprintf(stdout,"NSEQS %d\n",nseqs); fprintf(stdout,"MD %.3f\n",col); fprintf(stdout,"maxMD %.3f\n",max_colscore); fprintf(stdout,"GOP %.3f\n",gap_openscore); fprintf(stdout,"GEP %.3f\n",gap_extscore); fprintf(stdout,"LQR %.3f\n",q1); } for(i=0;i<nseqs;i++) ckfree(gop[i]); ckfree(gop); for(i=0;i<nseqs;i++) ckfree(gep[i]); ckfree(gep); for(i=0;i<nseqs;i++) ckfree(egp[i]); ckfree(egp); for(i=0;i<nseqs;i++) ckfree(seqweight[i]); ckfree(seqweight); ckfree(qpw); ckfree(use_seq); ckfree(fragment); ckfree(useqlen_array); /* write out the sequences */ strcpy(opt.alnout_opt->relacs_outname,outfile); if(!open_alignment_output(outfile,opt.alnout_opt)) exit(1); create_alignment_output(mult_aln,*opt.alnout_opt); return 0; }