PUBLIC int move_standard(char *seq, char *struc, enum MOVE_TYPE type, int verbosity_level, int shifts, int noLP){ make_pair_matrix(); short int *s0 = encode_sequence(seq, 0); short int *s1 = encode_sequence(seq, 1); short int *str = make_pair_table(struc); int energy = 0; switch (type){ case GRADIENT: energy = move_gradient(seq, str, s0, s1, verbosity_level, shifts, noLP); break; case FIRST: energy = move_first(seq, str, s0, s1, verbosity_level, shifts, noLP); break; case ADAPTIVE: energy = move_adaptive(seq, str, s0, s1, verbosity_level); break; } int i=1; for (; i<=str[0]; i++) { if (str[i]==0) struc[i-1]='.'; else if (str[i]>str[str[i]]) struc[i-1]='('; else struc[i-1]=')'; } free(s0); free(s1); free(str); return energy; }
bool is_legal_pair_content( char* seq, char* secstr, int ip ) { if( cnf->max_solid_GCs <= 0 ) return true; char* mark = strdup( secstr ); short* pt = make_pair_table( secstr ); int o; for( o = 1; o <= strlen( cnf->hairpin_ss ); o++ ) { int i = ip + o; if( pt[i] < i ) continue; mark[i-1] = ((seq[i-1] ^ seq[pt[i]-1] ^ 'G' ^ 'C')==0)? 'X' : '-'; } bool ok = true; char* p = mark; while( (*p) && ok ) { p += strcspn( p, "X" ); if( *p ) { int n = strspn( p, "X" ); if( n > cnf->max_solid_GCs ) ok = false; p += n; } } free( pt ); free( mark ); return ok; }
int main (int argc, char *argv[]) { char *line, *sequence, *structure; int i, j, length; float energy; short *ptable; char *pair_seen; int *iindx; if (argc>1) help(); line = get_line (stdin); if (*line=='>') { free (line); line = get_line (stdin); } /* read the sequence */ sequence = (char *) space (sizeof (char) * (strlen(line)+1)); (void) sscanf(line, "%s", sequence); free (line); length = (int) strlen(sequence); iindx = (int *) space(sizeof(int)*(length+1)); for (i=1; i<=length; i++) iindx[i] = ((length+1-i)*(length-i))/2 +length+1; pair_seen = (char *) space(((length+1)*(length+2))/2 * sizeof(char)); structure = (char *) space (sizeof (char) * (length+1)); /* get of suboptimal structures */ while ((line = get_line (stdin))) { int r, popt; r = sscanf(line, "%s %f", structure, &energy); free(line); if (r==0) continue; ptable = make_pair_table(structure); popt = 0; for (i = 0; i < length; i++) { if ((j=ptable[i])>i) if (pair_seen[iindx[i]-j]==0) { pair_seen[iindx[i]-j]=(char) 1; if (popt==0) { printf("%s %6.2f", structure, energy); popt = 1; } printf(" (%d,%d)", i,j); } } if (popt==1) printf("\n"); free(ptable); } free(sequence); free(structure); free(iindx); free(pair_seen); return 0; }
PRIVATE void print_aliout(char **AS, plist *pl, double threshold, int n_seq, char * mfe, FILE *aliout) { int i, j, k, n, num_p=0, max_p = 64; pair_info *pi; double *duck, p; short *ptable; for (n=0; pl[n].i>0; n++); max_p = 64; pi = space(max_p*sizeof(pair_info)); duck = (double *) space((strlen(mfe)+1)*sizeof(double)); ptable = make_pair_table(mfe); for (k=0; k<n; k++) { int s, type; p = pl[k].p; i=pl[k].i; j = pl[k].j; duck[i] -= p * log(p); duck[j] -= p * log(p); if (p<threshold) continue; pi[num_p].i = i; pi[num_p].j = j; pi[num_p].p = p; pi[num_p].ent = duck[i]+duck[j]-p*log(p); for (type=0; type<8; type++) pi[num_p].bp[type]=0; for (s=0; s<n_seq; s++) { int a,b; a=encode_char(toupper(AS[s][i-1])); b=encode_char(toupper(AS[s][j-1])); type = pair[a][b]; if ((AS[s][i-1] == '-')||(AS[s][j-1] == '-')) type = 7; if ((AS[s][i-1] == '~')||(AS[s][j-1] == '~')) type = 7; pi[num_p].bp[type]++; pi[num_p].comp = (ptable[i] == j) ? 1:0; } num_p++; if (num_p>=max_p) { max_p *= 2; pi = xrealloc(pi, max_p * sizeof(pair_info)); } } free(duck); pi[num_p].i=0; qsort(pi, num_p, sizeof(pair_info), compare_pair_info ); /* print it */ fprintf(aliout, "%d sequence; length of alignment %d\n", n_seq, (int) strlen(AS[0])); fprintf(aliout, "alifold output\n"); for (k=0; pi[k].i>0; k++) { pi[k].comp = (ptable[pi[k].i] == pi[k].j) ? 1:0; print_pi(pi[k], aliout); } fprintf(aliout, "%s\n", mfe); free(ptable); free(pi); }
PUBLIC int bp_distance(const char *str1, const char *str2) { /* dist = {number of base pairs in one structure but not in the other} */ /* same as edit distance with pair_open pair_close as move set */ int dist; short i,l; short *t1, *t2; dist = 0; t1 = make_pair_table(str1); t2 = make_pair_table(str2); l = (t1[0]<t2[0])?t1[0]:t2[0]; /* minimum of the two lengths */ for (i=1; i<=l; i++) if (t1[i]!=t2[i]) { if (t1[i]>i) dist++; if (t2[i]>i) dist++; } free(t1); free(t2); return dist; }
PRIVATE char *annote(const char *structure, const char *AS[]) { char *ps; int i, n, s, maxl; short *ptable; make_pair_matrix(); n = strlen(AS[0]); maxl = 1024; ps = (char *) space(maxl); ptable = make_pair_table(structure); for (i=1; i<=n; i++) { char pps[64], ci='\0', cj='\0'; int j, type, pfreq[8] = {0,0,0,0,0,0,0,0}, vi=0, vj=0; if ((j=ptable[i])<i) continue; for (s=0; AS[s]!=NULL; s++) { type = pair[encode_char(AS[s][i-1])][encode_char(AS[s][j-1])]; pfreq[type]++; if (type) { if (AS[s][i-1] != ci) { ci = AS[s][i-1]; vi++;} if (AS[s][j-1] != cj) { cj = AS[s][j-1]; vj++;} } } if (maxl - strlen(ps) < 128) { maxl *= 2; ps = realloc(ps, maxl); if (ps==NULL) nrerror("out of memory in realloc"); } if (pfreq[0]>0) { snprintf(pps, 64, "%d %d %d gmark\n", i, j, pfreq[0]); strcat(ps, pps); } if (vi>1) { snprintf(pps, 64, "%d cmark\n", i); strcat(ps, pps); } if (vj>1) { snprintf(pps, 64, "%d cmark\n", j); strcat(ps, pps); } } free(ptable); return ps; }
/*---------------------------------------------------------------------------*/ PUBLIC int xrna_plot(char *string, char *structure, char *ssfile) { /* produce input for XRNA RNA drawing program */ FILE *ss_file; int i; int length; short *pair_table; float *X, *Y; ss_file = fopen(ssfile, "w"); if (ss_file == NULL) { fprintf(stderr, "can't open file %s - not doing xy_plot\n", ssfile); return 0; } length = strlen(string); pair_table = make_pair_table(structure); /* make coordinates */ X = (float *) space((length+1)*sizeof(float)); Y = (float *) space((length+1)*sizeof(float)); if (rna_plot_type == 0) i = simple_xy_coordinates(pair_table, X, Y); else i = naview_xy_coordinates(pair_table, X, Y); if (i!=length) fprintf(stderr,"strange things happening in xrna_plot...\n"); fprintf(ss_file, "# Vienna RNA Package %s, XRNA output\n" "# CreationDate: %s\n" "# Options: %s\n", VERSION, time_stamp(), option_string()); for (i=1; i<=length; i++) /* XRNA likes to have coordinate mirrored, so we use (-X, Y) */ fprintf(ss_file, "%d %c %6.2f %6.2f %d %d\n", i, string[i-1], -X[i-1], Y[i-1], (pair_table[i]?1:0), pair_table[i]); fclose(ss_file); free(pair_table); free(X); free(Y); return 1; /* success */ }
PRIVATE struct plist *b2plist(const char *struc) { /* convert bracket string to plist */ short *pt; struct plist *pl; int i,k=0; pt = make_pair_table(struc); pl = (struct plist *)space(strlen(struc)/2*sizeof(struct plist)); for (i=1; i<strlen(struc); i++) { if (pt[i]>i) { pl[k].i = i; pl[k].j = pt[i]; pl[k++].p = 0.95*0.95; } } free(pt); pl[k].i=0; pl[k].j=0; pl[k++].p=0.; return pl; }
PUBLIC int browse_neighs(char *seq, char *struc, int verbosity_level, int shifts, int noLP, int (*funct) (struct_en*, struct_en*)){ make_pair_matrix(); short int *s0 = encode_sequence(seq, 0); short int *s1 = encode_sequence(seq, 1); short int *str = make_pair_table(struc); int res = browse_neighs_pt(seq, str, s0, s1, verbosity_level, shifts, noLP, funct); free(s0); free(s1); free(str); return res; }
PUBLIC int gmlRNA(char *string, char *structure, char *ssfile, char option) { FILE *gmlfile; int i; int length; int labels=0; short *pair_table; float *X, *Y; if (isupper(option)) labels = 1; gmlfile = fopen(ssfile, "w"); if (gmlfile == NULL) { fprintf(stderr, "can't open file %s - not doing xy_plot\n", ssfile); return 0; } length = strlen(string); pair_table = make_pair_table(structure); switch(option){ case 'X' : case 'x' : /* Simple XY Plot */ X = (float *) space((length+1)*sizeof(float)); Y = (float *) space((length+1)*sizeof(float)); if (rna_plot_type == 0) i = simple_xy_coordinates(pair_table, X, Y); else i = naview_xy_coordinates(pair_table, X, Y); if(i!=length) fprintf(stderr,"strange things happening in gmlRNA ...\n"); break; default: /* No Graphics Information */ X = NULL; Y = NULL; } fprintf(gmlfile, "# Vienna RNA Package %s\n" "# GML Output\n" "# CreationDate: %s\n" "# Name: %s\n" "# Options: %s\n", VERSION, time_stamp(), ssfile, option_string()); fprintf(gmlfile, "graph [\n" " directed 0\n"); for (i=1; i<=length; i++){ fprintf(gmlfile, " node [ id %d ", i); if (option) fprintf(gmlfile, "label \"%c\"",string[i-1]); if ((option == 'X')||(option=='x')) fprintf(gmlfile, "\n graphics [ x %9.4f y %9.4f ]\n", X[i-1], Y[i-1]); fprintf(gmlfile," ]\n"); } for (i=1; i<length; i++) fprintf(gmlfile, "edge [ source %d target %d ]\n", i, i+1); for (i=1; i<=length; i++) { if (pair_table[i]>i) fprintf(gmlfile, "edge [ source %d target %d ]\n", i, pair_table[i]); } fprintf(gmlfile, "]\n"); fclose(gmlfile); free(pair_table); free(X); free(Y); return 1; /* success */ }
PUBLIC int ssv_rna_plot(char *string, char *structure, char *ssfile) { /* produce input for the SStructView java applet */ FILE *ssvfile; int i, bp; int length; short *pair_table; float *X, *Y; float xmin, xmax, ymin, ymax; ssvfile = fopen(ssfile, "w"); if (ssvfile == NULL) { fprintf(stderr, "can't open file %s - not doing xy_plot\n", ssfile); return 0; } length = strlen(string); pair_table = make_pair_table(structure); /* make coordinates */ X = (float *) space((length+1)*sizeof(float)); Y = (float *) space((length+1)*sizeof(float)); if (rna_plot_type == 0) i = simple_xy_coordinates(pair_table, X, Y); else i = naview_xy_coordinates(pair_table, X, Y); if (i!=length) fprintf(stderr,"strange things happening in ssv_rna_plot...\n"); /* make coords nonegative */ xmin = xmax = X[0]; ymin = ymax = Y[0]; for (i = 1; i < length; i++) { xmin = X[i] < xmin ? X[i] : xmin; xmax = X[i] > xmax ? X[i] : xmax; ymin = Y[i] < ymin ? Y[i] : ymin; ymax = Y[i] > ymax ? Y[i] : ymax; } if (xmin<1) { for (i = 0; i <= length; i++) X[i] -= xmin-1; xmin = 1; } if (ymin<1) { for (i = 0; i <= length; i++) Y[i] -= ymin-1; ymin = 1; } #if 0 { float size, xoff, yoff; float JSIZE = 500; /* size of the java applet window */ /* rescale coordinates, center on square of size HSIZE */ size = MAX((xmax-xmin),(ymax-ymin)); xoff = (size - xmax + xmin)/2; yoff = (size - ymax + ymin)/2; for (i = 0; i <= length; i++) { X[i] = (X[i]-xmin+xoff)*(JSIZE-10)/size + 5; Y[i] = (Y[i]-ymin+yoff)*(JSIZE-10)/size + 5; } } #endif /* */ fprintf(ssvfile, "# Vienna RNA Package %s\n" "# SStructView Output\n" "# CreationDate: %s\n" "# Name: %s\n" "# Options: %s\n", VERSION, time_stamp(), ssfile, option_string()); for (i=1; i<=length; i++) fprintf(ssvfile, "BASE\t%d\t%c\t%d\t%d\n", i, string[i-1], (int) (X[i-1]+0.5), (int) (Y[i-1]+0.5)); for (bp=1, i=1; i<=length; i++) if (pair_table[i]>i) fprintf(ssvfile, "BASE-PAIR\tbp%d\t%d\t%d\n", bp++, i, pair_table[i]); fclose(ssvfile); free(pair_table); free(X); free(Y); return 1; /* success */ }
int svg_rna_plot(char *string, char *structure, char *ssfile) { float xmin, xmax, ymin, ymax, size; int i, length; float *X, *Y; FILE *xyplot; short *pair_table; length = strlen(string); xyplot = fopen(ssfile, "w"); if (xyplot == NULL) { fprintf(stderr, "can't open file %s - not doing xy_plot\n", ssfile); return 0; } pair_table = make_pair_table(structure); X = (float *) space((length+1)*sizeof(float)); Y = (float *) space((length+1)*sizeof(float)); if (rna_plot_type == 0) i = simple_xy_coordinates(pair_table, X, Y); else i = naview_xy_coordinates(pair_table, X, Y); if(i!=length) fprintf(stderr,"strange things happening in PS_rna_plot...\n"); xmin = xmax = X[0]; ymin = ymax = Y[0]; for (i = 1; i < length; i++) { xmin = X[i] < xmin ? X[i] : xmin; xmax = X[i] > xmax ? X[i] : xmax; ymin = Y[i] < ymin ? Y[i] : ymin; ymax = Y[i] > ymax ? Y[i] : ymax; } for (i = 0; i < length; i++) Y[i] = ymin+ymax - Y[i]; /* mirror coordinates so they look as in PS */ size = MAX((xmax-xmin),(ymax-ymin)); size += 10; /* add some so the bounding box isn't too tight */ fprintf(xyplot, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"452\" width=\"452\">\n"); fprintf(xyplot, "<script type=\"text/ecmascript\">\n" " <![CDATA[\n" " var shown = 1;\n" " function click() {\n" " var seq = document.getElementById(\"seq\");\n" " if (shown==1) {\n" " seq.setAttribute(\"style\", \"visibility: hidden\");\n" " shown = 0;\n" " } else {\n" " seq.setAttribute(\"style\", \"visibility: visible\");\n" " shown = 1;\n" " }\n" " }\n" " ]]>\n" "</script>\n"); fprintf(xyplot, " <rect style=\"stroke: white; fill: white\" height=\"452\" x=\"0\" y=\"0\" width=\"452\" onclick=\"click(evt)\" />\n" " <g transform=\"scale(%7f,%7f) translate(%7f,%7f)\">\n", SIZE/size, SIZE/size, (size-xmin-xmax)/2, (size-ymin-ymax)/2); fprintf(xyplot, " <polyline style=\"stroke: black; fill: none; stroke-width: 1.5\" id=\"outline\" points=\"\n"); for (i = 0; i < length; i++) fprintf(xyplot, " %3.3f,%3.3f\n", X[i], Y[i]); fprintf(xyplot," \" />\n"); fprintf(xyplot," <g style=\"stroke: black; stroke-width: 1\" id=\"pairs\">\n"); for (i = 1; i <= length; i++) { int j; if ((j=pair_table[i])>i) fprintf(xyplot, " <line id=\"%d,%d\" x1=\"%6.3f\" y1=\"%6.3f\" x2=\"%6.3f\" y2=\"%6.3f\" />\n", i,j, X[i-1], Y[i-1], X[j-1], Y[j-1]); } fprintf(xyplot, " </g>\n"); fprintf(xyplot, " <g style=\"font-family: SansSerif\" transform=\"translate(-4.6, 4)\" id=\"seq\">\n"); for (i = 0; i < length; i++) fprintf(xyplot, " <text x=\"%.3f\" y=\"%.3f\">%c</text>\n", X[i], Y[i], string[i]); fprintf(xyplot, " </g>\n"); fprintf(xyplot, " </g>\n"); fprintf(xyplot, "</svg>\n"); fclose(xyplot); free(pair_table); free(X); free(Y); return 1; /* success */ }
int PS_rna_plot_a(char *string, char *structure, char *ssfile, char *pre, char *post) { float xmin, xmax, ymin, ymax, size; int i, length; float *X, *Y; FILE *xyplot; short *pair_table; char *c; length = strlen(string); xyplot = fopen(ssfile, "w"); if (xyplot == NULL) { fprintf(stderr, "can't open file %s - not doing xy_plot\n", ssfile); return 0; } pair_table = make_pair_table(structure); X = (float *) space((length+1)*sizeof(float)); Y = (float *) space((length+1)*sizeof(float)); if (rna_plot_type == 0) i = simple_xy_coordinates(pair_table, X, Y); else i = naview_xy_coordinates(pair_table, X, Y); if(i!=length) fprintf(stderr,"strange things happening in PS_rna_plot...\n"); xmin = xmax = X[0]; ymin = ymax = Y[0]; for (i = 1; i < length; i++) { xmin = X[i] < xmin ? X[i] : xmin; xmax = X[i] > xmax ? X[i] : xmax; ymin = Y[i] < ymin ? Y[i] : ymin; ymax = Y[i] > ymax ? Y[i] : ymax; } size = MAX((xmax-xmin),(ymax-ymin)); fprintf(xyplot, "%%!PS-Adobe-3.0 EPSF-3.0\n" "%%%%Creator: %s, ViennaRNA-%s\n" "%%%%CreationDate: %s" "%%%%Title: RNA Secondary Structure Plot\n" "%%%%BoundingBox: 66 210 518 662\n" "%%%%DocumentFonts: Helvetica\n" "%%%%Pages: 1\n" "%%%%EndComments\n\n" "%%Options: %s\n", rcsid+5, VERSION, time_stamp(), option_string()); fprintf(xyplot, "%% to switch off outline pairs of sequence comment or\n" "%% delete the appropriate line near the end of the file\n\n"); fprintf(xyplot, "%s", RNAss_head); if (pre || post) { fprintf(xyplot, "%s", anote_macros); } fprintf(xyplot, "%%%%EndProlog\n"); fprintf(xyplot, "RNAplot begin\n" "%% data start here\n"); if ((c = strchr(structure, '&'))) { int cutpoint; cutpoint = c - structure; string[cutpoint] = ' '; /* replace & with space */ fprintf(xyplot, "/cutpoint %d def\n", cutpoint); } /* sequence */ fprintf(xyplot,"/sequence (\\\n"); i=0; while (i<length) { fprintf(xyplot, "%.255s\\\n", string+i); /* no lines longer than 255 */ i+=255; } fprintf(xyplot,") def\n"); /* coordinates */ fprintf(xyplot, "/coor [\n"); for (i = 0; i < length; i++) fprintf(xyplot, "[%3.3f %3.3f]\n", X[i], Y[i]); fprintf(xyplot, "] def\n"); /* base pairs */ fprintf(xyplot, "/pairs [\n"); for (i = 1; i <= length; i++) if (pair_table[i]>i) fprintf(xyplot, "[%d %d]\n", i, pair_table[i]); fprintf(xyplot, "] def\n\n"); fprintf(xyplot, "init\n\n"); /* draw the data */ if (pre) { fprintf(xyplot, "%% Start Annotations\n"); fprintf(xyplot, "%s\n", pre); fprintf(xyplot, "%% End Annotations\n"); } fprintf(xyplot, "%% switch off outline pairs or bases by removing these lines\n" "drawoutline\n" "drawpairs\n" "drawbases\n"); if (post) { fprintf(xyplot, "%% Start Annotations\n"); fprintf(xyplot, "%s\n", post); fprintf(xyplot, "%% End Annotations\n"); } fprintf(xyplot, "%% show it\nshowpage\n"); fprintf(xyplot, "end\n"); fprintf(xyplot, "%%%%EOF\n"); fclose(xyplot); free(pair_table); free(X); free(Y); return 1; /* success */ }
void DSU::ComputeTBD(TBD &pqueue, int maxkeep, int num_threshold, bool outer, bool noLP, bool shifts, bool debug, vector<RNAsaddle> *output_saddles, int conn_neighs, bool no_new) { int cnt = 0; clock_t time_tbd = clock(); // go through all pairs in queue while (pqueue.size()>0) { // check time: double time_secs = ((clock() - time)/(double)CLOCKS_PER_SEC); if (stop_after && (time_secs > stop_after)) { fprintf(stderr, "Time threshold reached (%d secs.), processed %d/%d\n", stop_after, cnt, pqueue.size()+cnt); break; } // just visualisation if (!output_saddles && cnt%100==0) { double tim = (clock() - time_tbd)/(double)CLOCKS_PER_SEC; std::pair<int, int> mem = getValue(); //double one = ((sizeof(char)*strlen(seq) + sizeof(short)*strlen(seq)) + sizeof(RNAsaddle)) / 1024.0; fprintf(stderr, "Finding path: %7d/%7d; Time: %6.2f; Est.:%6.2f Mem.:%6.1fMB VM %6.1fMB PM\n", cnt, pqueue.size()+cnt, tim, tim/(double)cnt*pqueue.size(), mem.first/1024.0, mem.second/1024.0); } // apply threshold if (cnt>num_threshold) { fprintf(stderr, "Number threshold reached, processed %d/%d\n", cnt, pqueue.size()+cnt); break; } else { cnt++; } // get next TBDentry tbd = pqueue.get_first(); if (tbd.i==-1) { fprintf(stderr, "Ending the path-finding -- i = %5d ; j = %5d ; fiber = %c ; type = %s \n", tbd.i, tbd.j, tbd.fiber?'Y':'N', type1_str[tbd.type_clust]); break; } // check no-conn if (conectivity.size() > 0 && !tbd.fiber && conectivity.joint(tbd.i, tbd.j)) continue; // get path if (debug) fprintf(stderr, "path between (%3d, %3d) type=%s fiber=%d:\n", tbd.i, tbd.j, type1_str[tbd.type_clust], tbd.fiber); //2fprintf(stderr, "depth: %d\n%s\n%s\n%s\n", maxkeep, seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch); if (pknots) { path_pk *path = get_path_light_pk(seq, LM[tbd.i].structure, LM[tbd.j].structure, maxkeep); // variables for outer insertion double max_energy= -1e8; path_pk *max_path = path; // variables for inner loops and insertions // get the length of path for speed up int length = 0; for (path_pk *tmp = path; tmp && tmp->structure; tmp++) { if (max_path->en < tmp->en) max_path = tmp; length ++; } // create vector of known LM numbers on path (where 0 and length-1 are known) vector<int> lm_numbers(length, -1); lm_numbers[0] = tbd.i; lm_numbers[length-1] = tbd.j; // debug if (debug) { for (int i=0; i<length; i++) { fprintf(stderr, "path[%3d] %s %6.2f\n", i, pt_to_str_pk(path[i].structure).c_str(), path[i].en/100.0); } } // bisect the path and find new LMs: unsigned int old_size = LM.size(); FindNumbers(0, length-1, path, lm_numbers, shifts, noLP, debug, no_new); // if we have found new minima and we want to do more than simple reevaluation of path (--conn-neighs>0) if (LM.size() - old_size > 0 && conn_neighs > 0 && !no_new) { for (unsigned int j=old_size; j<LM.size(); j++) { // sort 'em according to Hamming D. and take first "conn_neighs" multimap<int, int> distances; for (unsigned int i=0; i<old_size; i++) { distances.insert(make_pair(HammingDist(LM[i].structure, LM[j].structure), i)); } int cnt = 0; int last_hd = -1; for (auto it=distances.begin(); it!=distances.end(); it++) { if (cnt > conn_neighs && last_hd != it->first) { break; } pqueue.insert(it->second, j, EXPERIM, false); cnt++; last_hd = it->first; } } } // debug if (debug) { int diff = 1; int last_num = lm_numbers[0]; for (int i=0; i<length; i++) { fprintf(stderr, "path[%3d]= %4d (%s %6.2f)\n", i, lm_numbers[i], pt_to_str_pk(path[i].structure).c_str(), path[i].en/100.0); if (lm_numbers[i]!=last_num && lm_numbers[i]!=-1) { diff++; last_num=lm_numbers[i]; } } histo[length][diff]++; histo[length][0]++; } // now process the array of found numbers: int last_num = lm_numbers[0]; for (int i=1; i<length; i++) { if (lm_numbers[i]!=-1 && lm_numbers[i]!=last_num) { // get the highest saddle in case we traveled through many "-1" saddles: int j=i-1; int highest_num = i; while (j>0) { // check if not higher saddle: if (path[highest_num].en < path[j].en) highest_num = j; // we found first that is not -1 if (lm_numbers[j]!=-1) break; j--; } // save saddle SDtype typ = (j==i-1?DIRECT:REDUCED); RNAsaddle saddle(last_num, lm_numbers[i], typ); saddle.energy = path[highest_num].en; saddle.str_ch = NULL; saddle.structure = allocopy(path[highest_num].structure); bool inserted = InsertUB(saddle, debug); // ??? if (output_saddles && inserted) { output_saddles->push_back(saddle); } // try to insert new things into TBD: if ((lm_numbers[i]!=lm_numbers[length-1] || lm_numbers[i-1]!=lm_numbers[0]) && !no_new) { // check no-conn if (conectivity.size() > 0) conectivity.union_set(tbd.i, tbd.j); pqueue.insert(lm_numbers[i-1], lm_numbers[i], NEW_FOUND, true); } last_num = lm_numbers[i]; } } // insert saddle between outer structures if (outer) { RNAsaddle tmp(tbd.i, tbd.j, NOT_SURE); tmp.energy = en_fltoi(max_energy); tmp.str_ch = NULL; tmp.structure = allocopy(max_path->structure); bool inserted = InsertUB(tmp, debug); if (output_saddles && inserted) { output_saddles->push_back(tmp); } } free_path_pk(path); } else { //fprintf(stderr, "%s\n%s\n%s\n", seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch); path_t *path = get_path(seq, LM[tbd.i].str_ch, LM[tbd.j].str_ch, maxkeep); // variables for outer insertion double max_energy= -1e8; path_t *max_path = path; // variables for inner loops and insertions // get the length of path for speed up int length = 0; for (path_t *tmp = path; tmp && tmp->s; tmp++) { if (max_path->en < tmp->en) max_path = tmp; length ++; } // create vector of known LM numbers on path (where 0 and length-1 are known) vector<int> lm_numbers(length, -1); lm_numbers[0] = tbd.i; lm_numbers[length-1] = tbd.j; // bisect the path and find new LMs: unsigned int old_size = LM.size(); FindNumbers(0, length-1, path, lm_numbers, shifts, noLP, debug, no_new); // if we have found new minima and we want to do more than simple reevaluation of path (--conn-neighs>0) if (LM.size() - old_size > 0 && conn_neighs > 0 && !no_new) { for (unsigned int j=old_size; j<LM.size(); j++) { // sort 'em according to Hamming D. and take first "conn_neighs" multimap<int, int> distances; for (unsigned int i=0; i<old_size; i++) { distances.insert(make_pair(HammingDist(LM[i].structure, LM[j].structure), i)); } int cnt = 0; int last_hd = -1; for (auto it=distances.begin(); it!=distances.end(); it++) { if (cnt > conn_neighs && last_hd != it->first) { break; } pqueue.insert(it->second, j, EXPERIM, false); cnt++; last_hd = it->first; } } } // debug if (debug) { int diff = 1; int last_num = lm_numbers[0]; for (int i=0; i<length; i++) { fprintf(stderr, "path[%3d]= %4d (%s %6.2f)\n", i, lm_numbers[i], path[i].s, path[i].en); if (lm_numbers[i]!=last_num && lm_numbers[i]!=-1) { diff++; last_num=lm_numbers[i]; } } histo[length][diff]++; histo[length][0]++; } // now process the array of found numbers: int last_num = lm_numbers[0]; for (int i=1; i<length; i++) { if (lm_numbers[i]!=-1 && lm_numbers[i]!=last_num) { // get the highest saddle in case we traveled through many "-1" saddles: int j=i-1; int highest_num = i; while (j>0) { // check if not higher saddle: if (path[highest_num].en < path[j].en) highest_num = j; // we found first that is not -1 if (lm_numbers[j]!=-1) break; j--; } // save saddle SDtype typ = (j==i-1?DIRECT:REDUCED); RNAsaddle saddle(last_num, lm_numbers[i], typ); saddle.energy = en_fltoi(path[highest_num].en); saddle.str_ch = NULL; saddle.structure = make_pair_table(path[highest_num].s); bool inserted = InsertUB(saddle, debug); // ??? if (output_saddles && inserted) { output_saddles->push_back(saddle); } // try to insert new things into TBD: if ((lm_numbers[i]!=lm_numbers[length-1] || lm_numbers[i-1]!=lm_numbers[0]) && !no_new) { // check no-conn if (conectivity.size() > 0) conectivity.union_set(tbd.i, tbd.j); pqueue.insert(lm_numbers[i-1], lm_numbers[i], NEW_FOUND, true); } last_num = lm_numbers[i]; } } // insert saddle between outer structures if (outer) { RNAsaddle tmp(tbd.i, tbd.j, NOT_SURE); tmp.energy = en_fltoi(max_energy); tmp.str_ch = NULL; tmp.structure = make_pair_table(max_path->s); bool inserted = InsertUB(tmp, debug); if (output_saddles && inserted) { output_saddles->push_back(tmp); } } free_path(path); } // free stuff //if (last_str) free(last_str); } // all doing while fprintf(stderr, "The end of finding paths(%d). Size of pqueue = %d\n", cnt, (int)pqueue.size()); }
void RNAFuncs::generateRNAAlignmentXML(const string &structure, const string &altStructure, const string &seq1, const string &seq2, const string &strname1, const string &strname2, ostream &s) { string base; Uint i; float *X, *Y; short *pair_table,*alt_pair_table; Uint basenr_x=1, basenr_y=1; string filename; X = new float[structure.size()]; Y = new float[structure.size()]; assert(seq1.size() == seq2.size()); assert(seq1.size() == structure.size()); assert(seq1.size() == altStructure.size()); // calculate coordinates pair_table = make_pair_table(structure.c_str()); alt_pair_table = make_pair_table(altStructure.c_str()); i = naview_xy_coordinates(pair_table, X, Y); if(i!=structure.size()) cerr << "strange things happening in squigglePlot ..." << endl; // generate XML s << "<alignment xname=\"" << strname1 << "\" yname=\"" << strname2 << "\">" << endl; // seqalignment s << " <seqalignment>" << endl; for(i=0;i<seq1.length();i++) { s << " <base id=\"" << i+1 << "\" xbasenr=\"" << basenr_x << "\" ybasenr=\"" << basenr_y << "\" xbase=\"" << seq1[i] <<"\" ybase=\"" << seq2[i] << "\" />" << endl; if(seq1[i]!='-') basenr_x++; if(seq2[i]!='-') basenr_y++; } s << " </seqalignment>" << endl; // pairs s << " <pairs>" << endl; for(i=0;i<structure.size();i++) { // both if((unsigned short)pair_table[i+1]>i+1 && (unsigned short)alt_pair_table[i+1]>i+1) { s << " <pair drawbaseid1=\"" << i+1 << "\" drawbaseid2=\"" << pair_table[i+1] << "\"/>" << endl; } else { if((unsigned short)pair_table[i+1]>i+1) { s << " <pair drawbaseid1=\"" << i+1 << "\" drawbaseid2=\"" << pair_table[i+1] << "\" structid=\"1\"/>" << endl; } else { if((unsigned short)alt_pair_table[i+1]>i+1) s << " <pair drawbaseid1=\"" << i+1 << "\" drawbaseid2=\"" << alt_pair_table[i+1] << "\" structid=\"2\"/>" << endl; } } } s << " </pairs>" << endl; // coordinates // x structure s << " <structure id=\"1\">" << endl; s << " <drawbases>" << endl; for(i=0;i<seq1.length();i++) { s << " <drawbase id=\"" << i+1 << "\" xcoor=\"" << X[i] << "\" ycoor=\"" << -Y[i] << "\"/>" << endl; } s << " </drawbases>" << endl; s << " </structure>" << endl; DELETE(X); DELETE(Y); X = new float[altStructure.size()]; Y = new float[altStructure.size()]; // y structure i = naview_xy_coordinates(alt_pair_table, X, Y); if(i!=structure.size()) cerr << "strange things happening in squigglePlot ..." << endl; s << " <structure id=\"2\">" << endl; s << " <drawbases>" << endl; for(i=0;i<seq1.length();i++) { s << " <drawbase id=\"" << i+1 << "\" xcoor=\"" << X[i] << "\" ycoor=\"" << -Y[i] << "\"/>" << endl; } s << " </drawbases>" << endl; s << " </structure>" << endl; s << "</alignment>" << endl; free(pair_table); free(alt_pair_table); DELETE(X); DELETE(Y); }
void RNAProfileAlignment::squigglePlot(const string &filename, SquigglePlotOptions &options) const { const double base_fontsize=8; const Uint num_grey_colors=100; const double min_grey_color=1.0; string seq,structure; string base,structname; float *X,*Y,min_X=0,max_X=0,min_Y=0,max_Y=0; Uint i; short *pair_table; int id_PS,id; int ps_grey_colors[num_grey_colors]; int ps_color_red; int ps_color_black; double xpos,ypos; deque<double> pairprob; deque<BaseProbs> baseprobs; getStructureAlignment(options.minPairProb,structure,pairprob); getSequenceAlignment(baseprobs); // filterConsensus(structure,pairprob,baseprobs,0.5); //assert(baseprobs.size() == structure.size()); if(baseprobs.size() != structure.size()) cerr << "Error in resolving consensus structure!" << endl; X = new float[structure.size()]; Y = new float[structure.size()]; pair_table = make_pair_table(structure.c_str()); i = naview_xy_coordinates(pair_table, X, Y); if(i!=structure.size()) cerr << "strange things happening in squigglePlot ..." << endl; // calculate image dimesions for(i=0;i<structure.size();i++) { min_X=min(min_X,X[i]); max_X=max(max_X,X[i]); min_Y=min(min_Y,Y[i]); max_Y=max(max_Y,Y[i]); } // id_PS = g2_open_PS("ali.ps", g2_A4, g2_PS_port); id_PS = g2_open_EPSF((char*)filename.c_str()); id = g2_open_vd(); g2_attach(id,id_PS); // cout << "min_X: " << min_X <<",max_X: " << max_X << ",min_Y: " << min_Y << "max_Y: " << max_Y << endl; g2_set_coordinate_system(id_PS,595/2.0,842/2.0,0.5,0.5); g2_set_line_width(id,0.2); // set colors double intv=min_grey_color/(double)num_grey_colors; for(i=0;i<num_grey_colors;i++) { double grey_color=min_grey_color-i*intv; ps_grey_colors[i]=g2_ink(id_PS,grey_color,grey_color,grey_color); } ps_color_black=g2_ink(id_PS,0,0,0); if(options.greyColors) ps_color_red=g2_ink(id_PS,0,0,0); else ps_color_red=g2_ink(id_PS,1,0,0); // draw sequence g2_set_font_size(id,base_fontsize); for(i=0;i<structure.size();i++) { if(options.mostLikelySequence) { double p=getMlBaseFreq(baseprobs[i]); //base color if(p==1) g2_pen(id,ps_color_red); else g2_pen(id,ps_grey_colors[(int)floor(p*num_grey_colors-1)]); base=getMlBase(baseprobs[i]); xpos=X[i]-base.length()*base_fontsize/2.0; ypos=Y[i]-4; g2_string(id,xpos,ypos,(char*)base.c_str()); } else { drawBaseCircles(id_PS,baseprobs[i],X[i],Y[i]); } // connection to next base if(i<structure.size()-1) { if((1-baseprobs[i].gap)*(1-baseprobs[i+1].gap)==1) g2_pen(id,ps_color_red); else g2_pen(id,ps_grey_colors[(int)floor((1-baseprobs[i].gap)*(1-baseprobs[i+1].gap)*num_grey_colors-1)]); g2_line(id,X[i],Y[i],X[i+1],Y[i+1]); } } // draw pairings // !!! pair_table indexing begins at 1 !!! for(i=0;i<structure.size();i++) { if((unsigned short)pair_table[i+1]>i+1) { // pairs in both structures if(pairprob[i]==1) g2_pen(id,ps_color_red); else g2_pen(id,ps_grey_colors[(int)floor(pairprob[i]*num_grey_colors-1)]); g2_line(id,X[i],Y[i],X[pair_table[i+1]-1],Y[pair_table[i+1]-1]); } } g2_flush(id); g2_close(id); free(pair_table); DELETE(X); DELETE(Y); }
int main(int arfc, char **arfv) { /* arfv[1] is the input file with sequence on the first line and constraint on the second line takes constraints of the ( ) and [ ] variety arfv[2] is the list of structures output by subopt to check */ if (arfc != 3) { usage(arfv[0]); exit(1); } FILE *fin, *fout; int i, *pos, *neg; char *C; if (arfv[1][0] == '-' && arfv[1][1] == '\0') fin = stdin; else if (!(fin = fopen(arfv[1], "rb"))) { fprintf(stderr, "unable to open %s, bailing\n", arfv[1]); exit(1); } if (arfv[2][0] == '-' && arfv[2][1] == '\0') fout = stdin; else if (!(fout = fopen(arfv[2], "rb"))) { fprintf(stderr, "unable to open %s, bailing\n", arfv[2]); if (stdin != fin) fclose(fin); exit(1); } skip_sequence(fin); C = get_structure(fin); make_pair_table(C, &pos, &neg); printf("constraint string:\n%s\n", C); free(C); printf("forced pairs:"); for (i = 1; i <= len; ++i) { if (pos[i]) printf(" (%d,%d)", i, pos[i]); } printf("\n"); printf("forbidden pairs:"); for (i = 1; i <= len; ++i) { if (neg[i]) printf(" (%d,%d)", i, neg[i]); } printf("\n"); while (!feof(fout)) { char *s; int *ps; s = get_structure(fout); make_pair_table(s, &ps, NULL); if (s[0]) { compare(s, ps, pos, neg); } free(s); free(ps); } free(pos); free(neg); if (stdin != fin) fclose(fin); if (stdin != fin) fclose(fout); return 0; }
int main(int argc, char *argv[]) { struct RRNAfold_args_info args_info; /* let's call our cmdline parser */ if (RRNAfold_parser (argc, argv, &args_info) != 0) exit(1) ; // for ( unsigned i = 0 ; i < args_info.inputs_num ; ++i ) // printf("%s\n",args_info.inputs[i]) ; int type = args_info.fold_type_arg; char *iname = args_info.input_file_arg; char *oname = args_info.output_file_arg; double temp = args_info.temperature_arg; printf("Using temp: %f",temp); int length,n,i,j; /* Reads in a file containing(-I) one sequence per line and outputs (-O) a file containing the corresponiding structures */ char *let,*seq1, *struct1,* struct2,* xstruc,*ffname; float *x,*y,e1, e2, tree_dist, string_dist, profile_dist, kT,**pf1, **pf2; FILE *fi,*fo; short *pair_table; /* open the file */ fi = fopen(iname, "r"); fo = fopen(oname, "a"); if (fi == NULL) {printf("I couldn't open results.dat for reading.\n");} i=0;//Index used to keep track of line in the file /* Scanning each line in the file and folding the given sequence */ seq1 = (char* ) space(sizeof(char)*(1000+1)); while (fscanf(fi, "%s\n",seq1) == 1){ printf("%s\n",seq1); length=strlen(seq1); /* fold at 30C instead of the default 37C */ temperature = temp; /* must be set *before* initializing */ update_fold_params(); /* allocate memory for fold(), could be skipped */ //initialize_fold(strlen(seq1)); /* allocate memory for structure and fold */ struct1 = (char* ) space(sizeof(char)*(length+1)); let = (char* ) space(sizeof(char)*(length+1)); x = (float* ) space(sizeof(float)*(length+1)); y = (float* ) space(sizeof(float)*(length+1)); /* Folding the sequence */ e1 = fold(seq1, struct1); // fprintf(fo,"%s\n",struct1); pair_table=make_pair_table(struct1); if(type==2){ j=simple_xy_coordinates(pair_table,x,y); }else{ j=naview_xy_coordinates(pair_table,x,y); } j=0; let = seq1; while(j < (length)){ fprintf(fo,"%d,%f,%f,%c,%d,%d\n",i,x[j],y[j],*let,j,(pair_table[(j+1)]-1)); let++; j++; } /* Makes .ps file of RNA fold */ /////////////////////////////////////////// /* ffname=(char*) space(sizeof(char)*10); n=sprintf(ffname, "rna%d.ps",i); printf("%s\n",ffname); (void) PS_rna_plot(seq1, struct1, ffname); */ /////////////////////////////////////////// free_arrays(); /* free arrays used in fold() */ i++; } /* close the file */ fclose(fi); fclose(fo); }
/* len1 is the length of the LONGER input seq ! */ void seperate_bp(char **inter, int len1, char **intra_l, char **intra_s) { int i,j,len; short *pt=NULL; char *temp_inter, *pt_inter; len=strlen((*inter)); /* printf("inter\n%s\n",(*inter)); */ i = len+1; temp_inter=(char*)space(sizeof(char)*i); /* to make a pair_table convert <|> to (|) */ pt_inter=(char*)space(sizeof(char)*i); /* if shorter seq is first seq in constrained string, write the longer one as the first one */ temp_inter[strlen((*inter))] = '\0'; pt_inter[strlen((*inter))] = '\0'; if (cut_point < len1) { /* write the constrain for the longer seq first */ for (j=0,i=cut_point-1;i<len;i++,j++) { switch ((*inter)[i]){ case '(': temp_inter[j] = ')'; pt_inter[j] = ')'; break; case ')': temp_inter[j] = '('; pt_inter[j] = '('; break; default: temp_inter[j] = (*inter)[i]; pt_inter[j] = '.'; } } /* then add the constrain for the shorter seq */ for (i=0;i< cut_point-1;i++,j++) { switch ((*inter)[i]){ case '(': temp_inter[j] = ')'; pt_inter[j] = ')'; break; case ')': temp_inter[j] = '('; pt_inter[j] = '('; break; default: temp_inter[j] = (*inter)[i]; pt_inter[j] = '.'; } } cut_point = len1+1; strcpy((*inter),temp_inter); } else { for (i=0;i<strlen((*inter));i++) { switch ((*inter)[i]){ case '(': pt_inter[i] = '('; break; case ')': pt_inter[i] = ')'; break; default: pt_inter[i] = '.'; } } } pt = make_pair_table(pt_inter); /* intramolecular structure in longer (_l) and shorter (_s) seq */ (*intra_l)=(char*)space(sizeof(char)*(len1+1)); (*intra_s)=(char*)space(sizeof(char)*(strlen((*inter))-len1+2)); (*intra_l)[len1] = '\0'; (*intra_s)[strlen((*inter))-len1+1] = '\0'; /* now seperate intermolecular from intramolecular bp */ for (i=1;i<=pt[0];i++) { if (pt[i] == 0) { temp_inter[i-1] = (*inter)[i-1]; if (i<cut_point) { (*intra_l)[i-1] = (*inter)[i-1]; if ((*inter)[i-1] == '|') (*intra_l)[i-1] = '.'; } else { (*intra_s)[i-cut_point] = (*inter)[i-1]; if ((*inter)[i-1] == '|') (*intra_s)[i-cut_point] = '.'; } } else { if (i<cut_point) { /* intermolekular bp */ if (pt[i]>=cut_point){ temp_inter[i-1] = (*inter)[i-1]; (*intra_l)[i-1] = '.'; (*intra_s)[pt[i]-cut_point] = '.'; } else { /* intramolekular bp */ (*intra_l)[i-1] = (*inter)[i-1]; temp_inter[i-1] = '.'; } } else { /* i>=cut_point */ /* intermolekular bp */ if (pt[i] < cut_point){ temp_inter[i-1] = (*inter)[i-1]; /* (*intra_s)[i-1] = '.'; */ } else { /* intramolekular bp */ (*intra_s)[i-cut_point] = (*inter)[i-1]; temp_inter[i-1] = '.'; } } } } /* printf("%s -1\n%s -2\n%s -3\n%s -4\n",(*inter),temp_inter,(*intra_l),(*intra_s)); */ strcpy((*inter),temp_inter); free(temp_inter); free(pt_inter); free(pt); }
void encode_str(encoded *enc, const char *str) { if (enc->pt) free(enc->pt); enc->pt = make_pair_table(str); }
void RNAFuncs::drawRNAStructure(const string &seq, const string &structure, const string &filename_prefix, const string &structname, const list<pair<Uint,Uint> > ®ions, const SquigglePlotOptions &options) { const double base_fontsize=12; float *X, *Y,min_X=0,max_X=0,min_Y=0,max_Y=0; Uint i; short *pair_table; int id_PS,id_FIG=0,id; int color_black, *colors; double xpos,ypos; char buf[10]; string filename; double *points; int numPoints; #ifdef HAVE_LIBGD int id_PNG=0,id_JPG=0; #endif X = new float[structure.size()]; Y = new float[structure.size()]; points=new double[2*structure.size()]; colors=new int[NUM_COLORS]; assert(seq.size() == structure.size()); pair_table = make_pair_table(structure.c_str()); i = naview_xy_coordinates(pair_table, X, Y); if(i!=structure.size()) cerr << "strange things happening in squigglePlot ..." << endl; // scale image for(i=0;i<structure.size();i++) { X[i]*=static_cast<float>(options.scale); Y[i]*=static_cast<float>(options.scale); } // calculate image dimensions for(i=0;i<structure.size();i++) { min_X=min(min_X,X[i]); max_X=max(max_X,X[i]); min_Y=min(min_Y,Y[i]); max_Y=max(max_Y,Y[i]); } // add a border to image size min_X-=10; max_X+=10; min_Y-=10; max_Y+=10; //id_PS = g2_open_PS("ali.ps", g2_A4, g2_PS_port); filename=filename_prefix + ".ps"; id_PS = g2_open_EPSF((char*)filename.c_str()); g2_set_coordinate_system(id_PS,-min_X,-min_Y,1,1); if(options.generateFIG) { filename=filename_prefix + ".fig"; id_FIG=g2_open_FIG((char*)filename.c_str()); g2_set_coordinate_system(id_FIG,-min_X,-min_Y,1,1); } #ifdef HAVE_LIBGD if(options.generatePNG) { filename=filename_prefix + ".png"; id_PNG=g2_open_gd((char*)filename.c_str(),(int)(max_X-min_X),(int)(max_Y-min_Y),g2_gd_png); g2_set_coordinate_system(id_PNG,-min_X,-min_Y,1,1); } if(options.generateJPG) { filename=filename_prefix + ".jpg"; id_JPG=g2_open_gd((char*)filename.c_str(),(int)(max_X-min_X),(int)(max_Y-min_Y),g2_gd_jpeg); g2_set_coordinate_system(id_PS,-min_X,-min_Y,1,1); } #endif id = g2_open_vd(); g2_attach(id,id_PS); if(options.generateFIG) { g2_attach(id,id_FIG); } #ifdef HAVE_LIBGD if(options.generatePNG) g2_attach(id,id_PNG); if(options.generateJPG) g2_attach(id,id_JPG); #endif // cout << "min_X: " << min_X <<",max_X: " << max_X << ",min_Y: " << min_Y << "max_Y: " << max_Y << endl; // g2_set_coordinate_system(id_PS,595/2.0,842/2.0,0.5,0.5); // define colors if(options.greyColors) { color_black=g2_ink(id_PS,0,0,0); for(i=0;i<NUM_COLORS;i++) colors[i]=g2_ink(id_PS,0,0,0); if(options.generateFIG) { color_black=g2_ink(id_FIG,0,0,0); for(i=0;i<NUM_COLORS;i++) colors[i]=g2_ink(id_FIG,0,0,0); } #ifdef HAVE_LIBGD if(options.generatePNG) { color_black=g2_ink(id_PNG,0,0,0); for(i=0;i<NUM_COLORS;i++) colors[i]=g2_ink(id_PNG,0,0,0); } if(options.generateJPG) { color_black=g2_ink(id_JPG,0,0,0); for(i=0;i<NUM_COLORS;i++) colors[i]=g2_ink(id_JPG,0,0,0); } #endif } else { color_black=g2_ink(id,0,0,0); colors[COLOR_RED]=g2_ink(id_PS,COLOR_DEF_RED); colors[COLOR_GREEN]=g2_ink(id_PS,COLOR_DEF_GREEN); colors[COLOR_BLUE]=g2_ink(id_PS,COLOR_DEF_BLUE); colors[COLOR_YELLOW]=g2_ink(id_PS,COLOR_DEF_YELLOW); colors[COLOR_MAGENTA]=g2_ink(id_PS,COLOR_DEF_MAGENTA); colors[COLOR_TURKIS]=g2_ink(id_PS,COLOR_DEF_TURKIS); if(options.generateFIG) { color_black=g2_ink(id_FIG,0,0,0); colors[COLOR_RED]=g2_ink(id_FIG,COLOR_DEF_RED); colors[COLOR_GREEN]=g2_ink(id_FIG,COLOR_DEF_GREEN); colors[COLOR_BLUE]=g2_ink(id_FIG,COLOR_DEF_BLUE); colors[COLOR_YELLOW]=g2_ink(id_FIG,COLOR_DEF_YELLOW); colors[COLOR_MAGENTA]=g2_ink(id_FIG,COLOR_DEF_MAGENTA); colors[COLOR_TURKIS]=g2_ink(id_FIG,COLOR_DEF_TURKIS); } #ifdef HAVE_LIBGD if(options.generatePNG) { color_black=g2_ink(id_PNG,0,0,0); colors[COLOR_RED]=g2_ink(id_PNG,COLOR_DEF_RED); colors[COLOR_GREEN]=g2_ink(id_PNG,COLOR_DEF_GREEN); colors[COLOR_BLUE]=g2_ink(id_PNG,COLOR_DEF_BLUE); colors[COLOR_YELLOW]=g2_ink(id_PNG,COLOR_DEF_YELLOW); colors[COLOR_MAGENTA]=g2_ink(id_PNG,COLOR_DEF_MAGENTA); colors[COLOR_TURKIS]=g2_ink(id_PNG,COLOR_DEF_TURKIS); } if(options.generateJPG) { color_black=g2_ink(id_JPG,0,0,0); colors[COLOR_RED]=g2_ink(id_JPG,COLOR_DEF_RED); colors[COLOR_GREEN]=g2_ink(id_JPG,COLOR_DEF_GREEN); colors[COLOR_BLUE]=g2_ink(id_JPG,COLOR_DEF_BLUE); colors[COLOR_YELLOW]=g2_ink(id_JPG,COLOR_DEF_YELLOW); colors[COLOR_MAGENTA]=g2_ink(id_JPG,COLOR_DEF_MAGENTA); colors[COLOR_TURKIS]=g2_ink(id_JPG,COLOR_DEF_TURKIS); } #endif } for(i=0;i<structure.size();i++) { // connection to next base if(i<structure.size()-1) { // if the connected bases are only in one of the structures // use the appropriate color g2_line(id,X[i],Y[i],X[i+1],Y[i+1]); // draw circles at line endpoints g2_filled_circle(id,X[i],Y[i],0.7*options.scale); // circles are drawn twice, but thats ok ... } } // draw pairings // !!! pair_table indexing begins at 1 !!! for(i=0;i<structure.size();i++) { if((unsigned short)pair_table[i+1]>i+1) { // pairs in both structures g2_line(id,X[i],Y[i],X[pair_table[i+1]-1],Y[pair_table[i+1]-1]); } } // draw regions g2_set_line_width(id,0.4); list<pair<Uint,Uint> >::const_iterator it; Uint regionNr=0; for(it=regions.begin();it!=regions.end();it++) { double center_x=0,center_y=0; // fill coordinate list for(i=0;i < it->second;i++) { points[2*i]=X[it->first+i]; points[2*i+1]=Y[it->first+i]; center_x+=X[it->first+i]; center_y+=Y[it->first+i]; } numPoints=it->second-1; center_x/=numPoints; // center of gravity center_y/=numPoints; g2_pen(id,colors[regionNr % NUM_COLORS]); g2_poly_line(id,numPoints,points); sprintf(buf,"%d",regionNr+1); g2_string(id,center_x,center_y,buf); // draw region number regionNr++; } // mark 5' end g2_pen(id,color_black); g2_string(id,X[0]-20,Y[0],"5'"); g2_set_font_size(id,base_fontsize*options.scale); g2_set_line_width(id,0.2); // draw sequence for(i=0;i<structure.size();i++) { g2_pen(id,color_black); // match xpos=X[i]-(base_fontsize*options.scale)/2; ypos=Y[i]-4; sprintf(buf,"%c",seq[i]); g2_string(id,xpos,ypos,buf); /* if(!options.hideBaseNumbers) { // draw base number if(basenr % options.baseNumInterval == 0) { sprintf(buf,"%d",basenr); g2_string(id,xpos-20,ypos,buf); } }*/ } // draw structure name g2_string(id,min_X,max_Y-10,(char*)structname.c_str()); g2_flush(id); g2_close(id); free(pair_table); DELETE(X); DELETE(Y); DELETE(points); DELETE(colors); }
int main(int argc, char *argv[]) { char *string; char *structure=NULL; char *cstruc=NULL; char *ns_bases=NULL; char *c; int n_seq; int i; int length; int sym; int endgaps = 0; int mis = 0; double min_en; double real_en; double sfact = 1.07; int pf = 0; int istty; char *AS[MAX_NUM_NAMES]; /* aligned sequences */ char *names[MAX_NUM_NAMES]; /* sequence names */ AjPSeqset seq = NULL; AjPFile confile = NULL; AjPFile alifile = NULL; AjPFile paramfile = NULL; AjPFile outf = NULL; AjPFile essfile = NULL; AjPFile dotfile = NULL; AjPStr constring = NULL; float eT = 0.; AjBool eGU; AjBool eclose; AjBool lonely; AjPStr ensbases = NULL; AjBool etloop; AjPStr eenergy = NULL; char ewt = '\0'; float escale = 0.; AjPStr edangles = NULL; char edangle = '\0'; ajint len; AjPSeq tseq = NULL; AjPStr tname = NULL; int circ = 0; int doAlnPS = 0; int doColor = 0; embInitPV("vrnaalifoldpf",argc,argv,"VIENNA",VERSION); constring = ajStrNew(); seq = ajAcdGetSeqset("sequence"); confile = ajAcdGetInfile("constraintfile"); paramfile = ajAcdGetInfile("paramfile"); eT = ajAcdGetFloat("temperature"); eGU = ajAcdGetBoolean("gu"); eclose = ajAcdGetBoolean("closegu"); lonely = ajAcdGetBoolean("lp"); ensbases = ajAcdGetString("nsbases"); etloop = ajAcdGetBoolean("tetraloop"); eenergy = ajAcdGetListSingle("energy"); escale = ajAcdGetFloat("scale"); edangles = ajAcdGetListSingle("dangles"); mis = !!ajAcdGetBoolean("most"); endgaps = !!ajAcdGetBoolean("endgaps"); nc_fact = (double) ajAcdGetFloat("nspenalty"); cv_fact = (double) ajAcdGetFloat("covariance"); outf = ajAcdGetOutfile("outfile"); essfile = ajAcdGetOutfile("ssoutfile"); alifile = ajAcdGetOutfile("alignoutfile"); circ = !!ajAcdGetBoolean("circular"); doColor = !!ajAcdGetBoolean("colour"); dotfile = ajAcdGetOutfile("dotoutfile"); do_backtrack = 1; pf = 1; string = NULL; istty = 0; dangles = 2; temperature = (double) eT; noGU = (eGU) ? 0 : 1; no_closingGU = (eclose) ? 0 : 1; noLonelyPairs = (lonely) ? 0 : 1; ns_bases = (ajStrGetLen(ensbases)) ? MAJSTRGETPTR(ensbases) : NULL; tetra_loop = !!etloop; ewt = *ajStrGetPtr(eenergy); if(ewt == '0') energy_set = 0; else if(ewt == '1') energy_set = 1; else if(ewt == '2') energy_set = 2; sfact = (double) escale; edangle = *ajStrGetPtr(edangles); if(edangle == '0') dangles = 0; else if(edangle == '1') dangles = 1; else if(edangle == '2') dangles = 2; else if(edangle == '3') dangles = 3; if(paramfile) read_parameter_file(paramfile); if (ns_bases != NULL) { nonstandards = space(33); c=ns_bases; i=sym=0; if (*c=='-') { sym=1; c++; } while (*c!='\0') { if (*c!=',') { nonstandards[i++]=*c++; nonstandards[i++]=*c; if ((sym)&&(*c!=*(c-1))) { nonstandards[i++]=*c; nonstandards[i++]=*(c-1); } } c++; } } if(alifile) doAlnPS = 1; if(confile) vienna_GetConstraints(confile,&constring); n_seq = ajSeqsetGetSize(seq); if(n_seq > MAX_NUM_NAMES - 1) ajFatal("[e]RNAalifold is restricted to %d sequences\n", MAX_NUM_NAMES - 1); if (n_seq==0) ajFatal("No sequences found"); for(i=0;i<n_seq;++i) { tseq = (AjPSeq) ajSeqsetGetseqSeq(seq,i); ajSeqGapStandard(tseq, '-'); tname = (AjPStr) ajSeqsetGetseqNameS(seq,i); len = ajSeqGetLen(tseq); AS[i] = (char *) space(len+1); names[i] = (char *) space(ajStrGetLen(tname)+1); strcpy(AS[i],ajSeqGetSeqC(tseq)); strcpy(names[i],ajStrGetPtr(tname)); } AS[n_seq] = NULL; names[n_seq] = NULL; if (endgaps) for (i=0; i<n_seq; i++) mark_endgaps(AS[i], '~'); length = (int) strlen(AS[0]); structure = (char *) space((unsigned) length+1); if(confile) { fold_constrained = 1; strcpy(structure,ajStrGetPtr(constring)); } if (circ && noLonelyPairs) ajWarn( "warning, depending on the origin of the circular sequence, " "some structures may be missed when using -noLP\n" "Try rotating your sequence a few times\n"); if (circ) min_en = circalifold((const char **)AS, structure); else min_en = alifold(AS, structure); { int i; double s=0; extern int eos_debug; eos_debug=-1; /* shut off warnings about nonstandard pairs */ for (i=0; AS[i]!=NULL; i++) if (circ) s += energy_of_circ_struct(AS[i], structure); else s += energy_of_struct(AS[i], structure); real_en = s/i; } string = (mis) ? consens_mis((const char **) AS) : consensus((const char **) AS); ajFmtPrintF(outf,"%s\n%s", string, structure); ajFmtPrintF(outf," (%6.2f = %6.2f + %6.2f) \n", min_en, real_en, min_en-real_en ); if (length<=2500) { char **A; A = annote(structure, (const char**) AS); if (doColor) (void) PS_rna_plot_a(string, structure, essfile, A[0], A[1]); else (void) PS_rna_plot_a(string, structure, essfile, NULL, A[1]); free(A[0]); free(A[1]);free(A); } else ajWarn("INFO: structure too long, not doing xy_plot\n"); if (doAlnPS) PS_color_aln(structure, alifile, AS, names); { /* free mfe arrays but preserve base_pair for PS_dot_plot */ struct bond *bp; bp = base_pair; base_pair = space(16); free_alifold_arrays(); /* free's base_pair */ free_alipf_arrays(); base_pair = bp; } if (pf) { double energy, kT; pair_info *pi; char * mfe_struc; mfe_struc = strdup(structure); kT = (temperature+273.15)*1.98717/1000.; /* in Kcal */ pf_scale = exp(-(sfact*min_en)/kT/length); if (length>2000) ajWarn("scaling factor %f\n", pf_scale); /* init_alipf_fold(length); */ if (confile) strncpy(structure, ajStrGetPtr(constring), length+1); energy = (circ) ? alipf_circ_fold(AS, structure, &pi) : alipf_fold(AS, structure, &pi); if (do_backtrack) { ajFmtPrintF(outf,"%s", structure); ajFmtPrintF(outf," [%6.2f]\n", energy); } if ((istty)||(!do_backtrack)) ajFmtPrintF(outf," free energy of ensemble = %6.2f kcal/mol\n", energy); ajFmtPrintF(outf," frequency of mfe structure in ensemble %g\n", exp((energy-min_en)/kT)); if (do_backtrack) { FILE *aliout; cpair *cp; short *ptable; int k; ptable = make_pair_table(mfe_struc); ajFmtPrintF(outf,"\n# Alignment section\n\n"); aliout = ajFileGetFileptr(outf); fprintf(aliout, "%d sequences; length of alignment %d\n", n_seq, length); fprintf(aliout, "alifold output\n"); for (k=0; pi[k].i>0; k++) { pi[k].comp = (ptable[pi[k].i] == pi[k].j) ? 1:0; print_pi(pi[k], aliout); } fprintf(aliout, "%s\n", structure); free(ptable); cp = make_color_pinfo(pi); (void) PS_color_dot_plot(string, cp, dotfile); free(cp); free(mfe_struc); free(pi); } } if (cstruc!=NULL) free(cstruc); free(base_pair); (void) fflush(stdout); free(string); free(structure); for (i=0; AS[i]; i++) { free(AS[i]); free(names[i]); } ajSeqsetDel(&seq); ajStrDel(&constring); ajStrDel(&eenergy); ajStrDel(&edangles); ajStrDel(&ensbases); ajFileClose(&confile); ajFileClose(¶mfile); ajFileClose(&outf); ajFileClose(&essfile); ajFileClose(&alifile); ajFileClose(&dotfile); embExit(); return 0; }
void RNAFuncs::drawRNAAlignment(const string &structure, const string &altStructure, const string &seq1, const string &seq2, const string &strname1, const string &strname2, const string &filename_prefix, const bool atX, const SquigglePlotOptions &options) { const double base_fontsize=12; string base,structname; float *X, *Y,min_X=0,max_X=0,min_Y=0,max_Y=0; Uint i; short *pair_table,*alt_pair_table; int id_PS,id; int ps_color_black,ps_color_red,ps_color_blue,ps_color_green; Uint basenr_x=0, basenr_y=0; double xpos,ypos; char buf[10]; bool isDel,isIns; string filename; #ifdef HAVE_LIBGD int id_PNG=0,id_JPG=0; int png_color_black,png_color_red,png_color_blue,png_color_green; int jpg_color_black,jpg_color_red,jpg_color_blue,jpg_color_green; #endif X = new float[structure.size()]; Y = new float[structure.size()]; assert(seq1.size() == seq2.size()); assert(seq1.size() == structure.size()); assert(seq1.size() == altStructure.size()); pair_table = make_pair_table(structure.c_str()); alt_pair_table = make_pair_table(altStructure.c_str()); i = naview_xy_coordinates(pair_table, X, Y); if(i!=structure.size()) cerr << "strange things happening in squigglePlot ..." << endl; // scale image for(i=0;i<structure.size();i++) { X[i]*=static_cast<float>(options.scale); Y[i]*=static_cast<float>(options.scale); } // calculate image dimensions for(i=0;i<structure.size();i++) { min_X=min(min_X,X[i]); max_X=max(max_X,X[i]); min_Y=min(min_Y,Y[i]); max_Y=max(max_Y,Y[i]); } // add a border to image size min_X-=10; max_X+=10; min_Y-=10; max_Y+=10; //id_PS = g2_open_PS("ali.ps", g2_A4, g2_PS_port); filename=filename_prefix + ".ps"; id_PS = g2_open_EPSF((char*)filename.c_str()); g2_set_coordinate_system(id_PS,-min_X,-min_Y,1,1); #ifdef HAVE_LIBGD if(options.generatePNG) { filename=filename_prefix + ".png"; id_PNG=g2_open_gd((char*)filename.c_str(),(int)(max_X-min_X),(int)(max_Y-min_Y),g2_gd_png); g2_set_coordinate_system(id_PNG,-min_X,-min_Y,1,1); } if(options.generateJPG) { filename=filename_prefix + ".jpg"; id_JPG=g2_open_gd((char*)filename.c_str(),(int)(max_X-min_X),(int)(max_Y-min_Y),g2_gd_jpeg); g2_set_coordinate_system(id_PS,-min_X,-min_Y,1,1); } #endif id = g2_open_vd(); g2_attach(id,id_PS); #ifdef HAVE_LIBGD if(options.generatePNG) g2_attach(id,id_PNG); if(options.generateJPG) g2_attach(id,id_JPG); #endif // cout << "min_X: " << min_X <<",max_X: " << max_X << ",min_Y: " << min_Y << "max_Y: " << max_Y << endl; // g2_set_coordinate_system(id_PS,595/2.0,842/2.0,0.5,0.5); g2_set_line_width(id,0.2); // mark 5' end g2_string(id,X[0]-20,Y[0],"5'"); // define colors if(options.greyColors) { ps_color_black=g2_ink(id_PS,0,0,0); ps_color_red=g2_ink(id_PS,0,0,0); ps_color_blue=g2_ink(id_PS,0.7,0.7,0.7); ps_color_green=g2_ink(id_PS,0.4,0.4,0.4); #ifdef HAVE_LIBGD if(options.generatePNG) { png_color_black=g2_ink(id_PNG,0,0,0); png_color_red=g2_ink(id_PNG,0,0,0); png_color_blue=g2_ink(id_PNG,0.7,0.7,0.7); png_color_green=g2_ink(id_PNG,0.4,0.4,0.4); } if(options.generateJPG) { jpg_color_black=g2_ink(id_JPG,0,0,0); jpg_color_red=g2_ink(id_JPG,0,0,0); jpg_color_blue=g2_ink(id_JPG,0.7,0.7,0.7); jpg_color_green=g2_ink(id_JPG,0.4,0.4,0.4); } #endif } else { ps_color_black=g2_ink(id_PS,0,0,0); ps_color_red=g2_ink(id_PS,1,0,0); ps_color_blue=g2_ink(id_PS,0,0,0.5); ps_color_green=g2_ink(id_PS,0,0.5,0); #ifdef HAVE_LIBGD if(options.generatePNG) { png_color_black=g2_ink(id_PNG,0,0,0); png_color_red=g2_ink(id_PNG,1,0,0); png_color_blue=g2_ink(id_PNG,0,0,0.5); png_color_green=g2_ink(id_PNG,0,0.5,0); } if(options.generateJPG) { jpg_color_black=g2_ink(id_JPG,0,0,0); jpg_color_red=g2_ink(id_JPG,1,0,0); jpg_color_blue=g2_ink(id_JPG,0,0,0.5); jpg_color_green=g2_ink(id_JPG,0,0.5,0); } #endif } // draw sequence g2_set_font_size(id,base_fontsize*options.scale); for(i=0;i<structure.size();i++) { isDel = false; isIns = false; //base g2_pen(id,ps_color_black); // match base = ""; if(seq1[i]!='-') { base += seq1[i]; basenr_x++; } else { g2_pen(id,ps_color_green); // insertion isIns=true; } if(seq2[i]!='-') { base += seq2[i]; basenr_y++; } else { g2_pen(id,ps_color_blue); // deletion isDel=true; } if(base.size()==2 && base[0]!=base[1]) g2_pen(id,ps_color_red); // mismatch else base = base[0]; // show duplicate base only once xpos=X[i]-base.length()*(base_fontsize*options.scale)/2; ypos=Y[i]-4; g2_string(id,xpos,ypos,(char*)base.c_str()); if(!options.hideBaseNumbers) { // draw base number if(!isIns && basenr_x % options.baseNumInterval == 0) { g2_pen(id,ps_color_blue); sprintf(buf,"%d",basenr_x); g2_string(id,xpos-20,ypos,buf); } if(!isDel && basenr_y % options.baseNumInterval == 0) { g2_pen(id,ps_color_green); sprintf(buf,"%d",basenr_y); g2_string(id,xpos+20,ypos,buf); } } // connection to next base if(i<structure.size()-1) { // if the connected bases are only in one of the structures // use the appropriate color if(seq1[i]=='-' && seq1[i+1]=='-') g2_pen(id,ps_color_green); else if(seq2[i]=='-' && seq2[i+1]=='-') g2_pen(id,ps_color_blue); else g2_pen(id,ps_color_black); g2_line(id,X[i],Y[i],X[i+1],Y[i+1]); // draw circles at line endpoints if(seq1[i]=='-') g2_pen(id,ps_color_green); else if(seq2[i]=='-') g2_pen(id,ps_color_blue); else g2_pen(id,ps_color_black); g2_filled_circle(id,X[i],Y[i],0.7*options.scale); // circles are drawn twice, but thats ok ... if(seq1[i+1]=='-') g2_pen(id,ps_color_green); else if(seq2[i+1]=='-') g2_pen(id,ps_color_blue); else g2_pen(id,ps_color_black); g2_filled_circle(id,X[i+1],Y[i+1],0.7*options.scale); } } // draw pairings // !!! pair_table indexing begins at 1 !!! for(i=0;i<structure.size();i++) { if((unsigned short)pair_table[i+1]>i+1 && (unsigned short)alt_pair_table[i+1]>i+1) { // pairs in both structures g2_pen(id,ps_color_black); g2_line(id,X[i],Y[i],X[pair_table[i+1]-1],Y[pair_table[i+1]-1]); } else { if((unsigned short)pair_table[i+1]>i+1 && (unsigned short)alt_pair_table[i+1]<=i+1) { // pairs only in first structure if(atX) g2_pen(id,ps_color_blue); else g2_pen(id,ps_color_green); g2_line(id,X[i],Y[i],X[pair_table[i+1]-1],Y[pair_table[i+1]-1]); } else { if((unsigned short)pair_table[i+1]<=i+1 && (unsigned short)alt_pair_table[i+1]>i+1) { // pairs only in second structure if(atX) g2_pen(id,ps_color_green); else g2_pen(id,ps_color_blue); double dashes=2.0; g2_set_dash(id,1,&dashes); g2_line(id,X[i],Y[i],X[alt_pair_table[i+1]-1],Y[alt_pair_table[i+1]-1]); dashes=0; g2_set_dash(id,1,&dashes); } } } } // draw structure names // x-at-y or y-at-x if(atX) { g2_pen(id,ps_color_green); g2_string(id,min_X,max_Y-10,(char*)strname2.c_str()); g2_pen(id,ps_color_black); g2_string(id,min_X,max_Y-20,"at"); g2_pen(id,ps_color_blue); g2_string(id,min_X,max_Y-30,(char*)strname1.c_str()); } else { g2_pen(id,ps_color_blue); g2_string(id,min_X,max_Y-10,(char*)strname1.c_str()); g2_pen(id,ps_color_black); g2_string(id,min_X,max_Y-20,"at"); g2_pen(id,ps_color_green); g2_string(id,min_X,max_Y-30,(char*)strname2.c_str()); } g2_flush(id); g2_close(id); free(pair_table); free(alt_pair_table); DELETE(X); DELETE(Y); }
int PS_color_aln(const char *structure, const char *filename, const char *seqs[], const char *names[]) { /* produce PS sequence alignment color-annotated by consensus structure */ int N,i,j,k,x,y,tmp,columnWidth; char *tmpBuffer,*ssEscaped,*ruler, *cons; char c; float fontWidth, fontHeight, imageHeight, imageWidth,tmpColumns; int length, maxName, maxNum, currPos; float lineStep,blockStep,consStep,ssStep,rulerStep,nameStep,numberStep; float maxConsBar,startY,namesX,seqsX, currY; float score,barHeight,xx,yy; int match,block; FILE *outfile; short *pair_table; char * colorMatrix[6][3] = { {"0.0 1", "0.0 0.6", "0.0 0.2"}, /* red */ {"0.16 1","0.16 0.6", "0.16 0.2"}, /* ochre */ {"0.32 1","0.32 0.6", "0.32 0.2"}, /* turquoise */ {"0.48 1","0.48 0.6", "0.48 0.2"}, /* green */ {"0.65 1","0.65 0.6", "0.65 0.2"}, /* blue */ {"0.81 1","0.81 0.6", "0.81 0.2"} /* violet */ }; const char *alnPlotHeader = "%%!PS-Adobe-3.0 EPSF-3.0\n" "%%%%BoundingBox: %i %i %i %i\n" "%%%%EndComments\n" "%% draws Vienna RNA like colored boxes\n" "/box { %% x1 y1 x2 y2 hue saturation\n" " gsave\n" " dup 0.3 mul 1 exch sub sethsbcolor\n" " exch 3 index sub exch 2 index sub rectfill\n" " grestore\n" "} def\n" "%% draws a box in current color\n" "/box2 { %% x1 y1 x2 y2\n" " exch 3 index sub exch 2 index sub rectfill\n" "} def\n" "/string { %% (Text) x y\n" " 6 add\n" " moveto\n" " show\n" "} def\n" "0 %i translate\n" "1 -1 scale\n" "/Courier findfont\n" "[10 0 0 -10 0 0] makefont setfont\n"; outfile = fopen(filename, "w"); if (outfile == NULL) { fprintf(stderr, "can't open file %s - not doing alignment plot\n", filename); return 0; } columnWidth=60; /* Display long alignments in blocks of this size */ fontWidth=6; /* Font metrics */ fontHeight=6.5; lineStep=fontHeight+2; /* distance between lines */ blockStep=3.5*fontHeight; /* distance between blocks */ consStep=fontHeight*0.5; /* distance between alignment and conservation curve */ ssStep=2; /* distance between secondary structure line and sequences */ rulerStep=2; /* distance between sequences and ruler */ nameStep=3*fontWidth; /* distance between names and sequences */ numberStep=fontWidth; /* distance between sequeces and numbers */ maxConsBar=2.5*fontHeight; /* Height of conservation curve */ startY=2; /* "y origin" */ namesX=fontWidth; /* "x origin" */ /* Number of columns of the alignment */ length=strlen(seqs[0]); /* Allocate memory for various strings, length*2 is (more than) enough for all of them */ tmpBuffer = (char *) space((unsigned) length*2); ssEscaped=(char *) space((unsigned) length*2); ruler=(char *) space((unsigned) length*2); pair_table=make_pair_table(structure); /* Get length of longest name and count sequences in alignment*/ for (i=maxName=N=0; names[i] != NULL; i++) { N++; tmp=strlen(names[i]); if (tmp>maxName) maxName=tmp; } /* x-coord. where sequences start */ seqsX=namesX+maxName*fontWidth+nameStep; /* calculate number of digits of the alignment length */ snprintf(tmpBuffer,length, "%i",length); maxNum=strlen(tmpBuffer); /* Calculate bounding box */ tmpColumns=columnWidth; if (length<columnWidth){ tmpColumns=length; } imageWidth=ceil(namesX+(maxName+tmpColumns+maxNum)*fontWidth+2*nameStep+fontWidth+numberStep); imageHeight=startY+ceil((float)length/columnWidth)*((N+2)*lineStep+blockStep+consStep+ssStep+rulerStep); /* Write postscript header including correct bounding box */ fprintf(outfile,alnPlotHeader,0,0,(int)imageWidth,(int)imageHeight,(int)imageHeight); /* Create ruler and secondary structure lines */ i=0; /* Init all with dots */ for (i=0;i<(length);i++){ ruler[i]='.'; } i=0; for (i=0;i<length;i++){ /* Write number every 10th position, leave out block breaks */ if ((i+1)%10==0 && (i+1)%columnWidth!=0){ snprintf(tmpBuffer,length,"%i",i+1); strncpy(ruler+i,tmpBuffer,strlen(tmpBuffer)); } } ruler[length]='\0'; /* Draw color annotation first */ /* Repeat for all pairs */ for (i=1; i<=length; i++) { if ((j=pair_table[i])>i) { /* Repeat for open and closing position */ for (k=0;k<2;k++){ int pairings, nonpair, s, col; int ptype[8] = {0,0,0,0,0,0,0,0}; char *color; col = (k==0)?i-1:j-1; block=ceil((float)(col+1)/columnWidth); xx=seqsX+(col-(block-1)*columnWidth)*fontWidth; /* Repeat for each sequence */ for (s=pairings=nonpair=0; s<N; s++) { ptype[BP_pair[ENCODE(seqs[s][i-1])][ENCODE(seqs[s][j-1])]]++; } for (pairings=0,s=1; s<=7; s++) { if (ptype[s]) pairings++; } nonpair=ptype[0]; if (nonpair <=2) { color = colorMatrix[pairings-1][nonpair]; for (s=0; s<N; s++) { yy=startY+(block-1)*(lineStep*(N+2)+blockStep+consStep+rulerStep)+ssStep*(block)+(s+1)*lineStep; /* Color according due color information in pi-array, only if base pair is possible */ if (BP_pair[ENCODE(seqs[s][i-1])][ENCODE(seqs[s][j-1])]) { fprintf(outfile, "%.1f %.1f %.1f %.1f %s box\n", xx,yy-1,xx+fontWidth,yy+fontHeight+1,color); } } } } } } free(pair_table); /* Process rest of the output in blocks of columnWidth */ currY=startY; currPos=0; cons = consensus(seqs); while (currPos<length) { /* Display secondary structure line */ fprintf(outfile,"0 setgray\n"); strncpy(tmpBuffer,structure+currPos,columnWidth); tmpBuffer[columnWidth]='\0'; x=0;y=0; while ((c=tmpBuffer[x])){ if (c=='.'){ ssEscaped[y++]='.'; } else { ssEscaped[y++]='\\'; ssEscaped[y++]=c; } x++; } ssEscaped[y]='\0'; fprintf(outfile, "(%s) %.1f %.1f string\n", ssEscaped,seqsX,currY); currY+=ssStep+lineStep; /* Display names, sequences and numbers */ for (i=0; i<N; i++) { strncpy(tmpBuffer,seqs[i]+currPos,columnWidth); tmpBuffer[columnWidth]='\0'; match=0; for (j=0;j<(currPos+strlen(tmpBuffer));j++){ if (seqs[i][j] != '-') match++; } fprintf(outfile, "(%s) %.1f %.1f string\n", names[i],namesX,currY); fprintf(outfile, "(%s) %.1f %.1f string\n", tmpBuffer,seqsX,currY); fprintf(outfile, "(%i) %.1f %.1f string\n", match,seqsX+fontWidth*(strlen(tmpBuffer))+numberStep,currY); currY+=lineStep; } currY+=rulerStep; strncpy(tmpBuffer,ruler+currPos,columnWidth); tmpBuffer[columnWidth]='\0'; fprintf(outfile, "(%s) %.1f %.1f string\n", tmpBuffer,seqsX,currY); currY+=lineStep; currY+=consStep; /*Display conservation bar*/ fprintf(outfile,"0.6 setgray\n"); for (i=currPos;(i<currPos+columnWidth && i<length);i++){ match=0; for (j=0;j<N;j++){ if (cons[i] == seqs[j][i]) match++; if (cons[i]=='U' && seqs[j][i]=='T') match++; if (cons[i]=='T' && seqs[j][i]=='U') match++; } score=(float)(match-1)/(N-1); if (cons[i] == '-' || cons[i] == '_' || cons[i] == '.'){ score=0; } barHeight=maxConsBar*score; if (barHeight==0){ barHeight=1; } xx=seqsX+(i-(columnWidth*currPos/columnWidth))*fontWidth; fprintf(outfile,"%.1f %.1f %.1f %.1f box2\n", xx, currY+maxConsBar-barHeight, xx+fontWidth, currY+maxConsBar); } currY+=blockStep; currPos+=columnWidth; } free(cons); fprintf(outfile,"showpage\n"); fclose(outfile); free(tmpBuffer); free(ssEscaped);free(ruler); return 0; }
PRIVATE int find_path_once(const char *struc1, const char *struc2, int maxE, int maxl) { short *pt1, *pt2; move_t *mlist; int i, len, d, dist=0, result; intermediate_t *current, *next; pt1 = make_pair_table(struc1); pt2 = make_pair_table(struc2); len = (int) strlen(struc1); mlist = (move_t *) space(sizeof(move_t)*len); /* bp_dist < n */ for (i=1; i<=len; i++) { if (pt1[i] != pt2[i]) { if (i<pt1[i]) { /* need to delete this pair */ mlist[dist].i = -i; mlist[dist].j = -pt1[i]; mlist[dist++].when = 0; } if (i<pt2[i]) { /* need to insert this pair */ mlist[dist].i = i; mlist[dist].j = pt2[i]; mlist[dist++].when = 0; } } } free(pt2); BP_dist = dist; current = (intermediate_t *) space(sizeof(intermediate_t)*(maxl+1)); current[0].pt = pt1; current[0].Sen = current[0].curr_en = energy_of_structure_pt(seq, pt1, S, S1, 0); current[0].moves = mlist; next = (intermediate_t *) space(sizeof(intermediate_t)*(dist*maxl+1)); for (d=1; d<=dist; d++) { /* go through the distance classes */ int c, u, num_next=0; intermediate_t *cc; for (c=0; current[c].pt != NULL; c++) { num_next += try_moves(current[c], maxE, next+num_next, d); } if (num_next==0) { for (cc=current; cc->pt != NULL; cc++) free_intermediate(cc); current[0].Sen=INT_MAX; break; } /* remove duplicates via sort|uniq if this becomes a bottleneck we can use a hash instead */ qsort(next, num_next, sizeof(intermediate_t),compare_ptable); for (u=0,c=1; c<num_next; c++) { if (memcmp(next[u].pt,next[c].pt,sizeof(short)*len)!=0) { next[++u] = next[c]; } else { free_intermediate(next+c); } } num_next = u+1; qsort(next, num_next, sizeof(intermediate_t),compare_energy); /* free the old stuff */ for (cc=current; cc->pt != NULL; cc++) free_intermediate(cc); for (u=0; u<maxl && u<num_next; u++) { current[u] = next[u]; } for (; u<num_next; u++) free_intermediate(next+u); num_next=0; } free(next); path = current[0].moves; result = current[0].Sen; free(current[0].pt); free(current); return(result); }
void DSU::FindNumbers(int begin, int end, path_t *path, vector<int> &lm_numbers, bool shifts, bool noLP, bool debug, bool no_new) { // first resolve small case: if (end-begin<4) { bool begins = true; for (int i=begin+1; i<end; i++) { // get the minimum short *tmp_str = make_pair_table(path[i].s); int tmp_en; if (pknots) { Structure str(seq, tmp_str, s0, s1); tmp_en = move_gradient_pk(seq, &str, s0, s1, shifts, 0); copy_arr(tmp_str, str.str); } else { tmp_en = move_gradient(seq, tmp_str, s0, s1, 0, shifts, noLP); } // speedup if (lm_numbers[begin] != -1 && begins && tmp_en == LM[lm_numbers[begin]].energy && str_eq(LM[lm_numbers[begin]].structure, tmp_str)) { lm_numbers[i] = lm_numbers[begin]; } else { begins = false; if (lm_numbers[end] != -1 && tmp_en == LM[lm_numbers[end]].energy && str_eq(LM[lm_numbers[end]].structure, tmp_str)) { lm_numbers[i] = lm_numbers[end]; } } if (lm_numbers[i]==-1) { lm_numbers[i] = FindNum(tmp_en, tmp_str); // update UBlist if (lm_numbers[i]==-1 && !no_new) { if (gl_maxen < tmp_en) { //fprintf(stderr, "exceeds en.: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0); lm_numbers[i] = AddLMtoTBD(tmp_str, tmp_en, EE_DSU, debug); } else { if (debug) fprintf(stderr, "cannot find: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0); // add to list of minima and count with them later... lm_numbers[i] = AddLMtoTBD(tmp_str, tmp_en, NORM_CF, debug); } } } else debug_c++; free(tmp_str); } return ; } // da middle one int pivot = (end+begin)/2; short *tmp_str = make_pair_table(path[pivot].s); //fprintf(stderr, "%s\n", pt_to_str(tmp_str).c_str()); int tmp_en = move_gradient(seq, tmp_str, s0, s1, 0, shifts, noLP); //fprintf(stderr, "%s\n", pt_to_str(tmp_str).c_str()); // speed up: if (lm_numbers[begin] != -1 && tmp_en == LM[lm_numbers[begin]].energy && str_eq(LM[lm_numbers[begin]].structure, tmp_str)) { lm_numbers[pivot] = lm_numbers[begin]; } else { if (lm_numbers[end] != -1 && tmp_en == LM[lm_numbers[end]].energy && str_eq(LM[lm_numbers[end]].structure, tmp_str)) { lm_numbers[pivot] = lm_numbers[end]; } } // normal behaviour if (lm_numbers[pivot]==-1) { lm_numbers[pivot] = FindNum(tmp_en, tmp_str); // update UBlist if (lm_numbers[pivot]==-1 && !no_new) { if (gl_maxen < tmp_en) { //fprintf(stderr, "exceeds en.: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0); lm_numbers[pivot] = AddLMtoTBD(tmp_str, tmp_en, EE_DSU, debug); } else { if (debug) fprintf(stderr, "cannot find: %s %6.2f\n", pt_to_str(tmp_str).c_str(), tmp_en/100.0); // add to list of minima and count with them later... lm_numbers[pivot] = AddLMtoTBD(tmp_str, tmp_en, NORM_CF, debug); } } } else debug_c++; free(tmp_str); // continue recursion: if ((lm_numbers[pivot]!=lm_numbers[begin] || lm_numbers[pivot]==-1) && pivot-begin>1) FindNumbers(begin, pivot, path, lm_numbers, shifts, noLP, debug, no_new); if ((lm_numbers[pivot]!=lm_numbers[end] || lm_numbers[pivot]==-1) && end-pivot>1) FindNumbers(pivot, end, path, lm_numbers, shifts, noLP, debug, no_new); // return maximal energy return ; }