Bsoft  2.1.0
Bernard's software package
ps_sequence.cpp
Go to the documentation of this file.
1 
9 #include "ps_plot.h"
10 #include "Complex.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
16 // Internal function prototype
17 int ps_seq_representation(ofstream* fps, int length, double* nseq);
18 int ps_seq_periodicity(ofstream* fps, int length, Complex<float>* per);
19 
33 int ps_seq_info(Bstring& filename, Bstring& title, int nres, int length,
34  double* info, double* nseq, Complex<float>* per, double* freq, char* pattern)
35 {
36  int i, j, n, page(1);
37  int left(70), bottom(500), width(500), height(200);
38 
39  int npages = 2 + (length-1)/500;
40 
41  ofstream* fps = ps_open_and_init(filename, title, npages, 600, 800);
42 
43  // The first page contains the information content and representation plots
44  *fps << "%%Page: Info 1" << endl;
45  *fps << "/Helvetica findfont 14 scalefont setfont" << endl;
46  *fps << "40 740 moveto (" << filename << ": " << title << ") show" << endl;
47  *fps << "/Left " << left << " def\n/Bottom " << bottom << " def" << endl;
48  *fps << "/Height " << height << " def\n/Width " << width << " def" << endl;
49 
50  *fps << "/Data [\n%Pos Info nSeq" << endl;
51  for ( i=0; i<length; i++ )
52  *fps << i+1 << " " << info[i] << " " << nseq[i] << endl;
53  *fps << "] def" << endl;
54 
55  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
56  *fps << "/x_Min 0 def\n/y_Min 0 def\n/x_Max " << length << " def\n/y_Max 4.3 def" << endl;
57  *fps << "/x_Scale Width x_Max x_Min sub div def" << endl;
58  *fps << "/y_Scale Height y_Max y_Min sub div def" << endl;
59  *fps << "/Frame { newpath 0 0 moveto 0 Height lineto Width Height lineto Width 0 lineto closepath } def" << endl;
60 
61  *fps << left-45 << " " << bottom+40 << " moveto gsave 90 rotate (Information content) show grestore" << endl;
62 
63  ps_scale(fps, left, 200, left+width, 200, 0, length, 10*(int)(length/100 + 1),
64  10, M_PI_2, 0, 12, 0);
65 
66  ps_scale(fps, left, bottom, left, bottom+height, 0, 4.3, 1.0,
67  10, 0, 1, 12, 0);
68 
69  *fps << "gsave" << endl;
70  *fps << " Left Bottom translate Frame stroke Frame clip" << endl;
71  *fps << " /x Data 0 get x_Min sub x_Scale mul def" << endl;
72  *fps << " /y Data 1 get y_Min sub y_Scale mul def" << endl;
73  *fps << " x y moveto" << endl;
74  *fps << " 3 3 Data length 3 sub {" << endl;
75  *fps << " /x_Index exch def" << endl;
76  *fps << " /y_Index x_Index 1 add def" << endl;
77  *fps << " /x Data x_Index get x_Min sub x_Scale mul def" << endl;
78  *fps << " /y Data y_Index get y_Min sub y_Scale mul def" << endl;
79  *fps << " x y lineto" << endl;
80  *fps << " } for stroke" << endl;
81  *fps << "grestore" << endl;
82 
83  ps_seq_periodicity(fps, length, per);
84 
85  // The residue representation plot
86  ps_seq_representation(fps, length, nseq);
87 
88  *fps << "showpage" << endl;
89 
90  // The following pages contain the sequence logo
91  int pagestart = 0;
92  for ( page=2; page<=npages; page++ ) {
93  *fps << "%Page: Logo " << page << endl;
94  *fps << "/Helvetica findfont 16 scalefont setfont" << endl;
95  *fps << "40 740 moveto (Sequence Logo: " << filename << ") show" << endl;
96  *fps << "/TopLine 640 def\n/LineHeight 60 def" << endl;
97  *fps << "/FontSize 14 def" << endl;
98  *fps << "/FontHeight FontSize 0.6 mul def" << endl;
99  *fps << "/CharWidth FontSize 0.6 mul def" << endl;
100  *fps << "/Courier-Bold findfont FontSize scalefont setfont" << endl;
101  *fps << "/Letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ) def" << endl;
102  *fps << "/Red [0.5 0.0 1.0 1.0 1.0 0.5 0.8 0.0 0.5 0.9 0.0 0.5 1.0 0.0 0.9 " << endl;
103  *fps << " 0.8 0.0 0.0 0.8 0.8 0.9 0.5 0.5 0.1 0.5 0.0 ] def " << endl;
104  *fps << "/Green [0.5 1.0 1.0 0.0 0.0 0.5 0.5 0.0 0.5 0.9 0.0 0.5 1.0 1.0 0.9 " << endl;
105  *fps << " 0.5 1.0 0.0 0.5 0.5 0.9 0.5 0.5 0.1 0.5 1.0 ] def " << endl;
106  *fps << "/Blue [0.5 0.0 0.0 0.0 0.0 0.5 0.8 1.0 0.5 0.9 1.0 0.5 0.0 0.0 0.9 " << endl;
107  *fps << " 0.8 0.0 1.0 0.8 0.8 0.9 0.5 0.5 0.1 0.5 0.0 ] def " << endl;
108  *fps << "/SeqPattern [" << endl;
109  for ( i=pagestart; i<pagestart+500 && i<length; i++ ) {
110  *fps << "(";
111  n = 0;
112  for ( j=nres-1; j>=0; j-- ) {
113  if ( pattern[nres*i+j] >= 'A' && freq[nres*i+j] > 0.1 ) {
114 // *fps << "%c", pattern[nres*i+j]);
115  *fps << pattern[nres*i+j];
116  n++;
117  }
118  }
119  if ( n < 1 ) *fps << "X";
120  *fps << ")" << endl;
121  }
122  *fps << "] def" << endl;
123  *fps << "/Height [" << endl;
124  for ( i=pagestart; i<pagestart+500 && i<length; i++ ) {
125  n = 0;
126  for ( j=nres-1; j>=0; j-- ) {
127  if ( pattern[nres*i+j] >= 'A' && freq[nres*i+j] > 0.1 ) {
128  *fps << freq[nres*i+j] << " ";
129  n++;
130  }
131  }
132  if ( n < 1 ) *fps << "0.05 ";
133  *fps << endl;
134  }
135  *fps << "] def" << endl;
136  *fps << "/HeightIndex 0 def" << endl;
137  *fps << "/LineIndex 0 def" << endl;
138  *fps << "/theLine TopLine def" << endl;
139  *fps << "gsave" << endl;
140  *fps << " 40 theLine 15 sub moveto (" << pagestart << ") show" << endl;
141  *fps << " 40 theLine moveto" << endl;
142  *fps << " 0 1 SeqPattern length 1 sub { %% Get a pattern at each position" << endl;
143  *fps << " /PatternIndex exch def" << endl;
144  *fps << " LineIndex PatternIndex 50 div truncate lt {" << endl;
145  *fps << " /theLine theLine LineHeight sub def" << endl;
146  *fps << " /LineIndex PatternIndex 50 div truncate def" << endl;
147  *fps << " 40 theLine 15 sub moveto" << endl;
148  *fps << " PatternIndex " << pagestart << " add (xxxx) cvs show" << endl;
149  *fps << " 40 theLine moveto" << endl;
150  *fps << " } if" << endl;
151  *fps << " /LineIndex PatternIndex 50 div def" << endl;
152  *fps << " /aPattern {SeqPattern PatternIndex get} def" << endl;
153  *fps << " gsave" << endl;
154  *fps << " 0 1 aPattern length 1 sub { %% Get the character and its colour and height" << endl;
155  *fps << " /CharIndex exch def" << endl;
156  *fps << " /aChar aPattern CharIndex 1 getinterval def" << endl;
157  *fps << " /CharHeight Height HeightIndex get def" << endl;
158  *fps << " 0 1 Letters length 1 sub {" << endl;
159  *fps << " /anIndex exch def" << endl;
160  *fps << " aChar Letters anIndex 1 getinterval eq { /LetterIndex anIndex def } if" << endl;
161  *fps << " } for" << endl;
162  *fps << " gsave" << endl;
163  *fps << " Red LetterIndex get Green LetterIndex get Blue LetterIndex get setrgbcolor" << endl;
164  *fps << " 1 CharHeight scale" << endl;
165  *fps << " aChar show" << endl;
166  *fps << " grestore" << endl;
167  *fps << " 0 CharHeight FontHeight mul rmoveto" << endl;
168  *fps << " /HeightIndex HeightIndex 1 add def" << endl;
169  *fps << " } for" << endl;
170  *fps << " grestore" << endl;
171  *fps << " CharWidth 0 rlineto" << endl;
172  *fps << " } for stroke" << endl;
173  *fps << "grestore" << endl;
174  *fps << "showpage" << endl;
175  pagestart += 500;
176  }
177 
178  ps_close(fps);
179 
180  return 0;
181 }
182 
194 int ps_seq_hydrophob(Bstring& filename, Bstring& title, int length,
195  double* Hphob, int* HPseg, double* nseq, Complex<float>* per)
196 {
197  int i;
198  int left(70), bottom(500), width(500), height(200);
199  double y_min, y_max;
200 
201  ofstream* fps = ps_open_and_init(filename, title, 1, 600, 800);
202 
203  // The first page contains the hydrophobicity and representation plots
204  *fps << "%%Page: Hphob 1" << endl;
205  *fps << "/Helvetica findfont 16 scalefont setfont" << endl;
206  *fps << "40 740 moveto (" << filename << ": " << title << ") show" << endl;
207  *fps << "/Left " << left << " def\n/Bottom " << bottom << " def" << endl;
208  *fps << "/Height " << height << " def\n/Width " << width << " def" << endl;
209 
210  y_min = y_max = 0;
211  *fps << "/Data [" << endl << "%Pos Hphob HPseg" << endl;
212  for ( i=0; i<length; i++ ) {
213  *fps << i+1 << " " << Hphob[i] << " " << HPseg[i] << endl;
214  if ( y_min > Hphob[i] ) y_min = Hphob[i];
215  if ( y_max < Hphob[i] ) y_max = Hphob[i];
216  }
217  *fps << "] def" << endl;
218 
219  y_min = 0.01*floor(100*y_min);
220  y_max = 0.01*floor(100*y_max + 1);
221 
222  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
223  *fps << "/x_Min 0 def\n/y_Min " << y_min << " def" << endl;
224  *fps << "/x_Max " << length << " def\n/y_Max " << y_max << " def" << endl;
225  *fps << "/x_Scale Width x_Max x_Min sub div def" << endl;
226  *fps << "/y_Scale Height y_Max y_Min sub div def" << endl;
227 
228  *fps << "/Frame { newpath 0 0 moveto 0 Height lineto Width Height lineto Width 0 lineto closepath } def" << endl;
229 
230  *fps << left-45 << " " << bottom+30 << " moveto gsave 90 rotate (Hydrophobicity (kcal/mol)) show grestore" << endl;
231 
232  ps_scale(fps, left, 200, left+width, 200, 0, length, 10*(int)(length/100),
233  10, M_PI_2, 0, 12, 0);
234 
235  ps_scale(fps, left, bottom, left, bottom+height, y_min, y_max, (y_max - y_min)/5,
236  10, 0, 1, 12, 0);
237 
238  *fps << "gsave" << endl;
239  *fps << " Left Bottom translate Frame stroke Frame clip stroke" << endl;
240  *fps << " 20 setlinewidth 0.5 setgray" << endl;
241  *fps << " /x Data 0 get x_Min sub x_Scale mul def" << endl;
242  *fps << " /y 1 y_Min sub y_Scale mul def" << endl;
243  *fps << " x y moveto" << endl;
244  *fps << " 3 3 Data length 3 sub {" << endl;
245  *fps << " /x_Index exch def" << endl;
246  *fps << " /x Data x_Index get x_Min sub x_Scale mul def" << endl;
247  *fps << " Data x_Index 2 add get 0 gt {" << endl;
248  *fps << " x y lineto" << endl;
249  *fps << " }{" << endl;
250  *fps << " x y moveto" << endl;
251  *fps << " } ifelse" << endl;
252  *fps << " } for stroke" << endl;
253  *fps << "grestore" << endl;
254 
255  *fps << "gsave" << endl;
256  *fps << " Left Bottom translate Frame stroke Frame clip" << endl;
257  *fps << " /x Data 0 get x_Min sub x_Scale mul def" << endl;
258  *fps << " /y Data 1 get y_Min sub y_Scale mul def" << endl;
259  *fps << " x y moveto" << endl;
260  *fps << " 3 3 Data length 3 sub {" << endl;
261  *fps << " /x_Index exch def" << endl;
262  *fps << " /y_Index x_Index 1 add def" << endl;
263  *fps << " /x Data x_Index get x_Min sub x_Scale mul def" << endl;
264  *fps << " /y Data y_Index get y_Min sub y_Scale mul def" << endl;
265  *fps << " x y lineto" << endl;
266  *fps << " } for stroke" << endl;
267  *fps << "grestore" << endl;
268 
269  ps_seq_periodicity(fps, length, per);
270 
271  // The residue representation plot
272  ps_seq_representation(fps, length, nseq);
273 
274  *fps << "showpage" << endl;
275 
276  ps_close(fps);
277 
278  return 0;
279 }
280 
281 // The residue representation plot
282 int ps_seq_representation(ofstream* fps, int length, double* nseq)
283 {
284  int i, bar_height(30);
285  double x_scale(500/(length - 1.0));
286  double y_scale(bar_height);
287 
288  *fps << "/Data [" << endl << "%Pos nSeq" << endl;
289  for ( i=0; i<length; i++ ) {
290  *fps << i+1 << " " << nseq[i] << endl;
291  if ( nseq[i]*y_scale > bar_height ) y_scale = bar_height/nseq[i];
292  }
293  *fps << "] def" << endl;
294  *fps << "/Left 70 def\n/Bottom 70 def" << endl;
295  *fps << "/x_Min 1 def\n/x_Scale " << x_scale << " def\n/y_Scale " << y_scale << " def" << endl;
296  *fps << "gsave" << endl;
297  *fps << " Left Bottom translate" << endl;
298  *fps << " /x Data 0 get x_Min sub x_Scale mul def" << endl;
299  *fps << " /y " << bar_height << " Data 1 get y_Scale mul add def" << endl;
300  *fps << " x y moveto" << endl;
301  *fps << " 2 2 Data length 2 sub {" << endl;
302  *fps << " /x_Index exch def" << endl;
303  *fps << " /y_Index x_Index 1 add def" << endl;
304  *fps << " /x Data x_Index get x_Min sub x_Scale mul def" << endl;
305  *fps << " /y " << bar_height << " Data y_Index get y_Scale mul add def" << endl;
306  *fps << " x y lineto" << endl;
307  *fps << " } for stroke" << endl;
308  *fps << " /x Data 0 get x_Min sub x_Scale mul def" << endl;
309  *fps << " /y " << bar_height << " Data 1 get y_Scale mul sub def" << endl;
310  *fps << " x y moveto" << endl;
311  *fps << " 2 2 Data length 2 sub {" << endl;
312  *fps << " /x_Index exch def" << endl;
313  *fps << " /y_Index x_Index 1 add def" << endl;
314  *fps << " /x Data x_Index get x_Min sub x_Scale mul def" << endl;
315  *fps << " /y " << bar_height << " Data y_Index get y_Scale mul sub def" << endl;
316  *fps << " x y lineto" << endl;
317  *fps << " } for stroke" << endl;
318  *fps << "grestore" << endl;
319 
320  return 0;
321 }
322 
323 // The periodicity plot
324 int ps_seq_periodicity(ofstream* fps, int length, Complex<float>* per)
325 {
326  int i;
327  int left(70), bottom(300), width(500), height(200);
328  double y_max;
329 
330  *fps << "/Per [" << endl << "%Pos Amp Phi" << endl;
331  y_max = 0;
332  for ( i=0; i<length; i++ ) {
333  *fps << i+1 << " " << per[i].amp() << " " << per[i].phi()*180/M_PI << endl;
334  if ( y_max < per[i].amp() ) y_max = per[i].amp();
335  }
336  *fps << "] def" << endl;
337 
338  y_max = 0.1*floor(10*y_max + 1);
339 
340  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
341  *fps << "/Left " << left << " def\n/Bottom " << bottom << " def" << endl;
342  *fps << "/Height " << height << " def\n/Width " << width << " def" << endl;
343  *fps << "/x_Min 1 def\n/y_Min 0 def" << endl;
344  *fps << "/x_Max " << length << " def\n/y_Max " << y_max << " def" << endl;
345  *fps << "/x_Scale Width x_Max x_Min sub div def" << endl;
346  *fps << "/y_Scale Height y_Max y_Min sub div def" << endl;
347  *fps << left-45 << " " << bottom+70 << " moveto gsave 90 rotate (Amplitude) show grestore" << endl;
348 
349  ps_scale(fps, left, bottom, left, bottom+height, 0, y_max, y_max/4.5,
350  10, 0, 1, 12, 0);
351 
352  *fps << "/Frame { newpath 0 0 moveto 0 Height lineto Width Height lineto Width 0 lineto closepath } def" << endl;
353  *fps << "gsave" << endl;
354  *fps << " Left Bottom translate Frame stroke Frame clip" << endl;
355  *fps << " /x Per 0 get x_Min sub x_Scale mul def" << endl;
356  *fps << " /y Per 1 get y_Min sub y_Scale mul def" << endl;
357  *fps << " x y moveto" << endl;
358  *fps << " 3 3 Per length 3 sub {" << endl;
359  *fps << " /x_Index exch def" << endl;
360  *fps << " /y_Index x_Index 1 add def" << endl;
361  *fps << " /x Per x_Index get x_Min sub x_Scale mul def" << endl;
362  *fps << " /y Per y_Index get y_Min sub y_Scale mul def" << endl;
363  *fps << " x y lineto" << endl;
364  *fps << " } for stroke" << endl;
365  *fps << "grestore" << endl;
366 
367  bottom = 200;
368  height = 100;
369 
370  *fps << "/Left " << left << " def\n/Bottom " << bottom << " def" << endl;
371  *fps << "/Height " << height << " def\n/Width " << width << " def" << endl;
372  *fps << left-40 << " " << bottom << " moveto (-180) show" << endl;
373  *fps << left-40 << " " << bottom+height/2-8 << " moveto ( 0) show" << endl;
374  *fps << left-40 << " " << bottom+height-16 << " moveto ( 180) show" << endl;
375  *fps << left-45 << " " << bottom << " moveto gsave 90 rotate (Phase (degrees)) show grestore" << endl;
376 
377  *fps << left+190 << " " << bottom-40 << " moveto (Alignment position) show" << endl;
378  *fps << "/y_Min -180 def\n/y_Max 180 def" << endl;
379  *fps << "/y_Scale Height y_Max y_Min sub div def" << endl;
380  *fps << "/Frame { newpath 0 0 moveto 0 Height lineto Width Height lineto Width 0 lineto closepath } def" << endl;
381  *fps << "gsave" << endl;
382  *fps << " Left Bottom translate Frame stroke Frame clip" << endl;
383  *fps << " /x Per 0 get x_Min sub x_Scale mul def" << endl;
384  *fps << " /y Per 2 get y_Min sub y_Scale mul def" << endl;
385  *fps << " x y moveto" << endl;
386  *fps << " /last_x x def" << endl;
387  *fps << " /last_y y def" << endl;
388  *fps << " 3 3 Per length 3 sub {" << endl;
389  *fps << " /x_Index exch def" << endl;
390  *fps << " /y_Index x_Index 2 add def" << endl;
391  *fps << " /x Per x_Index get x_Min sub x_Scale mul def" << endl;
392  *fps << " /y Per y_Index get y_Min sub y_Scale mul def" << endl;
393  *fps << " /y_diff y last_y sub def" << endl;
394  *fps << " y_diff 0 lt {" << endl;
395  *fps << " y_diff Height 2 div neg gt { x y lineto }" << endl;
396  *fps << " { x y Height add lineto last_x last_y Height sub moveto x y lineto" << endl;
397  *fps << " } ifelse" << endl;
398  *fps << " }{" << endl;
399  *fps << " y_diff Height 2 div lt { x y lineto }" << endl;
400  *fps << " { x y Height sub lineto last_x last_y Height add moveto x y lineto" << endl;
401  *fps << " } ifelse" << endl;
402  *fps << " } ifelse" << endl;
403  *fps << " x y lineto" << endl;
404  *fps << " /last_x x def" << endl;
405  *fps << " /last_y y def" << endl;
406  *fps << " } for stroke" << endl;
407  *fps << "grestore" << endl;
408 
409  return 0;
410 }
411 
Complex
Definition: Complex.h:23
Bstring
Definition: Bstring.h:29
Complex.h
Class for complex numbers.
ps_seq_hydrophob
int ps_seq_hydrophob(Bstring &filename, Bstring &title, int length, double *Hphob, int *HPseg, double *nseq, Complex< float > *per)
Generates a postscript plot from a hydrophobicity analysis of a protein sequence alignment.
Definition: ps_sequence.cpp:194
length
double length(vector< T > v)
Definition: Vector3.h:530
M_PI_2
#define M_PI_2
Definition: utilities.h:66
ps_plot.h
Header file for postscript output functions.
ps_close
int ps_close(ofstream *fps)
Closes a postscript file.
Definition: ps_plot.cpp:105
ps_seq_info
int ps_seq_info(Bstring &filename, Bstring &title, int nres, int length, double *info, double *nseq, Complex< float > *per, double *freq, char *pattern)
Generates a postscript plot from an information content analysis of a protein sequence alignment.
Definition: ps_sequence.cpp:33
ps_open_and_init
ofstream * ps_open_and_init(Bstring filename, Bstring title, int npages, int width, int height)
Opens and initializes a postscript file.
Definition: ps_plot.cpp:32
ps_seq_periodicity
int ps_seq_periodicity(ofstream *fps, int length, Complex< float > *per)
Definition: ps_sequence.cpp:324
verbose
int verbose
ps_scale
int ps_scale(ofstream *fps, double x1, double y1, double x2, double y2, double min, double max, double increment, double tick_length, double tick_angle, int digits, int fontsize, int inverse)
Generates a scale for a postscript graph.
Definition: ps_plot.cpp:488
Complex::phi
double phi()
Definition: Complex.h:105
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
ps_seq_representation
int ps_seq_representation(ofstream *fps, int length, double *nseq)
Definition: ps_sequence.cpp:282
Complex::amp
double amp()
Definition: Complex.h:104