/*---------------------------------------------------------------------------*/ 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 */ }
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 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); }
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); }
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 */ }
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); }
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); }