Debian Codegraph @

hmmer/easel/miniapps/esl-ssdraw.c

raw symbols SCIP
1 /* Draw secondary structure diagrams given a postscript SS template.
2 * Initial development of this program was for SSU rRNA structures
3 * with templates derived from Gutell's CRW (Comparative RNA Website,
4 * http://www.rna.ccbb.utexas.edu/).
5 *
6 */
7 #include <esl_config.h>
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <math.h>
14 #include <time.h>
15
16 #include "easel.h"
17 #include "esl_arr2.h"
18 #include "esl_arr3.h"
19 #include "esl_distance.h"
20 #include "esl_dmatrix.h"
21 #include "esl_fileparser.h"
22 #include "esl_getopts.h"
23 #include "esl_keyhash.h"
24 #include "esl_msa.h"
25 #include "esl_msafile.h"
26 #include "esl_msafile2.h"
27 #include "esl_sq.h"
28 #include "esl_sqio.h"
29 #include "esl_stack.h"
30 #include "esl_tree.h"
31 #include "esl_vectorops.h"
32 #include "esl_wuss.h"
33
34 #define ERRBUFSIZE 1024
35 #define MAXMBWITHOUTFORCE 100
36
37 #define ALIMODE 0
38 #define INDIMODE 1
39 #define SIMPLEMASKMODE 2
40 #define INFILEMODE 3
41
42 /* CMYK colors, a 4 value array defines a color with values 0..1,
43 * [0] is cyan value, [1] is magenta, [2] is yellow, [3] is black */
44 #define NCMYK 4
45 #define ICYAN 0
46 #define IMGTA 1
47 #define IYELW 2
48 #define IBLCK 3
49
50 /* indices and sizes for hard-coded color schemes */
51 #define NSCHEMES 7
52
53 #define RB_11_RH_SCHEME 0 /* RainBow, 11 colors, Red High, blue low */
54 #define RB_11_RL_SCHEME 1 /* RainBow, 11 colors, Red Low, blue high */
55 #define RB_6_RH_SCHEME 2 /* RainBow, 6 colors, Red High, blue low */
56 #define RB_6_RL_SCHEME 3 /* RainBow, 6 colors, Red Low, blue high */
57 #define RB_5_RH_SCHEME 4 /* RainBow, 5 colors, Red High, teal low */
58 #define RB_5_RL_SCHEME 5 /* RainBow, 5 colors, Red low, teal high */
59 #define RB_W5_OH_SCHEME 6 /* RainBow with white, 5 colors, orange high, teal low, white lowest */
60
61 #define NRB_11_RH_SCHEME 11
62 #define NRB_11_RL_SCHEME 11
63 #define NRB_6_RH_SCHEME 6
64 #define NRB_6_RL_SCHEME 6
65 #define NRB_5_RH_SCHEME 5
66 #define NRB_5_RL_SCHEME 5
67 #define NRB_W5_OH_SCHEME 5
68
69 /* the actual CMYK color values for the scheme colors (_C = cyan, _M = magenta, _Y = yellow, _K = black) */
70
71 /* the 11-color rainbow scheme */
72 #define RED2BLUE_1_OF_11_C 0.00
73 #define RED2BLUE_1_OF_11_M 0.94
74 #define RED2BLUE_1_OF_11_Y 1.00
75 #define RED2BLUE_1_OF_11_K 0.00
76
77 #define RED2BLUE_2_OF_11_C 0.00
78 #define RED2BLUE_2_OF_11_M 0.84
79 #define RED2BLUE_2_OF_11_Y 1.00
80 #define RED2BLUE_2_OF_11_K 0.00
81
82 #define RED2BLUE_3_OF_11_C 0.00
83 #define RED2BLUE_3_OF_11_M 0.63
84 #define RED2BLUE_3_OF_11_Y 1.00
85 #define RED2BLUE_3_OF_11_K 0.00
86
87 #define RED2BLUE_4_OF_11_C 0.00
88 #define RED2BLUE_4_OF_11_M 0.42
89 #define RED2BLUE_4_OF_11_Y 1.00
90 #define RED2BLUE_4_OF_11_K 0.00
91
92 #define RED2BLUE_5_OF_11_C 0.00
93 #define RED2BLUE_5_OF_11_M 0.21
94 #define RED2BLUE_5_OF_11_Y 1.00
95 #define RED2BLUE_5_OF_11_K 0.00
96
97 #define RED2BLUE_6_OF_11_C 0.00
98 #define RED2BLUE_6_OF_11_M 0.00
99 #define RED2BLUE_6_OF_11_Y 1.00
100 #define RED2BLUE_6_OF_11_K 0.00
101
102 #define RED2BLUE_7_OF_11_C 0.42
103 #define RED2BLUE_7_OF_11_M 0.00
104 #define RED2BLUE_7_OF_11_Y 1.00
105 #define RED2BLUE_7_OF_11_K 0.00
106
107 #define RED2BLUE_8_OF_11_C 0.61
108 #define RED2BLUE_8_OF_11_M 0.00
109 #define RED2BLUE_8_OF_11_Y 0.56
110 #define RED2BLUE_8_OF_11_K 0.22
111
112 #define RED2BLUE_9_OF_11_C 0.50
113 #define RED2BLUE_9_OF_11_M 0.00
114 #define RED2BLUE_9_OF_11_Y 0.00
115 #define RED2BLUE_9_OF_11_K 0.50
116
117 #define RED2BLUE_10_OF_11_C 0.78
118 #define RED2BLUE_10_OF_11_M 0.56
119 #define RED2BLUE_10_OF_11_Y 0.00
120 #define RED2BLUE_10_OF_11_K 0.22
121
122 #define RED2BLUE_11_OF_11_C 0.92
123 #define RED2BLUE_11_OF_11_M 0.84
124 #define RED2BLUE_11_OF_11_Y 0.00
125 #define RED2BLUE_11_OF_11_K 0.08
126
127
128 /* the 6-color rainbow scheme */
129 /* red */
130 #define RED2BLUE_1_OF_6_C 0.00
131 #define RED2BLUE_1_OF_6_M 0.94
132 #define RED2BLUE_1_OF_6_Y 1.00
133 #define RED2BLUE_1_OF_6_K 0.00
134
135 /* orange-ish */
136 #define RED2BLUE_2_OF_6_C 0.00
137 #define RED2BLUE_2_OF_6_M 0.63
138 #define RED2BLUE_2_OF_6_Y 1.00
139 #define RED2BLUE_2_OF_6_K 0.00
140
141 /* gold-ish */
142 #define RED2BLUE_3_OF_6_C 0.00
143 #define RED2BLUE_3_OF_6_M 0.21
144 #define RED2BLUE_3_OF_6_Y 1.00
145 #define RED2BLUE_3_OF_6_K 0.00
146
147 /* light-green-ish */
148 #define RED2BLUE_4_OF_6_C 0.42
149 #define RED2BLUE_4_OF_6_M 0.00
150 #define RED2BLUE_4_OF_6_Y 1.00
151 #define RED2BLUE_4_OF_6_K 0.00
152
153 /* teal-ish */
154 #define RED2BLUE_5_OF_6_C 0.50
155 #define RED2BLUE_5_OF_6_M 0.00
156 #define RED2BLUE_5_OF_6_Y 0.00
157 #define RED2BLUE_5_OF_6_K 0.50
158
159 /* blue */
160 #define RED2BLUE_6_OF_6_C 0.92
161 #define RED2BLUE_6_OF_6_M 0.84
162 #define RED2BLUE_6_OF_6_Y 0.00
163 #define RED2BLUE_6_OF_6_K 0.08
164
165
166 /* single colors for one-cell legends */
167 #define NOC 17
168 #define CYANOC 0
169 #define MAGENTAOC 1
170 #define YELLOWOC 2
171 #define BLACKOC 3
172 #define WHITEOC 4
173 #define LIGHTGREYOC 5
174 #define GREYOC 6
175 #define DARKGREYOC 7
176 #define REDOC 8
177 #define ORANGEOC 9
178 #define TEALOC 10
179 #define LIGHTGREENOC 11
180 #define GREENOC 12
181 #define DARKGREENOC 13
182 #define LIGHTPURPLEOC 14
183 #define PURPLEOC 15
184 #define DARKPURPLEOC 16
185
186 /* the actual CMYK color values for the single colors (_C = cyan, _M = magenta, _Y = yellow, _K = black) */
187 #define CYAN_C 1.0
188 #define CYAN_M 0.0
189 #define CYAN_Y 0.0
190 #define CYAN_K 0.0
191
192 #define MAGENTA_C 0.0
193 #define MAGENTA_M 1.0
194 #define MAGENTA_Y 0.0
195 #define MAGENTA_K 0.0
196
197 #define YELLOW_C 0.0
198 #define YELLOW_M 0.0
199 #define YELLOW_Y 1.0
200 #define YELLOW_K 0.0
201
202 #define BLACK_C 0.0
203 #define BLACK_M 0.0
204 #define BLACK_Y 0.0
205 #define BLACK_K 1.0
206
207 #define WHITE_C 0.0
208 #define WHITE_M 0.0
209 #define WHITE_Y 0.0
210 #define WHITE_K 0.0
211
212 #define LIGHTGREY_C 0.0
213 #define LIGHTGREY_M 0.0
214 #define LIGHTGREY_Y 0.0
215 #define LIGHTGREY_K 0.2
216
217 #define GREY_C 0.0
218 #define GREY_M 0.0
219 #define GREY_Y 0.0
220 #define GREY_K 0.5
221
222 #define DARKGREY_C 0.0
223 #define DARKGREY_M 0.0
224 #define DARKGREY_Y 0.0
225 #define DARKGREY_K 0.75
226
227 #define RED_C 0.0
228 #define RED_M 1.0
229 #define RED_Y 1.0
230 #define RED_K 0.0
231
232 #define ORANGE_C 0.0
233 #define ORANGE_M 0.5
234 #define ORANGE_Y 1.0
235 #define ORANGE_K 0.0
236
237 #define TEAL_C 0.5
238 #define TEAL_M 0.0
239 #define TEAL_Y 0.0
240 #define TEAL_K 0.5
241
242 #define LIGHTGREEN_C 0.5
243 #define LIGHTGREEN_M 0.0
244 #define LIGHTGREEN_Y 0.5
245 #define LIGHTGREEN_K 0.0
246
247 #define GREEN_C 1.0
248 #define GREEN_M 0.0
249 #define GREEN_Y 1.0
250 #define GREEN_K 0.0
251
252 #define DARKGREEN_C 1.0
253 #define DARKGREEN_M 0.5
254 #define DARKGREEN_Y 1.0
255 #define DARKGREEN_K 0.0
256
257 #define LIGHTPURPLE_C 0.3
258 #define LIGHTPURPLE_M 0.6
259 #define LIGHTPURPLE_Y 0.0
260 #define LIGHTPURPLE_K 0.0
261
262 #define PURPLE_C 0.5
263 #define PURPLE_M 1.0
264 #define PURPLE_Y 0.0
265 #define PURPLE_K 0.0
266
267 #define DARKPURPLE_C 1.0
268 #define DARKPURPLE_M 1.0
269 #define DARKPURPLE_Y 0.0
270 #define DARKPURPLE_K 0.0
271
272 /* hard-coded values for the legends */
273 #define LEG_NBOXES 11
274 #define LEG_MINFONTSIZE 10
275 #define SPECIAL_FONT "Courier-BoldOblique"
276 #define LEG_FONT "Courier-Bold"
277 #define LEG_EXTRA_COLUMNS 12 /* how many extra columns we need for printing stats in the legend */
278 #define COURIER_HEIGHT_WIDTH_RATIO 1.65
279 #define LEG_EXTRA_TEXT_FONT "Helvetica"
280
281 /* fonts and other sizes */
282 #define DEFAULT_FONT "Courier-Bold"
283 #define FOOTER_FONT "Helvetica"
284 #define NUCLEOTIDES_FONT "Courier-Bold" /* NOTE: THIS MUST BE Courier-Bold or Courier, the offsets for drawing cells (CELL_{X,Y}OFFSET_FRACTION)
285 * and placing nucleotides (NUCLEOTIDE_{X,Y}OFFSET_FRACTION_LOWERCASE_GJPQY) depend on it */
286 /*#define NUCLEOTIDES_FONT "Monaco-Bold"*/
287 #define POSNTEXT_FONT "Helvetica"
288 #define NUCLEOTIDES_FONTSIZE 8.
289 #define POSNTEXT_FONTSIZE 8.
290 #define LEG_FONTSIZE_UNSCALED 8
291 #define LEG_EXTRA_TEXT_FONTSIZE_UNSCALED 8
292 #define HEADER_FONTSIZE_UNSCALED 12
293 #define HEADER_MODELNAME_MAXCHARS 20
294 #define TICKS_LINEWIDTH 2.
295 #define BP_LINEWIDTH 1.
296 #define CELLSIZE 8. /* default size of a cell, a single position in the structure diagram */
297 #define CELLSIZE_INT 8 /* default size of a cell, a single position in the structure diagram */
298 /* NOTE, *OFFSET_FRACTION* constants below depend on NUCLEOTIDES_FONT being either "Courier-Bold" or "Courier" */
299 #define CELL_XOFFSET_FRACTION 0.2
300 #define CELL_YOFFSET_FRACTION 0.2
301 #define LOWERCASE_LOW_HANGING_CHARS "gjpqy"
302 #define NUCLEOTIDE_YOFFSET_FRACTION_LOWERCASE_LOW_HANGING_CHARS 0.1875
303
304 /* postscript info */
305 #define POSTSCRIPT_PAGEWIDTH 612.
306 #define POSTSCRIPT_PAGEHEIGHT 792.
307 #define PAGE_TOPBUF 30. /* 30 blank pts required at top */
308 #define PAGE_SIDEBUF 32. /* 32 blank pts required at sides */
309 #define PAGE_BOTBUF 30. /* 30 blank pts required at bot */
310
311 /* cell outline info */
312 #define OUTLINE_LINEWIDTH_CELL_FRACTION_MIN 0.04
313 #define OUTLINE_LINEWIDTH_CELL_FRACTION_MAX 0.16
314 #define NOUTLINETYPES 2
315 #define OUTLINE_NONE_IDX 0
316 #define OUTLINE_MIN_IDX 1
317 #define OUTLINE_MAX_IDX 2
318 #define OUTLINE_PROCEDURE "box"
319
320 /* one cell color legends, special values */
321 #define OCCL_BLANK_COUNT -1
322
323 /* Structure: scheme_color_legend
324 * Incept: EPN, Thu Jun 25 20:20:38 2009
325 *
326 * Parameters describing a one-dimensional legend of colors
327 * from a preset scheme for use in a SSPostscript_t data structure.
328 */
329 typedef struct scheme_color_legend_s {
330 int scheme; /* preset color scheme index */
331 int nbins; /* number of colors (bins) in this scheme */
332 char *text1; /* first line of text for legend, a single string */
333 char *text2; /* second line of text for legend, a single string */
334 float *limits; /* [nbins+1] limits for each bin, limits[0] is min value we would expect to see, limits[nbins] is max */
335 int *counts; /* [nbins] number of cells we've painted each color */
336 int *counts_masked; /* [nbins] number of cells within mask ('1's) that we've painted each color */
337 int ints_only_flag; /* TRUE if possible values are only integers, legend values will be drawn differently in this case */
338 int low_inclusive; /* TRUE if bin 0 is inclusive of limits[0], FALSE if not */
339 int high_inclusive; /* TRUE if bin[nbins-1] is inclusive of max value, FALSE if not */
340 int use_mask; /* TRUE to use ps->mask if it is available to draw masked positions differently, FALSE not to */
341 } SchemeColorLegend_t;
342
343 /* Structure: onecell_color_legend
344 * Incept: EPN, Tue Sep 30 13:06:15 2008
345 *
346 * Parameters describing a single colored cell legend for a
347 * SSPostscript_t data structure.
348 */
349 typedef struct onecell_color_legend_s {
350 float col[NCMYK]; /* [CMYK] color value for the cell */
351 char *text; /* description text for legend */
352 char *celltext; /* colored text to use instead of a block, if NULL a colored block will be used */
353 char *procname; /* name of a procedure for drawing the block, if NULL a colored block will be used */
354 /* NOTE only one of celltext or procname can be non-NULL */
355 float *procstack; /* array of <nprocstack> stack elements for the procedure */
356 int nprocstack; /* num els in <procstack> */
357 int nres; /* number of nucleotides colored by the color in col[NCMYK] */
358 int nres_masked; /* number of nucleotides within a mask colored by the color in col[NCMYK] */
359 int do_separator; /* TRUE to draw a separator line below this one cell color legend, FALSE not to */
360 int do_no_vspace; /* when printing one cell, legend, don't add any vertical space after it, usually this is FALSE */
361 } OneCellColorLegend_t;
362
363 /* Structure: text_legend
364 * Incept: EPN, Mon Apr 19 07:11:44 2010
365 *
366 * Parameters describing a text section of a legend for a
367 * SSPostscript_t data structure.
368 */
369 typedef struct text_legend_s {
370 char **text_per_line; /* description text for legend */
371 int nlines; /* colored text to use instead of a block, if NULL a colored block will be used */
372 int do_separator; /* TRUE to draw a separator line below this text section, FALSE not to */
373 } TextLegend_t;
374
375 /* Structure: ss_postscript
376 * Incept: EPN, Mon Jun 23 15:50:30 2008
377 *
378 * A clumsy data structure for storing the information that will
379 * become a postscript secondary structure diagram based on a
380 * template created by Robin Gutell and colleagues.
381 *
382 */
383 typedef struct ss_postscript_s {
384 int npage; /* number of pages in eventual postscript */
385 char *modelname; /* name of model, read from template file */
386 int *modeA; /* [0..npage-1] page mode, ALIMODE, INDIMODE, or SIMPLEMASKMODE */
387 char **descA; /* [0..npage-1] description for each page */
388 int desc_max_chars; /* max num characters for a page description */
389 float headerx; /* x coordinate (bottom left corner) of header area */
390 float headery; /* y coordinate (bottom left corner) of header area */
391 float headerx_charsize;/* size of a character in x-dimension in the header */
392 float headery_charsize;/* size of a character in y-dimension in the header */
393 float headerx_desc; /* x coordinate (bottom left corner) of header area */
394 int leg_posn; /* consensus position for placing legend, read from template */
395 int leg_cellsize; /* size of a cell in the legend, (ex. 24 for SSU models) */
396 float leg_rhs_space;/* extra space to leave to the right of the legend */
397 float legx_offset; /* offset in x coordinate for placing legend, legx will be ps->rxA[leg_posn-1] + legx_offset */
398 float legy_offset; /* offset in y coordinate for placing legend, legy will be ps->ryA[leg_posn-1] + legy_offset */
399 float legx; /* x coordinate (top left corner) of legend area */
400 float legy; /* y coordinate (top left corner) of legend area */
401 float cur_legy; /* y coordinate of current line in legend */
402 float legx_charsize;/* size of a character in x-dimension in the legend */
403 float legy_charsize;/* size of a character in y-dimension in the legend */
404 int legx_max_chars; /* max num nucleotides in x direction we can print in legend before running off page */
405 int legy_max_chars; /* max num nucleotides in y direction we can print in legend before running off page */
406 int legx_stats; /* x position for printing stats in the legend */
407 float pagex_max; /* max x position on page */
408 float pagey_max; /* max y position on page */
409 float scale; /* scale parameter, read from template file */
410 char **regurgA; /* [0..nregurg-1][] lines from the template file to regurgitate, these are unchanged. */
411 int nregurg; /* number of lines (char *'s) in the regurg_textAA 2D array */
412 char **posntextA; /* [0..i..nposntext-1] string for element i of position text, read from template */
413 float *posntextxA; /* [0..i..nposntext-1] x value for posntextA[i] */
414 float *posntextyA; /* [0..i..nposntext-1] y value for posntextA[i] */
415 int nposntext; /* number of elements in posntextx and posntexty */
416 float *ticksx1A; /* [0..nticks-1] x begin value for ticks */
417 float *ticksx2A; /* [0..nticks-1] x end value for ticks */
418 float *ticksy1A; /* [0..nticks-1] y begin value for ticks */
419 float *ticksy2A; /* [0..nticks-1] x end value for ticks */
420 int nticks; /* number of ticks */
421 float *bpx1A; /* [0..nbp-1] x begin value for bp connect line */
422 float *bpx2A; /* [0..nbp-1] x end value for bp connect line */
423 float *bpy1A; /* [0..nbp-1] y begin value for bp connect line */
424 float *bpy2A; /* [0..nbp-1] x end value for bp connect line */
425 int nbp; /* number of bp */
426 float *rxA; /* [0..rflen-1] x coordinate for each nucleotide in the eventual postscript */
427 float *ryA; /* [0..rflen-1] y coordinate for each nucleotide in the eventual postscript */
428 int rflen; /* the number of nucleotides in the template file */
429 char **rAA; /* [0..npage-1][0..rflen-1] nucleotide character in the eventual postscript */
430 float ***rcolAAA; /* [0..npage-1][0..rflen-1][0..3] color for nucleotide on page p, position c, CMYK in the eventual postscript */
431 float ***bcolAAA; /* [0..npage-1][0..rflen-1][0..3] color for block on page p, position c, CMYK in the eventual postscript */
432 char **otypeAA; /* [0..npage-1][0..rflen-1] outline type on page p, position c in the eventual postscript
433 * '0' '1' or '2', OUTLINE_MIN_IDX, OUTLINE_MID_IDX, OUTLINE_MAX_IDX, respectively */
434 OneCellColorLegend_t ***occlAAA;/* [0..npage-1][0..l..nocclA[p]] ptr to one cell color legend l for page p */
435 int *nocclA; /* [0..npage-1] number of one cell color legends for each page */
436 SchemeColorLegend_t **sclAA;/* [0..npage-1] ptr to scheme color legend l for page p, NULL if none */
437 TextLegend_t ***tlAAA;/* [0..npage-1][0..l..ntlA[p]] ptr to text legend l for page p */
438 int *ntlA; /* [0..npage-1] number of text legends for page p, NULL if none */
439 char *mask; /* mask for this postscript, columns which are '0' get drawn differently */
440 int nalloc; /* number of elements to add to arrays when reallocating */
441 int msa_nseq; /* number of sequences in the msa, impt b/c msa->nseq will be 0 if --small */
442 char *msa_cseq_maj;/* [0..rfpos..ps->rflen-1]: consensus sequence for the msa, determined using majority rule */
443 char *msa_cseq_amb;/* [0..rfpos..ps->rflen-1]: different consensus sequence for the msa, least ambiguous
444 * nt per nongap RF position that represents >= esl_opt_GetReal(go, --athresh) fraction of
445 * nongap nucleotides at position rfpos */
446 int *msa_ct; /* [1..ps->rflen] CT array for msa this postscript corresponds to,
447 * msa_ct[i] is the position that consensus nucleotide i base pairs to, or 0 if i is unpaired. */
448 int msa_nbp; /* number of bps read from current MSA (in msa_ct), should equal nbp, but only if bps read from template file */
449 int *msa_rf2a_map;/* [0..ps->rflen-1] = apos, apos is the alignment position (0..msa->alen-1) that is non-gap RF position rfpos (for rfpos in 0..rflen-1) */
450 int *msa_a2rf_map;/* [0..ps->msa->alen-1] = rfpos, rfpos is the non-gap RF position (0..ps->rflen) that is alignment position apos (for apos in 0..ps->msa_alen-1) */
451 int *uaseqlenA; /* [0..ps->msa->nseq-1] unaligned sequence length for all sequences in the MSA, only computed if --indi */
452 int *seqidxA; /* [0..ps->npage-1] the sequence index in the MSA each page corresponds to, only valid if --indi */
453 ESL_MSA *msa; /* pointer to MSA this object corresponds to */
454 const ESL_ALPHABET *abc; /* ptr to alphabet used to decipher gap chars (msa itself is text mode) */
455 } SSPostscript_t;
456
457 static SSPostscript_t *create_sspostscript(const ESL_GETOPTS *go);
458 static int setup_sspostscript(SSPostscript_t *ps, char *errbuf);
459 static OneCellColorLegend_t *create_onecell_colorlegend(float *cmykA, int nres, int nres_masked, int do_separator, int do_no_vspace);
460 static SchemeColorLegend_t *create_scheme_colorlegend(int scheme, int ncols, float *limits, int ints_only_flag, int low_inclusive, int high_inclusive, int use_mask);
461 static TextLegend_t *create_text_legend(int nlines, char **text_per_line, int do_separator);
462 static TextLegend_t *create_text_legend_for_consensus_sequence(const ESL_GETOPTS *go, int do_separator);
463 static int add_text_to_scheme_colorlegend(SSPostscript_t *ps, SchemeColorLegend_t *scl, char *text, int legx_max_chars, char *errbuf);
464 static int add_text_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *text, int legx_max_chars, char *errbuf);
465 static int add_celltext_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *celltext, char *errbuf);
466 static int add_procedure_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *procname, float *procstack, int nprocstack, char *errbuf);
467 static int add_page_desc_to_sspostscript(SSPostscript_t *ps, int page, char *text, char *errbuf);
468 static int add_diffmask_page_desc_to_sspostscript(SSPostscript_t *ps, int page, char *mask_file, char *maskdiff_file, char *errbuf);
469 static int draw_sspostscript(FILE *fp, const ESL_GETOPTS *go, char *errbuf, char *command, char *date, float ***hc_scheme, SSPostscript_t *ps);
470 static int draw_legend_column_headers(FILE *fp, SSPostscript_t *ps, int pagenum, char *errbuf);
471 static int draw_onecell_colorlegend(FILE *fp, OneCellColorLegend_t *occl, SSPostscript_t *ps, int occl_idx, int pagenum);
472 static int draw_scheme_colorlegend(const ESL_GETOPTS *go, FILE *fp, SchemeColorLegend_t *scl, float **hc_scheme, SSPostscript_t *ps, int pagenum);
473 static void free_sspostscript(SSPostscript_t *ps);
474 static int add_pages_sspostscript(SSPostscript_t *ps, int ntoadd, int page_mode);
475 static int parse_template_file(char *filename, const ESL_GETOPTS *go, char *errbuf, int msa_rflen, SSPostscript_t **ret_ps);
476 static int parse_template_page(ESL_FILEPARSER *efp, const ESL_GETOPTS *go, char *errbuf, SSPostscript_t **ret_ps);
477 static int parse_modelname_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
478 static int parse_legend_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
479 static int parse_scale_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
480 static int parse_ignore_section(ESL_FILEPARSER *efp, char *errbuf, int *ret_read_showpage);
481 static int parse_regurgitate_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
482 static int parse_text_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
483 static int parse_lines_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps);
484 static int validate_justread_sspostscript(SSPostscript_t *ps, char *errbuf);
485 static int validate_and_update_sspostscript_given_msa(const ESL_GETOPTS *go, const ESL_ALPHABET *abc, SSPostscript_t *ps, ESL_MSA *msa, int msa_nseq, char *errbuf);
486 static int read_mask_file(char *filename, char *errbuf, char **ret_mask, int *ret_masklen, int *ret_mask_has_internal_zeroes);
487 static void PairCount(const ESL_ALPHABET *abc, double *counters, ESL_DSQ syml, ESL_DSQ symr, double wt);
488 static int get_command(const ESL_GETOPTS *go, char *errbuf, char **ret_command);
489 static int get_date(char *errbuf, char **ret_date);
490 static int set_scheme_values(char *errbuf, float *vec, int ncolvals, float **scheme, float val, SchemeColorLegend_t *scl, int within_mask, int *ret_bi);
491 static int set_onecell_values(char *errbuf, float *vec, int ncolvals, float *onecolor);
492 static int add_mask_to_ss_postscript(SSPostscript_t *ps, char *mask);
493 static int draw_masked_block(FILE *fp, float x, float y, float *colvec, int do_circle_mask, int do_square_mask, int do_x_mask, int do_border, float cellsize);
494 static int draw_header_and_footer(FILE *fp, const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int page, int pageidx2print);
495 static void get_insert_info_from_msa(const ESL_ALPHABET *abc, ESL_MSA *msa, int rflen, int **ret_nseq_with_ins_ct, int **ret_nins_ct, int ***ret_per_seq_ins_ct);
496 static void get_insert_info_from_abc_ct(double **abc_ct, ESL_ALPHABET *abc, char *msa_rf, int64_t msa_alen, int rflen, int **ret_nseq_with_ins_ct, int **ret_nins_ct);
497 static void get_insert_info_from_ifile(char *ifile, int rflen, int msa_nseq, ESL_KEYHASH *useme_keyhash, int **ret_nseq_with_ins_ct, int **ret_nins_ct, int ***ret_per_seq_ins_ct, int **ret_soff_ct, int **ret_eoff_ct);
498 static int count_msa(const ESL_ALPHABET *abc, ESL_MSA *msa, char *errbuf, double ***ret_abc_ct, double ****ret_bp_ct, double ***ret_pp_ct, int **ret_spos_ct, int **ret_epos_ct);
499 static int get_consensus_seqs_from_abc_ct(const ESL_GETOPTS *go,SSPostscript_t *ps, char *errbuf, double **abc_ct, ESL_ALPHABET *abc, int64_t msa_alen);
500 static int infocontent_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_onecell_idx, FILE *tabfp);
501 static int mutual_information_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double ***bp_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int ss_idx, int zerores_idx, FILE *tabfp);
502 static int delete_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, int *span_ct, int msa_nseq, int do_all, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zerodel_idx, int hc_fewdel_idx, FILE *tabfp);
503 static int avg_posteriors_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **pp_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_onecell_idx, FILE *tabfp);
504 static int insertfreq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *nseq_with_ins_ct, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zeroins_idx, int hc_fewins_idx, FILE *tabfp);
505 static int insertavglen_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *nseq_with_ins_ct, int *nins_ct, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zeroins_idx, FILE *tabfp);
506 static int span_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int zercov_idx, int maxcov_idx, FILE *tabfp);
507 static int individuals_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, ESL_MSA *msa, int **per_seq_ins_ct, int do_prob, int do_rescol, float ***hc_scheme, int hc_scheme_idx_s, int hc_scheme_idx_p, int hc_nbins_s, int hc_nbins_p, float **hc_onecell, int extdel_idx_s, int wcbp_idx_s, int gubp_idx_s, int ncbp_idx_s, int dgbp_idx_s, int hgbp_idx_s, int gap_idx_p, FILE *tabfp);
508 static int cons_seq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, float **hc_onecell, int wcbp_idx_s, int gubp_idx_s, int ncbp_idx_s, int dgbp_idx_s, int hgbp_idx_s);
509 static int rf_seq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, int do_rescol, float **hc_onecell, int wcbp_idx_s, int gubp_idx_s, int ncbp_idx_s);
510 static int colormask_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, float **hc_onecell, int incmask_idx, int excmask_idx);
511 static int diffmask_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, char *mask2, float **hc_onecell, int incboth_idx, int inc1_idx, int inc2_idx, int excboth_idx);
512 static int drawfile2sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, float ***hc_scheme, int hc_scheme_idx, int hc_nbins);
513 static int expertfile2sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps);
514 static int get_pp_idx(const ESL_ALPHABET *abc, char ppchar);
515 static int get_span_ct(int *msa_rf2a_map, int64_t alen, int rflen, int nseq, int *spos_ct, int *epos_ct, int *srfoff_ct, int *erfoff_ct, int **ret_span_ct);
516 static int is_watson_crick_bp(char i, char j);
517 static int is_gu_or_ug_bp(char i, char j);
518 static int compare_two_cmyk_colors(float acol_C, float acol_M, float acol_Y, float acol_K, float bcol_C, float bcol_M, float bcol_Y, float bcol_K);
519 static void define_outline_procedure(FILE *fp);
520
521 static char banner[] = "draw postscript secondary structure diagrams";
522 static char usage[] = "[options] <msafile> <SS postscript template> <output postscript file name>\n\
523 The <msafile> must be in Stockholm format.";
524
525 static ESL_OPTIONS options[] = {
526 /* name type default env range togs reqs incomp help docgroup */
527 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "help; show brief info on version and usage", 1 },
528 { "-d", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw default set of alignment summary diagrams", 1 },
529 { "--mask", eslARG_INFILE, NULL, NULL, NULL, NULL,NULL, NULL, "for all diagrams, mark masked ('0') columns from mask in <f>", 1 },
530 { "--small", eslARG_NONE, NULL, NULL, NULL, NULL,NULL, NULL, "operate in small memory mode (aln must be 1 line/seq Pfam format)", 1 },
531
532 { "--cons", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw diagram showing the alignment's consensus sequence", 2 },
533 { "--info", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw information content diagram", 2 },
534 { "--mutinfo",eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw base pair mutual information diagram", 2 },
535 { "--ifreq", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw insert frequency diagram", 2 },
536 { "--iavglen",eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw average insert length diagram", 2 },
537 { "--dall", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw delete diagram w/all deletions (incl. terminal deletes)", 2 },
538 { "--prob", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw average posterior probability diagram", 2 },
539 { "--span", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw diagram showing fraction of seqs that span each posn", 2 },
540 { "--rf", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw diagram showing reference (#=GC RF) sequence", 2 },
541 { "--dint", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "draw delete diagram w/only internal (non-terminal) deletions", 2 },
542 { "--tabfile",eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "output per position data in tabular format to file <f>", 2 },
543
544 { "--indi", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, "--small", "draw diagrams for individual sequences in the alignment", 3 },
545 { "-f", eslARG_NONE, FALSE, NULL, NULL, NULL,"--indi", NULL, "force; w/--indi draw all seqs, even if predicted output >100 Mb", 3 },
546
547 { "--no-pp", eslARG_NONE, FALSE, NULL, NULL, NULL,"--indi", NULL, "with --indi, do not draw indi posterior probability diagrams", 9 },
548 { "--no-bp", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "do not color nucleotides based on basepair type", 9 },
549 { "--no-ol", eslARG_NONE, FALSE, NULL, NULL, NULL,"--indi", NULL, "w/--indi, do not outline nts that are not most common nt", 9 },
550 { "--no-ntpp", eslARG_NONE, FALSE, NULL, NULL, NULL,"--indi", "--no-pp", "w/--indi, do not draw nts on individual post prob diagrams", 9 },
551
552 { "--no-cnt", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "do not draw consensus nts on alignment summary diagrams", 7 },
553 { "--cthresh",eslARG_REAL, "0.75", NULL,"0<x<=1.0", NULL,NULL, "--no-cnt", "capitalize cons nts occuring in >= <x> fraction of seqs", 7 },
554 { "--cambig", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, "--no-cnt", "allow ambiguous nts in consensus sequence", 7 },
555 { "--athresh",eslARG_REAL, "0.9", NULL,"0<x<=1.0", NULL,"--cambig",NULL, "w/--cambig, cons nt must represent >= <x> fraction of seqs", 7 },
556
557 { "--mask-u", eslARG_NONE, FALSE, NULL, NULL, NULL,"--mask", "--mask-x","with --mask, mark masked columns as squares", 4 },
558 { "--mask-x", eslARG_NONE, FALSE, NULL, NULL, NULL,"--mask", "--mask-u","with --mask, mark masked columns as x's", 4 },
559 { "--mask-a", eslARG_NONE, FALSE, NULL, NULL, NULL,"--mask", NULL, "with --mask-u or --mask-x, draw alternative mask style", 4 },
560
561 { "--mask-col", eslARG_NONE, NULL,NULL, NULL, NULL,"--mask", NULL, "w/--mask draw two color diagram denoting masked columns", 5 },
562 { "--mask-diff",eslARG_INFILE,NULL,NULL, NULL, NULL,"--mask", NULL, "with --mask <f1>, compare mask in <f1> to mask in <f>", 5 },
563
564 { "--dfile", eslARG_INFILE, NULL,NULL, NULL, NULL,NULL, NULL, "read 'draw file' specifying >=1 diagrams", 6 },
565 { "--efile", eslARG_INFILE, NULL,NULL, NULL, NULL,NULL, NULL, "read 'expert draw file' specifying >=1 diagrams", 6 },
566 { "--ifile", eslARG_INFILE, NULL,NULL, NULL, NULL,NULL, NULL, "read insert information from cmalign insert file <f>", 6 },
567
568 { "--no-leg", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "do not draw legend", 8 },
569 { "--no-head",eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "do not draw header", 8 },
570 { "--no-foot",eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "do not draw footer", 8 },
571 { 0,0,0,0,0,0,0,0,0,0 },
572 };
573
574 int
575 main(int argc, char **argv)
576 {
577 ESL_GETOPTS *go = NULL; /* application configuration */
578 ESL_ALPHABET *abc = NULL; /* biological alphabet */
579 char *alifile = NULL; /* alignment file name */
580 char *outfile = NULL; /* output ps file name */
581 char *templatefile = NULL; /* template file, specifying >= 1 SS diagrams
582 * (each must have a unique consensus length) */
583 int fmt; /* format code for alifile */
584 ESL_MSAFILE *afp = NULL; /* open alignment file, normal interface */
585 ESL_MSAFILE2 *afp2 = NULL; /* open alignment file, legacy small-memory interface */
586 ESL_MSA *msa = NULL; /* one multiple sequence alignment */
587 int status; /* easel return code */
588 int rflen; /* non-gap RF (consensus) length of each alignment */
589 int apos; /* counter over alignment positions */
590 char errbuf[ERRBUFSIZE]; /* for printing error messages */
591 FILE *ofp = NULL; /* output file for postscript */
592 FILE *tabfp = NULL; /* output file for text data, only set to non-null if --tabfile enabled */
593 SSPostscript_t *ps = NULL; /* the postscript data structure we create */
594 int *hc_nbins = NULL; /* hard-coded number of bins (colors) for each possible scheme */
595 float ***hc_scheme = NULL; /* colors for each pre-defined scheme */
596 float **hc_onecell = NULL; /* single colors used for binary color schemes */
597 int z; /* counter */
598 char *command = NULL; /* string for printing command to stdout */
599 char *date = NULL; /* date command was executed */
600 char *mask = NULL; /* first mask we'll use, string of '0's and '1's */
601 int masklen; /* length of mask */
602 char *mask2 = NULL; /* second mask we'll use, string of '0's and '1's */
603 int mask2len; /* length of second mask */
604 int mask_has_internal_zeroes = FALSE; /* does mask have any '0's with at least '1' on both sides? */
605 int mask2_has_internal_zeroes = FALSE; /* does mask have any '0's with at least '1' on both sides? */
606 /* counts of relevant values from the msa */
607 int *nseq_with_ins_ct = NULL; /* [0..ps->rflen] number of sequences with >=1 insert after each consensus position, only used if --ifreq */
608 int *nins_ct = NULL; /* [0..ps->rflen] total number of inserted nucleotides (over all seqs) after each consensus position, only used if --ifreq */
609 int **per_seq_ins_ct = NULL; /* [0..msa->nseq-1][0..ps->rflen] for each sequence, the number of inserts after each consensus position */
610 double **abc_ct = NULL; /* [0..msa->alen-1][0..abc->K], count of each nucleotide at each position, over all sequences, missing and nonnucleotides are *not counted* */
611 double ***bp_ct = NULL; /* [0..msa->alen-1][0..abc->Kp][0..abc->Kp], count of each possible base pair at each position, over all sequences, missing and nonnucleotides are *not counted*
612 base pairs are indexed by 'i' for a base pair between positions i and j, where i < j. */
613 double **pp_ct = NULL; /* [0..msa->alen-1][0..11], count of reach posterior probability (PP) code, over all sequences, gap is 11 */
614 int *spos_ct = NULL; /* [0..msa->alen-1] per position count of first non-gap position, over all seqs */
615 int *epos_ct = NULL; /* [0..msa->alen-1] per position count of final non-gap position, over all seqs */
616 int *srfoff_ct = NULL; /* [0..rfpos..rflen-1], correction for spos_ct for rfpos, derived from ifile, only used if msa has had all insert columns removed */
617 int *erfoff_ct = NULL; /* [0..rfpos..rflen-1], correction for epos_ct for rfpos, derived from ifile, only used if msa has had all insert columns removed */
618 int *span_ct = NULL; /* [0..rfpos..rflen-1], number of sequences that 'span' position rfpos */
619 int64_t msa_alen; /* msa->alen */
620 int msa_nseq; /* msa->nseq */
621 /* variables related to small memory mode */
622 int do_small = TRUE; /* TRUE to operate in small memory mode, FALSE not to */
623 /* variables storing which pages to print */
624 int do_default_set = TRUE; /* TRUE if no options telling specifying what pages to draw were selected */
625 int do_cons = FALSE;
626 int do_rf = FALSE;
627 int do_info = FALSE;
628 int do_mutinfo = FALSE;
629 int do_ifreq = FALSE;
630 int do_iavglen = FALSE;
631 int do_dall = FALSE;
632 int do_dint = FALSE;
633 int do_prob = FALSE;
634 int do_span = FALSE;
635 int do_indi = FALSE;
636 int do_maskcol = FALSE;
637 int do_maskdiff = FALSE;
638 int do_dfile = FALSE;
639 int do_efile = FALSE;
640 int need_span_ct = FALSE; /* TRUE if span_ct must be calculated (if do_dint || do_ifreq || do_iavglen || do_span) */
641 int tmp_Mb = 0;
642 int predicted_Mb = 0; /* predicted size of the output file, calced if --indi */
643 /***********************************************
644 * Parse command line
645 ***********************************************/
646
647 go = esl_getopts_Create(options);
648 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
649 esl_opt_VerifyConfig(go) != eslOK)
650 {
651 printf("Failed to parse command line: %s\n", go->errbuf);
652 esl_usage(stdout, argv[0], usage);
653 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
654 exit(1);
655 }
656
657 if (esl_opt_GetBoolean(go, "-h") )
658 {
659 esl_banner(stdout, argv[0], banner);
660 esl_usage (stdout, argv[0], usage);
661 puts("\n where basic options are:");
662 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
663 puts("\noptions for alignment summary diagrams (incompatible with --indi):");
664 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
665 puts("\noptions for drawing individual sequences (require --indi):");
666 esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
667 puts("\noptions for omitting parts of the diagram:");
668 esl_opt_DisplayHelp(stdout, go, 8, 2, 80);
669 puts("\noptions for drawing simple two color diagrams of masks:");
670 esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
671 puts("\nexpert options for controlling individual seq diagrams (require --indi):");
672 esl_opt_DisplayHelp(stdout, go, 9, 2, 80);
673 puts("\nexpert options related to consensus sequence definition:");
674 esl_opt_DisplayHelp(stdout, go, 7, 2, 80);
675 puts("\nexpert options controlling style of masked positions:");
676 esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
677 puts("\nexpert options related to input files:");
678 esl_opt_DisplayHelp(stdout, go, 6, 2, 80);
679 exit(0);
680 }
681
682 if (esl_opt_ArgNumber(go) != 3)
683 {
684 printf("Incorrect number of command line arguments.\n");
685 esl_usage(stdout, argv[0], usage);
686 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
687 exit(1);
688 }
689
690 /* Check for incompatible options that aren't simple to check with esl_getopts */
691 /* --mask-a requires either --mask-x or --mask-u */
692 if (esl_opt_IsOn(go, "--mask-a") && (! esl_opt_IsOn(go, "--mask-u")) && (! esl_opt_IsOn(go, "--mask-x"))) {
693 esl_fatal("--mask-a requires either --mask-u or mask-x");
694 }
695
696 alifile = esl_opt_GetArg(go, 1);
697 templatefile = esl_opt_GetArg(go, 2);
698 outfile = esl_opt_GetArg(go, 3);
699
700 if((status = get_command(go, errbuf, &command)) != eslOK) esl_fatal(errbuf);
701 if((status = get_date(errbuf, &date)) != eslOK) esl_fatal(errbuf);
702
703 /****************/
704 /* Premlinaries */
705 /****************/
706 /* allocate and fill predefined one-cell colors, these are hardcoded */
707 ESL_ALLOC(hc_onecell, sizeof(float *) * NOC);
708 for(z = 0; z < NOC; z++) hc_onecell[z] = NULL;
709 for(z = 0; z < NOC; z++) { ESL_ALLOC(hc_onecell[z], sizeof(float) * NCMYK); }
710
711 hc_onecell[CYANOC][ICYAN] = CYAN_C;
712 hc_onecell[CYANOC][IMGTA] = CYAN_M;
713 hc_onecell[CYANOC][IYELW] = CYAN_Y;
714 hc_onecell[CYANOC][IBLCK] = CYAN_K;
715
716 hc_onecell[MAGENTAOC][ICYAN] = MAGENTA_C;
717 hc_onecell[MAGENTAOC][IMGTA] = MAGENTA_M;
718 hc_onecell[MAGENTAOC][IYELW] = MAGENTA_Y;
719 hc_onecell[MAGENTAOC][IBLCK] = MAGENTA_K;
720
721 hc_onecell[YELLOWOC][ICYAN] = YELLOW_C;
722 hc_onecell[YELLOWOC][IMGTA] = YELLOW_M;
723 hc_onecell[YELLOWOC][IYELW] = YELLOW_Y;
724 hc_onecell[YELLOWOC][IBLCK] = YELLOW_K;
725
726 hc_onecell[BLACKOC][ICYAN] = BLACK_C;
727 hc_onecell[BLACKOC][IMGTA] = BLACK_M;
728 hc_onecell[BLACKOC][IYELW] = BLACK_Y;
729 hc_onecell[BLACKOC][IBLCK] = BLACK_K;
730
731 hc_onecell[WHITEOC][ICYAN] = WHITE_C;
732 hc_onecell[WHITEOC][IMGTA] = WHITE_M;
733 hc_onecell[WHITEOC][IYELW] = WHITE_Y;
734 hc_onecell[WHITEOC][IBLCK] = WHITE_K;
735
736 hc_onecell[LIGHTGREYOC][ICYAN] = LIGHTGREY_C;
737 hc_onecell[LIGHTGREYOC][IMGTA] = LIGHTGREY_M;
738 hc_onecell[LIGHTGREYOC][IYELW] = LIGHTGREY_Y;
739 hc_onecell[LIGHTGREYOC][IBLCK] = LIGHTGREY_K;
740
741 hc_onecell[GREYOC][ICYAN] = GREY_C;
742 hc_onecell[GREYOC][IMGTA] = GREY_M;
743 hc_onecell[GREYOC][IYELW] = GREY_Y;
744 hc_onecell[GREYOC][IBLCK] = GREY_K;
745
746 hc_onecell[DARKGREYOC][ICYAN] = DARKGREY_C;
747 hc_onecell[DARKGREYOC][IMGTA] = DARKGREY_M;
748 hc_onecell[DARKGREYOC][IYELW] = DARKGREY_Y;
749 hc_onecell[DARKGREYOC][IBLCK] = DARKGREY_K;
750
751 hc_onecell[REDOC][ICYAN] = RED_C;
752 hc_onecell[REDOC][IMGTA] = RED_M;
753 hc_onecell[REDOC][IYELW] = RED_Y;
754 hc_onecell[REDOC][IBLCK] = RED_K;
755
756 hc_onecell[ORANGEOC][ICYAN] = ORANGE_C;
757 hc_onecell[ORANGEOC][IMGTA] = ORANGE_M;
758 hc_onecell[ORANGEOC][IYELW] = ORANGE_Y;
759 hc_onecell[ORANGEOC][IBLCK] = ORANGE_K;
760
761 hc_onecell[TEALOC][ICYAN] = TEAL_C;
762 hc_onecell[TEALOC][IMGTA] = TEAL_M;
763 hc_onecell[TEALOC][IYELW] = TEAL_Y;
764 hc_onecell[TEALOC][IBLCK] = TEAL_K;
765
766 hc_onecell[LIGHTGREENOC][ICYAN] = LIGHTGREEN_C;
767 hc_onecell[LIGHTGREENOC][IMGTA] = LIGHTGREEN_M;
768 hc_onecell[LIGHTGREENOC][IYELW] = LIGHTGREEN_Y;
769 hc_onecell[LIGHTGREENOC][IBLCK] = LIGHTGREEN_K;
770
771 hc_onecell[GREENOC][ICYAN] = GREEN_C;
772 hc_onecell[GREENOC][IMGTA] = GREEN_M;
773 hc_onecell[GREENOC][IYELW] = GREEN_Y;
774 hc_onecell[GREENOC][IBLCK] = GREEN_K;
775
776 hc_onecell[DARKGREENOC][ICYAN] = DARKGREEN_C;
777 hc_onecell[DARKGREENOC][IMGTA] = DARKGREEN_M;
778 hc_onecell[DARKGREENOC][IYELW] = DARKGREEN_Y;
779 hc_onecell[DARKGREENOC][IBLCK] = DARKGREEN_K;
780
781 hc_onecell[LIGHTPURPLEOC][ICYAN] = LIGHTPURPLE_C;
782 hc_onecell[LIGHTPURPLEOC][IMGTA] = LIGHTPURPLE_M;
783 hc_onecell[LIGHTPURPLEOC][IYELW] = LIGHTPURPLE_Y;
784 hc_onecell[LIGHTPURPLEOC][IBLCK] = LIGHTPURPLE_K;
785
786 hc_onecell[PURPLEOC][ICYAN] = PURPLE_C;
787 hc_onecell[PURPLEOC][IMGTA] = PURPLE_M;
788 hc_onecell[PURPLEOC][IYELW] = PURPLE_Y;
789 hc_onecell[PURPLEOC][IBLCK] = PURPLE_K;
790
791 hc_onecell[DARKPURPLEOC][ICYAN] = DARKPURPLE_C;
792 hc_onecell[DARKPURPLEOC][IMGTA] = DARKPURPLE_M;
793 hc_onecell[DARKPURPLEOC][IYELW] = DARKPURPLE_Y;
794 hc_onecell[DARKPURPLEOC][IBLCK] = DARKPURPLE_K;
795
796 /***********************************/
797 /* allocate and fill predefined color schemes, these are hardcoded */
798 ESL_ALLOC(hc_scheme, sizeof(float **) * NSCHEMES);
799 for (z = 0; z < NSCHEMES; z++) hc_scheme[z] = NULL;
800
801 ESL_ALLOC(hc_nbins, sizeof(int) * NSCHEMES);
802 hc_nbins[RB_11_RH_SCHEME] = NRB_11_RH_SCHEME;
803 hc_nbins[RB_11_RL_SCHEME] = NRB_11_RL_SCHEME;
804 hc_nbins[RB_6_RH_SCHEME] = NRB_6_RH_SCHEME;
805 hc_nbins[RB_6_RL_SCHEME] = NRB_6_RL_SCHEME;
806 hc_nbins[RB_5_RH_SCHEME] = NRB_5_RH_SCHEME;
807 hc_nbins[RB_5_RL_SCHEME] = NRB_5_RL_SCHEME;
808 hc_nbins[RB_W5_OH_SCHEME] = NRB_W5_OH_SCHEME;
809
810 /* RB_11_RH_SCHEME RainBow, 11 colors, Red High, blue low */
811 ESL_ALLOC(hc_scheme[RB_11_RH_SCHEME], sizeof(float *) * NRB_11_RH_SCHEME);
812 for(z = 0; z < NRB_11_RH_SCHEME; z++) hc_scheme[RB_11_RH_SCHEME][z] = NULL;
813 for(z = 0; z < NRB_11_RH_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_11_RH_SCHEME][z], sizeof(float) * NCMYK); }
814
815 /* RB_11_RL_SCHEME RainBow, 11 colors, Red Low, blue high */
816 ESL_ALLOC(hc_scheme[RB_11_RL_SCHEME], sizeof(float *) * NRB_11_RL_SCHEME);
817 for(z = 0; z < NRB_11_RL_SCHEME; z++) hc_scheme[RB_11_RL_SCHEME][z] = NULL;
818 for(z = 0; z < NRB_11_RL_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_11_RL_SCHEME][z], sizeof(float) * NCMYK); }
819
820 /* RB_6_RH_SCHEME RainBow, 6 colors, Red High, blue low */
821 ESL_ALLOC(hc_scheme[RB_6_RH_SCHEME], sizeof(float *) * NRB_6_RH_SCHEME);
822 for(z = 0; z < NRB_6_RH_SCHEME; z++) hc_scheme[RB_6_RH_SCHEME][z] = NULL;
823 for(z = 0; z < NRB_6_RH_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_6_RH_SCHEME][z], sizeof(float) * NCMYK); }
824
825 /* RB_6_RL_SCHEME RainBow, 6 colors, Red Low, blue high */
826 ESL_ALLOC(hc_scheme[RB_6_RL_SCHEME], sizeof(float *) * NRB_6_RL_SCHEME);
827 for(z = 0; z < NRB_6_RL_SCHEME; z++) hc_scheme[RB_6_RL_SCHEME][z] = NULL;
828 for(z = 0; z < NRB_6_RL_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_6_RL_SCHEME][z], sizeof(float) * NCMYK); }
829
830 /* RB_5_RH_SCHEME RainBow, 5 colors, Red High, blue low */
831 ESL_ALLOC(hc_scheme[RB_5_RH_SCHEME], sizeof(float *) * NRB_5_RH_SCHEME);
832 for(z = 0; z < NRB_5_RH_SCHEME; z++) hc_scheme[RB_5_RH_SCHEME][z] = NULL;
833 for(z = 0; z < NRB_5_RH_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_5_RH_SCHEME][z], sizeof(float) * NCMYK); }
834
835 /* RB_5_RL_SCHEME RainBow, 5 colors, Red Low, blue high */
836 ESL_ALLOC(hc_scheme[RB_5_RL_SCHEME], sizeof(float *) * NRB_5_RL_SCHEME);
837 for(z = 0; z < NRB_5_RL_SCHEME; z++) hc_scheme[RB_5_RL_SCHEME][z] = NULL;
838 for(z = 0; z < NRB_5_RL_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_5_RL_SCHEME][z], sizeof(float) * NCMYK); }
839
840 /* RB_W5_OH_SCHEME RainBow with white, 5 colors, Orange High, teal low, white lowest */
841 ESL_ALLOC(hc_scheme[RB_W5_OH_SCHEME], sizeof(float *) * NRB_W5_OH_SCHEME);
842 for(z = 0; z < NRB_W5_OH_SCHEME; z++) hc_scheme[RB_W5_OH_SCHEME][z] = NULL;
843 for(z = 0; z < NRB_W5_OH_SCHEME; z++) { ESL_ALLOC(hc_scheme[RB_W5_OH_SCHEME][z], sizeof(float) * NCMYK); }
844
845 /***********************************/
846 /* fill scheme colors */
847
848 /* the 5 and 6 color rainbow schemes (the 5 color schemes are identical to the 6 color ones, they just omit blue */
849 hc_scheme[RB_6_RL_SCHEME][0][ICYAN] = hc_scheme[RB_6_RH_SCHEME][5][ICYAN] = hc_scheme[RB_5_RL_SCHEME][0][ICYAN] = hc_scheme[RB_5_RL_SCHEME][4][ICYAN] = RED2BLUE_1_OF_6_C;
850 hc_scheme[RB_6_RL_SCHEME][0][IMGTA] = hc_scheme[RB_6_RH_SCHEME][5][IMGTA] = hc_scheme[RB_5_RL_SCHEME][0][IMGTA] = hc_scheme[RB_5_RL_SCHEME][4][IMGTA] = RED2BLUE_1_OF_6_M;
851 hc_scheme[RB_6_RL_SCHEME][0][IYELW] = hc_scheme[RB_6_RH_SCHEME][5][IYELW] = hc_scheme[RB_5_RL_SCHEME][0][IYELW] = hc_scheme[RB_5_RL_SCHEME][4][IYELW] = RED2BLUE_1_OF_6_Y;
852 hc_scheme[RB_6_RL_SCHEME][0][IBLCK] = hc_scheme[RB_6_RH_SCHEME][5][IBLCK] = hc_scheme[RB_5_RL_SCHEME][0][IBLCK] = hc_scheme[RB_5_RL_SCHEME][4][IBLCK] = RED2BLUE_1_OF_6_K;
853
854 hc_scheme[RB_6_RL_SCHEME][1][ICYAN] = hc_scheme[RB_6_RH_SCHEME][4][ICYAN] = hc_scheme[RB_5_RL_SCHEME][1][ICYAN] = hc_scheme[RB_5_RL_SCHEME][3][ICYAN] = RED2BLUE_2_OF_6_C;
855 hc_scheme[RB_6_RL_SCHEME][1][IMGTA] = hc_scheme[RB_6_RH_SCHEME][4][IMGTA] = hc_scheme[RB_5_RL_SCHEME][1][IMGTA] = hc_scheme[RB_5_RL_SCHEME][3][IMGTA] = RED2BLUE_2_OF_6_M;
856 hc_scheme[RB_6_RL_SCHEME][1][IYELW] = hc_scheme[RB_6_RH_SCHEME][4][IYELW] = hc_scheme[RB_5_RL_SCHEME][1][IYELW] = hc_scheme[RB_5_RL_SCHEME][3][IYELW] = RED2BLUE_2_OF_6_Y;
857 hc_scheme[RB_6_RL_SCHEME][1][IBLCK] = hc_scheme[RB_6_RH_SCHEME][4][IBLCK] = hc_scheme[RB_5_RL_SCHEME][1][IBLCK] = hc_scheme[RB_5_RL_SCHEME][3][IBLCK] = RED2BLUE_2_OF_6_K;
858
859 hc_scheme[RB_6_RL_SCHEME][2][ICYAN] = hc_scheme[RB_6_RH_SCHEME][3][ICYAN] = hc_scheme[RB_5_RL_SCHEME][2][ICYAN] = RED2BLUE_3_OF_6_C;
860 hc_scheme[RB_6_RL_SCHEME][2][IMGTA] = hc_scheme[RB_6_RH_SCHEME][3][IMGTA] = hc_scheme[RB_5_RL_SCHEME][2][IMGTA] = RED2BLUE_3_OF_6_M;
861 hc_scheme[RB_6_RL_SCHEME][2][IYELW] = hc_scheme[RB_6_RH_SCHEME][3][IYELW] = hc_scheme[RB_5_RL_SCHEME][2][IYELW] = RED2BLUE_3_OF_6_Y;
862 hc_scheme[RB_6_RL_SCHEME][2][IBLCK] = hc_scheme[RB_6_RH_SCHEME][3][IBLCK] = hc_scheme[RB_5_RL_SCHEME][2][IBLCK] = RED2BLUE_3_OF_6_K;
863
864 hc_scheme[RB_6_RL_SCHEME][3][ICYAN] = hc_scheme[RB_6_RH_SCHEME][2][ICYAN] = hc_scheme[RB_5_RL_SCHEME][3][ICYAN] = hc_scheme[RB_5_RL_SCHEME][1][ICYAN] = RED2BLUE_4_OF_6_C;
865 hc_scheme[RB_6_RL_SCHEME][3][IMGTA] = hc_scheme[RB_6_RH_SCHEME][2][IMGTA] = hc_scheme[RB_5_RL_SCHEME][3][IMGTA] = hc_scheme[RB_5_RL_SCHEME][1][IMGTA] = RED2BLUE_4_OF_6_M;
866 hc_scheme[RB_6_RL_SCHEME][3][IYELW] = hc_scheme[RB_6_RH_SCHEME][2][IYELW] = hc_scheme[RB_5_RL_SCHEME][3][IYELW] = hc_scheme[RB_5_RL_SCHEME][1][IYELW] = RED2BLUE_4_OF_6_Y;
867 hc_scheme[RB_6_RL_SCHEME][3][IBLCK] = hc_scheme[RB_6_RH_SCHEME][2][IBLCK] = hc_scheme[RB_5_RL_SCHEME][3][IBLCK] = hc_scheme[RB_5_RL_SCHEME][1][IBLCK] = RED2BLUE_4_OF_6_K;
868
869 hc_scheme[RB_6_RL_SCHEME][4][ICYAN] = hc_scheme[RB_6_RH_SCHEME][1][ICYAN] = hc_scheme[RB_5_RL_SCHEME][4][ICYAN] = hc_scheme[RB_5_RL_SCHEME][0][ICYAN] = RED2BLUE_5_OF_6_C;
870 hc_scheme[RB_6_RL_SCHEME][4][IMGTA] = hc_scheme[RB_6_RH_SCHEME][1][IMGTA] = hc_scheme[RB_5_RL_SCHEME][4][IMGTA] = hc_scheme[RB_5_RL_SCHEME][0][IMGTA] = RED2BLUE_5_OF_6_M;
871 hc_scheme[RB_6_RL_SCHEME][4][IYELW] = hc_scheme[RB_6_RH_SCHEME][1][IYELW] = hc_scheme[RB_5_RL_SCHEME][4][IYELW] = hc_scheme[RB_5_RL_SCHEME][0][IYELW] = RED2BLUE_5_OF_6_Y;
872 hc_scheme[RB_6_RL_SCHEME][4][IBLCK] = hc_scheme[RB_6_RH_SCHEME][1][IBLCK] = hc_scheme[RB_5_RL_SCHEME][4][IBLCK] = hc_scheme[RB_5_RL_SCHEME][0][IBLCK] = RED2BLUE_5_OF_6_K;
873
874 hc_scheme[RB_6_RL_SCHEME][5][ICYAN] = hc_scheme[RB_6_RH_SCHEME][0][ICYAN] = RED2BLUE_6_OF_6_C;
875 hc_scheme[RB_6_RL_SCHEME][5][IMGTA] = hc_scheme[RB_6_RH_SCHEME][0][IMGTA] = RED2BLUE_6_OF_6_M;
876 hc_scheme[RB_6_RL_SCHEME][5][IYELW] = hc_scheme[RB_6_RH_SCHEME][0][IYELW] = RED2BLUE_6_OF_6_Y;
877 hc_scheme[RB_6_RL_SCHEME][5][IBLCK] = hc_scheme[RB_6_RH_SCHEME][0][IBLCK] = RED2BLUE_6_OF_6_K;
878
879 /* the special 4 color, plus white color scheme */
880 hc_scheme[RB_W5_OH_SCHEME][0][ICYAN] = WHITE_C;
881 hc_scheme[RB_W5_OH_SCHEME][0][IMGTA] = WHITE_M;
882 hc_scheme[RB_W5_OH_SCHEME][0][IYELW] = WHITE_Y;
883 hc_scheme[RB_W5_OH_SCHEME][0][IBLCK] = WHITE_K;
884
885 hc_scheme[RB_W5_OH_SCHEME][1][ICYAN] = RED2BLUE_5_OF_6_C;
886 hc_scheme[RB_W5_OH_SCHEME][1][IMGTA] = RED2BLUE_5_OF_6_M;
887 hc_scheme[RB_W5_OH_SCHEME][1][IYELW] = RED2BLUE_5_OF_6_Y;
888 hc_scheme[RB_W5_OH_SCHEME][1][IBLCK] = RED2BLUE_5_OF_6_K;
889
890 hc_scheme[RB_W5_OH_SCHEME][2][ICYAN] = RED2BLUE_4_OF_6_C;
891 hc_scheme[RB_W5_OH_SCHEME][2][IMGTA] = RED2BLUE_4_OF_6_M;
892 hc_scheme[RB_W5_OH_SCHEME][2][IYELW] = RED2BLUE_4_OF_6_Y;
893 hc_scheme[RB_W5_OH_SCHEME][2][IBLCK] = RED2BLUE_4_OF_6_K;
894
895 hc_scheme[RB_W5_OH_SCHEME][3][ICYAN] = RED2BLUE_3_OF_6_C;
896 hc_scheme[RB_W5_OH_SCHEME][3][IMGTA] = RED2BLUE_3_OF_6_M;
897 hc_scheme[RB_W5_OH_SCHEME][3][IYELW] = RED2BLUE_3_OF_6_Y;
898 hc_scheme[RB_W5_OH_SCHEME][3][IBLCK] = RED2BLUE_3_OF_6_K;
899
900 hc_scheme[RB_W5_OH_SCHEME][4][ICYAN] = RED2BLUE_2_OF_6_C;
901 hc_scheme[RB_W5_OH_SCHEME][4][IMGTA] = RED2BLUE_2_OF_6_M;
902 hc_scheme[RB_W5_OH_SCHEME][4][IYELW] = RED2BLUE_2_OF_6_Y;
903 hc_scheme[RB_W5_OH_SCHEME][4][IBLCK] = RED2BLUE_2_OF_6_K;
904
905 /* the 11 color rainbow schemes */
906 hc_scheme[RB_11_RL_SCHEME][0][ICYAN] = hc_scheme[RB_11_RH_SCHEME][10][ICYAN] = RED2BLUE_1_OF_11_C;
907 hc_scheme[RB_11_RL_SCHEME][0][IMGTA] = hc_scheme[RB_11_RH_SCHEME][10][IMGTA] = RED2BLUE_1_OF_11_M;
908 hc_scheme[RB_11_RL_SCHEME][0][IYELW] = hc_scheme[RB_11_RH_SCHEME][10][IYELW] = RED2BLUE_1_OF_11_Y;
909 hc_scheme[RB_11_RL_SCHEME][0][IBLCK] = hc_scheme[RB_11_RH_SCHEME][10][IBLCK] = RED2BLUE_1_OF_11_K;
910
911 hc_scheme[RB_11_RL_SCHEME][1][ICYAN] = hc_scheme[RB_11_RH_SCHEME][9][ICYAN] = RED2BLUE_2_OF_11_C;
912 hc_scheme[RB_11_RL_SCHEME][1][IMGTA] = hc_scheme[RB_11_RH_SCHEME][9][IMGTA] = RED2BLUE_2_OF_11_M;
913 hc_scheme[RB_11_RL_SCHEME][1][IYELW] = hc_scheme[RB_11_RH_SCHEME][9][IYELW] = RED2BLUE_2_OF_11_Y;
914 hc_scheme[RB_11_RL_SCHEME][1][IBLCK] = hc_scheme[RB_11_RH_SCHEME][9][IBLCK] = RED2BLUE_2_OF_11_K;
915
916 hc_scheme[RB_11_RL_SCHEME][2][ICYAN] = hc_scheme[RB_11_RH_SCHEME][8][ICYAN] = RED2BLUE_3_OF_11_C;
917 hc_scheme[RB_11_RL_SCHEME][2][IMGTA] = hc_scheme[RB_11_RH_SCHEME][8][IMGTA] = RED2BLUE_3_OF_11_M;
918 hc_scheme[RB_11_RL_SCHEME][2][IYELW] = hc_scheme[RB_11_RH_SCHEME][8][IYELW] = RED2BLUE_3_OF_11_Y;
919 hc_scheme[RB_11_RL_SCHEME][2][IBLCK] = hc_scheme[RB_11_RH_SCHEME][8][IBLCK] = RED2BLUE_3_OF_11_K;
920
921 hc_scheme[RB_11_RL_SCHEME][3][ICYAN] = hc_scheme[RB_11_RH_SCHEME][7][ICYAN] = RED2BLUE_4_OF_11_C;
922 hc_scheme[RB_11_RL_SCHEME][3][IMGTA] = hc_scheme[RB_11_RH_SCHEME][7][IMGTA] = RED2BLUE_4_OF_11_M;
923 hc_scheme[RB_11_RL_SCHEME][3][IYELW] = hc_scheme[RB_11_RH_SCHEME][7][IYELW] = RED2BLUE_4_OF_11_Y;
924 hc_scheme[RB_11_RL_SCHEME][3][IBLCK] = hc_scheme[RB_11_RH_SCHEME][7][IBLCK] = RED2BLUE_4_OF_11_K;
925
926 hc_scheme[RB_11_RL_SCHEME][4][ICYAN] = hc_scheme[RB_11_RH_SCHEME][6][ICYAN] = RED2BLUE_5_OF_11_C;
927 hc_scheme[RB_11_RL_SCHEME][4][IMGTA] = hc_scheme[RB_11_RH_SCHEME][6][IMGTA] = RED2BLUE_5_OF_11_M;
928 hc_scheme[RB_11_RL_SCHEME][4][IYELW] = hc_scheme[RB_11_RH_SCHEME][6][IYELW] = RED2BLUE_5_OF_11_Y;
929 hc_scheme[RB_11_RL_SCHEME][4][IBLCK] = hc_scheme[RB_11_RH_SCHEME][6][IBLCK] = RED2BLUE_5_OF_11_K;
930
931 hc_scheme[RB_11_RL_SCHEME][5][ICYAN] = hc_scheme[RB_11_RH_SCHEME][5][ICYAN] = RED2BLUE_6_OF_11_C;
932 hc_scheme[RB_11_RL_SCHEME][5][IMGTA] = hc_scheme[RB_11_RH_SCHEME][5][IMGTA] = RED2BLUE_6_OF_11_M;
933 hc_scheme[RB_11_RL_SCHEME][5][IYELW] = hc_scheme[RB_11_RH_SCHEME][5][IYELW] = RED2BLUE_6_OF_11_Y;
934 hc_scheme[RB_11_RL_SCHEME][5][IBLCK] = hc_scheme[RB_11_RH_SCHEME][5][IBLCK] = RED2BLUE_6_OF_11_K;
935
936 hc_scheme[RB_11_RL_SCHEME][6][ICYAN] = hc_scheme[RB_11_RH_SCHEME][4][ICYAN] = RED2BLUE_7_OF_11_C;
937 hc_scheme[RB_11_RL_SCHEME][6][IMGTA] = hc_scheme[RB_11_RH_SCHEME][4][IMGTA] = RED2BLUE_7_OF_11_M;
938 hc_scheme[RB_11_RL_SCHEME][6][IYELW] = hc_scheme[RB_11_RH_SCHEME][4][IYELW] = RED2BLUE_7_OF_11_Y;
939 hc_scheme[RB_11_RL_SCHEME][6][IBLCK] = hc_scheme[RB_11_RH_SCHEME][4][IBLCK] = RED2BLUE_7_OF_11_K;
940
941 hc_scheme[RB_11_RL_SCHEME][7][ICYAN] = hc_scheme[RB_11_RH_SCHEME][3][ICYAN] = RED2BLUE_8_OF_11_C;
942 hc_scheme[RB_11_RL_SCHEME][7][IMGTA] = hc_scheme[RB_11_RH_SCHEME][3][IMGTA] = RED2BLUE_8_OF_11_M;
943 hc_scheme[RB_11_RL_SCHEME][7][IYELW] = hc_scheme[RB_11_RH_SCHEME][3][IYELW] = RED2BLUE_8_OF_11_Y;
944 hc_scheme[RB_11_RL_SCHEME][7][IBLCK] = hc_scheme[RB_11_RH_SCHEME][3][IBLCK] = RED2BLUE_8_OF_11_K;
945
946 hc_scheme[RB_11_RL_SCHEME][8][ICYAN] = hc_scheme[RB_11_RH_SCHEME][2][ICYAN] = RED2BLUE_9_OF_11_C;
947 hc_scheme[RB_11_RL_SCHEME][8][IMGTA] = hc_scheme[RB_11_RH_SCHEME][2][IMGTA] = RED2BLUE_9_OF_11_M;
948 hc_scheme[RB_11_RL_SCHEME][8][IYELW] = hc_scheme[RB_11_RH_SCHEME][2][IYELW] = RED2BLUE_9_OF_11_Y;
949 hc_scheme[RB_11_RL_SCHEME][8][IBLCK] = hc_scheme[RB_11_RH_SCHEME][2][IBLCK] = RED2BLUE_9_OF_11_K;
950
951 hc_scheme[RB_11_RL_SCHEME][9][ICYAN] = hc_scheme[RB_11_RH_SCHEME][1][ICYAN] = RED2BLUE_10_OF_11_C;
952 hc_scheme[RB_11_RL_SCHEME][9][IMGTA] = hc_scheme[RB_11_RH_SCHEME][1][IMGTA] = RED2BLUE_10_OF_11_M;
953 hc_scheme[RB_11_RL_SCHEME][9][IYELW] = hc_scheme[RB_11_RH_SCHEME][1][IYELW] = RED2BLUE_10_OF_11_Y;
954 hc_scheme[RB_11_RL_SCHEME][9][IBLCK] = hc_scheme[RB_11_RH_SCHEME][1][IBLCK] = RED2BLUE_10_OF_11_K;
955
956 hc_scheme[RB_11_RL_SCHEME][10][ICYAN] = hc_scheme[RB_11_RH_SCHEME][0][ICYAN] = RED2BLUE_11_OF_11_C;
957 hc_scheme[RB_11_RL_SCHEME][10][IMGTA] = hc_scheme[RB_11_RH_SCHEME][0][IMGTA] = RED2BLUE_11_OF_11_M;
958 hc_scheme[RB_11_RL_SCHEME][10][IYELW] = hc_scheme[RB_11_RH_SCHEME][0][IYELW] = RED2BLUE_11_OF_11_Y;
959 hc_scheme[RB_11_RL_SCHEME][10][IBLCK] = hc_scheme[RB_11_RH_SCHEME][0][IBLCK] = RED2BLUE_11_OF_11_K;
960
961 /*****************************************
962 * Open the MSA file; determine alphabet;
963 *****************************************/
964
965 do_small = esl_opt_GetBoolean(go, "--small") ? TRUE : FALSE;
966 fmt = do_small ? eslMSAFILE_PFAM : eslMSAFILE_STOCKHOLM;
967 abc = esl_alphabet_Create(eslRNA); /* Assert RNA alphabet */
968
969 /* <abc> is only used for gap counting, I believe
970 * alignments are opened in TEXT mode.
971 */
972
973 if (do_small)
974 {
975 status = esl_msafile2_Open(alifile, NULL, &afp2);
976 if (status == eslENOTFOUND) esl_fatal("Alignment file %s doesn't exist or is not readable\n", alifile);
977 else if (status != eslOK) esl_fatal("Alignment file open failed with error %d\n", status);
978 }
979 else
980 {
981 status = esl_msafile_Open(NULL, alifile, NULL, fmt, NULL, &afp);
982 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
983 }
984
985 /* Read the mask files, if nec */
986 if(esl_opt_IsOn(go, "--mask")) {
987 if((status = read_mask_file(esl_opt_GetString(go, "--mask"), errbuf, &mask, &masklen, &mask_has_internal_zeroes)) != eslOK) esl_fatal(errbuf);
988 }
989 if(esl_opt_IsOn(go, "--mask-diff")) {
990 if((status = read_mask_file(esl_opt_GetString(go, "--mask-diff"), errbuf, &mask2, &mask2len, &mask2_has_internal_zeroes)) != eslOK) esl_fatal(errbuf);
991 if(masklen != mask2len) esl_fatal("Mask in %f length (%d) differs from mask in %f (%d)!", esl_opt_GetString(go, "--mask"), masklen, esl_opt_GetString(go, "--mask-diff"), mask2len);
992 }
993
994 /* Open output files, if necessary */
995 if (esl_opt_IsOn(go, "--tabfile")) {
996 if((tabfp = fopen(esl_opt_GetString(go, "--tabfile"), "w")) == NULL) esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "--tabfile"));
997 }
998
999 /**********************
1000 * Read the alignment *
1001 **********************/
1002 if (do_small)
1003 {
1004 status = esl_msafile2_ReadInfoPfam(afp2, NULL, abc, -1, NULL, NULL, &msa, &msa_nseq, &msa_alen, NULL, NULL, NULL, NULL, NULL, &abc_ct, &pp_ct, NULL, NULL, NULL);
1005 if (status == eslEFORMAT) esl_fatal("Alignment file parse error:\n%s\n", afp2->errbuf);
1006 else if (status == eslEINVAL) esl_fatal("Alignment file parse error:\n%s\n", afp2->errbuf);
1007 else if (status == eslEOF) esl_fatal("No alignments found in file %s\n", alifile);
1008 else if (status != eslOK) esl_fatal("Alignment file read failed with error code %d\n%s", status, afp2);
1009
1010 msa->alen = msa_alen;
1011 }
1012 else
1013 {
1014 status = esl_msafile_Read(afp, &msa); /* if ! do_small, we read full aln into memory */
1015 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
1016
1017 msa_nseq = msa->nseq;
1018 msa_alen = msa->alen;
1019 }
1020
1021 if(msa->rf == NULL) esl_fatal("First MSA in %s does not have RF annotation.", alifile);
1022
1023 /* determine non-gap RF length (consensus length) */
1024 rflen = 0;
1025 for(apos = 0; apos < msa->alen; apos++) {
1026 if(esl_abc_CIsResidue(abc, msa->rf[apos])) {
1027 rflen++;
1028 }
1029 }
1030 /* We've read the alignment, now read the template postscript file (we do this second b/c the RF len of the alignment tells us which postscript template to use) */
1031 if((status = parse_template_file(templatefile, go, errbuf, rflen, &ps) != eslOK)) esl_fatal(errbuf);
1032
1033
1034 /**************************************************************************************************************
1035 * Now that we have the msa and know what template we're using, determine what type of pages we'll be drawing *
1036 **************************************************************************************************************/
1037 /* do_default_set: this is TRUE if either -d is enabled AND/OR zero of:
1038 * {--info, --mutinfo, --ifreq, --iavglen, --dall, --dint, --span, --mask-col, --mask-diff,
1039 * --dfile, --efile, --cons, --prob, --rf, --indi} are enabled.
1040 * When do_default_set is TRUE, we draw cons, info, mutinfo, ifreq, iavglen, dall, and prob
1041 * (if the alignment has posterior probabilities).
1042 *
1043 * Careful: this block assumes that all boolean options are OFF (FALSE) by default */
1044 do_default_set = TRUE;
1045 if(esl_opt_GetBoolean(go, "--info")) { do_info = TRUE; do_default_set = FALSE; }
1046 if(esl_opt_GetBoolean(go, "--mutinfo")) { do_mutinfo = TRUE; do_default_set = FALSE; }
1047 if(esl_opt_GetBoolean(go, "--ifreq")) { do_ifreq = TRUE; do_default_set = FALSE; }
1048 if(esl_opt_GetBoolean(go, "--iavglen")) { do_iavglen = TRUE; do_default_set = FALSE; }
1049 if(esl_opt_GetBoolean(go, "--dall")) { do_dall = TRUE; do_default_set = FALSE; }
1050 if(esl_opt_GetBoolean(go, "--dint")) { do_dint = TRUE; do_default_set = FALSE; }
1051 if(esl_opt_GetBoolean(go, "--prob")) { do_prob = TRUE; do_default_set = FALSE; }
1052 if(esl_opt_GetBoolean(go, "--span")) { do_span = TRUE; do_default_set = FALSE; }
1053 if(esl_opt_GetBoolean(go, "--mask-col")) { do_maskcol = TRUE; do_default_set = FALSE; }
1054 if(esl_opt_IsOn (go, "--mask-diff")) { do_maskdiff = TRUE; do_default_set = FALSE; }
1055 if(esl_opt_IsOn (go, "--dfile")) { do_dfile = TRUE; do_default_set = FALSE; }
1056 if(esl_opt_IsOn (go, "--efile")) { do_efile = TRUE; do_default_set = FALSE; }
1057 if(esl_opt_GetBoolean(go, "--cons")) { do_cons = TRUE; do_default_set = FALSE; }
1058 if(esl_opt_GetBoolean(go, "--rf")) {
1059 if(msa->rf == NULL) esl_fatal("--rf selected by msa does not have #=GC RF annotation");
1060 do_rf = TRUE;
1061 do_default_set = FALSE;
1062 }
1063 if(esl_opt_GetBoolean(go, "--indi")) {
1064 do_indi = TRUE;
1065 do_default_set = FALSE;
1066 /* Predict size of indi output file, based on two data points:
1067 * 2000 page tRNA rflen=71 is 35 Mb, 2000 page archaeal SSU rflen 1508 is 560 Mb,
1068 * =~ 0.0002 Mb per page per rfpos */
1069 predicted_Mb = (int) (ps->rflen * 0.0002 * msa_nseq);
1070 if(! esl_opt_GetBoolean(go, "--no-pp")) predicted_Mb *= 2;
1071 /* round to nearest 10 Mb */
1072 tmp_Mb = 10; while(tmp_Mb < predicted_Mb) tmp_Mb += 10;
1073 predicted_Mb = tmp_Mb;
1074 if(predicted_Mb > MAXMBWITHOUTFORCE && (! esl_opt_GetBoolean(go, "-f"))) {
1075 esl_fatal("WARNING: drawing individual seqs and msa has %d seqs in it,\noutput postcript file will be large (~%d Mb).\nUse -f to override this warning and do it anyway.", msa_nseq, predicted_Mb);
1076 }
1077 }
1078 /* if -d: if we've made do_default_set as FALSE, we set it back to TRUE */
1079 if(esl_opt_GetBoolean(go, "-d")) do_default_set = TRUE;
1080 if(do_default_set) { /* set default pages */
1081 do_cons = do_info = do_mutinfo = do_ifreq = do_iavglen = do_dall = TRUE;
1082 if((do_small && pp_ct != NULL) || (msa->pp != NULL)) do_prob = TRUE;
1083 }
1084 /* determine if tabfile was incorrectly used */
1085 if(tabfp != NULL &&
1086 do_info == FALSE && do_mutinfo == FALSE && do_ifreq == FALSE && do_prob == FALSE &&
1087 do_iavglen == FALSE && do_dall == FALSE && do_dint == FALSE && do_span == FALSE && do_indi == FALSE) {
1088 esl_fatal("--tabfile only makes sense w/0 other options, or with >= 1 of --info,--mutinfo,--ifreq,--iavglen,--prob,--dall,--dint,--span,--indi");
1089 }
1090 need_span_ct = (do_dint || do_span || do_ifreq || do_iavglen) ? TRUE : FALSE;
1091
1092 if(do_small && (do_dint || do_span || do_ifreq || do_iavglen || do_mutinfo)) {
1093 /* If we're in small mem mode, now that we know msa->rf and msa->ss_cons,
1094 * we do a second read of the msa to get bpct, spos_ct and epos_ct,
1095 * but only if we need them (if --span, --dint, or --mutinfo).
1096 * To do this, we close the alifile and reopen it, so we can read the 1st
1097 * alignment again.
1098 */
1099 esl_msafile2_Close(afp2);
1100 status = esl_msafile2_Open(alifile, NULL, &afp2);
1101 if (status == eslENOTFOUND) esl_fatal("2nd pass, alignment file %s doesn't exist or is not readable\n", alifile);
1102 else if (status == eslEFORMAT) esl_fatal("2nd pass, couldn't determine format of alignment %s\n", alifile);
1103 else if (status != eslOK) esl_fatal("2nd pass, alignment file open failed with error %d\n", status);
1104
1105 status = esl_msafile2_ReadInfoPfam(afp2, NULL, abc, msa_alen, msa->rf, msa->ss_cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bp_ct, &spos_ct, &epos_ct);
1106 if (status == eslEFORMAT) esl_fatal("2nd pass, Alignment file parse error:\n%s\n", afp2->errbuf);
1107 else if (status == eslEINVAL) esl_fatal("2nd pass, Alignment file parse error:\n%s\n", afp2->errbuf);
1108 else if (status == eslEOF) esl_fatal("2nd pass, No alignments found in file %s\n", alifile);
1109 else if (status != eslOK) esl_fatal("2nd pass, Alignment file read failed with error code %d\n", status);
1110 }
1111
1112 /* if we get here, the postscript file has been successfully read; now we open the output file */
1113 if(ofp == NULL) {
1114 /* open postscript output file for writing */
1115 if ((ofp = fopen(outfile, "w")) == NULL)
1116 esl_fatal("Failed to open output postscript file %s\n", esl_opt_GetArg(go, 2));
1117 }
1118
1119 /* determine position for header and legend */
1120 if((status = setup_sspostscript(ps, errbuf) != eslOK)) esl_fatal(errbuf);
1121 if(ps->rflen == 0) esl_fatal("MSA has consensus (non-gap RF) length of %d which != template file consensus length of %d.", rflen, ps->rflen);
1122 if(rflen != ps->rflen) esl_fatal("MSA has consensus (non-gap RF) length of %d which != template file consensus length of %d.", rflen, ps->rflen);
1123
1124 /* add the mask if there is one */
1125 if(mask != NULL) add_mask_to_ss_postscript(ps, mask);
1126 if(mask != NULL && ps->rflen != masklen) esl_fatal("MSA has consensus (non-gap RF) length of %d which != lane mask length of %d from mask file %s.", rflen, masklen, esl_opt_GetString(go, "--mask"));
1127
1128 if ((status = validate_and_update_sspostscript_given_msa(go, abc, ps, msa, msa_nseq, errbuf)) != eslOK) esl_fatal(errbuf);
1129
1130 /* get the information we need from the alignment */
1131 if(! do_small) { /* derive counts from the msa for postscript diagrams, we do this our functions for drawing diagrams work in small mem or big mem mode */
1132 if((status = count_msa(abc, msa, errbuf, &(abc_ct), &(bp_ct), (do_prob ? &(pp_ct) : NULL), &(spos_ct), &(epos_ct))) != eslOK) esl_fatal(errbuf);
1133 }
1134 if (esl_opt_GetBoolean(go, "--prob") && pp_ct == NULL) esl_fatal("--prob requires all sequences have PP annotation");
1135
1136 /* read the insert file, if nec, we have to do this before we determine the span count in case inserts have been removed from the msa,
1137 * in which case the spos_ct and epos_ct's from either count_msa or esl_msafile2_ReadInfoPfam() could be slightly incorrect, we'll use
1138 * srfoff_ct and erfoff_ct from get_insert_info_from_ifile() to correct them when we derive span_ct from spos_ct and epos_ct together in get_span_ct(). */
1139 if(esl_opt_IsOn(go, "--ifile")) {
1140 get_insert_info_from_ifile((esl_opt_GetString(go, "--ifile")), ps->rflen, msa_nseq, NULL, &(nseq_with_ins_ct), &(nins_ct), NULL, &srfoff_ct, &erfoff_ct); /* dies with esl_fatal() upon an error */
1141 }
1142 /* determine span count */
1143 if(need_span_ct) {
1144 if((status = get_span_ct(ps->msa_rf2a_map, msa_alen, ps->rflen, msa_nseq, spos_ct, epos_ct, srfoff_ct, erfoff_ct, &span_ct)) != eslOK) esl_fatal("Out of memory, getting span_ct array.");
1145 }
1146 /* determine consensus sequence */
1147 if((status = get_consensus_seqs_from_abc_ct(go, ps, errbuf, abc_ct, abc, msa_alen)) != eslOK) esl_fatal(errbuf);
1148
1149 /* step through each type of page, creating it if nec */
1150 if(do_cons) {
1151 /* this will work in either small memory or normal memory mode */
1152 if((status = cons_seq_sspostscript(go, errbuf, ps, hc_onecell,
1153 BLACKOC, /* one-cell legend, watson-crick bp color */
1154 GREENOC, /* one-cell legend, G-U or U-G bp color */
1155 REDOC, /* one-cell legend, color for noncanonical bps */
1156 LIGHTPURPLEOC, /* one-cell legend, internal double-gap bp color */
1157 LIGHTPURPLEOC)) != eslOK) /* one-cell legend, internal half-gap bp color, only relevant if do_rescol == TRUE */
1158 esl_fatal(errbuf);
1159 }
1160
1161 if(do_rf) {
1162 /* this will work in either small memory or normal memory mode */
1163 if((status = rf_seq_sspostscript(go, errbuf, ps, msa, (! esl_opt_GetBoolean(go, "--no-bp")), hc_onecell,
1164 BLACKOC, /* one-cell legend, watson-crick bp color, only relevant if do_rescol == TRUE */
1165 GREENOC, /* one-cell legend, G-U or U-G bp color, only relevant if do_rescol == TRUE */
1166 REDOC)) != eslOK) /* one-cell legend, color for noncanonical bps */
1167 esl_fatal(errbuf);
1168 }
1169
1170 if(do_info) {
1171 if((status = infocontent_sspostscript(go, abc, errbuf, ps, abc_ct, msa_nseq, hc_scheme, RB_6_RL_SCHEME, hc_nbins[RB_6_RL_SCHEME], hc_onecell, LIGHTGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1172 }
1173
1174 if(do_mutinfo) { /* mutual info page */
1175 if((status = mutual_information_sspostscript(go, abc, errbuf, ps, bp_ct, msa_nseq, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME], hc_onecell, GREYOC, LIGHTGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1176 }
1177
1178 if(do_ifreq || do_iavglen) { /* insert frequency page */
1179 /* first, determine number of sequences with inserts after each position, 3 different ways depending on command line options */
1180 if(esl_opt_IsOn(go, "--ifile")) { /* read the insert file from cmalign */
1181 /* we've already read the ifile above, and filled nseq_with_ins_ct, but we check here */
1182 if(nseq_with_ins_ct == NULL) esl_fatal("Internal error, --ifile selected, but not read");
1183 }
1184 else if (do_small) { /* use abc_ct to derive nseq_with_ins_ct */
1185 get_insert_info_from_abc_ct(abc_ct, abc, msa->rf, msa_alen, ps->rflen, &(nseq_with_ins_ct), &(nins_ct)); /* dies with esl_fatal() upon an error */
1186 }
1187 else {
1188 get_insert_info_from_msa(abc, msa, ps->rflen, &(nseq_with_ins_ct), &(nins_ct), NULL); /* dies with esl_fatal() upon an error */
1189 }
1190 /* now draw the insert diagram */
1191 if(do_ifreq) {
1192 if((status = insertfreq_sspostscript(go, errbuf, ps, nseq_with_ins_ct, span_ct, msa_nseq, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME], hc_onecell, LIGHTGREYOC, GREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1193 }
1194 if(do_iavglen) {
1195 if((status = insertavglen_sspostscript(go, errbuf, ps, nseq_with_ins_ct, nins_ct, span_ct, msa_nseq, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME], hc_onecell, LIGHTGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1196 }
1197 }
1198
1199 if(do_dall) { /* make a new postscript page marking all deletes */
1200 if((status = delete_sspostscript(go, abc, errbuf, ps, abc_ct, span_ct, msa_nseq, TRUE, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME], hc_onecell, LIGHTGREYOC, DARKGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1201 }
1202
1203 if(do_dint) { /* internal deletes */
1204 if((status = delete_sspostscript(go, abc, errbuf, ps, abc_ct, span_ct, msa_nseq, FALSE, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME], hc_onecell, LIGHTGREYOC, DARKGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1205 }
1206
1207 if(do_prob && pp_ct != NULL) { /* avg post prob */
1208 if((status = avg_posteriors_sspostscript(go, abc, errbuf, ps, pp_ct, msa_nseq, hc_scheme, RB_6_RL_SCHEME, hc_nbins[RB_6_RL_SCHEME], hc_onecell, LIGHTGREYOC, tabfp)) != eslOK) esl_fatal(errbuf);
1209 }
1210
1211 if(do_span) { /* span */
1212 if((status = span_sspostscript(go, errbuf, ps, span_ct, msa_nseq, hc_scheme, RB_6_RL_SCHEME, hc_nbins[RB_6_RL_SCHEME], hc_onecell, LIGHTGREYOC, BLACKOC, tabfp)) != eslOK) esl_fatal(errbuf);
1213 }
1214
1215 if(do_maskcol) {
1216 /* Paint positions excluded by the mask magenta, unless the mask has zero internal exclusions.
1217 * Such a mask is a 'truncating' mask, that excludes only a 5' contiguous set of columns, and a 3' contiguous set of columns, in this case paint excluded positions light grey. */
1218 if((status = colormask_sspostscript(go, errbuf, ps, msa, hc_onecell, BLACKOC, (mask_has_internal_zeroes ? MAGENTAOC : LIGHTGREYOC))) != eslOK) esl_fatal(errbuf);
1219 }
1220
1221 if(do_maskdiff) {
1222 if((status = diffmask_sspostscript(go, errbuf, ps, msa, mask2, hc_onecell, BLACKOC, CYANOC, MAGENTAOC, LIGHTGREYOC)) != eslOK) esl_fatal(errbuf);
1223 }
1224
1225 if(do_dfile) {
1226 if((status = drawfile2sspostscript(go, errbuf, ps, hc_scheme, RB_6_RH_SCHEME, hc_nbins[RB_6_RH_SCHEME])) != eslOK) esl_fatal(errbuf);
1227 }
1228
1229 if(do_efile) {
1230 if((status = expertfile2sspostscript(go, errbuf, ps)) != eslOK) esl_fatal(errbuf);
1231 }
1232
1233 if(do_indi) { /* we're printing all individual seqs */
1234 /* get insert info we'll use in individuals_sspostscript() */
1235 if(esl_opt_IsOn(go, "--ifile")) { /* read the insert file from cmalign, with info from all seqs */
1236 get_insert_info_from_ifile((esl_opt_GetString(go, "--ifile")), ps->rflen, msa_nseq, NULL, NULL, NULL, &(per_seq_ins_ct), NULL, NULL); /* dies with esl_fatal() upon an error */
1237 }
1238 else {
1239 get_insert_info_from_msa(abc, msa, ps->rflen, NULL, NULL, &per_seq_ins_ct); /* dies with esl_fatal() upon an error */
1240 }
1241 /* we have msa and per_seq_ins_ct for all possible combos of --small and --ifile,
1242 * draw the individual sequence pages */
1243 if((status = individuals_sspostscript(go, abc, errbuf, ps, abc_ct,
1244 msa, per_seq_ins_ct,
1245 ((! esl_opt_GetBoolean(go, "--no-pp")) && (msa->pp != NULL)), /* do_prob? draw pp pages? */
1246 (! esl_opt_GetBoolean(go, "--no-bp")), /* do_rescol: paint nucleotides different colors based on basepair type? */
1247 hc_scheme, RB_W5_OH_SCHEME, RB_6_RL_SCHEME, hc_nbins[RB_W5_OH_SCHEME], hc_nbins[RB_6_RL_SCHEME], hc_onecell,
1248 LIGHTGREYOC, /* one-cell legend, 5'/3' flush color */
1249 BLACKOC, /* one-cell legend, watson-crick bp color, only relevant if do_rescol == TRUE */
1250 GREENOC, /* one-cell legend, G-U or U-G bp color, only relevant if do_rescol == TRUE */
1251 REDOC, /* one-cell legend, non-canonical bp color, only relevant if do_rescol == TRUE */
1252 LIGHTPURPLEOC, /* one-cell legend, internal double-gap bp color, only relevant if do_rescol == TRUE */
1253 LIGHTPURPLEOC, /* one-cell legend, internal half-gap bp color, only relevant if do_rescol == TRUE */
1254 LIGHTGREYOC, /* one-cell legend, color for gap in post prob pages */
1255 tabfp)) != eslOK)
1256 esl_fatal(errbuf);
1257 }
1258 if((status = draw_sspostscript(ofp, go, errbuf, command, date, hc_scheme, ps)) != eslOK) esl_fatal(errbuf);
1259 free(command);
1260 fclose(ofp);
1261 printf("# %d page postscript saved to file %s.\n", ps->npage, outfile);
1262
1263 /* Cleanup, normal return
1264 */
1265 if(tabfp != NULL) {
1266 fclose(tabfp);
1267 if(do_indi && (do_info || do_mutinfo || do_ifreq || do_iavglen || do_dall || do_dint || do_prob || do_span)) {
1268 printf("# Per-position and per-sequence data saved to tab-delimited text file %s.\n", esl_opt_GetString(go, "--tabfile"));
1269 }
1270 else if (do_indi) {
1271 printf("# Per-sequence data saved to tab-delimited text file %s.\n", esl_opt_GetString(go, "--tabfile"));
1272 }
1273 else {
1274 printf("# Per-position data saved to tab-delimited text file %s.\n", esl_opt_GetString(go, "--tabfile"));
1275 }
1276 }
1277
1278 esl_arr2_Destroy((void **) abc_ct, msa? msa->alen:0);
1279 esl_arr2_Destroy((void **) pp_ct, msa? msa->alen:0);
1280 esl_arr3_Destroy((void ***) bp_ct, msa? msa->alen:0, abc? abc->Kp: 0);
1281 esl_arr2_Destroy((void **) per_seq_ins_ct, msa_nseq);
1282
1283 if(nins_ct){
1284 free(nins_ct);
1285 }
1286 esl_free(spos_ct);
1287 esl_free(epos_ct);
1288 esl_free(srfoff_ct);
1289 esl_free(erfoff_ct);
1290 esl_free(span_ct);
1291 esl_free(nseq_with_ins_ct);
1292 esl_free(mask);
1293 if(mask2 !=0){
1294 free(mask2);
1295 }
1296 esl_free(date);
1297 esl_free(hc_nbins);
1298
1299 free_sspostscript(ps);
1300 esl_alphabet_Destroy(abc);
1301 esl_msafile_Close(afp);
1302 esl_msafile2_Close(afp2);
1303 esl_getopts_Destroy(go);
1304
1305 for(z = 0; z < NOC; z++) esl_free(hc_onecell[z]);
1306 esl_free(hc_onecell);
1307 for(z = 0; z < NRB_11_RH_SCHEME; z++) esl_free(hc_scheme[RB_11_RH_SCHEME][z]);
1308 for(z = 0; z < NRB_11_RL_SCHEME; z++) esl_free(hc_scheme[RB_11_RL_SCHEME][z]);
1309 for(z = 0; z < NRB_6_RH_SCHEME; z++) esl_free(hc_scheme[RB_6_RH_SCHEME][z]);
1310 for(z = 0; z < NRB_6_RL_SCHEME; z++) esl_free(hc_scheme[RB_6_RL_SCHEME][z]);
1311 for(z = 0; z < NRB_5_RH_SCHEME; z++) esl_free(hc_scheme[RB_5_RH_SCHEME][z]);
1312 for(z = 0; z < NRB_5_RL_SCHEME; z++) esl_free(hc_scheme[RB_5_RL_SCHEME][z]);
1313 for(z = 0; z < NRB_W5_OH_SCHEME; z++) esl_free(hc_scheme[RB_W5_OH_SCHEME][z]);
1314 for(z = 0; z < NSCHEMES; z++) { esl_free(hc_scheme[z]); }
1315 free(hc_scheme);
1316 esl_msa_Destroy(msa);
1317 return 0;
1318
1319 ERROR:
1320 esl_fatal("Memory allocation error in main().");
1321 return eslFAIL;/* silence compiler warning about void return*/
1322 }
1323
1324 /* Function: create_sspostscript()
1325 *
1326 * Purpose: Create and initialize a SS postscript data structure.
1327 * Return: ps
1328 */
1329 static SSPostscript_t *
1330 create_sspostscript(const ESL_GETOPTS *go)
1331 {
1332 int status;
1333 SSPostscript_t *ps;
1334
1335 ESL_ALLOC(ps, sizeof(SSPostscript_t));
1336
1337 ps->npage = 0;
1338 ps->modelname = NULL;
1339 ps->modeA = NULL;
1340 ps->descA = NULL;
1341 ps->headerx = 0.;
1342 ps->headery = 0.;
1343 ps->headerx_desc = 0.;
1344 ps->headerx_charsize = 0.;
1345 ps->headery_charsize = 0.;
1346 ps->desc_max_chars = 0;
1347 ps->leg_posn = -1;
1348 ps->leg_cellsize = -1;
1349 ps->leg_rhs_space = 0.;
1350 ps->legx_offset = 0.;
1351 ps->legy_offset = 0.;
1352 ps->legx = 0.;
1353 ps->legy = 0.;
1354 ps->cur_legy = 0.;
1355 ps->legx_charsize = 0.;
1356 ps->legy_charsize = 0.;
1357 ps->legx_max_chars = 0;
1358 ps->legx_stats = 0.;
1359 ps->pagex_max = 0.;
1360 ps->pagey_max = 0.;
1361 ps->scale = -1.; /* we'll check if this is still negative after reading the template file, if so it's an error */
1362 ps->regurgA = NULL;
1363 ps->nregurg = 0;
1364 ps->posntextA = NULL;
1365 ps->posntextxA = ps->posntextyA = NULL;
1366 ps->nposntext = 0;
1367 ps->ticksx1A = ps->ticksx2A = ps->ticksy1A = ps->ticksy2A = NULL;
1368 ps->nticks = 0;
1369 ps->bpx1A = ps->bpx2A = ps->bpy1A = ps->bpy2A = NULL;
1370 ps->nbp = 0;
1371 ps->rxA = ps->ryA = NULL;
1372 ps->rflen = 0;
1373 ps->rAA = NULL;
1374 ps->rcolAAA = NULL;
1375 ps->bcolAAA = NULL;
1376 ps->otypeAA = NULL;
1377 ps->occlAAA = NULL;
1378 ps->nocclA = NULL;
1379 ps->sclAA = NULL;
1380 ps->tlAAA = NULL;
1381 ps->ntlA = NULL;
1382 ps->mask = NULL;
1383 ps->nalloc = 50;
1384 ps->msa_cseq_maj = NULL;
1385 ps->msa_cseq_amb = NULL;
1386 ps->msa_ct = NULL;
1387 ps->msa_nbp = 0;
1388 ps->msa_rf2a_map = NULL;
1389 ps->msa_a2rf_map = NULL;
1390 ps->uaseqlenA = NULL;
1391 ps->seqidxA = NULL;
1392 ps->msa = NULL;
1393 ps->abc = NULL;
1394 return ps;
1395
1396 ERROR: esl_fatal("create_sspostscript(): memory allocation error.");
1397 return NULL; /* NEVERREACHED */
1398 }
1399
1400 /* Function: setup_sspostscript()
1401 *
1402 * Purpose: Determine positions for header and legend in a SSPostscript_t()
1403 * Return: eslOK
1404 */
1405 static int
1406 setup_sspostscript(SSPostscript_t *ps, char *errbuf)
1407 {
1408 float xroom, yroom;
1409 float header_max_chars;
1410
1411 if(ps->rflen == 0) ESL_FAIL(eslEINVAL, errbuf, "Failed to ready any nucleotides in template file.");
1412
1413 /* set up legx, legy, this is a hack (takes advantage of position of 3' nucleotide in all SSU models) */
1414 ps->legx = ps->rxA[ps->leg_posn-1] + ps->legx_offset;
1415 ps->legy = ps->ryA[ps->leg_posn-1] + ps->legy_offset;
1416 ps->cur_legy = ps->legy;
1417
1418 ps->pagex_max = POSTSCRIPT_PAGEWIDTH / ps->scale;
1419 ps->pagey_max = POSTSCRIPT_PAGEHEIGHT / ps->scale;
1420
1421 ps->headerx = 0. + PAGE_SIDEBUF;
1422 ps->headery = ps->pagey_max - PAGE_TOPBUF - ((HEADER_FONTSIZE_UNSCALED) / ps->scale);
1423
1424 /* determine max number of nucleotides we can print before we run off the page in the legend section */
1425 ps->legx_charsize = (LEG_FONTSIZE_UNSCALED / COURIER_HEIGHT_WIDTH_RATIO) / ps->scale;
1426 ps->legy_charsize = (LEG_FONTSIZE_UNSCALED) / ps->scale;
1427 xroom = ps->pagex_max - ps->legx - (ps->leg_cellsize - ps->legx_charsize);
1428 yroom = ps->pagey_max - ps->legy - (ps->leg_cellsize - ps->legy_charsize);
1429 ps->legx_max_chars = (int) (xroom / ps->legx_charsize);
1430 ps->legy_max_chars = (int) (yroom / ps->legy_charsize);
1431 ps->legx_stats = ps->pagex_max - PAGE_SIDEBUF - ps->leg_rhs_space - (LEG_EXTRA_COLUMNS * ps->legx_charsize);
1432
1433 /* determine max size of description that will fit in header */
1434 //header_fontwidth = (HEADER_FONTSIZE_UNSCALED / COURIER_HEIGHT_WIDTH_RATIO) / ps->scale; // SRE: appears to be unused.
1435 ps->headerx_charsize = (HEADER_FONTSIZE_UNSCALED / COURIER_HEIGHT_WIDTH_RATIO) / ps->scale;
1436 header_max_chars = (int) ((ps->pagex_max - 2*PAGE_SIDEBUF) / ps->headerx_charsize);
1437 ps->headery_charsize = (HEADER_FONTSIZE_UNSCALED) / ps->scale;
1438 ps->desc_max_chars = header_max_chars - (HEADER_MODELNAME_MAXCHARS + 6 + 6 + 8 +2); /*6,6,8 for #pos,#bps,#seq plus 2 spaces each, plus 2 for after name */
1439 ps->headerx_desc = ps->pagex_max - PAGE_SIDEBUF - (ps->desc_max_chars * ps->headerx_charsize);
1440
1441 return eslOK;
1442 }
1443
1444 /* Function: free_sspostscript()
1445 *
1446 * Purpose: Free a SS postscript data structure.
1447 * Return: (void)
1448 */
1449 static void
1450 free_sspostscript(SSPostscript_t *ps)
1451 {
1452 int i, p, c, l, l2;
1453
1454 if(ps->modelname != NULL) free(ps->modelname);
1455
1456 if(ps->modeA != NULL) free(ps->modeA);
1457
1458 if(ps->descA != NULL) {
1459 for(i = 0; i < ps->npage; i++) {
1460 if(ps->descA[i] != NULL) {
1461 free(ps->descA[i]);
1462 }
1463 }
1464 free(ps->descA);
1465 }
1466
1467 if(ps->regurgA != NULL) {
1468 for(i = 0; i < ps->nregurg; i++) {
1469 if(ps->regurgA[i] != NULL) {
1470 free(ps->regurgA[i]);
1471 }
1472 }
1473 free(ps->regurgA);
1474 }
1475
1476 if(ps->posntextA != NULL) {
1477 for(i = 0; i < ps->nposntext; i++) free(ps->posntextA[i]);
1478 free(ps->posntextA);
1479 }
1480 if(ps->posntextxA != NULL) free(ps->posntextxA);
1481 if(ps->posntextyA != NULL) free(ps->posntextyA);
1482 if(ps->ticksx1A != NULL) free(ps->ticksx1A);
1483 if(ps->ticksy1A != NULL) free(ps->ticksy1A);
1484 if(ps->ticksx2A != NULL) free(ps->ticksx2A);
1485 if(ps->ticksy2A != NULL) free(ps->ticksy2A);
1486 if(ps->bpx1A != NULL) free(ps->bpx1A);
1487 if(ps->bpy1A != NULL) free(ps->bpy1A);
1488 if(ps->bpx2A != NULL) free(ps->bpx2A);
1489 if(ps->bpy2A != NULL) free(ps->bpy2A);
1490 if(ps->rxA != NULL) free(ps->rxA);
1491 if(ps->ryA != NULL) free(ps->ryA);
1492
1493 if(ps->rAA != NULL) {
1494 for(p = 0; p < ps->npage; p++)
1495 if(ps->rAA[p] != NULL) free(ps->rAA[p]);
1496 free(ps->rAA);
1497 }
1498
1499 if(ps->rcolAAA != NULL) {
1500 for(p = 0; p < ps->npage; p++) {
1501 if(ps->rcolAAA[p] != NULL) {
1502 for(c = 0; c < ps->rflen; c++) free(ps->rcolAAA[p][c]);
1503 free(ps->rcolAAA[p]);
1504 }
1505 }
1506 free(ps->rcolAAA);
1507 }
1508
1509 if(ps->bcolAAA != NULL) {
1510 for(p = 0; p < ps->npage; p++) {
1511 if(ps->bcolAAA[p] != NULL) {
1512 for(c = 0; c < ps->rflen; c++) free(ps->bcolAAA[p][c]);
1513 free(ps->bcolAAA[p]);
1514 }
1515 }
1516 free(ps->bcolAAA);
1517 }
1518
1519 if(ps->otypeAA != NULL) {
1520 for(p = 0; p < ps->npage; p++) {
1521 if(ps->otypeAA[p] != NULL) {
1522 free(ps->otypeAA[p]);
1523 }
1524 }
1525 free(ps->otypeAA);
1526 }
1527
1528 if(ps->occlAAA != NULL) {
1529 for(p = 0; p < ps->npage; p++) {
1530 if(ps->occlAAA[p] != NULL) {
1531 for(l = 0; l < ps->nocclA[p]; l++) {
1532 if(ps->occlAAA[p][l]->text != NULL) free(ps->occlAAA[p][l]->text);
1533 if(ps->occlAAA[p][l]->celltext != NULL) free(ps->occlAAA[p][l]->celltext);
1534 if(ps->occlAAA[p][l]->procname != NULL) free(ps->occlAAA[p][l]->procname);
1535 if(ps->occlAAA[p][l]->procstack != NULL) free(ps->occlAAA[p][l]->procstack);
1536 free(ps->occlAAA[p][l]); /* the rest is statically allocated memory */
1537 }
1538 free(ps->occlAAA[p]);
1539 }
1540 }
1541 free(ps->occlAAA);
1542 }
1543
1544 if(ps->sclAA != NULL) {
1545 for(p = 0; p < ps->npage; p++) {
1546 if(ps->sclAA[p] != NULL) {
1547 if(ps->sclAA[p]->limits != NULL) free(ps->sclAA[p]->limits);
1548 if(ps->sclAA[p]->counts != NULL) free(ps->sclAA[p]->counts);
1549 if(ps->sclAA[p]->counts_masked != NULL) free(ps->sclAA[p]->counts_masked);
1550 if(ps->sclAA[p]->text1 != NULL) free(ps->sclAA[p]->text1);
1551 if(ps->sclAA[p]->text2 != NULL) free(ps->sclAA[p]->text2);
1552 free(ps->sclAA[p]); /* statically allocated memory */
1553 }
1554 }
1555 free(ps->sclAA);
1556 }
1557
1558 if(ps->tlAAA != NULL) {
1559 for(p = 0; p < ps->npage; p++) {
1560 if(ps->tlAAA[p] != NULL) {
1561 for(l = 0; l < ps->ntlA[p]; l++) {
1562 for(l2 = 0; l2 < ps->tlAAA[p][l]->nlines; l2++) {
1563 free(ps->tlAAA[p][l]->text_per_line[l2]);
1564 }
1565 free(ps->tlAAA[p][l]->text_per_line);
1566 free(ps->tlAAA[p][l]); /* the rest is statically allocated memory */
1567 }
1568 free(ps->tlAAA[p]);
1569 }
1570 }
1571 free(ps->tlAAA);
1572 }
1573
1574 if(ps->ntlA != NULL) free(ps->ntlA);
1575 if(ps->nocclA != NULL) free(ps->nocclA);
1576 if(ps->msa_cseq_maj != NULL) free(ps->msa_cseq_maj);
1577 if(ps->msa_cseq_amb != NULL) free(ps->msa_cseq_amb);
1578 if(ps->msa_ct != NULL) free(ps->msa_ct);
1579 if(ps->msa_rf2a_map != NULL) free(ps->msa_rf2a_map);
1580 if(ps->msa_a2rf_map != NULL) free(ps->msa_a2rf_map);
1581 if(ps->mask != NULL) free(ps->mask);
1582 if(ps->seqidxA != NULL) free(ps->seqidxA);
1583 if(ps->uaseqlenA != NULL) free(ps->uaseqlenA);
1584
1585 free(ps);
1586 return;
1587 }
1588
1589 /* Function: create_onecell_colorlegend()
1590 *
1591 * Purpose: Create and initialize a one cell color legend data structure.
1592 * As a special case, pass nres as OCCL_BLANK_COUNT if you don't
1593 * want a count printed for this one cell.
1594 * Return: occl
1595 */
1596 static OneCellColorLegend_t *
1597 create_onecell_colorlegend(float *col, int nres, int nres_masked, int do_separator, int do_no_vspace)
1598 {
1599 int status;
1600 OneCellColorLegend_t *occl;
1601
1602 ESL_ALLOC(occl, sizeof(OneCellColorLegend_t));
1603
1604 /* initialize */
1605 esl_vec_FSet(occl->col, NCMYK, 0.);
1606 occl->text = NULL;
1607 occl->celltext = NULL;
1608 occl->procname = NULL;
1609 occl->procstack = NULL;
1610 occl->nprocstack = 0;
1611
1612 /* set caller specified values */
1613 esl_vec_FCopy(col, NCMYK, occl->col);
1614
1615 occl->nres = nres;
1616 occl->nres_masked = nres_masked;
1617 occl->do_separator = do_separator;
1618 occl->do_no_vspace = do_no_vspace;
1619
1620 return occl;
1621
1622 ERROR: esl_fatal("create_onecell_colorlegend(): memory allocation error.");
1623 return NULL; /* NEVERREACHED */
1624 }
1625
1626
1627
1628 /* Function: create_onecell_colorlegend()
1629 *
1630 * Purpose: Create and initialize a one cell color legend data structure.
1631 * Return: occl
1632 */
1633 static TextLegend_t *
1634 create_text_legend(int nlines, char **text_per_line, int do_separator)
1635 {
1636 int status;
1637 TextLegend_t *tl;
1638 int i;
1639
1640 ESL_ALLOC(tl, sizeof(TextLegend_t));
1641
1642 /* initialize */
1643 tl->text_per_line = NULL;
1644
1645 /* set caller specified values */
1646 tl->nlines = nlines;
1647 tl->do_separator = do_separator;
1648 ESL_ALLOC(tl->text_per_line, sizeof(char *) * nlines);
1649 for(i = 0; i < nlines; i++) {
1650 if((status = esl_strdup(text_per_line[i], -1, &(tl->text_per_line[i]))) != eslOK) esl_fatal("create_text_legend(), error copying text");
1651 }
1652
1653 return tl;
1654
1655 ERROR: esl_fatal("create_text_legend(): memory allocation error.");
1656 return NULL; /* NEVERREACHED */
1657 }
1658
1659
1660 /* Function: create_text_legend_for_consensus_sequence()
1661 *
1662 * Purpose: Create text explaining how a consensus sequence is calculated
1663 * for the legend, and return it in the form of a TextLegend_t
1664 * object.
1665 *
1666 * Return: tl
1667 */
1668 static TextLegend_t *
1669 create_text_legend_for_consensus_sequence(const ESL_GETOPTS *go, int do_separator)
1670 {
1671 int status;
1672 TextLegend_t *tl;
1673 int i;
1674 int nlines;
1675 char **text;
1676
1677 if(esl_opt_GetBoolean(go, "--cambig")) {
1678 nlines = 5;
1679 ESL_ALLOC(text, sizeof(char *) * nlines);
1680 for (i = 0; i < nlines; i++) text[i] = NULL;
1681 if ((status = esl_strcat(&(text[0]), -1, "Consensus nucleotides (nt) are displayed, calculated", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1682 esl_sprintf(&(text[1]), "as the least ambiguous nt that represents >= %0.2f", esl_opt_GetReal(go, "--athresh"));
1683 if((status = esl_strcat(&(text[2]), -1, "of all non-gap nts at each position.", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1684 if((status = esl_strcat(&(text[3]), -1, "K=G|U, M=A|C, R=A|G, S=C|G, Y=C|U, W=A|U", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1685 if((status = esl_strcat(&(text[4]), -1, "B=C|G|U, D=A|G|U, H=A|C|U, V=A|C|G, N=A|C|G|U", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1686 }
1687 else { /* --cambig not enabled, majority rule was used */
1688 nlines = 4;
1689 ESL_ALLOC(text, sizeof(char *) * nlines);
1690 for(i = 0; i < nlines; i++) { text[i] = NULL; }
1691 if((status = esl_strcat(&(text[0]), -1, "Consensus nucleotides (nt) are displayed, defined", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1692 if((status = esl_strcat(&(text[1]), -1, "as the most frequent nt at each position.", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1693 esl_sprintf(&(text[2]), "Capitalized nts occur in >= %0.2f fraction of sequences", esl_opt_GetReal(go, "--cthresh"));
1694 if((status = esl_strcat(&(text[3]), -1, "that do not have a gap at the position.", -1)) != eslOK) esl_fatal("create_text_legend_for_consensus_sequence(), error copying text");
1695 }
1696
1697 tl = create_text_legend(nlines, text, do_separator);
1698 for(i = 0; i < nlines; i++) {
1699 free(text[i]);
1700 }
1701 free(text);
1702 return tl;
1703
1704 ERROR: esl_fatal("create_text_legend(): memory allocation error.");
1705 return NULL; /* NEVERREACHED */
1706 }
1707
1708
1709 /* Function: create_scheme_colorlegend()
1710 *
1711 * Purpose: Create and initialize a scheme color legend data structure.
1712 * Return: scl
1713 */
1714 static SchemeColorLegend_t *
1715 create_scheme_colorlegend(int scheme, int nbins, float *limits, int ints_only_flag, int low_inclusive, int high_inclusive, int use_mask)
1716 {
1717 int status;
1718 SchemeColorLegend_t *scl;
1719 int i;
1720 int *counts;
1721 int *counts_masked;
1722 ESL_ALLOC(scl, sizeof(SchemeColorLegend_t));
1723
1724 /* initialize */
1725 scl->text1 = NULL;
1726 scl->text2 = NULL;
1727
1728 /* set caller specified values */
1729 scl->scheme = scheme;
1730 scl->nbins = nbins;
1731 ESL_ALLOC(scl->limits, sizeof(float) * (nbins+1));
1732 for(i = 0; i <= nbins; i++) { scl->limits[i] = limits[i]; }
1733
1734 ESL_ALLOC(counts, sizeof(int) * nbins);
1735 ESL_ALLOC(counts_masked, sizeof(int) * nbins);
1736 esl_vec_ISet(counts, nbins, 0);
1737 esl_vec_ISet(counts_masked, nbins, 0);
1738 scl->counts = counts;
1739 scl->counts_masked = counts_masked;
1740 scl->ints_only_flag = ints_only_flag;
1741 scl->low_inclusive = low_inclusive;
1742 scl->high_inclusive = high_inclusive;
1743 scl->use_mask = use_mask;
1744 return scl;
1745
1746 ERROR: esl_fatal("create_scheme_colorlegend(): memory allocation error.");
1747 return NULL; /* NEVERREACHED */
1748 }
1749
1750 /* Function: add_text_to_scheme_colorlegend()
1751 *
1752 * Purpose: Add text to an existing scheme color legend data structure.
1753 * Throws: Exception if the text is too long.
1754 */
1755 int
1756 add_text_to_scheme_colorlegend(SSPostscript_t *ps, SchemeColorLegend_t *scl, char *text, int legx_max_chars, char *errbuf)
1757 {
1758 int status;
1759 int i, idx;
1760 int max_chars_per_line;
1761
1762 if(scl->text1 != NULL) esl_fatal("add_text_to_scheme_colorlegend(), text already exists!\n");
1763 if(scl->text2 != NULL) esl_fatal("add_text_to_scheme_colorlegend(), text already exists!\n");
1764 if(text == NULL) esl_fatal("add_text_to_scheme_colorlegend(), passed in text is NULL!\n");
1765
1766 max_chars_per_line = ps->legx_max_chars -
1767 ((int) ps->leg_rhs_space / ps->legx_charsize) -
1768 ((int) (PAGE_SIDEBUF / ps->legx_charsize)) -
1769 LEG_EXTRA_COLUMNS - 2;
1770 if(((int) strlen(text)) <= max_chars_per_line) {
1771 /* case 1, entire text can fit in one line */
1772 if((status = esl_strdup(text, -1, &(scl->text1))) != eslOK) esl_fatal("add_text_to_scheme_colorlegend(), error copying text");
1773 return eslOK;
1774 }
1775 else if(((int) strlen(text)) > ((2 * max_chars_per_line) - 6)) {
1776 /* case 2, entire text can't even fit in two lines,
1777 * (this is inexact doesn't account for size of words which is an issue b/c we break at newline,
1778 * this is why I have the extra '- 6');
1779 */
1780 ESL_FAIL(eslEINVAL, errbuf, "add_text_to_scheme_colorlegend(), text is %d chars, max allowed is %d (%s)\n", (int) strlen(text), ((2 * max_chars_per_line) - 6), text);
1781 }
1782 else { /* split it up into two lines */
1783 idx = max_chars_per_line - 1;
1784 while(text[idx] != ' ') {
1785 idx--;
1786 if(idx < 0) ESL_FAIL(eslEINVAL, errbuf, "add_text_to_scheme_colorlegend(), couldn't find a breakpoint for splitting the string (%s)\n", text);
1787 }
1788 /* copy first string into text1 */
1789 ESL_ALLOC(scl->text1, sizeof(char) * (idx+1));
1790 for(i = 0; i < idx; i++) scl->text1[i] = text[i];
1791 scl->text1[idx] = '\0';
1792
1793 /* copy remainder into text2 */
1794 int len = (int) strlen(text);
1795 idx++;
1796 ESL_ALLOC(scl->text2, sizeof(char) * (len - idx+1));
1797 for(i = idx; i < len; i++) scl->text2[i-idx] = text[i];
1798 scl->text2[len-idx] = '\0';
1799 }
1800 return eslOK;
1801
1802 ERROR: ESL_FAIL(status, errbuf, "Error adding text to scheme_colorlegend probably out of memory");
1803 }
1804
1805
1806 /* Function: add_text_to_onecell_colorlegend()
1807 *
1808 * Purpose: Add text to an existing one cell color legend data structure.
1809 * Throws: Exception if the text is too long.
1810 */
1811 int
1812 add_text_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *text, int legx_max_chars, char *errbuf)
1813 {
1814 int status;
1815 int max_chars_per_line;
1816 if(occl->text != NULL) esl_fatal("add_text_to_onecell_colorlegend(), text description already exists!\n");
1817 if(text == NULL) esl_fatal("add_text_to_onecell_colorlegend(), passed in text is NULL!\n");
1818
1819 max_chars_per_line = legx_max_chars - LEG_EXTRA_COLUMNS - 2 - ((int) (((float) ps->leg_cellsize * 1.5) / ps->legx_charsize));
1820 /*printf("max: %d cur: %d text: %s\n", max_chars_per_line, (int) strlen(text), text);*/
1821 if(((int) strlen(text)) > (max_chars_per_line)) {
1822 ESL_FAIL(eslEINVAL, errbuf, "add_text_to_onecell_colorlegend(), text is %d chars, max allowed is %d (%s)\n", (int) strlen(text), max_chars_per_line, text);
1823 }
1824 if((status = esl_strdup(text, -1, &(occl->text))) != eslOK) esl_fatal("add_text_to_onecell_colorlegend(), error copying text");
1825 return eslOK;
1826 }
1827
1828 /* Function: add_celltext_to_onecell_colorlegend()
1829 *
1830 * Purpose: Add cell text to an existing one cell color legend data structure.
1831 */
1832 int
1833 add_celltext_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *celltext, char *errbuf)
1834 {
1835 int status;
1836 if(occl->celltext != NULL) esl_fatal("add_celltext_to_onecell_colorlegend(), cell text already exists!\n");
1837 if(celltext == NULL) esl_fatal("add_celltext_to_onecell_colorlegend(), passed in celltext is NULL!\n");
1838
1839 if((status = esl_strdup(celltext, -1, &(occl->celltext))) != eslOK) esl_fatal("add_celltext_to_onecell_colorlegend(), error copying text");
1840
1841 return eslOK;
1842 }
1843
1844 /* Function: add_procedure_to_onecell_colorlegend()
1845 *
1846 * Purpose: Add procedure for drawing one cell to an existing one cell color legend data structure.
1847 */
1848 int
1849 add_procedure_to_onecell_colorlegend(SSPostscript_t *ps, OneCellColorLegend_t *occl, char *procname, float *procstack, int nprocstack, char *errbuf)
1850 {
1851 int status;
1852 int i;
1853
1854 if(occl->celltext != NULL) esl_fatal("add_procedure_to_onecell_colorlegend(), cell text already exists!\n");
1855 if(occl->procname != NULL) esl_fatal("add_procedure_to_onecell_colorlegend(), procedure text already exists!\n");
1856 if(occl->procstack != NULL) esl_fatal("add_procedure_to_onecell_colorlegend(), procedure stack already exists!\n");
1857 if(procname == NULL) esl_fatal("add_procedure_to_onecell_colorlegend(), passed in procname is NULL!\n");
1858 if(procstack == NULL) esl_fatal("add_procedure_to_onecell_colorlegend(), passed in procstack is NULL!\n");
1859
1860 if((status = esl_strdup(procname, -1, &(occl->procname))) != eslOK) esl_fatal("add_procedure_to_onecell_colorlegend(), error copying text");
1861 ESL_ALLOC(occl->procstack, sizeof(float) * nprocstack);
1862 for(i = 0; i < nprocstack; i++) occl->procstack[i] = procstack[i];
1863 occl->nprocstack = nprocstack;
1864
1865 return eslOK;
1866
1867 ERROR:
1868 return eslEMEM;
1869 }
1870
1871 /* Function: add_page_desc_to_sspostscript()
1872 *
1873 * Purpose: Add text describing a particular page of a postscript object.
1874 * Throws: Exception if the text is too long.
1875 */
1876 int
1877 add_page_desc_to_sspostscript(SSPostscript_t *ps, int page, char *text, char *errbuf)
1878 {
1879 int status;
1880 int i, j;
1881 int max_both_lines;
1882
1883 if(ps->descA[page] != NULL) ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), description for page %d already exists!\n", page);
1884 if(text == NULL) ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), passed in text is NULL!\n");
1885
1886 max_both_lines =(2. * ps->desc_max_chars);
1887 if(ps->modeA[page] == INDIMODE || ps->modeA[page] == SIMPLEMASKMODE)
1888 max_both_lines--; /* b/c we have to add a '-' to split up the larger strings onto 2 lines */
1889
1890 /* check to see if we can fit the text onto two lines of max width ps->desc_max_chars */
1891 int textlen = (int) strlen(text);
1892 if(textlen <= ps->desc_max_chars) { /* fine, this will fit on one line */
1893 if((status = esl_strdup(text, -1, &(ps->descA[page]))) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), error copying text");
1894 }
1895 else if (textlen <= max_both_lines) {
1896 if(ps->modeA[page] == ALIMODE) {
1897 /* maybe fine, make sure there's a break point (space ' ') that will break this string into two strings of length <= ps->desc_max_chars */
1898 i = ps->desc_max_chars;
1899 while(text[i] != ' ' && text[i] != '-') {
1900 i--;
1901 if(i < 0) ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), first word of text (%s) is more than max allowed of %d chars", text, ps->desc_max_chars);
1902 }
1903 /* found last space before max width, make sure it breaks the line up into two chunks of valid size */
1904 if((textlen - (i+1)) <= ps->desc_max_chars) { /* we're good */
1905 if((status = esl_strdup(text, -1, &(ps->descA[page]))) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), error copying text");
1906 ps->descA[page][i] = '\n'; /* so we can remember where the break is */
1907 }
1908 else ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), couldn't find (' ') for splitting text into two chunks (%s)", text);
1909 }
1910 else { /* INDIMODE or SIMPLEMASKMODE, sequence/mask name bigger than 1 line, but not 2, we put a '-' in it at the end of line 1 and add a '\n' so we remember where it was */
1911 ESL_ALLOC(ps->descA[page], sizeof(char) * (textlen + 3)); /* +3 so we have space for the extra '-' and '\n' */
1912 for(i = 0; i < ps->desc_max_chars; i++) ps->descA[page][i] = text[i];
1913 i = ps->desc_max_chars;
1914 ps->descA[page][i] = '-';
1915 i++;
1916 ps->descA[page][i] = '\n';
1917 for(i = ps->desc_max_chars; i < textlen; i++) ps->descA[page][i+2] = text[i];
1918 ps->descA[page][textlen+2] = '\0';
1919 }
1920 }
1921 else /* the text won't fit on two lines */
1922 if(ps->modeA[page] != INDIMODE) { /* not fine, this won't fit on two lines */
1923 ESL_FAIL(eslEINVAL, errbuf, "add_page_desc_to_sspostscript(), text is %d chars, max allowed is %d (%s)\n", textlen, max_both_lines, text);
1924 }
1925 else { /* INDIMODE or SIMPLEMASKMODE, sequence/mask name exceeds max, we put a '-' in it at the end of line 1 and truncate it */
1926 ESL_ALLOC(ps->descA[page], sizeof(char) * (max_both_lines + 2)); /* +2 so we have space for the extra '\n' (which we won't print), and the '\0' */
1927 /* first look to see if there's a space before desc_max_chars, this will never happen for a seq name, but may for a mask description */
1928 j = ps->desc_max_chars;
1929 while(text[j] != ' ' && j > 0) {
1930 j--;
1931 }
1932 if(j == 0) { j = ps->desc_max_chars; } /* no space */
1933 for(i = 0; i < j; i++) ps->descA[page][i] = text[i];
1934 i = j;
1935 if(j == ps->desc_max_chars && text[j] != ' ') {
1936 ps->descA[page][i] = '-';
1937 }
1938 i++;
1939 ps->descA[page][i] = '\n';
1940 for(i = j; i < j + ps->desc_max_chars; i++) ps->descA[page][i+2] = text[i];
1941 ps->descA[page][i+2] = '\0';
1942 }
1943 return eslOK;
1944
1945 ERROR: ESL_FAIL(status, errbuf, "add_page_desc_to_sspostscript() error, probably out of memory.");
1946 }
1947
1948
1949 /* Function: add_diffmask_page_desc_to_sspostscript()
1950 *
1951 * Purpose: Add text describing a diff mask page of a postscript object.
1952 * Throws: Exception if the text is too long.
1953 */
1954 int
1955 add_diffmask_page_desc_to_sspostscript(SSPostscript_t *ps, int page, char *mask_file, char *maskdiff_file, char *errbuf)
1956 {
1957 int status;
1958 int i;
1959 char *mask1desc = NULL;
1960 char *mask2desc = NULL;
1961 int len2copy;
1962 int mask_file_len;
1963 int maskdiff_file_len;
1964 void *tmp;
1965
1966 if(ps->mask == NULL) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), ps->mask is NULL\n");
1967 if(ps->descA[page] != NULL) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), description for page %d already exists!\n", page);
1968 if(maskdiff_file == NULL) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), passed in maskdiff_file is NULL!\n");
1969
1970 /* check to see if we can fit the text onto two lines of max width ps->desc_max_chars */
1971 mask_file_len = (int) strlen(ps->mask);
1972 maskdiff_file_len = (int) strlen(maskdiff_file);
1973 if((status = esl_strcat(&(mask1desc), -1, "mask 1: ", -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
1974 if((mask_file_len + 8) <= ps->desc_max_chars) { /* this will fit on one line */
1975 if((status = esl_strcat(&(mask1desc), -1, mask_file, -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
1976 }
1977 else { /* won't fit on one line, include as much as we can */
1978 len2copy = ps->desc_max_chars - 8 - 3; /* 8 is for "mask 1: " at beginning, 3 is for "..." at end */
1979 ESL_RALLOC(mask1desc, tmp, sizeof(char) * ps->desc_max_chars+1);
1980 for(i = 0; i < len2copy; i++) {
1981 mask1desc[8+i] = mask_file[i];
1982 }
1983 mask1desc[8+len2copy] = '.';
1984 mask1desc[8+len2copy+1] = '.';
1985 mask1desc[8+len2copy+2] = '.';
1986 mask1desc[8+len2copy+3] = '\0';
1987 }
1988
1989 /* repeat for mask 2 */
1990 if((status = esl_strcat(&(mask2desc), -1, "mask 2: ", -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
1991 if((maskdiff_file_len + 8) <= ps->desc_max_chars) { /* this will fit on one line */
1992 if((status = esl_strcat(&(mask2desc), -1, maskdiff_file, -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
1993 }
1994 else { /* won't fit on one line, include as much as we can */
1995 len2copy = ps->desc_max_chars - 8 - 3; /* 8 is for "mask 1: " at beginning, 3 is for "..." at end */
1996 ESL_RALLOC(mask2desc, tmp, sizeof(char) * ps->desc_max_chars+1);
1997 for(i = 0; i < len2copy; i++) {
1998 mask2desc[8+i] = maskdiff_file[i];
1999 }
2000 mask2desc[8+len2copy] = '.';
2001 mask2desc[8+len2copy+1] = '.';
2002 mask2desc[8+len2copy+2] = '.';
2003 mask2desc[8+len2copy+3] = '\0';
2004 }
2005
2006 /* concatenate them in ps->descA[page] */
2007 if((status = esl_strcat(&(ps->descA[page]), -1, mask1desc, -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
2008 if((status = esl_strcat(&(ps->descA[page]), -1, "\n", -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
2009 if((status = esl_strcat(&(ps->descA[page]), -1, mask2desc, -1)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "add_diffmask_page_desc_to_sspostscript(), error copying text");
2010
2011 free(mask1desc);
2012 free(mask2desc);
2013 return eslOK;
2014
2015 ERROR: ESL_FAIL(status, errbuf, "add_page_desc_to_sspostscript() error, probably out of memory.");
2016 }
2017
2018 /* Function: add_mask_to_sspostscript
2019 *
2020 * Purpose: Add a mask to a sspostscript object.
2021 */
2022 int
2023 add_mask_to_ss_postscript(SSPostscript_t *ps, char *mask)
2024 {
2025 int status;
2026 if(ps->mask != NULL) { esl_fatal("add_mask_to_ss_postscript(), mask is non-null!\n"); }
2027 if(mask == NULL) esl_fatal("add_mask_to_ss_postscript(), passed in mask is NULL!\n");
2028 if((status = esl_strdup(mask, -1, &(ps->mask))) != eslOK) esl_fatal("add_mask_to_ss_postscript(), error copying mask");
2029 return eslOK;
2030 }
2031
2032
2033 /* Function: draw_legend_column_headers()
2034 *
2035 * Purpose: Draw the legend column headers.
2036 * Return: eslOK
2037 */
2038 static int
2039 draw_legend_column_headers(FILE *fp, SSPostscript_t *ps, int pagenum, char *errbuf)
2040 {
2041 int status;
2042 int i;
2043 float x, y;
2044 // float legend_fontsize;
2045 char *cur_string;
2046 int cur_width = 0;
2047
2048 //legend_fontsize = LEG_FONTSIZE_UNSCALED / ps->scale;
2049
2050 x = ps->legx;
2051 y = ps->cur_legy;
2052 if(ps->mask != NULL && ps->modeA[pagenum] == ALIMODE) {
2053 y -= 1.25 * (float) ps->leg_cellsize;
2054 }
2055 /*fprintf(fp, "/%s findfont %f scalefont setfont\n", SPECIAL_FONT, legend_fontsize);*/
2056 fprintf(fp, "%% begin legend column headers\n");
2057 fprintf(fp, "(%s) %.2f %.2f moveto show\n", "LEGEND", x, (y + ((float) ps->leg_cellsize * .25)));
2058 /*fprintf(fp, "/%s findfont %f scalefont setfont\n", LEG_FONT, legend_fontsize);*/
2059
2060 x = ps->legx_stats;
2061 y = ps->cur_legy;
2062 cur_width = ps->legx_max_chars -
2063 ((int) ps->leg_rhs_space / ps->legx_charsize) -
2064 ((int) (PAGE_SIDEBUF / ps->legx_charsize)) -
2065 LEG_EXTRA_COLUMNS - 2;
2066
2067 ESL_ALLOC(cur_string, sizeof(char) * (cur_width+1));
2068 for(i = 0; i < cur_width; i++) cur_string[i] = '-';
2069 cur_string[cur_width] = '\0';
2070
2071 if(ps->mask != NULL && ps->modeA[pagenum] == ALIMODE) {
2072 fprintf(fp, "(%4s %4s) %.2f %.2f moveto show\n", "", "incl", x, (y + ((float) ps->leg_cellsize * .25)));
2073 y -= 0.625 * (float) ps->leg_cellsize;
2074 fprintf(fp, "(%4s %4s) %.2f %.2f moveto show\n", "", " by ", x, (y + ((float) ps->leg_cellsize * .25)));
2075 y -= 0.625 * (float) ps->leg_cellsize;
2076 fprintf(fp, "(%4s %4s) %.2f %.2f moveto show\n", "all", "mask", x, (y + ((float) ps->leg_cellsize * .25)));
2077 y -= 0.625 * (float) ps->leg_cellsize;
2078 fprintf(fp, "(%s) %.2f %.2f moveto show\n", cur_string, ps->legx, (y + ((float) ps->leg_cellsize * .25)));
2079 fprintf(fp, "(---- ----) %.2f %.2f moveto show\n", x, (y + ((float) ps->leg_cellsize * .25)));
2080 }
2081 else {
2082 fprintf(fp, "(%5s) %.2f %.2f moveto show\n", "count", x, (y + ((float) ps->leg_cellsize * .25)));
2083 y -= 0.625 * (float) ps->leg_cellsize;
2084 fprintf(fp, "(%s) %.2f %.2f moveto show\n", cur_string, ps->legx, (y + ((float) ps->leg_cellsize * .25)));
2085 fprintf(fp, "(-----) %.2f %.2f moveto show\n", x, (y + ((float) ps->leg_cellsize * .25)));
2086 }
2087 ps->cur_legy = y - (1.0 * (float) ps->leg_cellsize);
2088
2089 fprintf(fp, "%% end legend column headers\n\n");
2090 free(cur_string);
2091
2092 return eslOK;
2093
2094 ERROR: ESL_FAIL(status, errbuf, "ERROR drawing legend column headers, probably out of memory.");
2095 }
2096
2097 /* Function: draw_onecell_colorlegend()
2098 *
2099 * Purpose: Print a one cell color legend to an open file.
2100 * Return: eslOK, dies if we run out of memory.
2101 */
2102 static int
2103 draw_onecell_colorlegend(FILE *fp, OneCellColorLegend_t *occl, SSPostscript_t *ps, int occl_idx, int pagenum)
2104 {
2105 int status;
2106 float x, y;
2107 int cp, i;
2108 float fontsize;
2109 char *cur_string;
2110 int cur_width = 0;
2111
2112 /* object is valid, print it */
2113 x = ps->legx;
2114 y = ps->cur_legy;
2115
2116 /* print cell */
2117 fprintf(fp, "%% begin one cell color legend\n");
2118 if(occl->celltext == NULL && occl->procname == NULL) { /* print a colored block as the cell */
2119 fprintf(fp, "newpath\n");
2120 fprintf(fp, " %.2f %.2f moveto", x, y);
2121 fprintf(fp, " 0 %.3f rlineto %.3f 0 rlineto 0 %.3f rlineto closepath\n", (float) ps->leg_cellsize, (float) ps->leg_cellsize, (-1 * (float) ps->leg_cellsize));
2122 fprintf(fp, " ");
2123 for(cp = 0; cp < NCMYK; cp++) fprintf(fp, "%.2f ", occl->col[cp]);
2124 fprintf(fp, "setcmykcolor\n");
2125 fprintf(fp, " fill\n");
2126
2127 /* draw a small box outlining the colored block we just drew */
2128 /*fprintf(fp, "%.2f setlinewidth\n", (ps->leg_cellsize / 24.));
2129 fprintf(fp, "newpath\n");
2130 fprintf(fp, " %.2f %.2f moveto", x, y);
2131 fprintf(fp, " 0 %.3f rlineto %.3f 0 rlineto 0 %.3f rlineto closepath\n", (float) ps->leg_cellsize, (float) ps->leg_cellsize, (-1 * (float) ps->leg_cellsize));
2132 fprintf(fp, " ");
2133 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", 0., 0., 0., 1.);
2134 fprintf(fp, " stroke\n");*/
2135 }
2136 else if (occl->procname != NULL) {
2137 fprintf(fp, "%.2f %.2f ", x, y);
2138 for(i = 0; i < occl->nprocstack; i++) { fprintf(fp, "%.2f ", occl->procstack[i]); }
2139 fprintf(fp, "%s\n", occl->procname);
2140 }
2141 else if (occl->celltext != NULL){
2142 /* fontsize = (float) ps->leg_cellsize / strlen(occl->celltext); */
2143 fontsize = (float) ps->leg_cellsize / 2;
2144 fprintf(fp, "/%s findfont %f scalefont setfont\n", NUCLEOTIDES_FONT, fontsize);
2145 for(cp = 0; cp < NCMYK; cp++) fprintf(fp, "%.2f ", occl->col[cp]);
2146 fprintf(fp, "setcmykcolor\n");
2147 fprintf(fp, "(%s) %.2f %.2f moveto show\n", occl->celltext, x, y + ((float) ps->leg_cellsize * 0.25));
2148 }
2149 if(occl->celltext == NULL || (strlen(occl->celltext) > 0)) {
2150 /* if we printed a block or text, modify x, so text for the legend is indented appropriately */
2151 x += (float) ps->leg_cellsize * 1.5;
2152 }
2153 fontsize = LEG_FONTSIZE_UNSCALED / ps->scale;
2154
2155 /* print text for this legend */
2156 if(occl->text != NULL) {
2157 /* back to black */
2158 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n");
2159 fprintf(fp, "/%s findfont %f scalefont setfont\n", LEG_FONT, fontsize);
2160 fprintf(fp, "(%s) %.2f %.2f moveto show\n", occl->text, x, y + ((float) ps->leg_cellsize * 0.25));
2161
2162 /* print stats */
2163 if(occl->nres != OCCL_BLANK_COUNT) {
2164 x = ps->legx_stats;
2165 if(ps->mask != NULL && ps->modeA[pagenum] == ALIMODE) {
2166 fprintf(fp, "(%4d %4d) %.2f %.2f moveto show\n", occl->nres, occl->nres_masked, x, y + ((float) ps->leg_cellsize * 0.25));
2167 }
2168 else {
2169 fprintf(fp, "(%5d) %.2f %.2f moveto show\n", occl->nres, x, y + ((float) ps->leg_cellsize * 0.25));
2170 }
2171 }
2172 }
2173
2174 /* reset color to black */
2175 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", 0., 0., 0., 1.);
2176
2177 if(occl->do_separator) {
2178 cur_width = ps->legx_max_chars -
2179 ((int) ps->leg_rhs_space / ps->legx_charsize) -
2180 ((int) (PAGE_SIDEBUF / ps->legx_charsize)) -
2181 LEG_EXTRA_COLUMNS - 2;
2182 ESL_ALLOC(cur_string, sizeof(char) * (cur_width+1));
2183 for(i = 0; i < cur_width; i++) cur_string[i] = '-';
2184 cur_string[cur_width] = '\0';
2185 fprintf(fp, "(%s) %.2f %.2f moveto show\n", cur_string, ps->legx, (y - ((float) ps->leg_cellsize * 0.5)));
2186 fprintf(fp, "(-----) %.2f %.2f moveto show\n", (float) ps->legx_stats, (y - ((float) ps->leg_cellsize * 0.5)));
2187 y -= (float) ps->leg_cellsize * 0.5;
2188 free(cur_string);
2189 }
2190
2191 /* y -= (float) ps->leg_cellsize * 1.5; */
2192 if(occl->do_no_vspace) { y -= (float) (ps->leg_cellsize / COURIER_HEIGHT_WIDTH_RATIO) * 1.25; }
2193 else { y -= (float) ps->leg_cellsize * 1.25; }
2194 ps->cur_legy = y;
2195
2196
2197 fprintf(fp, "%% end one cell color legend\n\n");
2198 return eslOK;
2199
2200 ERROR: esl_fatal("ERROR drawing one cell legend, probably out of memory.");
2201 return eslEMEM; /* never reached */
2202 }
2203
2204
2205 /* Function: draw_scheme_colorlegend()
2206 *
2207 * Purpose: Print a scheme color legend to an open file.
2208 * Return: eslOK
2209 */
2210 static int
2211 draw_scheme_colorlegend(const ESL_GETOPTS *go, FILE *fp, SchemeColorLegend_t *scl, float **hc_scheme, SSPostscript_t *ps, int pagenum)
2212 {
2213 float x, y;
2214 int cp;
2215 int c,i,n1s;
2216 float fontsize;
2217 int do_circle_mask, do_square_mask, do_x_mask, do_border;
2218 int do_mask;
2219 float old_x;
2220
2221 do_mask = ((ps->mask != NULL) && (scl->use_mask)) ? TRUE : FALSE;
2222 do_border = (!esl_opt_GetBoolean(go, "--mask-a"));
2223 do_circle_mask = do_square_mask = do_x_mask = FALSE;
2224 if(esl_opt_GetBoolean(go, "--mask-u")) { do_square_mask = TRUE; }
2225 else if(esl_opt_GetBoolean(go, "--mask-x")) { do_x_mask = TRUE; }
2226 else do_circle_mask = TRUE;
2227
2228 x = ps->legx;
2229 y = ps->cur_legy;
2230 //y = ps->legy - (ps->nocclA[pagenum] * ((float) ps->leg_cellsize * 1.5));
2231 fontsize = LEG_FONTSIZE_UNSCALED / ps->scale;
2232 fprintf(fp, "%% begin color scheme legend\n");
2233 fprintf(fp, "/%s findfont %f scalefont setfont\n", LEG_FONT, fontsize);
2234 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n");
2235
2236 float colvec[NCMYK];
2237 colvec[0] = colvec[1] = colvec[2] = 0.;
2238 colvec[3] = 1.0;
2239 if(do_mask) { /* print cells showing difference between masked and unmasked */
2240 /*x -= (float) ps->leg_cellsize;*/
2241 /*y -= (float) ps->leg_cellsize;*/
2242 fprintf(fp, "%.1f setlinewidth\n", (float) ps->leg_cellsize/4.);
2243 fprintf(fp, "newpath\n");
2244 fprintf(fp, " %.2f %.2f moveto", x, y);
2245 fprintf(fp, " 0 %.3f rlineto %.3f 0 rlineto 0 %.3f rlineto closepath\n", (float) ps->leg_cellsize, (float) ps->leg_cellsize, (-1 * (float) ps->leg_cellsize));
2246 fprintf(fp, " ");
2247 for(cp = 0; cp < NCMYK; cp++) {
2248 fprintf(fp, "%.2f ", colvec[cp]);
2249 }
2250 fprintf(fp, "setcmykcolor\n");
2251 fprintf(fp, " fill\n");
2252
2253 /* print label */
2254 x += (float) ps->leg_cellsize * 1.5;
2255 y += (float) ps->leg_cellsize * 0.625;
2256 fprintf(fp, "(included by mask) %.2f %.2f moveto show\n", x, y);
2257 y -= (float) ps->leg_cellsize * 0.625;
2258 fprintf(fp, "((all colors)) %.2f %.2f moveto show\n", x, y);
2259 x -= (float) ps->leg_cellsize * 1.5;
2260
2261 /* print stats for included by mask */
2262 old_x = x;
2263 n1s = 0;
2264 for(i = 0; i < ps->rflen; i++) if(ps->mask[i] == '1') n1s++;
2265 x = ps->legx_stats;
2266 y += (float) ps->leg_cellsize * 0.3125;
2267 fprintf(fp, "(%4d %4d) %.2f %.2f moveto show\n", n1s, n1s, x, y);
2268 y -= (float) ps->leg_cellsize * 0.3125;
2269
2270 x = old_x;
2271 y -= (float) ps->leg_cellsize * 1.5;
2272 draw_masked_block(fp, x, y, colvec, do_circle_mask, do_square_mask, do_x_mask, do_border, (float) ps->leg_cellsize);
2273
2274 x += (float) ps->leg_cellsize * 1.5;
2275 y += (float) ps->leg_cellsize * 0.625;
2276 fprintf(fp, "(excluded by mask) %.2f %.2f moveto show\n", x, y);
2277 y -= (float) ps->leg_cellsize * 0.625;
2278 fprintf(fp, "((all colors)) %.2f %.2f moveto show\n", x, y);
2279
2280 /* print stats for excluded by mask */
2281 x = ps->legx_stats;
2282 y += (float) ps->leg_cellsize * 0.3125;
2283 fprintf(fp, "(%4d %4d) %.2f %.2f moveto show\n", ps->rflen-n1s, 0, x, y);
2284
2285 y -= (float) ps->leg_cellsize * 1.8125;
2286 x = ps->legx;
2287 }
2288
2289 /* print text for this legend */
2290 if(scl->text1 != NULL) {
2291 if(scl->text2 == NULL) {
2292 fprintf(fp, "(%s:) %.2f %.2f moveto show\n", scl->text1, x, (y + ((float) ps->leg_cellsize * .25)));
2293 }
2294 else {
2295 fprintf(fp, "(%s) %.2f %.2f moveto show\n", scl->text1, x, (y + ((float) ps->leg_cellsize * .25)));
2296 y -= (float) ps->leg_cellsize * 0.625;
2297 fprintf(fp, "(%s:) %.2f %.2f moveto show\n", scl->text2, x, (y + ((float) ps->leg_cellsize * .25)));
2298 }
2299 }
2300 y -= (float) ps->leg_cellsize;
2301
2302 /* print scheme color cells and labels next to them */
2303 for(c = 0; c < scl->nbins; c++) {
2304 fprintf(fp, "newpath\n");
2305 fprintf(fp, " %.2f %.2f moveto", x, y);
2306 fprintf(fp, " 0 %.3f rlineto %.3f 0 rlineto 0 %.3f rlineto closepath\n", (float) ps->leg_cellsize, (float) ps->leg_cellsize, (-1 * (float) ps->leg_cellsize));
2307 fprintf(fp, " ");
2308 for(cp = 0; cp < NCMYK; cp++) {
2309 fprintf(fp, "%.2f ", hc_scheme[c][cp]);
2310 }
2311 fprintf(fp, "setcmykcolor\n");
2312 fprintf(fp, " fill\n");
2313
2314 /* draw a small box outlining the colored block we just drew */
2315 /*fprintf(fp, "%.2f setlinewidth\n", (ps->leg_cellsize / 24.));
2316 fprintf(fp, "newpath\n");
2317 fprintf(fp, " %.2f %.2f moveto", x, y);
2318 fprintf(fp, " 0 %.3f rlineto %.3f 0 rlineto 0 %.3f rlineto closepath\n", (float) ps->leg_cellsize, (float) ps->leg_cellsize, (-1 * (float) ps->leg_cellsize));
2319 fprintf(fp, " ");
2320 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", 0., 0., 0., 1.);
2321 fprintf(fp, " stroke\n");*/
2322
2323 /* print label */
2324 x += (float) ps->leg_cellsize * 1.5;
2325 y += (float) ps->leg_cellsize * 0.25;
2326 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n");
2327 if(esl_FCompare_old(scl->limits[c+1], eslINFINITY, eslSMALLX1) == eslOK) { /* max value is infinity, special case */
2328 if(c != scl->nbins-1) esl_fatal("ERROR when drawing color legend, limits[%d] is INFINITY, but this is reserved only for the max limit", c+1);
2329 if(scl->ints_only_flag) fprintf(fp, "(>=%d) %.2f %.2f moveto show\n", (int) scl->limits[c], x, y);
2330 else fprintf(fp, "(>=%3.f) %.2f %.2f moveto show\n", scl->limits[c], x, y);
2331 }
2332 else if(scl->ints_only_flag) {
2333 if(c == scl->nbins-1) {
2334 fprintf(fp, "(\\[%d-%d\\]) %.2f %.2f moveto show\n", (int) scl->limits[c], (int) scl->limits[c+1], x, y);
2335 }
2336 else if(esl_FCompare_old(scl->limits[c], scl->limits[c+1]-1, eslSMALLX1) == eslOK) { /* next limit is exactly 1 plus cur limit, don't do range, define single int */
2337 if(compare_two_cmyk_colors(hc_scheme[c][ICYAN], hc_scheme[c][IMGTA], hc_scheme[c][IYELW], hc_scheme[c][IBLCK], WHITE_C, WHITE_M, WHITE_Y, WHITE_K)) {
2338 /* color is white and won't show up on our white background, so we explicitly state (blank) next to it's color value */
2339 fprintf(fp, "(\\(blank\\) %d) %.2f %.2f moveto show\n", (int) scl->limits[c], x, y);
2340 }
2341 else {
2342 fprintf(fp, "(%d) %.2f %.2f moveto show\n", (int) scl->limits[c], x, y);
2343 }
2344 }
2345 else {
2346 fprintf(fp, "(\\[%d-%d\\]) %.2f %.2f moveto show\n", (int) scl->limits[c], (int) scl->limits[c+1]-1, x, y);
2347 }
2348 }
2349 else {
2350 if(c == scl->nbins-1) fprintf(fp, "(\\[%.3f-%.3f\\%c) %.2f %.2f moveto show\n", scl->limits[c], scl->limits[c+1], (scl->high_inclusive ? ']' : ')'), x, y);
2351 else if(c == 0) fprintf(fp, "(\\%c%.3f-%.3f\\)) %.2f %.2f moveto show\n", (scl->low_inclusive ? '[' : '('), scl->limits[c], scl->limits[c+1], x, y);
2352 else fprintf(fp, "(\\[%.3f-%.3f\\)) %.2f %.2f moveto show\n", scl->limits[c], scl->limits[c+1], x, y);
2353 }
2354 /* print stats */
2355 old_x = x;
2356 x = ps->legx_stats;
2357 if(do_mask) {
2358 fprintf(fp, "(%4d %4d) %.2f %.2f moveto show\n", scl->counts[c], scl->counts_masked[c], x, y);
2359 }
2360 else {
2361 fprintf(fp, "(%5d) %.2f %.2f moveto show\n", scl->counts[c], x, y);
2362 }
2363
2364 x = old_x - (float) ps->leg_cellsize * 1.5;
2365 y -= (float) ps->leg_cellsize * 0.25;
2366 y -= (float) ps->leg_cellsize;
2367 }
2368
2369 /* reset color to black */
2370 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", 0., 0., 0., 1.);
2371 fprintf(fp, "%% end color scheme legend\n\n");
2372
2373 ps->cur_legy = y;
2374 return eslOK;
2375 }
2376
2377
2378 /* Function: draw_text_section_in_legend()
2379 *
2380 * Purpose: Print a text section in the legend section.
2381 * Return: eslOK, dies if we run out of memory.
2382 */
2383 static int
2384 draw_text_section_in_legend(FILE *fp, TextLegend_t *tl, SSPostscript_t *ps, int tl_idx, int pagenum)
2385 {
2386 int status;
2387 float x, y;
2388 int i;
2389 float fontsize;
2390 char *cur_string;
2391 int cur_width = 0;
2392
2393 x = ps->legx;
2394 y = ps->cur_legy;
2395
2396 /* print cell */
2397 fprintf(fp, "%% begin text legend\n");
2398 fontsize = LEG_EXTRA_TEXT_FONTSIZE_UNSCALED / ps->scale;
2399 /* print text for this legend */
2400
2401 /* back to black */
2402 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n");
2403 fprintf(fp, "/%s findfont %f scalefont setfont\n", LEG_EXTRA_TEXT_FONT, fontsize);
2404 for(i = 0; i < tl->nlines; i++) {
2405 fprintf(fp, "(%s) %.2f %.2f moveto show\n", tl->text_per_line[i], x, y);
2406 y -= (float) fontsize * 1.25;
2407 }
2408 if(tl->do_separator) {
2409 cur_width = ps->legx_max_chars - ((int) (PAGE_SIDEBUF / ps->legx_charsize)) - LEG_EXTRA_COLUMNS - 2;
2410 ESL_ALLOC(cur_string, sizeof(char) * (cur_width+1));
2411 for(i = 0; i < cur_width; i++) cur_string[i] = '-';
2412 cur_string[cur_width] = '\0';
2413 fprintf(fp, "(%s) %.2f %.2f moveto show\n", cur_string, ps->legx, (y - ((float) ps->leg_cellsize * 0.5)));
2414 fprintf(fp, "(-----) %.2f %.2f moveto show\n", (float) ps->legx_stats, (y - ((float) ps->leg_cellsize * 0.5)));
2415 y -= (float) ps->leg_cellsize * 0.5;
2416 free(cur_string);
2417 }
2418 ps->cur_legy = y - (1.0 * (float) ps->leg_cellsize);
2419
2420 fprintf(fp, "%% end text legend\n\n");
2421 return eslOK;
2422
2423 ERROR: esl_fatal("ERROR drawing text legend, probably out of memory.");
2424 return eslEMEM; /* never reached */
2425 }
2426
2427 /* Function: draw_sspostscript()
2428 *
2429 * Purpose: Print a SS postscript data structure.
2430 * Return: eslOK on success;
2431 * eslEINCOMPAT if ps->npage == 0
2432 */
2433 static int
2434 draw_sspostscript(FILE *fp, const ESL_GETOPTS *go, char *errbuf, char *command, char *date, float ***hc_scheme, SSPostscript_t *ps)
2435 {
2436 int status;
2437 int p, pi, i, c, l;
2438 int do_circle_mask, do_square_mask, do_x_mask, do_border;
2439 int *page_orderA;
2440
2441 if(ps->modelname == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read modelname from template file.");
2442
2443 do_border = (!esl_opt_GetBoolean(go, "--mask-a"));
2444 do_circle_mask = do_square_mask = do_x_mask = FALSE;
2445 if(esl_opt_GetBoolean(go, "--mask-u")) { do_square_mask = TRUE; }
2446 else if(esl_opt_GetBoolean(go, "--mask-x")) { do_x_mask = TRUE; }
2447 else do_circle_mask = TRUE;
2448
2449 if(ps->npage == 0) ESL_FAIL(eslEINCOMPAT, errbuf, "draw_sspostscript, ps->npage == 0\n");
2450
2451 /* determine print order of pages, currently this is just 0..npage-1 */
2452 ESL_ALLOC(page_orderA, sizeof(int) * ps->npage);
2453 for(pi = 0; pi < ps->npage; pi++) page_orderA[pi] = pi;
2454
2455 /* define procedures */
2456 fprintf(fp, "%% ------------------------------------------------------------\n");
2457 fprintf(fp, "%% Procedure definitions\n");
2458 define_outline_procedure(fp);
2459 fprintf(fp, "%% ------------------------------------------------------------\n");
2460
2461 /* draw the pages */
2462 for(pi = 0; pi < ps->npage; pi++) {
2463 p = page_orderA[pi];
2464 ps->cur_legy = ps->legy;
2465
2466 /* print postscript comment header, only visible if viewed in text mode */
2467 fprintf(fp, "%% ------------------------------------------------------------\n");
2468 fprintf(fp, "%% Postscript file created by esl-ssdraw (page %d of %d)\n", pi+1, ps->npage);
2469 fprintf(fp, "%% ------------------------------------------------------------\n");
2470 fprintf(fp, "%% msafile: %s (%d seqs)\n", esl_opt_GetArg(go, 1), ps->msa_nseq);
2471 fprintf(fp, "%% templatefile: %s\n", esl_opt_GetArg(go, 2));
2472 fprintf(fp, "%% modelname: %s\n", ps->modelname);
2473 fprintf(fp, "%% consensus-len: %d\n", ps->rflen);
2474 if(esl_opt_IsOn(go, "--mask")) {
2475 fprintf(fp, "%% maskfile: %s\n", esl_opt_GetString(go, "--mask"));
2476 }
2477 if(esl_opt_IsOn(go, "--mask-diff")) {
2478 fprintf(fp, "%% difffile: %s\n", esl_opt_GetString(go, "--mask-diff"));
2479 }
2480 if(esl_opt_IsOn(go, "--dfile")) {
2481 fprintf(fp, "%% dfile: %s\n", esl_opt_GetString(go, "--dfile"));
2482 }
2483 if(esl_opt_IsOn(go, "--efile")) {
2484 fprintf(fp, "%% efile: %s\n", esl_opt_GetString(go, "--efile"));
2485 }
2486 if(esl_opt_IsOn(go, "--ifile")) {
2487 fprintf(fp, "%% ifile: %s\n", esl_opt_GetString(go, "--ifile"));
2488 }
2489 fprintf(fp, "%%\n");
2490
2491 /* scale section */
2492 fprintf(fp, "%.2f %.2f scale\n\n", ps->scale, ps->scale);
2493
2494 /* header section */
2495 if((status = draw_header_and_footer(fp, go, errbuf, ps, p, pi+1)) != eslOK) return status;
2496
2497 /* regurgitated section */
2498 if(ps->regurgA != NULL) {
2499 fprintf(fp, "%% begin regurgitate\n");
2500 for(i = 0; i < ps->nregurg; i++)
2501 fprintf(fp, "%s", ps->regurgA[i]);
2502 fprintf(fp, "%% end regurgitate\n\n");
2503 }
2504
2505 /* 'text positiontext' section */
2506 for(i = 0; i < ps->nposntext; i++) {
2507 if(i == 0) {
2508 fprintf(fp, "%% begin text positiontext\n");
2509 fprintf(fp, "/%s findfont %.2f scalefont setfont\n", POSNTEXT_FONT, POSNTEXT_FONTSIZE);
2510 fprintf(fp, "0.00 0.00 0.00 1.00 setcmykcolor\n"); /* black */
2511 }
2512 fprintf(fp, "%s %.2f %.2f moveto show\n", ps->posntextA[i], ps->posntextxA[i], ps->posntextyA[i]);
2513 if(i == (ps->nposntext-1)) {
2514 fprintf(fp, "%% end text positiontext\n\n");
2515 }
2516 }
2517
2518 /* 'lines bpconnects' section */
2519 for(i = 0; i < ps->nbp; i++) {
2520 if(i == 0) {
2521 fprintf(fp, "%% begin lines bpconnects\n");
2522 fprintf(fp, "%.2f setlinewidth\n", BP_LINEWIDTH);
2523 fprintf(fp, "0.00 0.00 0.00 1.00 setcmykcolor\n"); /* black */
2524 }
2525 fprintf(fp, "%.2f %.2f %.2f %.2f newpath moveto lineto stroke\n", ps->bpx1A[i], ps->bpy1A[i], ps->bpx2A[i], ps->bpy2A[i]);
2526 if(i == (ps->nbp-1)) {
2527 fprintf(fp, "%% end lines bpconnects\n\n");
2528 }
2529 }
2530
2531 /* print out remainder of the page */
2532 /* fprintf(fp, "%% begin ignore\n"); */
2533 fprintf(fp, "0.00 0.00 0.00 1.00 setcmykcolor\n"); /* set to black */
2534 fprintf(fp, "/%s findfont %f scalefont setfont\n\n", LEG_FONT, LEG_FONTSIZE_UNSCALED / ps->scale);
2535
2536 /* draw legend headers, if we have a legend (only legend text doesn't require legend headers) */
2537 if((ps->nocclA[p] > 0) || (ps->sclAA != NULL && ps->sclAA[p] != NULL)) {
2538 if(! (esl_opt_GetBoolean(go, "--no-leg"))) {
2539 if((status = draw_legend_column_headers(fp, ps, p, errbuf)) != eslOK) return status;
2540 }
2541 }
2542
2543 /* print one cell color legends, if any */
2544 if(ps->occlAAA != NULL && ps->occlAAA[p] != NULL) {
2545 for(l = 0; l < ps->nocclA[p]; l++) {
2546 if(! (esl_opt_GetBoolean(go, "--no-leg"))) {
2547 draw_onecell_colorlegend(fp, ps->occlAAA[p][l], ps, l, p);
2548 }
2549 }
2550 }
2551 /* print scheme color legends, if any */
2552 if(ps->sclAA != NULL && ps->sclAA[p] != NULL) {
2553 if(! (esl_opt_GetBoolean(go, "--no-leg"))) {
2554 draw_scheme_colorlegend(go, fp, ps->sclAA[p], hc_scheme[ps->sclAA[p]->scheme], ps, p);
2555 }
2556 }
2557 /* print text legends, if any */
2558 if(ps->tlAAA != NULL && ps->tlAAA[p] != NULL) {
2559 for(l = 0; l < ps->ntlA[p]; l++) {
2560 if(! (esl_opt_GetBoolean(go, "--no-leg"))) {
2561 draw_text_section_in_legend(fp, ps->tlAAA[p][l], ps, l, p);
2562 }
2563 }
2564 }
2565
2566 if(ps->bcolAAA != NULL && ps->bcolAAA[p] != NULL) {
2567 fprintf(fp, "%% begin colored positions\n");
2568 if(ps->mask != NULL && ps->modeA[p] != SIMPLEMASKMODE && ps->modeA[p] != INDIMODE) {
2569 fprintf(fp, "2.0 setlinewidth\n");
2570 if(do_border && do_x_mask) { fprintf(fp, "1.0 setlinewidth\n"); }
2571 if(do_border && do_square_mask) { fprintf(fp, "2.0 setlinewidth\n"); }
2572 if(do_border && do_circle_mask) { fprintf(fp, "2.5 setlinewidth\n"); }
2573 for(c = 0; c < ps->rflen; c++) {
2574 fprintf(fp, "%snucleotide %d\n", "%", c+1);
2575 if(ps->mask[c] == '0') {
2576 draw_masked_block(fp, ps->rxA[c]-1., ps->ryA[c]-1., ps->bcolAAA[p][c], do_circle_mask, do_square_mask, do_x_mask, do_border, CELLSIZE);
2577 }
2578 else { /* cell is within mask, ps->mask[c] == '1' */
2579 fprintf(fp, "newpath\n");
2580 fprintf(fp, " %.2f %.2f moveto", ps->rxA[c] - (CELLSIZE * CELL_XOFFSET_FRACTION), ps->ryA[c] - (CELLSIZE * CELL_YOFFSET_FRACTION));
2581 fprintf(fp, " 0 %d rlineto %d 0 rlineto 0 %d rlineto closepath\n", CELLSIZE_INT, CELLSIZE_INT, -1 * CELLSIZE_INT);
2582 fprintf(fp, " %.2f %.2f %.2f %.2f setcmykcolor\n", ps->bcolAAA[p][c][0], ps->bcolAAA[p][c][1], ps->bcolAAA[p][c][2], ps->bcolAAA[p][c][3]);
2583 fprintf(fp, " fill\n");
2584 }
2585 }
2586 fprintf(fp, "1.00 setlinewidth\n");
2587 }
2588 else { /* no mask, all cells are printed the same */
2589 for(c = 0; c < ps->rflen; c++) {
2590 fprintf(fp, "%snucleotide %d\n", "%", c+1);
2591 fprintf(fp, "newpath\n");
2592 fprintf(fp, " %.2f %.2f moveto", ps->rxA[c] - (CELLSIZE * CELL_XOFFSET_FRACTION), ps->ryA[c] - (CELLSIZE * CELL_YOFFSET_FRACTION));
2593 fprintf(fp, " 0 %d rlineto %d 0 rlineto 0 %d rlineto closepath\n", CELLSIZE_INT, CELLSIZE_INT, -1 * CELLSIZE_INT);
2594 fprintf(fp, " %.2f %.2f %.2f %.2f setcmykcolor\n", ps->bcolAAA[p][c][0], ps->bcolAAA[p][c][1], ps->bcolAAA[p][c][2], ps->bcolAAA[p][c][3]);
2595 fprintf(fp, " fill\n");
2596 }
2597 }
2598 /* back to black */
2599 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n");
2600 fprintf(fp, "%% end colored positions\n\n");
2601 }
2602
2603 if(ps->rAA[p] != NULL) {
2604 fprintf(fp, "/%s findfont %f scalefont setfont\n", NUCLEOTIDES_FONT, NUCLEOTIDES_FONTSIZE);
2605 fprintf(fp, " 0.00 0.00 0.00 1.00 setcmykcolor\n"); /* reset color as black */
2606 fprintf(fp, "%% begin text nucleotides\n");
2607 if(ps->rcolAAA[p] != NULL) {
2608 for(c = 0; c < ps->rflen; c++) {
2609 fprintf(fp, " %.2f %.2f %.2f %.2f setcmykcolor ", ps->rcolAAA[p][c][0], ps->rcolAAA[p][c][1], ps->rcolAAA[p][c][2], ps->rcolAAA[p][c][3]);
2610 if(strchr(LOWERCASE_LOW_HANGING_CHARS, ps->rAA[p][c]) != NULL) {
2611 fprintf(fp, "(%c) %.2f %.2f moveto show", ps->rAA[p][c], ps->rxA[c], ps->ryA[c] + (CELLSIZE * NUCLEOTIDE_YOFFSET_FRACTION_LOWERCASE_LOW_HANGING_CHARS));
2612 /* handle lowercase low hanging chars ("gjpqy") nucleotides special, so they're centered in the box! */
2613 }
2614 else if(ps->rAA[p][c] != ' ') {
2615 fprintf(fp, "(%c) %.2f %.2f moveto show", ps->rAA[p][c], ps->rxA[c], ps->ryA[c]);
2616 }
2617 fprintf(fp, "\n");
2618 }
2619 }
2620 else { /* ps->rcolAAA[p] is NULL */
2621 for(c = 0; c < ps->rflen; c++) {
2622 if(strchr(LOWERCASE_LOW_HANGING_CHARS, ps->rAA[p][c]) != NULL) {
2623 fprintf(fp, "(%c) %.2f %.2f moveto show", ps->rAA[p][c], ps->rxA[c], ps->ryA[c] + (CELLSIZE * NUCLEOTIDE_YOFFSET_FRACTION_LOWERCASE_LOW_HANGING_CHARS));
2624 /* handle lowercase low hanging chars ("gjpqy") nucleotides special, so they're centered in the box! */
2625 }
2626 else if(ps->rAA[p][c] != ' ') {
2627 fprintf(fp, "(%c) %.2f %.2f moveto show", ps->rAA[p][c], ps->rxA[c], ps->ryA[c]);
2628 }
2629 fprintf(fp, "\n");
2630 }
2631 }
2632 fprintf(fp, "%% end text nucleotides\n");
2633 fprintf(fp, " 0.000 0.000 0.000 1.000 setcmykcolor\n"); /* reset color as black */
2634 }
2635
2636 if(ps->otypeAA != NULL && ps->otypeAA[p] != NULL) {
2637 fprintf(fp, "%% begin outlines\n");
2638 for(c = 0; c < ps->rflen; c++) {
2639 if(ps->otypeAA[p][c] != OUTLINE_NONE_IDX) {
2640 /* push x and y and cellsize onto stack */
2641 fprintf(fp, "%.2f %.2f %.2f",
2642 ps->rxA[c] - (CELLSIZE * CELL_XOFFSET_FRACTION),
2643 ps->ryA[c] - (CELLSIZE * CELL_YOFFSET_FRACTION),
2644 CELLSIZE);
2645 if(ps->otypeAA[p][c] == OUTLINE_MIN_IDX) {
2646 fprintf(fp, " %.2f %s\n",
2647 OUTLINE_LINEWIDTH_CELL_FRACTION_MIN * CELLSIZE,
2648 OUTLINE_PROCEDURE);
2649 }
2650 else if(ps->otypeAA[p][c] == OUTLINE_MAX_IDX) {
2651 fprintf(fp, " %.2f %s\n",
2652 OUTLINE_LINEWIDTH_CELL_FRACTION_MAX * CELLSIZE,
2653 OUTLINE_PROCEDURE);
2654 }
2655 }
2656 }
2657 fprintf(fp, "%% end outlines\n");
2658 fprintf(fp, " 0.000 0.000 0.000 1.000 setcmykcolor\n"); /* reset color as black */
2659 }
2660
2661 /* 'lines positionsticks' section, drawn last so they're not buried beneath other objects */
2662 for(i = 0; i < ps->nticks; i++) {
2663 if(i == 0) {
2664 fprintf(fp, "%% begin lines positionticks\n");
2665 fprintf(fp, "%.2f setlinewidth\n", TICKS_LINEWIDTH);
2666 fprintf(fp, "0.00 0.00 0.00 1.00 setcmykcolor\n"); /* black */
2667 }
2668 fprintf(fp, "%.2f %.2f %.2f %.2f newpath moveto lineto stroke\n", ps->ticksx1A[i], ps->ticksy1A[i], ps->ticksx2A[i], ps->ticksy2A[i]);
2669 if(i == (ps->nticks-1)) {
2670 fprintf(fp, "%% end lines positionticks\n\n");
2671 }
2672 }
2673
2674 fprintf(fp, "showpage\n\n");
2675 /* fprintf(fp, "%% end ignore\n\n"); */
2676 }
2677 free(page_orderA);
2678 return eslOK;
2679
2680 ERROR: ESL_FAIL(eslEINVAL, errbuf, "draw_sspostscript() error, probably out of memory.");
2681 }
2682
2683
2684 /* parse_template_file
2685 *
2686 * Read secondary structure templates from a postscript
2687 * template file derived from the Gutell CRW website until
2688 * the one that corresponds to our alignment is found,
2689 * (defined as the first template structure that has the
2690 * same consensus length as the passed in <msa_rflen>)
2691 * or we run out structure templates. This function
2692 * repeatedly calls parse_template() which actually parses
2693 * the templates.
2694 *
2695 * If we run out of structure templates of anything is
2696 * invalid in any of the templates in the file, we
2697 * return a non-eslOK status code to caller and fill
2698 * errbuf with the error message.
2699 *
2700 * Returns: eslOK on success.
2701 */
2702 int
2703 parse_template_file(char *filename, const ESL_GETOPTS *go, char *errbuf, int msa_rflen, SSPostscript_t **ret_ps)
2704 {
2705 int status;
2706 ESL_FILEPARSER *efp;
2707 SSPostscript_t *ps;
2708 int found_match = FALSE;
2709
2710 if (esl_fileparser_Open(filename, NULL, &efp) != eslOK) esl_fatal("ERROR, failed to open template file %s in parse_template_file\n", filename);
2711 esl_fileparser_SetCommentChar(efp, '#');
2712
2713 status = eslOK;
2714 while((found_match == FALSE) && (status == eslOK)) {
2715 status = parse_template_page(efp, go, errbuf, &ps);
2716 if((status != eslOK) && (status != eslEOF)) {
2717 esl_fileparser_Close(efp);
2718 return status;
2719 }
2720 if(ps->rflen == msa_rflen) { found_match = TRUE; }
2721 else { free_sspostscript(ps); }
2722 }
2723 if(found_match == FALSE) {
2724 esl_fileparser_Close(efp);
2725 esl_fatal("ERROR, did not find template structure to match alignment consensus length of %d in:\n%s\n", msa_rflen, filename);
2726 }
2727
2728 /* if we get here, we've found a match */
2729
2730 /* validate the template we just read */
2731 if((status = validate_justread_sspostscript(ps, errbuf)) != eslOK) return status;
2732
2733 esl_fileparser_Close(efp);
2734 *ret_ps = ps;
2735
2736 return eslOK;
2737 }
2738
2739
2740 /* parse_template_page
2741 *
2742 * Read a single secondary structure template page for a
2743 * single CM from a postscript template file derived from the
2744 * Gutell CRW website. This function is called repeatedly
2745 * on the same file to read multiple structure templates.
2746 * The logic is to keep reading until we find one with the
2747 * same consensus length as our input alignment.
2748 *
2749 * The structure template is read in sections.
2750 * Each section begins with a line like this:
2751 * % begin <type1> <type2>
2752 *
2753 * list of valid tokens for <type1>:
2754 * modelname
2755 * legcoords
2756 * scale
2757 * regurgitate
2758 * ignore
2759 * lines
2760 * text
2761 *
2762 * if <type1> is lines or text, then <type2> is read,
2763 * valid tokens for <type2> if <type1> is 'text'
2764 * positiontext
2765 * nucleotides
2766 *
2767 * valid tokens for <type2> if <type1> is 'lines'
2768 * positionticks
2769 * bpconnects
2770 *
2771 * The 'regurgitate' lines are stored, but never changed.
2772 * The 'ignore' lines are not even stored.
2773 * All other <type1> lines are stored in data structures that
2774 * can be manipulated (though not all are at this point).
2775 *
2776 * If anything is invalid, we return a non-eslOK status code
2777 * to caller.
2778 *
2779 * Returns: eslOK on success.
2780 */
2781 int
2782 parse_template_page(ESL_FILEPARSER *efp, const ESL_GETOPTS *go, char *errbuf, SSPostscript_t **ret_ps)
2783 {
2784 int status;
2785 char *tok;
2786 int toklen;
2787 SSPostscript_t *ps = NULL;
2788 int read_showpage = FALSE;
2789 int reached_eof = FALSE;
2790
2791 /* Create the postscript object */
2792 ps = create_sspostscript(go);
2793
2794 while ((read_showpage == FALSE) && ((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK)) {
2795 if(strcmp(tok, "%") == 0) {
2796 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) {
2797 if(strcmp(tok, "begin") == 0) {
2798 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) {
2799 if(strcmp(tok, "modelname") == 0) {
2800 if((status = parse_modelname_section(efp, errbuf, ps)) != eslOK) return status;
2801 }
2802 else if(strcmp(tok, "legend") == 0) {
2803 /*printf("parsing legend\n");*/
2804 if((status = parse_legend_section(efp, errbuf, ps)) != eslOK) return status;
2805 }
2806 else if(strcmp(tok, "scale") == 0) {
2807 /*printf("parsing scale\n");*/
2808 if((status = parse_scale_section(efp, errbuf, ps)) != eslOK) return status;
2809 }
2810 else if(strcmp(tok, "ignore") == 0) {
2811 /*printf("parsing ignore\n");*/
2812 if((status = parse_ignore_section(efp, errbuf, &read_showpage)) != eslOK) return status;
2813 }
2814 else if(strcmp(tok, "regurgitate") == 0) {
2815 /*printf("parsing regurgitate\n");*/
2816 if((status = parse_regurgitate_section(efp, errbuf, ps)) != eslOK) return status;
2817 }
2818 else if(strcmp(tok, "text") == 0) {
2819 /*printf("parsing text\n");*/
2820 if((status = parse_text_section(efp, errbuf, ps)) != eslOK) return status;
2821 }
2822 else if(strcmp(tok, "lines") == 0) {
2823 /*printf("parsing lines\n");*/
2824 if((status = parse_lines_section(efp, errbuf, ps)) != eslOK) return status;
2825 }
2826 else {
2827 ESL_FAIL(eslEINVAL, errbuf, "parse_template_page(), error, unknown section type %s.", tok);
2828 }
2829 }
2830 else {
2831 ESL_FAIL(eslEINVAL, errbuf, "parse_template_page(), error last read line number %d.", efp->linenumber);
2832 }
2833 }
2834 else {
2835 ESL_FAIL(eslEINVAL, errbuf, "parse_template_page(), expected line beginning with %%%% begin, but read tok: %s instead of begin, last read line number %d.", tok, efp->linenumber);
2836 }
2837 }
2838 else {
2839 ESL_FAIL(eslEINVAL, errbuf, "parse_template_page(), ran out of tokens early, error last read line number %d.", efp->linenumber);
2840 }
2841 }
2842 else {
2843 ESL_FAIL(eslEINVAL, errbuf, "parse_template_page(), expected line beginning with %%%%, read tok: %s, last read line number %d.", tok, efp->linenumber);
2844 }
2845 }
2846 if(read_showpage == FALSE && status != eslEOF) {
2847 ESL_FAIL(status, errbuf, "parse_template_page(), error, ran out of tokens, but not at end of file?, last read line number %d.", efp->linenumber);
2848 }
2849 if(status == eslEOF) reached_eof = TRUE;
2850
2851 *ret_ps = ps;
2852
2853 if(reached_eof) return eslEOF;
2854 else return eslOK;
2855 }
2856
2857 /* parse_modelname_section
2858 *
2859 * Parse the modelname section of a template postscript file.
2860 * If anything is invalid, we return a non-eslOK status code
2861 * to caller.
2862 *
2863 * Returns: eslOK on success.
2864 */
2865 int
2866 parse_modelname_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
2867 {
2868 int status;
2869 char *tok;
2870 int toklen;
2871 char *curstr = NULL;
2872 int curlen = 0;
2873 int ntok = 0;
2874 /* this section should be exactly 3 lines, one of which we've already read,
2875 * here's an example, next token should be the first 0.65
2876 * % begin modelname
2877 * % archaea
2878 * % end scale
2879 */
2880 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing modelname section, reading token 1 of 3");
2881 if (strcmp(tok, "%") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing modelname section, middle line token 1 should be a percent sign but it's %s", tok);
2882 /* read remainder of line, this is the model name */
2883 while ((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) == eslOK) {
2884 if(ntok > 0) {
2885 if((status = esl_strcat(&curstr, curlen, " ", 1)) != eslOK) ESL_FAIL(status, errbuf, "parse_modelname_section(), error parsing model name.");
2886 curlen += 1;
2887 }
2888 if((status = esl_strcat(&curstr, curlen, tok, toklen)) != eslOK) ESL_FAIL(status, errbuf, "parse_modelname_section(), error parsing model name.");
2889 curlen += toklen;
2890 ntok++;
2891 }
2892 ESL_ALLOC(ps->modelname, sizeof(char) * (curlen+1));
2893 strcpy(ps->modelname, curstr);
2894
2895 /* next line should be '% end modelname' */
2896 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing modelname section, reading end line token 1 of 3");
2897 if (strcmp(tok, "%") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing modelname section, end line token 1 of 3 should be a percent sign but it's %s", tok);
2898 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing modelname section, reading end line token 2 of 3");
2899 if (strcmp(tok, "end") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing modelname section, end line token 2 of 3 should be 'end' but it's %s", tok);
2900 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing modelname section, reading end line token 3 of 3");
2901 if (strcmp(tok, "modelname") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing modelname section, end line token 3 of 3 should be 'modelname' but it's %s", tok);
2902
2903 if(curstr != NULL) free(curstr);
2904 return eslOK;
2905
2906 ERROR: ESL_FAIL(status, errbuf, "Error, parsing modelname section, memory error?");
2907 }
2908
2909 /* parse_legend_section
2910 *
2911 * Parse the legend (legend coordinates) section of a template postscript file.
2912 * If anything is invalid, we return a non-eslOK status code
2913 * to caller.
2914 *
2915 * Returns: eslOK on success.
2916 */
2917 int
2918 parse_legend_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
2919 {
2920 int status;
2921 char *tok;
2922 int toklen;
2923
2924 /* this section should be exactly 3 lines, one of which we've already read,
2925 * five tokens of the middle line are <rfpos> <x_offset> <y_offset> <leg_cellsize> <leg_right_buffer>
2926 * this tells us to put the top-left corner of the legend at
2927 * ps->legx[rfpos] + x_offset, ps->legy[rfpos] + y_offset,
2928 * and have the RHS of the buffer end <space_right_of_leg> points before the RHS of the page
2929 * cellsize is the size of the cells in the legend
2930 * here's an example, first token we'll read should be '%', followed by '1508'
2931 * % begin legend
2932 * % 1508 24. -24. 12 0.
2933 * % end legend
2934 */
2935 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 1 of 6");
2936 if (strcmp(tok, "%") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing legend section, middle line token 1 should be a percent sign but it's %s", tok);
2937 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 2 of 6");
2938 ps->leg_posn = atoi(tok);
2939 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 3 of 6");
2940 ps->legx_offset = atof(tok);
2941 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 4 of 6");
2942 ps->legy_offset = atof(tok);
2943 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 5 of 6");
2944 ps->leg_cellsize = atoi(tok);
2945 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 6 of 6");
2946 ps->leg_rhs_space = atof(tok);
2947
2948 /* read '% end legend' line */
2949 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading token 3 of 3");
2950 if (strcmp(tok, "%") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing legend section, end line token 1 of 3 should be a percent sign but it's %s", tok);
2951 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading end line token 2 of 3");
2952 if (strcmp(tok, "end") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing legend section, end line token 2 of 3 should be 'end' but it's %s", tok);
2953 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing legend section, reading end line token 3 of 3");
2954 if (strcmp(tok, "legend") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing legend section, end line token 3 of 3 should be 'legend' but it's %s", tok);
2955
2956 return eslOK;
2957 }
2958
2959 /* parse_scale_section
2960 *
2961 * Parse the scale section of a template postscript file.
2962 * If anything is invalid, we return a non-eslOK status code
2963 * to caller.
2964 *
2965 * Returns: eslOK on success.
2966 */
2967 int
2968 parse_scale_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
2969 {
2970 int status;
2971 char *tok;
2972 int toklen;
2973
2974 /* this section should be exactly 3 lines, one of which we've already read,
2975 * here's an example, next token should be the first 0.65
2976 * % begin scale
2977 * 0.65 0.65 scale
2978 * % end scale
2979 */
2980 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading token 1 of 3");
2981 ps->scale = atof(tok);
2982 if(ps->scale < 0.) ESL_FAIL(status, errbuf, "Error, parsing scale section, scale must be positive real number, read %s\n", tok);
2983 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading token 2 of 3");
2984 if(esl_FCompare_old(ps->scale, atof(tok), eslSMALLX1) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing scale section, x and y scales are not equal %.2f != %.2f", ps->scale, atof(tok));
2985 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading token 3 of 3");
2986 if (strcmp(tok, "scale") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing scale section, token 3 of 3 should be 'scale' but it's %s", tok);
2987
2988 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading end line token 1 of 3");
2989 if (strcmp(tok, "%") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing scale section, end line token 1 of 3 should be a percent sign but it's %s", tok);
2990 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading end line token 2 of 3");
2991 if (strcmp(tok, "end") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing scale section, end line token 2 of 3 should be 'end' but it's %s", tok);
2992 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing scale section, reading end line token 3 of 3");
2993 if (strcmp(tok, "scale") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing scale section, end line token 3 of 3 should be 'scale' but it's %s", tok);
2994
2995 return eslOK;
2996 }
2997
2998
2999 /* parse_ignore_section
3000 *
3001 * Parse an ignore section of a template postscript file.
3002 * We ignore this data. This function's purpose is to read
3003 * tokens until we see the "% end ignore" line signalling
3004 * the end of the ignore section.
3005 *
3006 * As a special case, if any line iss a single token, 'showpage', we
3007 * set *ret_read_showpage as TRUE upon return. This signals to caller
3008 * that the current page is finished.
3009 *
3010 * Returns: eslOK on success.
3011 */
3012 int
3013 parse_ignore_section(ESL_FILEPARSER *efp, char *errbuf, int *ret_read_showpage)
3014 {
3015 int status;
3016 char *tok;
3017 int keep_reading = TRUE;
3018 int read_showpage = FALSE;
3019
3020 while((keep_reading) && (status = esl_fileparser_NextLine(efp) == eslOK)) {
3021 /* we're going to keep reading until we've read the line that is '% end ignore', 3 tokens, '%', then 'end', then 'ignore' */
3022 if(((status = esl_fileparser_GetToken(efp, &tok, NULL)) == eslOK) && (strcmp(tok, "%") == 0)) { /* first token is '%' */
3023 if(((status = esl_fileparser_GetToken(efp, &tok, NULL)) == eslOK) && (strcmp(tok, "end") == 0)) { /* second token is 'end' */
3024 if(((status = esl_fileparser_GetToken(efp, &tok, NULL)) == eslOK) && (strcmp(tok, "ignore") == 0)) { /* final token is 'end' */
3025 keep_reading = FALSE;
3026 status = eslOK;
3027 }
3028 }
3029 }
3030 else if(strcmp(tok, "showpage") == 0) { /* first token is 'showpage' */
3031 read_showpage = TRUE;
3032 }
3033 }
3034 if(status == eslEOF) ESL_FAIL(status, errbuf, "Error, parsing ignore section, finished file looking for '%%%% end ignore' line");
3035 if(status != eslOK) ESL_FAIL(status, errbuf, "Error, parsing ignore section, last line number read %d", efp->linenumber);
3036
3037 *ret_read_showpage = read_showpage;
3038 return eslOK;
3039 }
3040
3041
3042 /* parse_regurgitate_section
3043 *
3044 * Parse a regurgitate section of a template postscript file.
3045 * If anything is invalid, we return a non-eslOK status code
3046 * to caller.
3047 *
3048 * Returns: eslOK on success.
3049 */
3050 int
3051 parse_regurgitate_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
3052 {
3053 int status;
3054 char *tok;
3055 int toklen;
3056 int seen_end = FALSE;
3057 char *curstr = NULL;
3058 char *newstr;
3059 int nalloc = ps->nregurg;
3060 int curlen;
3061 void *tmp;
3062 int ntok = 0;
3063
3064 while (((status = esl_fileparser_NextLine(efp)) == eslOK) && (!seen_end))
3065 {
3066 curlen = ntok = 0;
3067 while ((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) == eslOK) {
3068 if (strcmp(tok, "%") == 0) { /* should be the end, make sure it's properly formatted */
3069 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing regurgitate section, read %%%% prefixed line without ' end regurgitate' after it");
3070 if (strcmp(tok, "end") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing regurgitate section, read %%%% prefixed line without ' end regurgitate' after it");
3071 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing regurgitate section, read %%%% prefixed line without ' end regurgitate' after it");
3072 if (strcmp(tok, "regurgitate") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing regurgitate section, read %%%% prefixed line without ' end regurgitate' after it");
3073 seen_end = TRUE;
3074 break;
3075 }
3076 else {
3077 if(ntok > 0) {
3078 if((status = esl_strcat(&curstr, curlen, " ", 1)) != eslOK) ESL_FAIL(status, errbuf, "parse_regurgitate_section(), error (2).");
3079 curlen += 1;
3080 }
3081 if((status = esl_strcat(&curstr, curlen, tok, toklen)) != eslOK) ESL_FAIL(status, errbuf, "parse_regurgitate_section(), error (1).");
3082 curlen += toklen;
3083 ntok++;
3084 }
3085 }
3086 if(seen_end) break;
3087 if((status = esl_strcat(&curstr, curlen, "\n", 1)) != eslOK) ESL_FAIL(status, errbuf, "parse_regurgitate_section(), error (3).");
3088 curlen += 1;
3089 ESL_ALLOC(newstr, sizeof(char) * (curlen+1));
3090 strcpy(newstr, curstr);
3091 if(ps->nregurg == nalloc) {
3092 nalloc += ps->nalloc; ESL_RALLOC(ps->regurgA, tmp, sizeof(char *) * nalloc);
3093 }
3094 ps->regurgA[ps->nregurg++] = newstr;
3095 free(curstr);
3096 curstr = NULL;
3097 }
3098 if(status == eslEOF) ESL_FAIL(status, errbuf, "Error, parsing regurgitate section, finished file looking for '%%%% end regurgitate' line");
3099 if(status != eslOK) ESL_FAIL(status, errbuf, "Error, parsing regurgitate section, last line number read %d", efp->linenumber);
3100
3101 return eslOK;
3102
3103 ERROR: ESL_FAIL(status, errbuf, "Memory error parsing regurgitate section");
3104 }
3105
3106
3107 /* parse_text_section
3108 *
3109 * Parse a text section of a template postscript file.
3110 * If anything is invalid, we return a non-eslOK status code
3111 * to caller.
3112 *
3113 * Returns: eslOK on success.
3114 */
3115 int
3116 parse_text_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
3117 {
3118 int status;
3119 char *tok;
3120 int toklen;
3121 int seen_end = FALSE;
3122 int nalloc;
3123 void *tmp;
3124 int do_posntext = FALSE;
3125 int do_nucleotides = FALSE;
3126 int i;
3127
3128 /* find out which section we're in, 'posntext' or 'nucleotides' */
3129 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, last line %d\n", efp->linenumber);
3130 if (strcmp(tok, "positiontext") == 0) { do_posntext = TRUE; nalloc = ps->nposntext; }
3131 else if (strcmp(tok, "nucleotides") == 0) { do_nucleotides = TRUE; nalloc = ps->rflen; }
3132
3133 /* Parse each line. example line:
3134 * (G) 168.00 392.00 moveto show
3135 */
3136 while (((status = esl_fileparser_NextLine(efp)) == eslOK) && (!seen_end))
3137 {
3138 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, each non-comment line should be 5-tokens ending with 'show'");
3139 if (tok[0] == '%') { /* comment line, could be the end, check if it's '% end text {positiontext,nucleotides}', if not, ignore it */
3140 if(strcmp(tok, "%") == 0) { /* first token is '%', keep checking */
3141 if(((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) && (strcmp(tok, "end") == 0)) { /* second token is 'end', keep checking */
3142 if(((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) && (strcmp(tok, "text") == 0)) { /* third token is 'end', keep checking */
3143 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) { /* has a fourth token, keep checking */
3144 if(do_posntext && strcmp(tok, "positiontext") == 0) seen_end = TRUE;
3145 if(do_nucleotides && strcmp(tok, "nucleotides") == 0) seen_end = TRUE;
3146 }
3147 }
3148 }
3149 }
3150 }
3151 else {
3152 /* we're reading a non-comment line, tok is the string, if do_posntext, we store it, else we discard it */
3153 if(do_posntext) {
3154 if(ps->nposntext == nalloc) {
3155 ESL_RALLOC(ps->posntextA, tmp, sizeof(char *) * (nalloc + ps->nalloc));
3156 ESL_RALLOC(ps->posntextxA, tmp, sizeof(float) * (nalloc + ps->nalloc));
3157 ESL_RALLOC(ps->posntextyA, tmp, sizeof(float) * (nalloc + ps->nalloc));
3158 for(i = nalloc; i < nalloc + ps->nalloc; i++) ps->posntextA[i] = NULL;
3159 nalloc += ps->nalloc;
3160 }
3161 if((status = esl_strdup(tok, -1, &(ps->posntextA[ps->nposntext]))) != eslOK) goto ERROR;
3162 }
3163 if(do_nucleotides && ps->rflen == nalloc) {
3164 ESL_RALLOC(ps->rxA, tmp, sizeof(float) * (nalloc + ps->nalloc));
3165 ESL_RALLOC(ps->ryA, tmp, sizeof(float) * (nalloc + ps->nalloc));
3166 nalloc += ps->nalloc;
3167 }
3168
3169 /* get x */
3170 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, each non-comment line should be 5 tokens ending with 'show'");
3171 if(do_posntext) ps->posntextxA[ps->nposntext] = atof(tok);
3172 if(do_nucleotides) ps->rxA[ps->rflen] = atof(tok);
3173 /* get y */
3174 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, each non-comment line should be 5 tokens ending with 'show'");
3175 if(do_posntext) ps->posntextyA[ps->nposntext] = atof(tok);
3176 if(do_nucleotides) ps->ryA[ps->rflen] = atof(tok);
3177
3178 /* verify moveto */
3179 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, each non-comment line should be 5 tokens ending with 'show'");
3180 if (strcmp(tok, "moveto") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing text main section, fourth token should be 'moveto', line %d", efp->linenumber);
3181 /* verify show */
3182 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, each non-comment line should be 5 tokens ending with 'show'");
3183 if (strcmp(tok, "show") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing text main section, fifth token should be 'show', line %d", efp->linenumber);
3184
3185 if(do_posntext) ps->nposntext++;
3186 if(do_nucleotides) ps->rflen++;
3187 }
3188 }
3189 if(!seen_end) {
3190 if(do_posntext) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing text positiontext section, didn't see '%%%% end text positiontext' line: %d\n", efp->linenumber);
3191 if(do_nucleotides) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing text positiontext section, didn't see '%%%% end text nucleotides' line: %d\n", efp->linenumber);
3192 }
3193 if(status == eslEOF && do_posntext)
3194 ESL_FAIL(status, errbuf, "Error, parsing text section, finished file looking for '%%%% end text positiontext' line");
3195 if(status == eslEOF && do_nucleotides)
3196 ESL_FAIL(status, errbuf, "Error, parsing text section, finished file looking for '%%%% end text nucleotides' line");
3197 if(status != eslOK) ESL_FAIL(status, errbuf, "Error, parsing text section, last line number read %d", efp->linenumber);
3198
3199 return eslOK;
3200
3201 ERROR: ESL_FAIL(status, errbuf, "Memory error parsing text section");
3202 }
3203
3204 /* parse_lines_section
3205 *
3206 * Parse a lines section of a template postscript file.
3207 * If anything is invalid, we return a non-eslOK status code
3208 * to caller.
3209 *
3210 * Returns: eslOK on success.
3211 */
3212 int
3213 parse_lines_section(ESL_FILEPARSER *efp, char *errbuf, SSPostscript_t *ps)
3214 {
3215 int status;
3216 char *tok;
3217 int toklen;
3218 int seen_end = FALSE;
3219 int nalloc;
3220 void *tmp;
3221 int do_ticks = FALSE;
3222 int do_bpconnects = FALSE;
3223
3224 /* find out which section we're in, 'positionticks' or 'bpconnects' */
3225 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines section, last line %d\n", efp->linenumber);
3226 if (strcmp(tok, "positionticks") == 0) { do_ticks = TRUE; nalloc = ps->nticks; }
3227 else if (strcmp(tok, "bpconnects") == 0) { do_bpconnects = TRUE; nalloc = ps->nbp; }
3228 else ESL_FAIL(status, errbuf, "Error, parsing lines section unrecognized type: %s ('bpconnects' or 'positionticks' expected)\n", tok);
3229
3230 /* Parse each line. example line:
3231 * 151.82 331.76 148.86 338.65 newpath moveto lineto stroke
3232 */
3233 while (((status = esl_fileparser_NextLine(efp)) == eslOK) && (!seen_end))
3234 {
3235 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 5-tokens ending with 'show'");
3236 if (strcmp(tok, "%") == 0) { /* should be the end, make sure it's properly formatted */
3237 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines' after it");
3238 if (strcmp(tok, "end") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines' after it");
3239 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines' after it");
3240 if (strcmp(tok, "lines") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines' after it");
3241 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines' after it");
3242 if(do_ticks) {
3243 if (strcmp(tok, "positionticks") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines positionticks' after it");
3244 }
3245 if(do_bpconnects) {
3246 if (strcmp(tok, "bpconnects") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines section, read %%%% prefixed line without ' end lines bpconnects' after it");
3247 }
3248 seen_end = TRUE;
3249 break;
3250 }
3251 /* if we get here, we haven't seen the end, we're reading a normal line, tok is the first x coord, we record this */
3252 /* first we expand our arrays if nec */
3253 if(do_ticks && ps->nticks == nalloc) {
3254 nalloc += ps->nalloc;
3255 ESL_RALLOC(ps->ticksx1A, tmp, sizeof(float) * nalloc);
3256 ESL_RALLOC(ps->ticksy1A, tmp, sizeof(float) * nalloc);
3257 ESL_RALLOC(ps->ticksx2A, tmp, sizeof(float) * nalloc);
3258 ESL_RALLOC(ps->ticksy2A, tmp, sizeof(float) * nalloc);
3259 }
3260 if(do_bpconnects && ps->nbp == nalloc) {
3261 nalloc += ps->nalloc;
3262 ESL_RALLOC(ps->bpx1A, tmp, sizeof(float) * nalloc);
3263 ESL_RALLOC(ps->bpy1A, tmp, sizeof(float) * nalloc);
3264 ESL_RALLOC(ps->bpx2A, tmp, sizeof(float) * nalloc);
3265 ESL_RALLOC(ps->bpy2A, tmp, sizeof(float) * nalloc);
3266 }
3267 /* store x1 */
3268 if(do_ticks) ps->ticksx1A[ps->nticks] = atof(tok);
3269 if(do_bpconnects) ps->bpx1A[ps->nbp] = atof(tok);
3270 /* get y1 */
3271 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3272 if(do_ticks) ps->ticksy1A[ps->nticks] = atof(tok);
3273 if(do_bpconnects) ps->bpy1A[ps->nbp] = atof(tok);
3274 /* get x2 */
3275 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3276 if(do_ticks) ps->ticksx2A[ps->nticks] = atof(tok);
3277 if(do_bpconnects) ps->bpx2A[ps->nbp] = atof(tok);
3278 /* get y2 */
3279 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3280 if(do_ticks) ps->ticksy2A[ps->nticks] = atof(tok);
3281 if(do_bpconnects) ps->bpy2A[ps->nbp] = atof(tok);
3282
3283 /* verify newpath */
3284 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3285 if (strcmp(tok, "newpath") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines main section, fifth token should be 'newpath', line %d", efp->linenumber);
3286 /* verify moveto */
3287 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3288 if (strcmp(tok, "moveto") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines main section, sixth token should be 'moveto', line %d", efp->linenumber);
3289 /* verify lineto */
3290 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3291 if (strcmp(tok, "lineto") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines main section, seventh token should be 'lineto', line %d", efp->linenumber);
3292 /* verify stroke */
3293 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen)) != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines main section should include 8-tokens ending with 'stroke'");
3294 if (strcmp(tok, "stroke") != 0) ESL_FAIL(eslEINVAL, errbuf, "Error, parsing lines main section, eigth token should be 'stroke', line %d", efp->linenumber);
3295
3296 if(do_ticks) ps->nticks++;
3297 if(do_bpconnects) ps->nbp++;
3298 }
3299 if(!seen_end) ESL_FAIL(status, errbuf, "Error, parsing lines section, didn't see end! line: %d\n", efp->linenumber);
3300 if(status == eslEOF && do_ticks)
3301 ESL_FAIL(status, errbuf, "Error, parsing lines section, finished file looking for '%%%% end lines positionticks' line");
3302 if(status == eslEOF && do_bpconnects)
3303 ESL_FAIL(status, errbuf, "Error, parsing lines section, finished file looking for '%%%% end lines bpconnects' line");
3304
3305 if(status != eslOK) ESL_FAIL(status, errbuf, "Error, parsing lines section, last line number read %d", efp->linenumber);
3306
3307 return eslOK;
3308 ERROR: ESL_FAIL(status, errbuf, "Memory error parsing lines section");
3309 }
3310
3311 /* Function: individuals_sspostscript()
3312 *
3313 * Purpose: Fill a postscript data structure with info for individual seqs and
3314 * possibly their posteriors in the MSA.
3315 * Return: eslOK on success.
3316 *
3317 * per_seq_ins_ct - [0..i..msa->nseq-1][0..rfpos..rflen] number of inserts
3318 * insert after each nongap RF position per sequence.
3319 * If rfpos == 0: # inserts BEFORE first position.
3320 * If rfpos == n: # inserts after position rfpos n
3321 * This is off-by-one with respect to msa->rf for example,
3322 * msa->rf[n] is the 'n-1'th nucleotide of the RF annotation.
3323 */
3324 static int
3325 individuals_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, ESL_MSA *msa, int **per_seq_ins_ct,
3326 int do_prob, int do_rescol, float ***hc_scheme, int hc_scheme_idx_s, int hc_scheme_idx_p,
3327 int hc_nbins_s, int hc_nbins_p, float **hc_onecell, int extdel_idx_s, int wcbp_idx_s, int gubp_idx_s,
3328 int ncbp_idx_s, int dgbp_idx_s, int hgbp_idx_s, int gap_idx_p, FILE *tabfp)
3329 {
3330 int status;
3331 int p, i, pp;
3332 int rfpos, apos, lpos, rpos, lrfpos, rrfpos;
3333 int orig_npage = ps->npage;
3334 int new_npage;
3335
3336 /* variables for the sequence pages */
3337 int nextdel_s = 0; /* number of external deletes (3' and 5' flush deletes) */
3338 int nwc_s = 0; /* number of Watson/Crick (A:U,U:A,C:G,G:C) basepairs */
3339 int ngu_s = 0; /* number of G:U,U:G basepairs (2 * num G-U,U-G bps) */
3340 int nnc_s = 0; /* number of non-canonical basepairs (A:A,A:C,A:G,C:A,C:C,C:U,G:A,G:G,U:C,U:U) */
3341 int ndgi_s = 0; /* number of internal double-gap basepairs */
3342 int nhgi_s = 0; /* number of internal half-gap basepairs */
3343 int ndge_s = 0; /* number of external double-gap basepairs */
3344 int nhge_s = 0; /* number of external half-gap basepairs */
3345 float *limits_s; /* [nbins+1] limits for each bin, limits[0] is min value we would expect to see, limits[nbins] is max */
3346 int nins_s; /* number of inserts after the current rfpos for current sequence */
3347 int spos, epos; /* first/final nongap position for cur sequence */
3348 int apos_is_internal; /* is apos within spos..epos?, if not it's an external deletion */
3349 int lpos_is_internal; /* is lpos within spos..epos?, if not it's an external deletion */
3350 int rpos_is_internal; /* is rpos within spos..epos?, if not it's an external deletion */
3351 int namewidth; /* length in chars of longest seq name we'll print */
3352 char *namedashes = NULL;
3353 int ni;
3354 double cur_cfreq;
3355 int noutline_min = 0; /* number of positions with minimal outline */
3356 int noutline_max = 0; /* number of positions with maximal outline */
3357 int noutline_bp_good = 0; /* number of WC/GU basepairs that have left and/or right half outlined, consensus is WC/GU */
3358 int noutline_bp_bad = 0; /* number of NC basepairs that have left and/or right half outlined, consensus is WC/GU */
3359 int maj_bp_is_wc_or_gu; /* TRUE if current consensus bp is a watson-crick of GU in majority consensus sequence */
3360 float *stack; /* postscript stack for outline procedure */
3361
3362 /* variables for the postprob pages */
3363 int ngap_p = 0;
3364 int ngap_masked_p = 0;
3365 float *limits_p;
3366 int within_mask;
3367 float ppavgA[11];
3368 int ppidx;
3369 int do_prob_res = FALSE; /* set to TRUE if --no-ntpp is NOT enabled */
3370 int do_outline = FALSE; /* set to TRUE if --no-nt is NOT enabled */
3371 int noccl; /* number of one-cell color legends depends on value of do_prob_res and do_ouline */
3372
3373 /* contract check */
3374 if(do_prob) {
3375 if(msa->pp == NULL) ESL_FAIL(eslEINVAL, errbuf, "internal error, individuals_sspostscript() do_prob == TRUE, msa->pp == FALSE");
3376 for(i = 0; i < msa->nseq; i++) {
3377 if(msa->pp[i] == NULL)
3378 ESL_FAIL(eslEINVAL, errbuf, "with --indi, either all or none of the selected sequences must have PP annotation, seq %d does not", i);
3379 }
3380 do_prob_res = (! esl_opt_GetBoolean(go, "--no-ntpp"));
3381 }
3382 do_outline = (! esl_opt_GetBoolean(go, "--no-ol"));
3383
3384 /* PP values, hard coded */
3385 ppavgA[0] = 0.025;
3386 ppavgA[1] = 0.10;
3387 ppavgA[2] = 0.20;
3388 ppavgA[3] = 0.30;
3389 ppavgA[4] = 0.40;
3390 ppavgA[5] = 0.50;
3391 ppavgA[6] = 0.60;
3392 ppavgA[7] = 0.70;
3393 ppavgA[8] = 0.80;
3394 ppavgA[9] = 0.90;
3395 ppavgA[10] = 0.975;
3396
3397 /* determine number of pages we'll add */
3398 new_npage = do_prob ? msa->nseq*2 : msa->nseq;
3399 if((status = add_pages_sspostscript(ps, new_npage, INDIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
3400
3401 /* add the pages carefully, we allocate posterior pages and seq pages differently */
3402 for(p = orig_npage; p < ps->npage; p++) {
3403 /* allocate seq page */
3404 ESL_ALLOC(ps->rAA[p], sizeof(char) * (ps->rflen+1));
3405 if(do_rescol) ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
3406 if(do_outline) {
3407 ESL_ALLOC(ps->otypeAA[p], sizeof(char) * ps->rflen);
3408 for(rfpos = 0; rfpos < ps->rflen; rfpos++) ps->otypeAA[p][rfpos] = OUTLINE_NONE_IDX;
3409 }
3410 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
3411 ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1);
3412 noccl = 2;
3413 if (do_rescol) noccl += 5;
3414 if (do_outline) noccl += 7;
3415 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * noccl);
3416 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3417 if(do_rescol) ESL_ALLOC(ps->rcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
3418 ESL_ALLOC(ps->bcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
3419 }
3420
3421 if(do_prob) {
3422 /* allocate postprob page */
3423 p++;
3424 ESL_ALLOC(ps->rAA[p], sizeof(char) * (ps->rflen+1));
3425 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
3426 ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1);
3427 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 1);
3428 if(do_prob_res) ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
3429 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3430 ESL_ALLOC(ps->bcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
3431 if(do_prob_res) ESL_ALLOC(ps->rcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
3432 }
3433 }
3434 }
3435
3436 /* setup seq limits */
3437 ESL_ALLOC(limits_s, sizeof(float) * (hc_nbins_s+1));
3438 limits_s[0] = 0;
3439 limits_s[1] = 1;
3440 limits_s[2] = 2;
3441 limits_s[3] = 5;
3442 limits_s[4] = 10;
3443 limits_s[5] = eslINFINITY;
3444
3445 /* setup pp limits */
3446 ESL_ALLOC(limits_p, sizeof(float) * (hc_nbins_p+1));
3447 limits_p[0] = 0.0;
3448 limits_p[1] = 0.35;
3449 limits_p[2] = 0.55;
3450 limits_p[3] = 0.75;
3451 limits_p[4] = 0.85;
3452 limits_p[5] = 0.95;
3453 limits_p[6] = 1.00;
3454
3455 /* allocate the uaseqlenA data structure to store unaligned seq lengths */
3456 if(ps->uaseqlenA != NULL) { free(ps->uaseqlenA); ps->uaseqlenA = NULL; }
3457 ESL_ALLOC(ps->uaseqlenA, sizeof(int) * msa->nseq);
3458 esl_vec_ISet(ps->uaseqlenA, msa->nseq, 0);
3459
3460 if(tabfp != NULL) {
3461 /* determine max length of a name */
3462 namewidth = 8; /* length of 'seq name' */
3463 for(i = 0; i < msa->nseq; i++) {
3464 namewidth = ESL_MAX(namewidth, strlen(msa->sqname[i]));
3465 }
3466 ESL_ALLOC(namedashes, sizeof(char) * namewidth+1);
3467 namedashes[namewidth] = '\0';
3468 for(ni = 0; ni < namewidth; ni++) namedashes[ni] = '-';
3469 fprintf(tabfp, "# -----------------------\n");
3470 fprintf(tabfp, "# Per-sequence statistics\n");
3471 fprintf(tabfp, "# -----------------------\n");
3472 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each sequence\n", ps->rflen);
3473 fprintf(tabfp, "# printed to the output postscript file.\n");
3474 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", do_outline ? 15 : 11);
3475 fprintf(tabfp, "# \ttoken 1: 'perseq' (tag defining line type to ease parsing)\n");
3476 fprintf(tabfp, "# \ttoken 2: sequence name\n");
3477 fprintf(tabfp, "# \ttoken 3: spos: aligned position of first (5'-most) nongap nucleotide in sequence\n");
3478 fprintf(tabfp, "# \ttoken 4: epos: aligned position of final (3'-most) nongap nucleotide in sequence\n");
3479 fprintf(tabfp, "# \ttoken 5: number of Watson-Crick basepairs (A-U, C-G, G-C, or U-A) in the sequence\n");
3480 fprintf(tabfp, "# \ttoken 6: number of G-U or U-G basepairs in the sequence\n");
3481 fprintf(tabfp, "# \ttoken 7: number of non-canonical basepairs (A-A, A-C, A-G, C-A, C-C, C-U, G-A, G-G, U-C, U-U) in the sequence\n");
3482 fprintf(tabfp, "# \ttoken 8: number of internal half-gap basepairs in the sequence\n");
3483 fprintf(tabfp, "# \ttoken 9: number of internal double-gap basepairs in the sequence\n");
3484 fprintf(tabfp, "# \ttoken 10: number of external half-gap basepairs in the sequence\n");
3485 fprintf(tabfp, "# \ttoken 11: number of external double-gap basepairs in the sequence\n");
3486 if(do_outline) {
3487 fprintf(tabfp, "# \ttoken 12: number of nt changes relative to majority rule consensus sequence\n");
3488 fprintf(tabfp, "# \ttoken 13: subset of token 12 for which consensus nt occurs in > 0.75 of nongap sequences\n");
3489 fprintf(tabfp, "# \ttoken 14: number of internal basepairs in seq that differ from majority rule consensus basepair\n");
3490 fprintf(tabfp, "# \t *and* consensus bp is Watson-Crick, G-U or U-G\n");
3491 fprintf(tabfp, "# \ttoken 15: subset of token 14 which are Watson-Crick, G-U or U-G in seq\n");
3492 }
3493 fprintf(tabfp, "#\n");
3494 fprintf(tabfp, "# Alignment length: %" PRId64 " (this is max possible value for tokens 3 and 4)\n", msa->alen);
3495 fprintf(tabfp, "# Number of consensus basepairs: %d (this is max possible value for tokens 5-11)\n", ps->nbp);
3496 fprintf(tabfp, "# A basepair between positions i and j for sequence s is a half-gap basepair if s has\n");
3497 fprintf(tabfp, "# a gap at either position i or j, but not both. It is a double-gap basepair if it has\n");
3498 fprintf(tabfp, "# a gap at both positions i and j.\n");
3499 fprintf(tabfp, "# A basepair between i:j is 'internal' for sequence s if if spos <= i and epos >= j.\n");
3500 fprintf(tabfp, "# Otherwise, basepair i:j is 'external' for sequence s.\n");
3501 fprintf(tabfp, "# All basepairs that are nongaps in both i and j are necessarily internal.\n");
3502 fprintf(tabfp, "#\n");
3503 fprintf(tabfp, "# %6s %-*s %4s %4s %4s %4s %4s %4s %4s %4s %4s", "type", namewidth, "seq name", "spos", "epos", "nwc", "ngu", "nnc", "nhgi", "ndgi", "nhge", "ndge");
3504 if(do_outline) { fprintf(tabfp, " %4s %4s %5s %5s", "ndf", "nrdf", "nbpdf", "nbpcp"); }
3505 fprintf(tabfp, "\n");
3506 fprintf(tabfp, "# %6s %-*s %4s %4s %4s %4s %4s %4s %4s %4s %4s", "------", namewidth, namedashes, "----", "----", "----", "----", "----", "----", "----", "----", "----");
3507 if(do_outline) { fprintf(tabfp, " %4s %4s %5s %5s", "----", "----", "-----", "-----"); }
3508 fprintf(tabfp, "\n");
3509 }
3510
3511 /* Step through each seq, first fill seq page, then possibly postprob page */
3512 pp = orig_npage-1;
3513 for(i = 0; i < msa->nseq; i++) {
3514 /**************************
3515 * Draw the sequence page *
3516 **************************/
3517 pp++;
3518 spos = epos = -1;
3519 /* determine first and final non-gap position */
3520 for(apos = 0; apos < msa->alen; apos++) { /* find first non-gap RF position */
3521 if((! esl_abc_CIsGap(abc, msa->aseq[i][apos]))) {
3522 spos = apos;
3523 break;
3524 }
3525 }
3526 for(apos = msa->alen-1; apos >= 0; apos--) {
3527 if((! esl_abc_CIsGap(abc, msa->aseq[i][apos]))) {
3528 epos = apos;
3529 break;
3530 }
3531 }
3532 if(ps->sclAA[pp] != NULL){
3533 free(ps->sclAA[pp]); // prevent memory leak when create_scheme_colorlegend overwrites this
3534 }
3535 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx_s, hc_nbins_s, limits_s, TRUE, TRUE, TRUE, FALSE);
3536 /* init one cell legend counters */
3537 nextdel_s = nwc_s = ngu_s = nnc_s = ndgi_s = nhgi_s = ndge_s = nhge_s = noutline_min = noutline_max = noutline_bp_good = noutline_bp_bad = 0;
3538 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3539 apos = ps->msa_rf2a_map[rfpos];
3540 nins_s = per_seq_ins_ct[i][rfpos+1];
3541 apos_is_internal = TRUE; /* set to FALSE below if apos < spos || apos > epos */
3542
3543 if(! esl_abc_CIsGap(abc, msa->aseq[i][apos])) ps->uaseqlenA[i]++;
3544 ps->uaseqlenA[i] += nins_s;
3545 ps->rAA[pp][rfpos] = msa->aseq[i][apos];
3546 if(do_outline) {
3547 if((! esl_abc_CIsGap(abc, msa->aseq[i][apos])) && /* aseq is not a gap at this posn */
3548 (esl_abc_CIsCanonical(abc, ps->msa_cseq_maj[rfpos])) && /* cseq is canonical at this posn */
3549 (toupper(msa->aseq[i][apos]) != toupper(ps->msa_cseq_maj[rfpos]))) { /* aseq != cseq at this posn */
3550 cur_cfreq = abc_ct[apos][esl_abc_DigitizeSymbol(abc, toupper(ps->msa_cseq_maj[rfpos]))] /
3551 esl_vec_DSum(abc_ct[apos], abc->K);
3552 if(cur_cfreq > 0.75) { ps->otypeAA[pp][rfpos] = OUTLINE_MAX_IDX; noutline_max++; }
3553 else { ps->otypeAA[pp][rfpos] = OUTLINE_MIN_IDX; noutline_min++; }
3554 }
3555 }
3556 /* printf("ps->rAA[%3d][%4d]: %c\n", pp, rfpos, ps->rAA[pp][rfpos]); */
3557 if((spos != -1 && epos != -1) && /* this should always be true, unless seq has length 0! */
3558 (apos < spos || apos > epos)) {
3559 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[extdel_idx_s])) != eslOK) return status;
3560 nextdel_s++;
3561 apos_is_internal = FALSE;
3562 }
3563 else { /* color insert value */
3564 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx_s], nins_s, ps->sclAA[pp], TRUE, NULL)) != eslOK) return status;
3565 }
3566 if(do_rescol || tabfp != NULL) {
3567 if(ps->msa_ct[(rfpos+1)] == 0) {
3568 if(do_rescol) {
3569 /* single-stranded nucleotide or gap, draw same color as Watson-Cricks */
3570 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
3571 }
3572 }
3573 else { /* consensus basepaired nucleotide */
3574 lpos = apos;
3575 rpos = ps->msa_rf2a_map[ps->msa_ct[rfpos+1]-1];
3576 lrfpos = rfpos;
3577 rrfpos = ps->msa_ct[rfpos+1]-1;
3578 maj_bp_is_wc_or_gu = ((is_watson_crick_bp(ps->msa_cseq_maj[lrfpos], ps->msa_cseq_maj[rrfpos]))
3579 || (is_gu_or_ug_bp(ps->msa_cseq_maj[lrfpos], ps->msa_cseq_maj[rrfpos]))) ? TRUE : FALSE;
3580 lpos_is_internal = apos_is_internal;
3581 rpos_is_internal = TRUE; /* we set to FALSE below if nec */
3582 if((spos != -1 && epos != -1) && /* this should always be true, unless seq has length 0! */
3583 (rpos < spos || rpos > epos)) {
3584 rpos_is_internal = FALSE;
3585 }
3586 if(lpos_is_internal && rpos_is_internal) { /* BP is either Watson-Crick, G:U/U:G, non-canonical, internal double-gap bp or internal half-gap bp */
3587 if(is_watson_crick_bp(msa->aseq[i][lpos], msa->aseq[i][rpos])) { /* watson-crick */
3588 if(do_rescol) {
3589 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
3590 }
3591 if(lpos < rpos) nwc_s++;
3592 if(do_outline && (lpos > rpos) && maj_bp_is_wc_or_gu) {
3593 /* check if this seq has a different WC or GU bp than the majority consensus */
3594 if((ps->otypeAA[pp][lrfpos] != OUTLINE_NONE_IDX) ||
3595 (ps->otypeAA[pp][rrfpos] != OUTLINE_NONE_IDX)) {
3596 noutline_bp_good++;
3597 }
3598 }
3599 }
3600 else if(is_gu_or_ug_bp(msa->aseq[i][lpos], msa->aseq[i][rpos])) { /* G:U or U:G */
3601 if(do_rescol) {
3602 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[gubp_idx_s])) != eslOK) return status;
3603 }
3604 if(lpos < rpos) ngu_s++;
3605 if(do_outline && (lpos > rpos) && maj_bp_is_wc_or_gu) {
3606 if((ps->otypeAA[pp][lrfpos] != OUTLINE_NONE_IDX) ||
3607 (ps->otypeAA[pp][rrfpos] != OUTLINE_NONE_IDX)) {
3608 noutline_bp_good++;
3609 }
3610 }
3611 }
3612 else if ((! esl_abc_CIsResidue(abc, msa->aseq[i][lpos])) && (! esl_abc_CIsResidue(abc, msa->aseq[i][rpos]))) { /* internal double-gap basepair */
3613 if(do_rescol) {
3614 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[dgbp_idx_s])) != eslOK) return status;
3615 }
3616 if(lpos < rpos) ndgi_s++;
3617 if(do_outline && (lpos > rpos) && maj_bp_is_wc_or_gu) {
3618 if((ps->otypeAA[pp][lrfpos] != OUTLINE_NONE_IDX) ||
3619 (ps->otypeAA[pp][rrfpos] != OUTLINE_NONE_IDX)) {
3620 noutline_bp_bad++;
3621 }
3622 }
3623 }
3624 else if ((! esl_abc_CIsResidue(abc, msa->aseq[i][lpos])) || (! esl_abc_CIsResidue(abc, msa->aseq[i][rpos]))) { /* internal half-gap basepair */
3625 if(do_rescol) {
3626 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[hgbp_idx_s])) != eslOK) return status;
3627 }
3628 if(lpos < rpos) nhgi_s++;
3629 if(do_outline && (lpos > rpos) && maj_bp_is_wc_or_gu) {
3630 if((ps->otypeAA[pp][lrfpos] != OUTLINE_NONE_IDX) ||
3631 (ps->otypeAA[pp][rrfpos] != OUTLINE_NONE_IDX)) {
3632 noutline_bp_bad++;
3633 }
3634 }
3635 }
3636 else { /* non-canonical */
3637 if(do_rescol) {
3638 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[ncbp_idx_s])) != eslOK) return status;
3639 }
3640 if(lpos < rpos) nnc_s++;
3641 if(do_outline && (lpos > rpos) && maj_bp_is_wc_or_gu) {
3642 if((ps->otypeAA[pp][lrfpos] != OUTLINE_NONE_IDX) ||
3643 (ps->otypeAA[pp][rrfpos] != OUTLINE_NONE_IDX)) {
3644 noutline_bp_bad++;
3645 }
3646 }
3647 }
3648 } /* end of (if(lpos_is_internal && rpos_is_internal)) */
3649 else { /* we'll draw this nucleotide as black */
3650 if ((! esl_abc_CIsResidue(abc, msa->aseq[i][lpos])) && (! esl_abc_CIsResidue(abc, msa->aseq[i][rpos]))) { /* external double-gap basepair */
3651 ndge_s++;
3652 }
3653 if ((! esl_abc_CIsResidue(abc, msa->aseq[i][lpos])) || (! esl_abc_CIsResidue(abc, msa->aseq[i][rpos]))) { /* external half-gap basepair */
3654 nhge_s++;
3655 }
3656 if(do_rescol) {
3657 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[BLACKOC])) != eslOK) return status;
3658 }
3659 }
3660 }
3661 }
3662 }
3663 ps->rAA[pp][ps->rflen] = '\0';
3664 ps->seqidxA[pp] = i;
3665 ps->nocclA[pp] = 0;
3666
3667 if(tabfp != NULL) {
3668 fprintf(tabfp, " perseq %-*s %4d %4d %4d %4d %4d %4d %4d %4d %4d", namewidth, msa->sqname[i], spos+1, epos+1, nwc_s, ngu_s, nnc_s, nhgi_s, ndgi_s, nhge_s, ndge_s);
3669 if(do_outline) {
3670 fprintf(tabfp, " %4d %4d %5d %5d", noutline_min + noutline_max, noutline_max, noutline_bp_good + noutline_bp_bad, noutline_bp_good);
3671 }
3672 fprintf(tabfp, "\n");
3673 }
3674
3675 /* if nec, add one-cell color legends for different bp types */
3676 if(do_rescol) {
3677 /* add one-cell color legend for watson-crick basepairs */
3678 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[wcbp_idx_s], nwc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3679 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "Watson-Crick basepair (WC bp)", ps->legx_max_chars, errbuf)) != eslOK) return status;
3680 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "C-G", errbuf)) != eslOK) return status;
3681 ps->nocclA[pp]++;
3682
3683 /* add one-cell color legend for G-U, U-G basepairs */
3684 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[gubp_idx_s], ngu_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3685 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U or U-G bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3686 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U", errbuf)) != eslOK) return status;
3687 ps->nocclA[pp]++;
3688
3689 /* add one-cell color legend for non-canonical basepairs */
3690 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[ncbp_idx_s], nnc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3691 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "non-canonical bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3692 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "A-A", errbuf)) != eslOK) return status;
3693 ps->nocclA[pp]++;
3694
3695 /* add one-cell color legend for internal half-gap basepairs */
3696 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[hgbp_idx_s], nhgi_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3697 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "internal half-gap bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3698 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "A--", errbuf)) != eslOK) return status;
3699 ps->nocclA[pp]++;
3700
3701 /* add one-cell color legend for internal double-gap basepairs */
3702 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[dgbp_idx_s], ndgi_s, OCCL_BLANK_COUNT, TRUE, FALSE);
3703 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "internal double-gap bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3704 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "---", errbuf)) != eslOK) return status;
3705 ps->nocclA[pp]++;
3706 }
3707
3708 /* if nec, add one page cell legend for the two outline types */
3709 if(do_outline) {
3710 /* add a psuedo-one-cell color legends, the explanatory text for outlines: */
3711 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], OCCL_BLANK_COUNT, OCCL_BLANK_COUNT, FALSE, FALSE);
3712 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "Positions != to most common nt x:", ps->legx_max_chars, errbuf)) != eslOK) return status;
3713 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "", errbuf)) != eslOK) return status;
3714 ps->nocclA[pp]++;
3715
3716 /* allocate our stack for the procedure */
3717 ESL_ALLOC(stack, sizeof(float) * 2);
3718 stack[0] = ps->leg_cellsize;
3719 stack[1] = ps->leg_cellsize * OUTLINE_LINEWIDTH_CELL_FRACTION_MIN;
3720 /* add one-cell color legend for minimal outline */
3721 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], noutline_min, OCCL_BLANK_COUNT, FALSE, FALSE);
3722 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "!= x and freq(x) < 0.75", ps->legx_max_chars, errbuf)) != eslOK) {
3723 free(stack);
3724 return status;
3725 }
3726 if((status = add_procedure_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], OUTLINE_PROCEDURE, stack, 2, errbuf)) != eslOK) return status;
3727 ps->nocclA[pp]++;
3728
3729 stack[1] = ps->leg_cellsize * OUTLINE_LINEWIDTH_CELL_FRACTION_MAX;
3730 /* add one-cell color legend for maximal outline */
3731 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], noutline_max, OCCL_BLANK_COUNT, FALSE, FALSE);
3732 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "!= x and freq(x) >= 0.75", ps->legx_max_chars, errbuf)) != eslOK){
3733 free(stack);
3734 return status;
3735 }
3736 if((status = add_procedure_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], OUTLINE_PROCEDURE, stack, 2, errbuf)) != eslOK){
3737 free(stack);
3738 return status;
3739 }
3740 ps->nocclA[pp]++;
3741
3742 /* add a psuedo-one-cell color legends, the explanatory text for outline basepairs: */
3743 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], OCCL_BLANK_COUNT, OCCL_BLANK_COUNT, FALSE, TRUE);
3744 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "Number of bps != most common bp a:b", ps->legx_max_chars, errbuf)) != eslOK){
3745 free(stack);
3746 return status;
3747 }
3748 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "", errbuf)) != eslOK){
3749 free(stack);
3750 return status;
3751 }
3752 ps->nocclA[pp]++;
3753
3754 /* add a psuedo-one-cell color legends, the explanatory text for outline basepairs: */
3755 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], OCCL_BLANK_COUNT, OCCL_BLANK_COUNT, FALSE, FALSE);
3756 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "and a:b is Watson-Crick, GU or UG:", ps->legx_max_chars, errbuf)) != eslOK){
3757 free(stack);
3758 return status;
3759 }
3760 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "", errbuf)) != eslOK){
3761 free(stack);
3762 return status;
3763 }
3764 ps->nocclA[pp]++;
3765
3766 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], noutline_bp_good, OCCL_BLANK_COUNT, FALSE, FALSE);
3767 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], " Watson-Crick, GU or UG but != a:b", ps->legx_max_chars, errbuf)) != eslOK){
3768 free(stack);
3769 return status;
3770 }
3771 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "", errbuf)) != eslOK){
3772 free(stack);
3773 return status;
3774 }
3775 ps->nocclA[pp]++;
3776
3777 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[BLACKOC], noutline_bp_bad, OCCL_BLANK_COUNT, TRUE, FALSE);
3778 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], " non-canonical or w/gap and != a:b", ps->legx_max_chars, errbuf)) != eslOK){
3779 free(stack);
3780 return status;
3781 }
3782 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "", errbuf)) != eslOK){
3783 free(stack);
3784 return status;
3785 }
3786 ps->nocclA[pp]++;
3787 free(stack);
3788 }
3789
3790 /* add description to ps */
3791 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "# inserted nucleotides after each consensus position", ps->legx_max_chars, errbuf)) != eslOK) return status;
3792 if((status = add_page_desc_to_sspostscript(ps, pp, msa->sqname[i], errbuf)) != eslOK) return status;
3793 /* done with seq page */
3794
3795 /* add one-cell color legend for external gaps (deletes) */
3796 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[extdel_idx_s], nextdel_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3797 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "5'/3'-flush gaps", ps->legx_max_chars, errbuf)) != eslOK) return status;
3798 ps->nocclA[pp]++;
3799
3800 /***************************************
3801 * Draw the posterior probability page *
3802 ***************************************/
3803 if(do_prob) { /* contract checked that msa->pp[i] is non-NULL */
3804 pp++;
3805 if(ps->sclAA[pp] != NULL){ // Prevent memory leak when this was previously allocated
3806 free(ps->sclAA[pp]);
3807 }
3808 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx_p, hc_nbins_p, limits_p, FALSE, TRUE, TRUE, FALSE);
3809 ngap_p = 0;
3810 ngap_masked_p = (ps->mask == NULL) ? -1 : 0;
3811
3812 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3813 apos = ps->msa_rf2a_map[rfpos];
3814 if(do_prob_res) {
3815 ps->rAA[pp][rfpos] = msa->aseq[i][apos];
3816 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
3817 }
3818 else {
3819 ps->rAA[pp][rfpos] = ' '; /* no need to add color to a ' ' */
3820 }
3821 if(! esl_abc_CIsGap(abc, msa->aseq[i][apos])) {
3822 if((ppidx = get_pp_idx(abc, msa->pp[i][apos])) == -1) ESL_FAIL(eslEFORMAT, errbuf, "bad #=GR PP char: %c", msa->pp[i][apos]);
3823 if(ppidx == 11) ESL_FAIL(eslEFORMAT, errbuf, "nongap nucleotide: %c, annotated with gap #=GR PP char: %c", msa->aseq[i][apos], msa->pp[i][apos]);
3824 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
3825 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx_p], ppavgA[ppidx], ps->sclAA[pp], within_mask, NULL)) != eslOK) return status;
3826 }
3827 else {
3828 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[gap_idx_p])) != eslOK) return status;
3829 ngap_p++;
3830 if(ps->mask != NULL && ps->mask[rfpos] == '1') ngap_masked_p++;
3831 }
3832 }
3833
3834 /* add one-cell color legend */
3835 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[gap_idx_p], ngap_p, ngap_masked_p, FALSE, FALSE);
3836 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "gap", ps->legx_max_chars, errbuf)) != eslOK) return status;
3837 ps->nocclA[pp] = 1;
3838
3839 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "posterior probability \\(alignment confidence\\)", ps->legx_max_chars, errbuf)) != eslOK) return status;
3840 ps->seqidxA[pp] = i;
3841 if((status = add_page_desc_to_sspostscript(ps, pp, msa->sqname[i], errbuf)) != eslOK) return status;
3842
3843 ps->rAA[pp][ps->rflen] = '\0';
3844 }
3845 } /* end of for(i = 0; i < msa->nseq; i++) */
3846 free(limits_s);
3847 free(limits_p);
3848 return eslOK;
3849
3850 ERROR: ESL_FAIL(status, errbuf, "individuals_sspostscript(): memory allocation error.");
3851 return status; /* NEVERREACHED */
3852 }
3853
3854
3855 /* Function: cons_seq_sspostscript()
3856 *
3857 * Purpose: Fill a postscript data structure with 1 new page, the consensus sequence.
3858 * Return: eslOK on success.
3859 */
3860 static int
3861 cons_seq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps,
3862 float **hc_onecell, int wcbp_idx_s, int gubp_idx_s, int ncbp_idx_s, int dgbp_idx_s, int hgbp_idx_s)
3863 {
3864 int status;
3865 int p, pp;
3866 int rfpos, lpos, rpos;
3867 int orig_npage = ps->npage;
3868 int nwc_s; /* number of watson-crick bps */
3869 int ngu_s; /* number of GU/UG bps */
3870 int nnc_s; /* number of noncanonical bps */
3871 int ndgi_s = 0; /* number of internal double-gap basepairs */
3872 int nhgi_s = 0; /* number of internal half-gap basepairs */
3873 int spos, epos; /* first/final nongap position for consensus sequence */
3874 int lpos_is_internal; /* is lpos within spos..epos?, if not it's an external deletion */
3875 int rpos_is_internal; /* is rpos within spos..epos?, if not it's an external deletion */
3876 int lpos_is_gap; /* is lpos a gap */
3877 int rpos_is_gap; /* is rpos a gap */
3878
3879 nwc_s = ngu_s = nnc_s = ndgi_s = nhgi_s = 0;
3880
3881 if((status = add_pages_sspostscript(ps, 1, INDIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
3882
3883 for(p = orig_npage; p < ps->npage; p++) {
3884 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
3885 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
3886 }
3887
3888 /* fill ps->rAA with nucleotides and gaps for RF sequence */
3889 pp = orig_npage;
3890 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3891 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
3892 }
3893
3894 if((! esl_opt_GetBoolean(go, "--cambig")) && (! esl_opt_GetBoolean(go, "--no-bp"))) {
3895 spos = epos = -1;
3896 /* determine first and final non-gap position */
3897 for(rfpos = 0; rfpos < ps->rflen; rfpos++) { /* find first non-gap RF position */
3898 if(! esl_abc_CIsGap(ps->abc, ps->msa_cseq_maj[rfpos])) {
3899 spos = rfpos;
3900 break;
3901 }
3902 }
3903 for(rfpos = ps->rflen-1; rfpos >= 0; rfpos--) {
3904 if(! esl_abc_CIsGap(ps->abc, ps->msa_cseq_maj[rfpos])) {
3905 epos = rfpos;
3906 break;
3907 }
3908 }
3909
3910 ESL_ALLOC(ps->rcolAAA[pp], sizeof(float *) * ps->rflen);
3911 ESL_ALLOC(ps->occlAAA[pp], sizeof(OneCellColorLegend_t *) * 5);
3912 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
3913 ESL_ALLOC(ps->rcolAAA[pp][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
3914 if(ps->msa_ct[(rfpos+1)] == 0) { /* single stranded */
3915 /* single-stranded nucleotide or gap, draw same color as Watson-Cricks */
3916 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
3917 }
3918 else { /* basepair, either watson-crick, GU/UG, or non-canonical */
3919 lpos = rfpos;
3920 rpos = ps->msa_ct[rfpos+1]-1;
3921 lpos_is_internal = ((lpos < spos) || (lpos > epos)) ? FALSE : TRUE;
3922 rpos_is_internal = ((rpos < spos) || (rpos > epos)) ? FALSE : TRUE;
3923 lpos_is_gap = esl_abc_CIsGap(ps->abc, ps->msa_cseq_maj[lpos]) ? TRUE : FALSE;
3924 rpos_is_gap = esl_abc_CIsGap(ps->abc, ps->msa_cseq_maj[rpos]) ? TRUE : FALSE;
3925 if(lpos_is_internal && rpos_is_internal) { /* BP is either Watson-Crick, G:U/U:G, non-canonical, internal double-gap bp or internal half-gap bp */
3926 if(is_watson_crick_bp(ps->msa_cseq_maj[lpos], ps->msa_cseq_maj[rpos])) { /* watson-crick */
3927 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
3928 if(lpos < rpos) nwc_s++;
3929 }
3930 else if(is_gu_or_ug_bp(ps->msa_cseq_maj[lpos], ps->msa_cseq_maj[rpos])) { /* G:U or U:G */
3931 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[gubp_idx_s])) != eslOK) return status;
3932 if(lpos < rpos) ngu_s++;
3933 }
3934 else if (lpos_is_gap && rpos_is_gap) { /* internal double-gap basepair */
3935 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[dgbp_idx_s])) != eslOK) return status;
3936 if(lpos < rpos) ndgi_s++;
3937 }
3938 else if (lpos_is_gap || rpos_is_gap) { /* internal double-gap basepair */
3939 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[hgbp_idx_s])) != eslOK) return status;
3940 if(lpos < rpos) nhgi_s++;
3941 }
3942 else { /* non-canonical */
3943 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[ncbp_idx_s])) != eslOK) return status;
3944 if(lpos < rpos) nnc_s++;
3945 }
3946 } /* end of (if(lpos_is_internal && rpos_is_internal)) */
3947 else {
3948 /* either the left half of the bp is before the first nongap nt,
3949 * or the right half of the bp is after the final nongap nt,
3950 * for both cases, draw the nt as black, and don't count it as
3951 * any type of basepair
3952 */
3953 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[BLACKOC])) != eslOK) return status;
3954 }
3955 }
3956 }
3957
3958 /* add one-cell color legend for watson-crick basepairs */
3959 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[wcbp_idx_s], nwc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3960 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "Watson-Crick basepair (bp)", ps->legx_max_chars, errbuf)) != eslOK) return status;
3961 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "C-G", errbuf)) != eslOK) return status;
3962 ps->nocclA[pp]++;
3963
3964 /* add one-cell color legend for G-U, U-G basepairs */
3965 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[gubp_idx_s], ngu_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3966 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U or U-G bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3967 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U", errbuf)) != eslOK) return status;
3968 ps->nocclA[pp]++;
3969
3970 /* add one-cell color legend for non-canonical basepairs */
3971 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[ncbp_idx_s], nnc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3972 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "non-canonical bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3973 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "A-A", errbuf)) != eslOK) return status;
3974 ps->nocclA[pp]++;
3975
3976 /* add one-cell color legend for internal half-gap basepairs */
3977 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[hgbp_idx_s], nhgi_s, OCCL_BLANK_COUNT, FALSE, FALSE);
3978 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "internal half-gap bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3979 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "A--", errbuf)) != eslOK) return status;
3980 ps->nocclA[pp]++;
3981
3982 /* add one-cell color legend for internal double-gap basepairs */
3983 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[dgbp_idx_s], ndgi_s, OCCL_BLANK_COUNT, TRUE, FALSE);
3984 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "internal double-gap bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
3985 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "---", errbuf)) != eslOK) return status;
3986 ps->nocclA[pp]++;
3987 }
3988
3989 /* add description to ps */
3990 if((status = add_page_desc_to_sspostscript(ps, pp, "alignment consensus sequence", errbuf)) != eslOK) return status;
3991
3992 /* add text legend explaining how consensus nucleotides are determined */
3993 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
3994 ps->ntlA[pp] = 1;
3995
3996 return eslOK;
3997
3998 ERROR: ESL_FAIL(status, errbuf, "cons_seq_sspostscript(): memory allocation error.");
3999 return status; /* NEVERREACHED */
4000 }
4001
4002
4003 /* Function: rf_seq_sspostscript()
4004 *
4005 * Purpose: Fill a postscript data structure with 1 new page, the RF sequence.
4006 * Return: eslOK on success.
4007 */
4008 static int
4009 rf_seq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, int do_rescol,
4010 float **hc_onecell, int wcbp_idx_s, int gubp_idx_s, int ncbp_idx_s)
4011 {
4012 int status;
4013 int p, pp;
4014 int rfpos, apos, lpos, rpos;
4015 int orig_npage = ps->npage;
4016 int seen_canonical = FALSE;
4017 int nwc_s; /* number of watson-crick bps */
4018 int ngu_s; /* number of GU/UG bps */
4019 int nnc_s; /* number of noncanonical bps */
4020
4021 nwc_s = ngu_s = nnc_s = 0;
4022
4023 if((status = add_pages_sspostscript(ps, 1, INDIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
4024
4025 for(p = orig_npage; p < ps->npage; p++) {
4026 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
4027 }
4028
4029 /* fill ps->rAA with nucleotides and gaps for RF sequence */
4030 pp = orig_npage;
4031 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4032 apos = ps->msa_rf2a_map[rfpos];
4033 if(esl_abc_CIsCanonical(ps->abc, msa->rf[apos])) seen_canonical = TRUE;
4034 ps->rAA[pp][rfpos] = msa->rf[apos];
4035 }
4036 /* if seen_canonical is still FALSE, the reference sequences is probably all 'x's, so we don't color by bp type */
4037
4038 if(do_rescol && seen_canonical) { /* determine color for the nucleotide, depends if it is single-stranded or basepaired */
4039 ESL_ALLOC(ps->rcolAAA[pp], sizeof(float *) * ps->rflen);
4040 ESL_ALLOC(ps->occlAAA[pp], sizeof(OneCellColorLegend_t *) * 3);
4041 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4042 ESL_ALLOC(ps->rcolAAA[pp][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
4043 apos = ps->msa_rf2a_map[rfpos];
4044 if(ps->msa_ct[(rfpos+1)] == 0) { /* single stranded */
4045 /* single-stranded nucleotide or gap, draw same color as Watson-Cricks */
4046 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
4047 }
4048 else { /* basepair, either watson-crick, GU/UG, or non-canonical */
4049 lpos = apos;
4050 rpos = ps->msa_rf2a_map[ps->msa_ct[rfpos+1]-1];
4051 if(is_watson_crick_bp(msa->rf[lpos], msa->rf[rpos])) { /* watson-crick */
4052 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[wcbp_idx_s])) != eslOK) return status;
4053 if(lpos < rpos) nwc_s++;
4054 }
4055 else if(is_gu_or_ug_bp(msa->rf[lpos], msa->rf[rpos])) { /* G:U or U:G */
4056 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[gubp_idx_s])) != eslOK) return status;
4057 if(lpos < rpos) ngu_s++;
4058 }
4059 else { /* non-canonical */
4060 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[ncbp_idx_s])) != eslOK) return status;
4061 if(lpos < rpos) nnc_s++;
4062 }
4063 }
4064 }
4065
4066 /* add one-cell color legend for watson-crick basepairs */
4067 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[wcbp_idx_s], nwc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
4068 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "Watson-Crick basepair (bp)", ps->legx_max_chars, errbuf)) != eslOK) return status;
4069 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "C-G", errbuf)) != eslOK) return status;
4070 ps->nocclA[pp]++;
4071
4072 /* add one-cell color legend for G-U, U-G basepairs */
4073 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[gubp_idx_s], ngu_s, OCCL_BLANK_COUNT, FALSE, FALSE);
4074 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U or U-G bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
4075 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "G-U", errbuf)) != eslOK) return status;
4076 ps->nocclA[pp]++;
4077
4078 /* add one-cell color legend for non-canonical basepairs */
4079 ps->occlAAA[pp][ps->nocclA[pp]] = create_onecell_colorlegend(hc_onecell[ncbp_idx_s], nnc_s, OCCL_BLANK_COUNT, FALSE, FALSE);
4080 if((status = add_text_to_onecell_colorlegend (ps, ps->occlAAA[pp][ps->nocclA[pp]], "non-canonical bp", ps->legx_max_chars, errbuf)) != eslOK) return status;
4081 if((status = add_celltext_to_onecell_colorlegend(ps, ps->occlAAA[pp][ps->nocclA[pp]], "A-A", errbuf)) != eslOK) return status;
4082 ps->nocclA[pp]++;
4083 }
4084
4085 /* add description to ps */
4086 if((status = add_page_desc_to_sspostscript(ps, pp, "*REFERENCE* (\"#=GC RF\")", errbuf)) != eslOK) return status;
4087
4088 return eslOK;
4089
4090 ERROR: ESL_FAIL(status, errbuf, "rf_seq_sspostscript(): memory allocation error.");
4091 return status; /* NEVERREACHED */
4092 }
4093
4094 /* count_msa()
4095 *
4096 * Given an msa, count nucleotides, post probs, basepairs, and consensus start and end
4097 * positions and store them in <ret_abc_ct>, <ret_pp_ct>, <ret_bp_ct>,
4098 * <ret_spos_ct>, and <ret_epos_ct>.
4099 *
4100 * <ret_abc_ct> [0..apos..alen-1][0..abc->K]:
4101 * - per position count of each symbol in alphabet over all seqs.
4102 *
4103 * <ret_bp_ct> [0..apos..alen-1][0..abc->Kp-1][0..abc->Kp-1]
4104 * - per (non-pknotted) consensus basepair count of each possible basepair
4105 * over all seqs basepairs are indexed by 'i' the minimum of 'i:j' for a
4106 * pair between i and j, where i < j. Note that non-canonicals and
4107 * gaps and the like are all stored independently.
4108 *
4109 * <ret_pp_ct> [0..apos..alen-1][0..10]
4110 * - per position count of each posterior probability code over all seqs.
4111 *
4112 * <ret_spos_ct> [0..apos..alen-1]
4113 * - per position count of first nongap position over all seqs.
4114 *
4115 * <ret_epos_ct> [0..apos..alen-1]
4116 * - per position count of final nongap position over all seqs.
4117 *
4118 * A 'gap' has a looser defintion than in esl_abc here, esl_abc's gap,
4119 * missing nucleotides and nonnucleotides are all considered 'gaps' here.
4120 *
4121 * If we encounter an error, we return non-eslOK status and fill
4122 * errbuf with error message.
4123 *
4124 * Returns eslOK upon success.
4125 */
4126 int count_msa(const ESL_ALPHABET *abc, ESL_MSA *msa, char *errbuf, double ***ret_abc_ct, double ****ret_bp_ct, double ***ret_pp_ct, int **ret_spos_ct, int **ret_epos_ct)
4127 {
4128 int status;
4129 double **abc_ct = NULL;
4130 double ***bp_ct = NULL;
4131 int *spos_ct = NULL;
4132 int *epos_ct = NULL;
4133 int apos, i, j, x, epos;
4134 ESL_DSQ *tmp_dsq = NULL;
4135 int seen_start = FALSE;
4136 /* variables related to getting bp counts */
4137 int *ct = NULL; /* 0..alen-1 base pair partners array for current sequence */
4138 char *ss_nopseudo = NULL; /* no-pseudoknot version of structure */
4139 int nppvals = 12; /* '0'-'9' = 0-9, '*' = 10, gap = '11' */
4140 double **pp_ct = NULL; /* [0..alen-1][0..nppvals-1] per position count of each possible PP char over all seqs */
4141 int ppidx;
4142
4143 /* contract check, msa should be in text mode and have ss_cons */
4144 if(msa->flags & eslMSA_DIGITAL) ESL_FAIL(eslEINVAL, errbuf, "count_msa() contract violation, MSA is digitized");
4145 if(msa->ss_cons == NULL) ESL_FAIL(eslEINVAL, errbuf, "the alignment lacks SS_cons annotation");
4146
4147 /* allocate pp_ct array, if nec */
4148 if(ret_pp_ct != NULL && msa->pp != NULL) {
4149 ESL_ALLOC(pp_ct, sizeof(double *) * msa->alen);
4150 for(apos = 0; apos < msa->alen; apos++) {
4151 ESL_ALLOC(pp_ct[apos], sizeof(double) * nppvals);
4152 esl_vec_DSet(pp_ct[apos], nppvals, 0.);
4153 }
4154 }
4155
4156 /* get ct array which defines the consensus base pairs */
4157 ESL_ALLOC(ct, sizeof(int) * (msa->alen+1));
4158 ESL_ALLOC(ss_nopseudo, sizeof(char) * (msa->alen+1));
4159 esl_wuss_nopseudo(msa->ss_cons, ss_nopseudo);
4160 if ((status = esl_wuss2ct(ss_nopseudo, msa->alen, ct)) != eslOK) ESL_FAIL(status, errbuf, "Consensus structure string is inconsistent.");
4161
4162 ESL_ALLOC(tmp_dsq, (msa->alen+2) * sizeof(ESL_DSQ));
4163 ESL_ALLOC(abc_ct, sizeof(double *) * msa->alen);
4164 ESL_ALLOC(bp_ct, sizeof(double **) * msa->alen);
4165 for(apos = 0; apos < msa->alen; apos++) {
4166 ESL_ALLOC(abc_ct[apos], sizeof(double) * (abc->K+1));
4167 esl_vec_DSet(abc_ct[apos], (abc->K+1), 0.);
4168 /* careful ct is indexed 1..alen, not 0..alen-1 */
4169 if(ct[(apos+1)] > (apos+1)) { /* apos+1 is an 'i' in an i:j pair, where i < j */
4170 ESL_ALLOC(bp_ct[apos], sizeof(double *) * (abc->Kp));
4171 for(x = 0; x < abc->Kp; x++) {
4172 ESL_ALLOC(bp_ct[apos][x], sizeof(double) * (abc->Kp));
4173 esl_vec_DSet(bp_ct[apos][x], abc->Kp, 0.);
4174 }
4175 }
4176 else { /* apos+1 is not an 'i' in an i:j pair, where i < j, set to NULL */
4177 bp_ct[apos] = NULL;
4178 }
4179 }
4180 ESL_ALLOC(spos_ct, sizeof(int) * msa->alen);
4181 ESL_ALLOC(epos_ct, sizeof(int) * msa->alen);
4182 esl_vec_ISet(spos_ct, msa->alen, 0);
4183 esl_vec_ISet(epos_ct, msa->alen, 0);
4184
4185 for(i = 0; i < msa->nseq; i++) {
4186 seen_start = FALSE;
4187 epos = 0; // all seqs should have at least one residue, so we only need to initialize epos to make static analyzers happy.
4188 if((status = esl_abc_Digitize(abc, msa->aseq[i], tmp_dsq)) != eslOK) ESL_FAIL(status, errbuf, "problem digitizing sequence %d", i);
4189 for(apos = 0; apos < msa->alen; apos++) { /* update appropriate abc count, careful, tmp_dsq ranges from 1..msa->alen (not 0..msa->alen-1) */
4190 if((status = esl_abc_DCount(abc, abc_ct[apos], tmp_dsq[apos+1], 1.0)) != eslOK) ESL_FAIL(status, errbuf, "problem counting nucleotide %d of seq %d", apos, i);
4191 if(! esl_abc_XIsGap(abc, tmp_dsq[apos+1])) {
4192 if(! seen_start) {
4193 spos_ct[apos]++;
4194 seen_start = TRUE;
4195 }
4196 epos = apos; // as noted above: must get called at least once per seq.
4197 }
4198 /* get bp count, if nec */
4199 if(bp_ct[apos] != NULL) { /* our flag for whether position (apos+1) is an 'i' in an i:j pair where i < j */
4200 j = ct[apos+1] - 1; /* ct is indexed 1..alen */
4201 bp_ct[apos][tmp_dsq[(apos+1)]][tmp_dsq[(j+1)]]++;
4202 }
4203 }
4204 epos_ct[epos]++;
4205 /* get PP counts, if nec */
4206 if(ret_pp_ct != NULL) {
4207 if(msa->pp[i] == NULL) ESL_FAIL(eslEINVAL, errbuf, "--prob requires all sequences in the alignment have PP, seq %d does not.", i+1);
4208 for(apos = 0; apos < msa->alen; apos++) { /* update appropriate pp count, careful, tmp_dsq ranges from 1..msa->alen (not 0..msa->alen-1) */
4209 if((ppidx = get_pp_idx(abc, msa->pp[i][apos])) == -1) ESL_FAIL(eslEFORMAT, errbuf, "bad #=GR PP char: %c", msa->pp[i][apos]);
4210 pp_ct[apos][ppidx] += 1.;
4211 }
4212 }
4213 }
4214
4215 *ret_abc_ct = abc_ct;
4216 *ret_bp_ct = bp_ct;
4217 *ret_spos_ct = spos_ct;
4218 *ret_epos_ct = epos_ct;
4219 if(ret_pp_ct != NULL) *ret_pp_ct = pp_ct; /* we only allocated pp_ct if ret_pp_ct != NULL */
4220
4221 esl_free(tmp_dsq);
4222 esl_free(ss_nopseudo);
4223 esl_free(ct);
4224 return eslOK;
4225
4226 ERROR:
4227 esl_arr2_Destroy((void **) abc_ct, msa? msa->alen:0);
4228 esl_arr2_Destroy((void **) pp_ct, msa? msa->alen:0);
4229 esl_arr3_Destroy((void ***) bp_ct, msa? msa->alen:0, abc? abc->Kp:0);
4230 esl_free(spos_ct);
4231 esl_free(epos_ct);
4232 esl_free(tmp_dsq);
4233 ESL_FAIL(status, errbuf, "Error, out of memory while counting important values in the msa.");
4234 return status; /* NEVERREACHED */
4235 }
4236
4237 /* Function: infocontent_sspostscript()
4238 *
4239 * Purpose: Fill a postscript data structure with 1 new page, colored squares indicating
4240 * the information content of each consensus column.
4241 *
4242 * Return: eslOK on success.
4243 */
4244 static int
4245 infocontent_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_onecell_idx, FILE *tabfp)
4246 {
4247 int status;
4248 int p, pp, c;
4249 int rfpos, apos;
4250 int orig_npage = ps->npage;
4251 double *tmp_obs = NULL;
4252 double *ent = NULL;
4253 double *bg = NULL;
4254 float *limits = NULL;
4255 int zero_obs;
4256 int nonecell = 0;
4257 int nonecell_masked = 0;
4258 int within_mask;
4259 int bi; /* bin index for current position */
4260 int l;
4261
4262 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
4263
4264 for(p = orig_npage; p < ps->npage; p++) {
4265 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
4266 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); Don't need this because create_scheme_colorlegend allocates a
4267 //SchemeColorLegend_t object
4268 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 1);
4269 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4270 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
4271 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
4272 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
4273 }
4274 for(c = 0; c < ps->rflen; c++) {
4275 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4276 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4277 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4278 }
4279 }
4280 }
4281
4282 ESL_ALLOC(ent, sizeof(double) * ps->rflen);
4283 ESL_ALLOC(bg, sizeof(double) * abc->K);
4284 esl_vec_DSet(bg, abc->K, 1./(abc->K));
4285 ESL_ALLOC(tmp_obs, sizeof(double) * abc->K);
4286 esl_vec_DSet(tmp_obs, abc->K, 0.);
4287
4288 pp = orig_npage;
4289
4290 /* add color legend */
4291 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
4292 limits[0] = 0.0;
4293 limits[1] = 0.4;
4294 limits[2] = 0.8;
4295 limits[3] = 1.2;
4296 limits[4] = 1.6;
4297 limits[5] = 1.99;
4298 limits[6] = 2.00;
4299 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, TRUE, TRUE, TRUE);
4300
4301 if(tabfp != NULL) {
4302 fprintf(tabfp, "# ------------------------\n");
4303 fprintf(tabfp, "# Information content data\n");
4304 fprintf(tabfp, "# ------------------------\n");
4305 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
4306 fprintf(tabfp, "# in the alignment and corresponding template.\n");
4307 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 5 : 6);
4308 fprintf(tabfp, "# \ttoken 1: 'infocontent' (tag defining line type to ease parsing)\n");
4309 fprintf(tabfp, "# \ttoken 2: consensus position (starting at 1)\n");
4310 fprintf(tabfp, "# \ttoken 3: information content for position (bits)\n");
4311 fprintf(tabfp, "# \ttoken 4: number of non-gap nucleotides in position (max possible is %d (num seqs in aln))\n", msa_nseq);
4312 fprintf(tabfp, "# \ttoken 5: bin index this positions falls in (see bin values below)\n");
4313 if(ps->mask != NULL) {
4314 fprintf(tabfp, "# \ttoken 6: '1' if position is included by mask, '0' if not\n");
4315 }
4316 fprintf(tabfp, "#\n");
4317 fprintf(tabfp, "# Information content is calculated as 2.0 - H, where\n");
4318 fprintf(tabfp, "# H = - \\sum_x p_x \\log_2 p_x, for x in {A, C, G, U}\n");
4319 fprintf(tabfp, "# p_x is the frequency of x for *non-gap* nucleotides at the position.\n");
4320 fprintf(tabfp, "# For example, p_A in a column that includes 4 As, 3 Cs, 2 Gs, 1 U and 5 gaps\n");
4321 fprintf(tabfp, "# would be 4/10 = 0.4.\n");
4322 fprintf(tabfp, "# Maximum possible value for token 3 is %d, the number of sequences in the file.\n", msa_nseq);
4323 fprintf(tabfp, "#\n");
4324 fprintf(tabfp, "# Value ranges for bins:\n");
4325 fprintf(tabfp, "# \tbin 0: special case, 0 non-gap nucleotides in this position\n");
4326 for(l = 0; l < hc_nbins; l++) {
4327 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s information per position (bits)\n", l+1, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
4328 }
4329 fprintf(tabfp, "#\n");
4330 fprintf(tabfp, "# %11s %6s %8s %10s %3s", "type", "cpos", "info", "nongap", "bin");
4331 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
4332 fprintf(tabfp, "\n");
4333 fprintf(tabfp, "# %11s %6s %8s %10s %3s", "-----------", "------", "--------", "----------", "---");
4334 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
4335 fprintf(tabfp, "\n");
4336 }
4337
4338 if(ps->mask == NULL) nonecell_masked = -1; /* special flag */
4339 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4340 apos = ps->msa_rf2a_map[rfpos];
4341 esl_vec_DCopy(abc_ct[apos], abc->K, tmp_obs); /* only copy first abc->K values, don't copy gaps */
4342 zero_obs = (esl_DCompare_old(esl_vec_DSum(tmp_obs, abc->K), 0., eslSMALLX1) == eslOK) ? TRUE : FALSE;
4343 esl_vec_DNorm(tmp_obs, abc->K);
4344 ent[rfpos] = esl_vec_DEntropy(bg, abc->K) - esl_vec_DEntropy(tmp_obs, abc->K);
4345
4346 if(zero_obs) {
4347 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_onecell_idx])) != eslOK) return status;
4348 nonecell++;
4349 if(ps->mask != NULL && ps->mask[rfpos] == '1') nonecell_masked++;
4350 bi = -1;
4351 }
4352 else {
4353 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
4354 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], ent[rfpos], ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
4355 }
4356
4357 /* fill in info for the consensus nucleotide */
4358 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4359 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
4360 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
4361 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4362 }
4363 else { /* position is a '0' in the mask, leave it blank (' ') */
4364 ps->rAA[pp][rfpos] = ' ';
4365 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4366 }
4367 }
4368
4369 if(tabfp != NULL) {
4370 fprintf(tabfp, " infocontent %6d %8.5f %10d %3d", rfpos+1, ent[rfpos], (int) esl_vec_DSum(abc_ct[apos], abc->K), bi+1);
4371 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
4372 fprintf(tabfp, "\n");
4373 }
4374 }
4375
4376 /* add one-cell color legend */
4377 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[hc_onecell_idx], nonecell, nonecell_masked, FALSE, FALSE);
4378 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "100% gaps", ps->legx_max_chars, errbuf)) != eslOK) return status;
4379 ps->nocclA[pp] = 1;
4380
4381 /* add text to legend */
4382 /*sprintf(text, "information content (bits) (total: %.2f bits)", esl_vec_DSum(ent, ps->rflen));*/
4383 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "information content (bits)", ps->legx_max_chars, errbuf)) != eslOK) return status;
4384
4385 /* add the consensus nucleotide explanation text section to the legend */
4386 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4387 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
4388 ps->ntlA[pp] = 1;
4389 }
4390
4391 /* add description to ps */
4392 if((status = add_page_desc_to_sspostscript(ps, pp, "information content per position", errbuf)) != eslOK) return status;
4393
4394 free(ent);
4395 free(tmp_obs);
4396 free(bg);
4397 free(limits);
4398
4399 if(tabfp != NULL) fprintf(tabfp, "//\n");
4400 return eslOK;
4401
4402 ERROR: ESL_FAIL(status, errbuf, "infocontent_sspostscript(): memory allocation error.");
4403 return status; /* NEVERREACHED */
4404 }
4405
4406 /* Function: delete_sspostscript()
4407 *
4408 * Purpose: Fill a postscript data structure with a new page w/colored squares indicating
4409 * the number of sequences with gaps (deletions) at each consensus column.
4410 * If do_all is TRUE the page shows all deletions. If false only 'internal' deletions,
4411 * those that come after the first occupied consensus column of each sequence and
4412 * before the final occupied consensus column for each sequence, are shown.
4413 *
4414 * Return: eslOK on success.
4415 */
4416 static int
4417 delete_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **abc_ct, int *span_ct, int msa_nseq, int do_all, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zerodel_idx, int hc_fewdel_idx, FILE *tabfp)
4418 {
4419 int status;
4420 int p, pp, c, l, bi;
4421 int rfpos, apos;
4422 int orig_npage = ps->npage;
4423 double dfreq;
4424 int nzerodel = 0;
4425 int nzerodel_masked = 0;
4426 int nfewdel = 0;
4427 int nfewdel_masked = 0;
4428 int within_mask;
4429 float *limits = NULL;
4430 float fewdel_thresh;
4431 float n_ext_del; /* msa_nseq - span_ct[rfpos], number of external deletions */
4432 if(ps->mask == NULL) {
4433 nzerodel_masked = -1; /* special flag */
4434 nfewdel_masked = -1; /* special flag */
4435 }
4436
4437 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
4438
4439 for(p = orig_npage; p < ps->npage; p++) {
4440 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
4441 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); Don't need this because create_scheme_colorlegend allocates
4442 //a SchemeColorLegend_t object
4443 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 2);
4444 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4445 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
4446 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
4447 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
4448 }
4449 for(c = 0; c < ps->rflen; c++) {
4450 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4451 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4452 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4453 }
4454 }
4455 }
4456
4457 pp = orig_npage;
4458
4459 /* add color legend */
4460 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
4461 fewdel_thresh = 0.001; /* positions with delete freq < this value will be painted specially (dark grey) */
4462 limits[0] = fewdel_thresh;
4463 limits[1] = 0.05;
4464 limits[2] = 0.20;
4465 limits[3] = 0.35;
4466 limits[4] = 0.50;
4467 limits[5] = 0.75;
4468 limits[6] = 1.00;
4469 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, FALSE, TRUE, TRUE);
4470
4471 if(tabfp != NULL) {
4472 if(do_all) {
4473 fprintf(tabfp, "# -----------\n");
4474 fprintf(tabfp, "# Delete data\n");
4475 fprintf(tabfp, "# -----------\n");
4476 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
4477 fprintf(tabfp, "# in the alignment and corresponding template.\n");
4478 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 4 : 5);
4479 fprintf(tabfp, "# \ttoken 1: 'deleteall' (tag defining line type to ease parsing)\n");
4480 fprintf(tabfp, "# \ttoken 2: consensus position (starting at 1)\n");
4481 fprintf(tabfp, "# \ttoken 3: frequency of deletions (gaps) for position\n");
4482 fprintf(tabfp, "# \ttoken 4: bin index this positions falls in (see bin values below)\n");
4483 if(ps->mask != NULL) {
4484 fprintf(tabfp, "# \ttoken 5: '1' if position is included by mask, '0' if not\n");
4485 }
4486 fprintf(tabfp, "#\n");
4487 fprintf(tabfp, "# A sequence s has a 'delete' at consensus position x if position\n");
4488 fprintf(tabfp, "# x is a gap for aligned sequence s.\n");
4489 fprintf(tabfp, "# Total number of sequences in the alignment is %d\n", msa_nseq);
4490 fprintf(tabfp, "#\n");
4491 fprintf(tabfp, "# Value ranges for bins:\n");
4492 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have a delete at position\n");
4493 fprintf(tabfp, "# \tbin 1: special case, < %.5f fraction of sequences have internal deletes at position\n", fewdel_thresh);
4494 for(l = 0; l < hc_nbins; l++) {
4495 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s frequency of deletes per position\n", l+2, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
4496 }
4497 fprintf(tabfp, "#\n");
4498 fprintf(tabfp, "# %9s %6s %8s %3s", "type", "cpos", "dfreq", "bin");
4499 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
4500 fprintf(tabfp, "\n");
4501 fprintf(tabfp, "# %9s %6s %8s %3s", "---------", "------", "--------", "---");
4502 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
4503 fprintf(tabfp, "\n");
4504 }
4505 else { /* ! do_all (internal deletes only) */
4506 fprintf(tabfp, "# --------------------\n");
4507 fprintf(tabfp, "# Internal delete data\n");
4508 fprintf(tabfp, "# --------------------\n");
4509 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
4510 fprintf(tabfp, "# in the alignment and corresponding template.\n");
4511 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 5 : 6);
4512 fprintf(tabfp, "# \ttoken 1: 'deleteint' (tag defining line type to ease parsing)\n");
4513 fprintf(tabfp, "# \ttoken 2: consensus position (starting at 1)\n");
4514 fprintf(tabfp, "# \ttoken 3: frequency of internal deletions (gaps) for position\n");
4515 fprintf(tabfp, "# \ttoken 4: number of sequences that span (begin at or prior to and end at or after) position (max is %d)\n", msa_nseq);
4516 fprintf(tabfp, "# \ttoken 5: bin index this positions falls in (see bin values below)\n");
4517 if(ps->mask != NULL) fprintf(tabfp, "# \ttoken 6: '1' if position is included by mask, '0' if not\n");
4518 fprintf(tabfp, "#\n");
4519 fprintf(tabfp, "# A sequence s has an 'internal delete' at consensus position 'x' that is actual alignment position 'a' if\n");
4520 fprintf(tabfp, "# x is a gap for aligned sequence s, and s has at least one non-gap nucleotide aligned to a position 'b' <= 'a'\n");
4521 fprintf(tabfp, "# and at least one non-gap nucleotide aligned to a position 'c' >= 'a'\n");
4522 fprintf(tabfp, "#\n");
4523 fprintf(tabfp, "# Value ranges for bins:\n");
4524 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have an internal delete at position\n");
4525 fprintf(tabfp, "# \tbin 1: special case, < %.5f fraction of sequences have internal deletes at position\n", fewdel_thresh);
4526 for(l = 0; l < hc_nbins; l++) {
4527 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s frequency of internal deletes per position\n", l+2, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
4528 }
4529 fprintf(tabfp, "#\n");
4530 fprintf(tabfp, "# %9s %6s %8s %10s %3s", "type", "cpos", "dfreq", "nspan", "bin");
4531 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
4532 fprintf(tabfp, "\n");
4533 fprintf(tabfp, "# %9s %6s %8s %10s %3s", "---------", "------", "--------", "----------", "---");
4534 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
4535 fprintf(tabfp, "\n");
4536 }
4537 }
4538
4539 /* draw delete page */
4540 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4541 apos = ps->msa_rf2a_map[rfpos];
4542 n_ext_del = do_all ? -1. : (float) (msa_nseq - span_ct[rfpos]); /* num external deletes is num seqs minus number of seqs that 'span' apos, see function header comments for explanation */
4543 if((( do_all) && ( abc_ct[apos][abc->K] < eslSMALLX1)) || /* abc_ct[apos][abc->K] == 0 */
4544 ((!do_all) && ((abc_ct[apos][abc->K] - n_ext_del) < eslSMALLX1))) { /* abc_ct[apos][abc->K] == n_ext_del (all deletes are external) */
4545 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_zerodel_idx])) != eslOK) return status;
4546 nzerodel++;
4547 if(ps->mask != NULL && ps->mask[rfpos] == '1') nzerodel_masked++;
4548 bi = -2; /* special case */
4549 dfreq = 0.;
4550 }
4551 else {
4552 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
4553 dfreq = do_all ?
4554 ( abc_ct[apos][abc->K] / (float) msa_nseq) :
4555 ((abc_ct[apos][abc->K] - n_ext_del) / (float) msa_nseq);
4556 /* printf("do_all: %d rfpos: %d dfreq: %.2f\n", do_all, rfpos, dfreq); */
4557 if (dfreq < fewdel_thresh) {
4558 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_fewdel_idx])) != eslOK) return status;
4559 nfewdel++;
4560 if(ps->mask != NULL && ps->mask[rfpos] == '1') nfewdel_masked++;
4561 bi = -1; /* special case */
4562 }
4563 else {
4564 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], dfreq, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
4565 }
4566 }
4567 /* fill in info for the consensus nucleotide */
4568 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4569 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
4570 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
4571 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4572 }
4573 else { /* position is a '0' in the mask, leave it blank (' ') */
4574 ps->rAA[pp][rfpos] = ' ';
4575 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4576 }
4577 }
4578
4579 if(tabfp != NULL) {
4580 if(do_all) fprintf(tabfp, " deleteall %6d %8.5f %3d", rfpos+1, dfreq, bi+2);
4581 else fprintf(tabfp, " deleteint %6d %8.5f %10d %3d", rfpos+1, dfreq, span_ct[rfpos], bi+2);
4582 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
4583 fprintf(tabfp, "\n");
4584 }
4585 }
4586
4587 /* add one-cell color legend */
4588 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[hc_zerodel_idx], nzerodel, nzerodel_masked, FALSE, FALSE);
4589 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[hc_fewdel_idx], nfewdel, nfewdel_masked, FALSE, FALSE);
4590 ps->nocclA[pp] = 2;
4591
4592 if(do_all) {
4593 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "zero deletions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4594 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "< 0.001 seqs have delete", ps->legx_max_chars, errbuf)) != eslOK) return status;
4595 }
4596 else {
4597 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "zero internal deletions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4598 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "< 0.001 seqs have delete", ps->legx_max_chars, errbuf)) != eslOK) return status;
4599 }
4600
4601 /* add color legend and description */
4602 if(do_all) {
4603 /*sprintf(text, "fraction seqs w/deletes ('-'=0 deletes; avg/seq: %.2f)", (float) esl_vec_ISum(dct, ps->rflen) / (float) msa_nseq);*/
4604 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "fraction of seqs with deletes", ps->legx_max_chars, errbuf)) != eslOK) return status;
4605 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, "frequency of deletions at each position", errbuf)) != eslOK) return status;
4606 }
4607 else { /* !do_all, only internal deletes counted */
4608 /*sprintf(text, "fraction seqs w/internal deletes ('-'=0; avg/seq: %.2f)", (float) esl_vec_ISum(dct_internal, ps->rflen) / (float) msa_nseq);*/
4609 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "fraction of seqs w/internal deletions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4610 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, "frequency of internal deletions in each position", errbuf)) != eslOK) return status;
4611 }
4612
4613 /* add the consensus nucleotide explanation text section to the legend */
4614 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4615 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
4616 ps->ntlA[pp] = 1;
4617 }
4618
4619 free(limits);
4620
4621 if(tabfp != NULL) fprintf(tabfp, "//\n");
4622 return eslOK;
4623
4624 ERROR: ESL_FAIL(status, errbuf, "delete_sspostscript(): memory allocation error.");
4625 return status; /* NEVERREACHED */
4626 }
4627
4628 /* Function: insertfreq_sspostscript()
4629 *
4630 * Purpose: Fill a postscript data structure with 1 new page, with colors
4631 * indicating the fraction of seqs with inserts after each
4632 * position.
4633 *
4634 * Return: eslOK on success.
4635 */
4636 static int
4637 insertfreq_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *nseq_with_ins_ct, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zeroins_idx, int hc_fewins_idx, FILE *tabfp)
4638 {
4639 int status;
4640 int p, pp, c, l;
4641 int rfpos;
4642 int orig_npage = ps->npage;
4643 //int apos;
4644 int nzeroins = 0;
4645 int nzeroins_masked = 0;
4646 int nfewins = 0;
4647 int nfewins_masked = 0;
4648 float *limits;
4649 int within_mask;
4650 float ifreq;
4651 int bi;
4652 float fewins_thresh;
4653
4654 if(ps->mask == NULL) {
4655 nzeroins_masked = -1; /* special flag */
4656 nfewins_masked = -1; /* special flag */
4657 }
4658
4659 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
4660
4661 for(p = orig_npage; p < ps->npage; p++) {
4662 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
4663 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); not needed because create_scheme_colorlegend allocates the //
4664 //SchemeColorLegend_t object
4665 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 2);
4666 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4667 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
4668 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
4669 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
4670 }
4671 for(c = 0; c < ps->rflen; c++) {
4672 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4673 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4674 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4675 }
4676 }
4677 }
4678 pp = orig_npage;
4679
4680 /* add color legend */
4681 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
4682 fewins_thresh = 0.001;
4683 limits[0] = fewins_thresh;
4684 limits[1] = 0.01;
4685 limits[2] = 0.05;
4686 limits[3] = 0.10;
4687 limits[4] = 0.20;
4688 limits[5] = 0.50;
4689 limits[6] = 1.00;
4690 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, FALSE, TRUE, TRUE);
4691
4692 if(tabfp != NULL) {
4693 fprintf(tabfp, "# ---------------------\n");
4694 fprintf(tabfp, "# Insert frequency data\n");
4695 fprintf(tabfp, "# ---------------------\n");
4696 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each possible insert position\n", ps->rflen+1);
4697 fprintf(tabfp, "# after each of the %d consensus positions and one more for inserts prior to the first consensus position.\n", ps->rflen);
4698 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 5 : 6);
4699 fprintf(tabfp, "# \ttoken 1: 'insertfreq' (tag defining line type to ease parsing)\n");
4700 fprintf(tabfp, "# \ttoken 2: consensus position <cpos> after which inserts occur ('0' == before posn 1)\n");
4701 fprintf(tabfp, "# \ttoken 3: fraction of sequences that span <cpos> (see defn of span below) with >= 1 inserted nucleotides after position\n");
4702 fprintf(tabfp, "# \ttoken 4: number of sequences that span (begin at or prior to and end at or after) position (max is %d)\n", msa_nseq);
4703 fprintf(tabfp, "# \ttoken 5: bin index this positions falls in (see bin values below)\n");
4704 if(ps->mask != NULL) {
4705 fprintf(tabfp, "# \ttoken 6: '1' if position is included by mask, '0' if not\n");
4706 }
4707 fprintf(tabfp, "#\n");
4708 fprintf(tabfp, "# Total number of sequences in the alignment is %d\n", msa_nseq);
4709 fprintf(tabfp, "#\n");
4710 fprintf(tabfp, "# A sequence s spans consensus position 'x' that is actual alignment position 'a' if s has\n");
4711 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'b' <= 'a' and\n");
4712 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'c' >= 'a'\n");
4713 fprintf(tabfp, "#\n");
4714 fprintf(tabfp, "# Value ranges for bins:\n");
4715 fprintf(tabfp, "# \tbin -1: special case, reserved for inserts before position 1,\n");
4716 fprintf(tabfp, "# \t these are NOT SHOWN in the postscript diagram (!)\n");
4717 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have inserts after this position\n");
4718 fprintf(tabfp, "# \tbin 1: special case, < %.5f fraction of sequences have inserts after this position\n", fewins_thresh);
4719 for(l = 0; l < hc_nbins; l++) {
4720 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s fraction of sequences with >= 1 inserts after each position\n", l+2, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
4721 }
4722 fprintf(tabfp, "#\n");
4723 fprintf(tabfp, "# %10s %6s %8s %10s %3s", "type", "cpos", "ifreq", "nspan", "bin");
4724 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
4725 fprintf(tabfp, "\n");
4726 fprintf(tabfp, "# %10s %6s %8s %10s %3s", "----------", "------", "--------", "----------", "---");
4727 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
4728 fprintf(tabfp, "\n");
4729 }
4730
4731 /* print info on inserts before rfpos 1 to tabfile, if nec */
4732 if(tabfp != NULL) {
4733 //apos = ps->msa_rf2a_map[0];
4734 if(nseq_with_ins_ct[0] > span_ct[0]) ESL_FAIL(eslERANGE, errbuf, "drawing insert page, rfpos: 0 nseq_with_ins_ct (%d) exceeds span_ct (%d)", nseq_with_ins_ct[0], span_ct[0]);
4735 ifreq = (float) nseq_with_ins_ct[0] / (float) span_ct[0];
4736 fprintf(tabfp, " insertfreq %6d %8.5f %10d %3d", 0, ifreq, span_ct[0], -1);
4737 if(ps->mask != NULL) fprintf(tabfp, " %4d", 0);
4738 fprintf(tabfp, "\n");
4739 }
4740
4741 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4742 //apos = ps->msa_rf2a_map[rfpos];
4743 if(nseq_with_ins_ct[rfpos+1] > span_ct[rfpos]) ESL_FAIL(eslERANGE, errbuf, "drawing insert page, rfpos: %d nseq_with_ins_ct (%d) exceeds span_ct (%d)", rfpos, nseq_with_ins_ct[rfpos+1], span_ct[rfpos]);
4744 ifreq = (float) nseq_with_ins_ct[rfpos+1] / (float) span_ct[rfpos]; /* note we don't need to add one to span_ct, it is [0..rflen-1] */
4745 if(nseq_with_ins_ct[(rfpos+1)] == 0) { /* careful, nseq_with_ins_ct goes from 1..rflen, its off-by-one with other arrays */
4746 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_zeroins_idx])) != eslOK) return status;
4747 nzeroins++;
4748 if(ps->mask != NULL && ps->mask[rfpos] == '1') nzeroins_masked++;
4749 bi = -2; /* special case */
4750 }
4751 else if (ifreq < fewins_thresh) {
4752 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_fewins_idx])) != eslOK) return status;
4753 nfewins++;
4754 if(ps->mask != NULL && ps->mask[rfpos] == '1') nfewins_masked++;
4755 bi = -1; /* special case */
4756 }
4757 else {
4758 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
4759 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], ifreq, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
4760 }
4761 /* printf("rfpos: %5d ifreq: %.3f\n", rfpos, ifreq); */
4762
4763 /* fill in info for the consensus nucleotide */
4764 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4765 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
4766 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
4767 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4768 }
4769 else { /* position is a '0' in the mask, leave it blank (' ') */
4770 ps->rAA[pp][rfpos] = ' ';
4771 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4772 }
4773 }
4774
4775 if(tabfp != NULL) {
4776 fprintf(tabfp, " insertfreq %6d %8.5f %10d %3d", rfpos+1, ifreq, span_ct[rfpos], bi+1);
4777 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
4778 fprintf(tabfp, "\n");
4779 }
4780 }
4781
4782 /* add one-cell color legend */
4783 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[hc_zeroins_idx], nzeroins, nzeroins_masked, FALSE, FALSE);
4784 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "zero insertions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4785
4786 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[hc_fewins_idx], nfewins, nfewins_masked, FALSE, FALSE);
4787 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "< 0.001 seqs have insert", ps->legx_max_chars, errbuf)) != eslOK) return status;
4788 ps->nocclA[pp] = 2;
4789
4790 /* add color legend */
4791 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "fraction of seqs w/insertions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4792 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, "frequency of insertions after each position", errbuf)) != eslOK) return status;
4793
4794 /* add the consensus nucleotide explanation text section to the legend */
4795 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4796 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
4797 ps->ntlA[pp] = 1;
4798 }
4799
4800 free(limits);
4801
4802 if(tabfp != NULL) fprintf(tabfp, "//\n");
4803 return eslOK;
4804
4805 ERROR: ESL_FAIL(status, errbuf, "insertfreq_sspostscript(): memory allocation error.");
4806 return status; /* NEVERREACHED */
4807 }
4808
4809
4810 /* Function: insertavglen_sspostscript()
4811 *
4812 * Purpose: Fill a postscript data structure with 1 new page, with colors
4813 * indicating the average length of inserts after each
4814 * position.
4815 *
4816 * Return: eslOK on success.
4817 */
4818 static int
4819 insertavglen_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *nseq_with_ins_ct, int *nins_ct, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_zeroins_idx, FILE *tabfp)
4820 {
4821 int status;
4822 int p, pp, c, l;
4823 int rfpos;
4824 int orig_npage = ps->npage;
4825 //int apos;
4826 int nzeroins = 0;
4827 int nzeroins_masked = 0;
4828 float *limits;
4829 int within_mask;
4830 float ifreq;
4831 float iavglen;
4832 int bi;
4833
4834 if(ps->mask == NULL) nzeroins_masked = -1; /* special flag */
4835
4836 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
4837
4838 for(p = orig_npage; p < ps->npage; p++) {
4839 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
4840 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); Don't need this because create_scheme_colorlegend allocates
4841 //a SchemeColorLegend_t object
4842 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 1);
4843 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4844 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
4845 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
4846 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
4847 }
4848 for(c = 0; c < ps->rflen; c++) {
4849 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4850 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4851 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
4852 }
4853 }
4854 }
4855 pp = orig_npage;
4856
4857 /* add color legend */
4858 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
4859 limits[0] = 1.00;
4860 limits[1] = 1.01;
4861 limits[2] = 1.50;
4862 limits[3] = 3.00;
4863 limits[4] = 4.00;
4864 limits[5] = 10.00;
4865 limits[6] = eslINFINITY;
4866 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, TRUE, FALSE, TRUE);
4867
4868 if(tabfp != NULL) {
4869 fprintf(tabfp, "# --------------------------\n");
4870 fprintf(tabfp, "# Average insert length data\n");
4871 fprintf(tabfp, "# --------------------------\n");
4872 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each possible insert position\n", ps->rflen+1);
4873 fprintf(tabfp, "# after each of the %d consensus positions and one more for inserts prior to the first consensus position.\n", ps->rflen);
4874 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 5 : 6);
4875 fprintf(tabfp, "# \ttoken 1: 'insertlen' (tag defining line type to ease parsing)\n");
4876 fprintf(tabfp, "# \ttoken 2: consensus position <cpos> after which inserts occur ('0' == before posn 1)\n");
4877 fprintf(tabfp, "# \ttoken 3: average number of inserted nucleotides after each position for those seqs with >=1 inserted nucleotides)\n");
4878 fprintf(tabfp, "# \ttoken 4: fraction of sequences that span <cpos> (see defn of span below) with >= 1 inserted nucleotides after position\n");
4879 fprintf(tabfp, "# \ttoken 5: number of sequences that span (begin at or prior to and end at or after) position (max is %d)\n", msa_nseq);
4880 fprintf(tabfp, "# \ttoken 6: bin index this positions falls in (see bin values below)\n");
4881 if(ps->mask != NULL) {
4882 fprintf(tabfp, "# \ttoken 7: '1' if position is included by mask, '0' if not\n");
4883 }
4884 fprintf(tabfp, "#\n");
4885 fprintf(tabfp, "# Total number of sequences in the alignment is %d\n", msa_nseq);
4886 fprintf(tabfp, "#\n");
4887 fprintf(tabfp, "# A sequence s spans consensus position 'x' that is actual alignment position 'a' if s has\n");
4888 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'b' <= 'a' and\n");
4889 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'c' >= 'a'\n");
4890 fprintf(tabfp, "#\n");
4891 fprintf(tabfp, "# Value ranges for bins:\n");
4892 fprintf(tabfp, "# \tbin -1: special case, reserved for inserts before position 1,\n");
4893 fprintf(tabfp, "# \t these are NOT SHOWN in the postscript diagram (!)\n");
4894 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have inserts after this position\n");
4895 for(l = 0; l < hc_nbins; l++) {
4896 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s average insert length after each position\n", l+1, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
4897 }
4898 fprintf(tabfp, "#\n");
4899 fprintf(tabfp, "# %12s %6s %8s %8s %10s %3s", "type", "cpos", "iavglen", "ifreq", "nspan", "bin");
4900 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
4901 fprintf(tabfp, "\n");
4902 fprintf(tabfp, "# %12s %6s %8s %8s %10s %3s", "------------", "------", "--------", "--------", "----------", "---");
4903 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
4904 fprintf(tabfp, "\n");
4905 }
4906
4907 /* print info on inserts before rfpos 1 to tabfile, if nec */
4908 if(tabfp != NULL) {
4909 //apos = ps->msa_rf2a_map[0];
4910 if(nseq_with_ins_ct[0] > span_ct[0]) ESL_FAIL(eslERANGE, errbuf, "drawing insert page, rfpos: 0 nseq_with_ins_ct (%d) exceeds span_ct (%d)", nseq_with_ins_ct[0], span_ct[0]);
4911 ifreq = (span_ct[0] == 0) ? 0. : (float) nseq_with_ins_ct[0] / (float) span_ct[0];
4912 iavglen = (nseq_with_ins_ct[0] == 0) ? 0. : (float) nins_ct[0] / (float) nseq_with_ins_ct[0];
4913 fprintf(tabfp, " insertavglen %6d %8.4f %8.5f %10d %3d", 0, iavglen, ifreq, span_ct[0], -1);
4914 if(ps->mask != NULL) fprintf(tabfp, " %4d", 0);
4915 fprintf(tabfp, "\n");
4916 }
4917
4918 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
4919 //apos = ps->msa_rf2a_map[rfpos];
4920 if(nseq_with_ins_ct[rfpos+1] > span_ct[rfpos]) ESL_FAIL(eslERANGE, errbuf, "drawing insert page, rfpos: %d nseq_with_ins_ct (%d) exceeds span_ct (%d)", rfpos, nseq_with_ins_ct[rfpos+1], span_ct[rfpos]);
4921 ifreq = (span_ct[rfpos] == 0) ? 0. : (float) nseq_with_ins_ct[rfpos+1] / (float) span_ct[rfpos];
4922 iavglen = (nseq_with_ins_ct[rfpos+1] == 0) ? 0. : (float) nins_ct[rfpos+1] / (float) nseq_with_ins_ct[rfpos+1];
4923 /* printf("rfpos: %5d ifreq: %.3f iavglen: %.3f nins: %10d nseq: %10d\n", rfpos, ifreq, iavglen, nins_ct[rfpos+1], nseq_with_ins_ct[rfpos+1]); */
4924 if(nseq_with_ins_ct[(rfpos+1)] == 0) { /* careful, nseq_with_ins_ct goes from 1..rflen, its off-by-one with other arrays */
4925 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_zeroins_idx])) != eslOK) return status;
4926 nzeroins++;
4927 if(ps->mask != NULL && ps->mask[rfpos] == '1') nzeroins_masked++;
4928 bi = -1; /* special case */
4929 }
4930 else {
4931 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
4932 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], iavglen, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
4933 }
4934
4935 /* fill in info for the consensus nucleotide */
4936 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4937 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
4938 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
4939 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4940 }
4941 else { /* position is a '0' in the mask, leave it blank (' ') */
4942 ps->rAA[pp][rfpos] = ' ';
4943 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
4944 }
4945 }
4946
4947 if(tabfp != NULL) {
4948 fprintf(tabfp, " insertavglen %6d %8.4f %8.5f %10d %3d", rfpos+1, iavglen, ifreq, span_ct[rfpos], bi+1);
4949 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
4950 fprintf(tabfp, "\n");
4951 }
4952 }
4953
4954 /* add one-cell color legend */
4955 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[hc_zeroins_idx], nzeroins, nzeroins_masked, FALSE, FALSE);
4956 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "zero insertions", ps->legx_max_chars, errbuf)) != eslOK) return status;
4957 ps->nocclA[pp] = 1;
4958
4959 /* add color legend */
4960 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "average insertion length", ps->legx_max_chars, errbuf)) != eslOK) return status;
4961 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, "average insertion length after each position", errbuf)) != eslOK) return status;
4962
4963 /* add the consensus nucleotide explanation text section to the legend */
4964 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
4965 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
4966 ps->ntlA[pp] = 1;
4967 }
4968
4969 free(limits);
4970
4971 if(tabfp != NULL) fprintf(tabfp, "//\n");
4972 return eslOK;
4973
4974 ERROR: ESL_FAIL(status, errbuf, "insertavglen_sspostscript(): memory allocation error.");
4975 return status; /* NEVERREACHED */
4976 }
4977
4978
4979 /* Function: span_sspostscript()
4980 *
4981 * Purpose: Fill a postscript data structure with 1 new page, with colors
4982 * indicating the fraction of seqs that 'span' each consensus
4983 * position. A consensus position cpos is spanned by a sequence x if
4984 * if at least 1 nucleotide in x is aligned to a consensus position <= cpos
4985 * *and* at least 1 nucleotide in x is aligned to a consensus position >= cpos.
4986 *
4987 * Return: eslOK on success.
4988 */
4989 static int
4990 span_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int *span_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int zerocov_idx, int maxcov_idx, FILE *tabfp)
4991 {
4992 int status;
4993 int p, pp, c, l;
4994 int rfpos;
4995 int orig_npage = ps->npage;
4996 int nzerocov = 0;
4997 int nzerocov_masked = 0;
4998 int nmaxcov = 0;
4999 int nmaxcov_masked = 0;
5000 float *limits;
5001 int within_mask;
5002 float cfract;
5003 int bi;
5004
5005 if(ps->mask == NULL) nzerocov_masked = nmaxcov_masked = -1; /* special flag */
5006
5007 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
5008
5009 for(p = orig_npage; p < ps->npage; p++) {
5010 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
5011 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); Don't need this because create_scheme_colorlegend allocates
5012 // a SchemeColorLegend_t object
5013 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 2);
5014 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5015 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
5016 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
5017 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
5018 }
5019 for(c = 0; c < ps->rflen; c++) {
5020 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5021 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5022 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5023 }
5024 }
5025 }
5026
5027 pp = orig_npage;
5028
5029 /* add color legend */
5030 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
5031 limits[0] = 0.0;
5032 limits[1] = 0.167;
5033 limits[2] = 0.333;
5034 limits[3] = 0.500;
5035 limits[4] = 0.667;
5036 limits[5] = 0.833;
5037 limits[6] = 1.00;
5038 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, FALSE, FALSE, TRUE);
5039
5040 if(tabfp != NULL) {
5041 fprintf(tabfp, "# ---------\n");
5042 fprintf(tabfp, "# Span data\n");
5043 fprintf(tabfp, "# ---------\n");
5044 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
5045 fprintf(tabfp, "# in the alignment and corresponding template.\n");
5046 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 4 : 5);
5047 fprintf(tabfp, "# \ttoken 1: 'span' (tag defining line type to ease parsing)\n");
5048 fprintf(tabfp, "# \ttoken 2: consensus position (starting at 1)\n");
5049 fprintf(tabfp, "# \ttoken 3: fraction of sequences that 'span' position\n");
5050 fprintf(tabfp, "# \ttoken 4: bin index this positions falls in (see bin values below)\n");
5051 if(ps->mask != NULL) {
5052 fprintf(tabfp, "# \ttoken 5: '1' if position is included by mask, '0' if not\n");
5053 }
5054 fprintf(tabfp, "#\n");
5055 fprintf(tabfp, "# A sequence s spans consensus position 'x' that is actual alignment position 'a' if s has\n");
5056 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'b' <= 'a' and\n");
5057 fprintf(tabfp, "# at least one non-gap nucleotide aligned to a position 'c' >= 'a'\n");
5058 fprintf(tabfp, "#\n");
5059 fprintf(tabfp, "# Value ranges for bins:\n");
5060 fprintf(tabfp, "# \tbin 0: special case, 0 sequences span this position\n");
5061 fprintf(tabfp, "# \tbin 1: special case, all %d sequences span this position\n", msa_nseq);
5062 for(l = 0; l < hc_nbins; l++) {
5063 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s fraction of sequences that span each position\n", l+2, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
5064 }
5065 fprintf(tabfp, "#\n");
5066 fprintf(tabfp, "# %4s %6s %8s %3s", "type", "cpos", "span", "bin");
5067 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
5068 fprintf(tabfp, "\n");
5069 fprintf(tabfp, "# %4s %6s %8s %3s", "----", "------", "--------", "---");
5070 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
5071 fprintf(tabfp, "\n");
5072 }
5073
5074 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
5075 if(span_ct[rfpos] == 0) {
5076 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[zerocov_idx])) != eslOK) return status;
5077 nzerocov++;
5078 if(ps->mask != NULL && ps->mask[rfpos] == '1') nzerocov_masked++;
5079 cfract = 0.;
5080 bi = -2;
5081 }
5082 else if(span_ct[rfpos] == msa_nseq) {
5083 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[maxcov_idx])) != eslOK) return status;
5084 nmaxcov++;
5085 if(ps->mask != NULL && ps->mask[rfpos] == '1') nmaxcov_masked++;
5086 cfract = 1.;
5087 bi = -1;
5088 }
5089 else {
5090 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
5091 cfract = (float) span_ct[rfpos] / (float) msa_nseq;
5092 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], cfract, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
5093 }
5094
5095 /* fill in info for the consensus nucleotide */
5096 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5097 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
5098 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
5099 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5100 }
5101 else { /* position is a '0' in the mask, leave it blank (' ') */
5102 ps->rAA[pp][rfpos] = ' ';
5103 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5104 }
5105 }
5106
5107 if(tabfp != NULL) {
5108 fprintf(tabfp, " span %6d %8.5f %3d", rfpos+1, cfract, bi+2);
5109 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
5110 fprintf(tabfp, "\n");
5111 }
5112 }
5113
5114 /* add one-cell color legend for zero span positions */
5115 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[zerocov_idx], nzerocov, nzerocov_masked, FALSE, FALSE);
5116 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "no sequences span", ps->legx_max_chars, errbuf)) != eslOK) return status;
5117
5118 /* add one-cell color legend for maximum span positions */
5119 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[maxcov_idx], nmaxcov, nmaxcov_masked, FALSE, FALSE);
5120 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "100% of seqs span", ps->legx_max_chars, errbuf)) != eslOK) return status;
5121 ps->nocclA[pp] = 2;
5122
5123 /* add color legend */
5124 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "fraction of seqs that span each position", ps->legx_max_chars, errbuf)) != eslOK) return status;
5125 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, "fraction of sequences that span each position", errbuf)) != eslOK) return status;
5126
5127 /* add the consensus nucleotide explanation text section to the legend */
5128 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5129 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
5130 ps->ntlA[pp] = 1;
5131 }
5132
5133 free(limits);
5134
5135 if(tabfp != NULL) fprintf(tabfp, "//\n");
5136 return eslOK;
5137
5138 ERROR: ESL_FAIL(status, errbuf, "span_sspostscript(): memory allocation error.");
5139 return status; /* NEVERREACHED */
5140 }
5141
5142
5143 /* Function: avg_posteriors_sspostscript()
5144 *
5145 * Purpose: Fill a postscript data structure with info average posterior probabilities in the MSA.
5146 * Return: eslOK on success.
5147 */
5148 static int
5149 avg_posteriors_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double **pp_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int hc_onecell_idx, FILE *tabfp)
5150 {
5151 int status;
5152 int p;
5153 int rfpos;
5154 int pp;
5155 float *limits; /* bin limits for the color scheme */
5156 int nonecell_allgap = 0;
5157 int nonecell_allgap_masked = 0;
5158 int within_mask;
5159 int orig_npage = ps->npage;
5160 float ppavgA[11];
5161 int l;
5162 int apos;
5163 double nnongap;
5164 double ppsum;
5165 int ppidx;
5166 double ppavg;
5167 int bi;
5168
5169 ppavgA[0] = 0.025;
5170 ppavgA[1] = 0.10;
5171 ppavgA[2] = 0.20;
5172 ppavgA[3] = 0.30;
5173 ppavgA[4] = 0.40;
5174 ppavgA[5] = 0.50;
5175 ppavgA[6] = 0.60;
5176 ppavgA[7] = 0.70;
5177 ppavgA[8] = 0.80;
5178 ppavgA[9] = 0.90;
5179 ppavgA[10] = 0.975;
5180
5181 if(ps->mask == NULL) { nonecell_allgap_masked = -1; } /* special flag */
5182
5183 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
5184
5185 for(p = orig_npage; p < ps->npage; p++) {
5186 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
5187 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); Don't need this because create_scheme_colorlegend allocates
5188 // a SchemeColorLegend_t object
5189 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 1);
5190 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5191 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
5192 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
5193 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
5194 }
5195 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
5196 ESL_ALLOC(ps->bcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
5197 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5198 ESL_ALLOC(ps->rcolAAA[p][rfpos], sizeof(float) * NCMYK); /* CMYK colors */
5199 }
5200 }
5201 }
5202
5203 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
5204 limits[0] = 0.0;
5205 limits[1] = 0.70;
5206 limits[2] = 0.80;
5207 limits[3] = 0.85;
5208 limits[4] = 0.90;
5209 limits[5] = 0.95;
5210 limits[6] = 1.00;
5211
5212 if(tabfp != NULL) {
5213 fprintf(tabfp, "# ----------------------------------\n");
5214 fprintf(tabfp, "# Average posterior probability data\n");
5215 fprintf(tabfp, "# ----------------------------------\n");
5216 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
5217 fprintf(tabfp, "# in the alignment and corresponding template.\n");
5218 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 5 : 6);
5219 fprintf(tabfp, "# \ttoken 1: 'avgpostprob' (tag defining line type to ease parsing)\n");
5220 fprintf(tabfp, "# \ttoken 2: consensus position (starting at 1)\n");
5221 fprintf(tabfp, "# \ttoken 3: average posterior probability of non-gap nucleotides for position\n");
5222 fprintf(tabfp, "# \ttoken 4: number of non-gap nucleotides in position (max possible is %d (num seqs in aln))\n", msa_nseq);
5223 fprintf(tabfp, "# \ttoken 5: bin index this positions falls in (see bin values below)\n");
5224 if(ps->mask != NULL) fprintf(tabfp, "# \ttoken 6: '1' if position is included by mask, '0' if not\n");
5225 fprintf(tabfp, "#\n");
5226 fprintf(tabfp, "# Posterior probability (PP) values in the alignment file can have 12 possible values,\n");
5227 fprintf(tabfp, "# the average per position is calculated by defining each as the average of its range given\n");
5228 fprintf(tabfp, "# below. (For example, a '8' which indicates PP between 0.75 and 0.85 is treated as 0.8).\n");
5229 fprintf(tabfp, "# \t'.': gap, corresponds to a gap in the sequence (not counted)\n");
5230 fprintf(tabfp, "# \t'0': posterior probability of between 0.00 and 0.05\n");
5231 fprintf(tabfp, "# \t'1': posterior probability of between 0.05 and 0.15\n");
5232 fprintf(tabfp, "# \t'2': posterior probability of between 0.15 and 0.25\n");
5233 fprintf(tabfp, "# \t'3': posterior probability of between 0.25 and 0.35\n");
5234 fprintf(tabfp, "# \t'4': posterior probability of between 0.35 and 0.45\n");
5235 fprintf(tabfp, "# \t'5': posterior probability of between 0.45 and 0.55\n");
5236 fprintf(tabfp, "# \t'6': posterior probability of between 0.55 and 0.65\n");
5237 fprintf(tabfp, "# \t'7': posterior probability of between 0.65 and 0.75\n");
5238 fprintf(tabfp, "# \t'8': posterior probability of between 0.75 and 0.85\n");
5239 fprintf(tabfp, "# \t'9': posterior probability of between 0.85 and 0.95\n");
5240 fprintf(tabfp, "# \t'*': posterior probability of between 0.95 and 1.00\n");
5241 fprintf(tabfp, "#\n");
5242 fprintf(tabfp, "# Value ranges for bins:\n");
5243 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have a non-gap nucleotide at position\n");
5244 for(l = 0; l < hc_nbins; l++) {
5245 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s average posterior probability per position\n", l+1, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
5246 }
5247 fprintf(tabfp, "#\n");
5248 fprintf(tabfp, "# %11s %6s %8s %10s %3s", "type", "cpos", "avgpp", "nongap", "bin");
5249 if(ps->mask != NULL) fprintf(tabfp, " %4s", "mask");
5250 fprintf(tabfp, "\n");
5251 fprintf(tabfp, "# %11s %6s %8s %10s %3s", "-----------", "------", "--------", "----------", "---");
5252 if(ps->mask != NULL) fprintf(tabfp, " %4s", "----");
5253 fprintf(tabfp, "\n");
5254 }
5255
5256 /* step through each sequence and each column, collecting stats */
5257 pp = orig_npage;
5258 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, TRUE, TRUE, TRUE);
5259 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
5260 apos = ps->msa_rf2a_map[rfpos];
5261 nnongap = esl_vec_DSum(pp_ct[apos], 11);
5262 if(esl_FCompare_old(nnongap, 0., eslSMALLX1) == eslOK) { /* effectively 0.0, all gaps */
5263 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[hc_onecell_idx])) != eslOK) return status;
5264 nonecell_allgap++;
5265 if(ps->mask != NULL && ps->mask[rfpos] == '1') nonecell_allgap_masked++;
5266 bi = -1;
5267 ppavg = 0.;
5268 }
5269 else { /* at least 1 non-gap nucleotide in this position */
5270 if(esl_FCompare_old(nnongap, pp_ct[apos][10], eslSMALLX1)) { /* all nongap nucleotides have highest possible posterior probability */
5271 }
5272 else {
5273 }
5274 ppsum = 0.;
5275 for(ppidx = 0; ppidx < 11; ppidx++) {
5276 ppsum += pp_ct[apos][ppidx] * ppavgA[ppidx]; /* Note: PP value is considered average of range, not minimum ('9' == 0.90 (0.95-0.85/2) */
5277 }
5278 ppavg = ppsum / nnongap;
5279 within_mask = (ps->mask != NULL && ps->mask[rfpos] == '1') ? TRUE : FALSE;
5280 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_scheme[hc_scheme_idx], ppavg, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
5281 }
5282
5283 /* fill in info for the consensus nucleotide */
5284 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5285 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
5286 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
5287 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5288 }
5289 else { /* position is a '0' in the mask, leave it blank (' ') */
5290 ps->rAA[pp][rfpos] = ' ';
5291 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5292 }
5293 }
5294
5295 if(tabfp != NULL) {
5296 fprintf(tabfp, " avgpostprob %6d %8.5f %10d %3d", rfpos+1, ppavg, (int) nnongap, bi+1);
5297 if(ps->mask != NULL) fprintf(tabfp, " %4d", ps->mask[rfpos] == '1' ? 1 : 0);
5298 fprintf(tabfp, "\n");
5299 }
5300 }
5301
5302 /* add one-cell color legend for all gap positions */
5303 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[hc_onecell_idx], nonecell_allgap, nonecell_allgap_masked, FALSE, FALSE);
5304 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "100% gaps", ps->legx_max_chars, errbuf)) != eslOK) return status;
5305
5306 ps->nocclA[pp] = 1;
5307
5308 /* add color legend */
5309 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "average posterior probability \\(confidence\\)", ps->legx_max_chars,errbuf)) != eslOK) return status;
5310
5311 /* add the consensus nucleotide explanation text section to the legend */
5312 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5313 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
5314 ps->ntlA[pp] = 1;
5315 }
5316
5317 /* add description to ps */
5318 if((status = add_page_desc_to_sspostscript(ps, pp, "average posterior probability per position", errbuf)) != eslOK) return status;
5319
5320 free(limits);
5321
5322 if(tabfp != NULL) fprintf(tabfp, "//\n");
5323 return eslOK;
5324
5325 ERROR:
5326 return status;
5327 }
5328
5329
5330 /* Function: colormask_sspostscript()
5331 *
5332 * Purpose: Fill a postscript data structure with 1 new page based on a lanemask, each column
5333 * is either black (if included, a '1' in the mask) or pink (not included, a '0' in the
5334 * mask.
5335 *
5336 * Return: eslOK on success.
5337 */
5338 static int
5339 colormask_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, float **hc_onecell, int incmask_idx, int excmask_idx)
5340 {
5341 int status;
5342 int p, pp, c;
5343 int cpos;
5344 int orig_npage = ps->npage;
5345 int ncols_inside_mask = 0;
5346 int ncols_outside_mask = 0;
5347 char *mask_desc = NULL;
5348 char *mask_file = NULL;
5349
5350 if(ps->mask == NULL) ESL_FAIL(eslEINVAL, errbuf, "ps->mask is null when trying to draw maskcol page");
5351 if((status = add_pages_sspostscript(ps, 1, SIMPLEMASKMODE)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "memory error adding pages to the postscript object.");
5352
5353 for(p = orig_npage; p < ps->npage; p++) {
5354 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
5355 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
5356 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 2);
5357 for(c = 0; c < ps->rflen; c++) {
5358 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5359 }
5360 }
5361 pp = orig_npage;
5362
5363 for(cpos = 0; cpos < ps->rflen; cpos++) {
5364 if(ps->mask[cpos] == '1') { /* included */
5365 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[incmask_idx])) != eslOK) return status;
5366 ncols_inside_mask++;
5367 }
5368 else if(ps->mask[cpos] == '0') {
5369 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[excmask_idx])) != eslOK) return status;
5370 ncols_outside_mask++;
5371 }
5372 else ESL_FAIL(eslEINVAL, errbuf, "--mask mask char number %d is not a 1 nor a 0, but a %c\n", cpos, ps->mask[cpos]);
5373 ps->rAA[pp][cpos] = ' ';
5374 }
5375
5376 /* add color legend */
5377 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[incmask_idx], ncols_inside_mask, OCCL_BLANK_COUNT, FALSE, FALSE);
5378 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "columns included by mask", ps->legx_max_chars, errbuf)) != eslOK) return status;
5379
5380 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[excmask_idx], ncols_outside_mask, OCCL_BLANK_COUNT, FALSE, FALSE);
5381 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "columns excluded by mask", ps->legx_max_chars, errbuf)) != eslOK) return status;
5382
5383 if((status = esl_strcat(&(mask_desc), -1, "mask file: ", -1)) != eslOK) ESL_FAIL(status, errbuf, "error copying mask file name string");;
5384 if((status = esl_FileTail(esl_opt_GetString(go, "--mask"), FALSE, &mask_file)) != eslOK) ESL_FAIL(status, errbuf, "error copying mask file name string (probably out of memory).");
5385 if((strlen(mask_file) + strlen(mask_desc)) > (ps->desc_max_chars*2 - 2)) { /* desc would be too long, shorten mask_file so desc is legal */
5386 /* the -5 below is so we can add '...' to end */
5387 if((status = esl_strcat(&(mask_desc), -1, mask_file, ((ps->desc_max_chars*2) - strlen(mask_desc) - 5))) != eslOK) ESL_FAIL(status, errbuf, "error copying mask file name string");
5388 if((status = esl_strcat(&(mask_desc), -1, "...", 3)) != eslOK) ESL_FAIL(status, errbuf, "error copying mask file name string");
5389 }
5390 else { /* desc will not be too long */
5391 if((status = esl_strcat(&(mask_desc), -1, mask_file, -1)) != eslOK) ESL_FAIL(status, errbuf, "error copying mask file name string");
5392 }
5393 free(mask_file);
5394 if((status = add_page_desc_to_sspostscript(ps, pp, mask_desc, errbuf)) != eslOK) return status;
5395
5396 ps->nocclA[pp] = 2;
5397 free(mask_desc);
5398 return eslOK;
5399
5400 ERROR: ESL_FAIL(status, errbuf, "colormask_sspostscript(): memory allocation error.");
5401 return status; /* NEVERREACHED */
5402 }
5403
5404
5405 /* Function: diffmask_sspostscript()
5406 *
5407 * Purpose: Fill a postscript data structure with 1 new page based on a comparison between
5408 * two masks one in ps->mask the other in <mask2>, each position becomes
5409 * one of four colors, one for each of the four possible combinations of 0 and 1.
5410 *
5411 * Return: eslOK on success.
5412 */
5413 static int
5414 diffmask_sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, ESL_MSA *msa, char *mask2, float **hc_onecell, int incboth_idx, int inc1_idx, int inc2_idx, int excboth_idx)
5415 {
5416 int status;
5417 int p, pp, c;
5418 int cpos;
5419 int orig_npage = ps->npage;
5420 int ncols_in_both = 0;
5421 int ncols_out_both = 0;
5422 int ncols_in_1_out_2 = 0;
5423 int ncols_out_1_in_2 = 0;
5424
5425 if(ps->mask == NULL) ESL_FAIL(eslEINVAL, errbuf, "ps->mask is null when trying to draw maskdiff page");
5426 if((status = add_pages_sspostscript(ps, 1, SIMPLEMASKMODE)) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "memory error adding pages to the postscript object.");
5427
5428 for(p = orig_npage; p < ps->npage; p++) {
5429 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
5430 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
5431 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 4);
5432 for(c = 0; c < ps->rflen; c++) {
5433 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5434 }
5435 }
5436 pp = orig_npage;
5437
5438 for(cpos = 0; cpos < ps->rflen; cpos++) {
5439 if(ps->mask[cpos] == '1' && mask2[cpos] == '1') {
5440 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[incboth_idx])) != eslOK) return status;
5441 ncols_in_both++;
5442 }
5443 else if(ps->mask[cpos] == '1' && mask2[cpos] == '0') {
5444 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[inc1_idx])) != eslOK) return status;
5445 ncols_in_1_out_2++;
5446 }
5447 else if(ps->mask[cpos] == '0' && mask2[cpos] == '1') {
5448 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[inc2_idx])) != eslOK) return status;
5449 ncols_out_1_in_2++;
5450 }
5451 else if(ps->mask[cpos] == '0' && mask2[cpos] == '0') {
5452 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][cpos], NCMYK, hc_onecell[excboth_idx])) != eslOK) return status;
5453 ncols_out_both++;
5454 }
5455 else if(ps->mask[cpos] != '0' && ps->mask[cpos] != '1') ESL_FAIL(eslEINVAL, errbuf, "--mask-col char number %d is not a 1 nor a 0, but a %c\n", cpos, ps->mask[cpos]);
5456 else if(mask2[cpos] != '0' && mask2[cpos] != '1') ESL_FAIL(eslEINVAL, errbuf, "--mask-diff char number %d is not a 1 nor a 0, but a %c\n", cpos, mask2[cpos]);
5457 ps->rAA[pp][cpos] = ' ';
5458 }
5459
5460 /* add color legend */
5461 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[incboth_idx], ncols_in_both, OCCL_BLANK_COUNT, FALSE, FALSE);
5462 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "included by both masks", ps->legx_max_chars, errbuf)) != eslOK) return status;
5463
5464 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[inc1_idx], ncols_in_1_out_2, OCCL_BLANK_COUNT, FALSE, FALSE);
5465 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "incl mask 1, excl mask 2", ps->legx_max_chars, errbuf)) != eslOK) return status;
5466
5467 ps->occlAAA[pp][2] = create_onecell_colorlegend(hc_onecell[inc2_idx], ncols_out_1_in_2, OCCL_BLANK_COUNT, FALSE, FALSE);
5468 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][2], "excl mask 1, incl mask 1", ps->legx_max_chars, errbuf)) != eslOK) return status;
5469
5470 ps->occlAAA[pp][3] = create_onecell_colorlegend(hc_onecell[excboth_idx], ncols_out_both, OCCL_BLANK_COUNT, FALSE, FALSE);
5471 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][3], "excluded by both masks", ps->legx_max_chars, errbuf)) != eslOK) return status;
5472 ps->nocclA[pp] = 4;
5473
5474 if((status = add_diffmask_page_desc_to_sspostscript(ps, pp, esl_opt_GetString(go, "--mask"), esl_opt_GetString(go, "--mask-diff"), errbuf)) != eslOK) return status;
5475
5476 return eslOK;
5477
5478 ERROR: ESL_FAIL(status, errbuf, "diffmask_sspostscript(): memory allocation error.");
5479 return status; /* NEVERREACHED */
5480 }
5481 /* Function: add_pages_sspostscript()
5482 *
5483 * Purpose: Add and initialize blank pages to a postscript object.
5484 */
5485 int
5486 add_pages_sspostscript(SSPostscript_t *ps, int ntoadd, int page_mode)
5487 {
5488 int status;
5489 void *tmp;
5490 int p;
5491
5492 if(ps->npage == 0) {
5493 assert(ps->rAA == NULL);
5494 assert(ps->bcolAAA == NULL);
5495 ESL_ALLOC(ps->rAA, sizeof(char *) * (ps->npage + ntoadd));
5496 ESL_ALLOC(ps->rcolAAA, sizeof(float **) * (ps->npage + ntoadd));
5497 ESL_ALLOC(ps->bcolAAA, sizeof(float **) * (ps->npage + ntoadd));
5498 ESL_ALLOC(ps->otypeAA, sizeof(char *) * (ps->npage + ntoadd));
5499 ESL_ALLOC(ps->occlAAA, sizeof(OneCellColorLegend_t **) * (ps->npage + ntoadd));
5500 ESL_ALLOC(ps->nocclA, sizeof(int) * (ps->npage + ntoadd));
5501 ESL_ALLOC(ps->tlAAA, sizeof(TextLegend_t **) * (ps->npage + ntoadd));
5502 ESL_ALLOC(ps->ntlA, sizeof(int) * (ps->npage + ntoadd));
5503 ESL_ALLOC(ps->sclAA, sizeof(SchemeColorLegend_t *) * (ps->npage + ntoadd));
5504 ESL_ALLOC(ps->descA, sizeof(char *) * (ps->npage + ntoadd));
5505 ESL_ALLOC(ps->modeA, sizeof(int) * (ps->npage + ntoadd));
5506 ESL_ALLOC(ps->seqidxA, sizeof(int) * (ps->npage + ntoadd));
5507 }
5508 else {
5509 assert(ps->rAA != NULL);
5510 assert(ps->bcolAAA != NULL);
5511 assert(ps->rcolAAA != NULL);
5512 ESL_RALLOC(ps->rAA, tmp, sizeof(char *) * (ps->npage + ntoadd));
5513 ESL_RALLOC(ps->rcolAAA,tmp, sizeof(float **) * (ps->npage + ntoadd));
5514 ESL_RALLOC(ps->bcolAAA,tmp, sizeof(float **) * (ps->npage + ntoadd));
5515 ESL_RALLOC(ps->otypeAA,tmp, sizeof(char *) * (ps->npage + ntoadd));
5516 ESL_RALLOC(ps->occlAAA,tmp, sizeof(OneCellColorLegend_t ***) * (ps->npage + ntoadd));
5517 ESL_RALLOC(ps->nocclA, tmp, sizeof(int) * (ps->npage + ntoadd));
5518 ESL_RALLOC(ps->tlAAA, tmp, sizeof(TextLegend_t ***) * (ps->npage + ntoadd));
5519 ESL_RALLOC(ps->ntlA, tmp, sizeof(int) * (ps->npage + ntoadd));
5520 ESL_RALLOC(ps->sclAA, tmp, sizeof(SchemeColorLegend_t **) * (ps->npage + ntoadd));
5521 ESL_RALLOC(ps->descA, tmp, sizeof(char *) * (ps->npage + ntoadd));
5522 ESL_RALLOC(ps->modeA, tmp, sizeof(int) * (ps->npage + ntoadd));
5523 ESL_RALLOC(ps->seqidxA,tmp, sizeof(int) * (ps->npage + ntoadd));
5524 }
5525 for(p = ps->npage; p < (ps->npage + ntoadd); p++) {
5526 ps->rAA[p] = NULL;
5527 ps->rcolAAA[p] = NULL;
5528 ps->bcolAAA[p] = NULL;
5529 ps->otypeAA[p] = NULL;
5530 ps->occlAAA[p] = NULL;
5531 ps->nocclA[p] = 0;
5532 ps->tlAAA[p] = NULL;
5533 ps->ntlA[p] = 0;
5534 ps->sclAA[p] = NULL;
5535 ps->descA[p] = NULL;
5536 ps->modeA[p] = page_mode;
5537 ps->seqidxA[p] = -1;
5538 }
5539 ps->npage += ntoadd;
5540
5541 return eslOK;
5542
5543 ERROR:
5544 return status;
5545 }
5546
5547 /* read_mask_file
5548 *
5549 * Given an open file pointer, read the first token of the
5550 * file and return it as *ret_mask. Also return length of
5551 * mask in *ret_masklen, and a flag indicating whether or
5552 * not the mask has any internal zeroes in *ret_mask_has_internal_zeroes.
5553 * An internal '0' is one that occurs between at least one
5554 * 5' and one 3' '1'
5555 *
5556 * Returns: eslOK on success.
5557 */
5558 int
5559 read_mask_file(char *filename, char *errbuf, char **ret_mask, int *ret_masklen, int *ret_mask_has_internal_zeroes)
5560 {
5561 int status;
5562 ESL_FILEPARSER *efp;
5563 char *tok;
5564 char *mask;
5565 int toklen;
5566 int n;
5567 /* for determining if we have internal zeroes */
5568 int seen_1 = FALSE; /* becomes TRUE when we see first (5'-most) '1' */
5569 int seen_1_then_0 = FALSE; /* becomes TRUE when we see first (5'-most) '0' that is 3' of first '1' */
5570 int seen_1_then_0_then_1 = FALSE; /* becomes TRUE when we see first (5'-most) '1' that is 3' of first '0' that is 3' of first '1' */
5571
5572 if (esl_fileparser_Open(filename, NULL, &efp) != eslOK) ESL_FAIL(eslFAIL, errbuf, "failed to open %s in read_mask_file\n", filename);
5573 esl_fileparser_SetCommentChar(efp, '#');
5574
5575 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_FAIL(eslFAIL, errbuf, "failed to read a single token from %s\n", filename);
5576
5577 ESL_ALLOC(mask, sizeof(char) * (toklen+1));
5578
5579 for(n = 0; n < toklen; n++) {
5580 mask[n] = tok[n];
5581 if(mask[n] == '0') {
5582 if((seen_1) && (!seen_1_then_0)) { seen_1_then_0 = TRUE; }
5583 }
5584 else if (mask[n] == '1') {
5585 if(!seen_1) { seen_1 = TRUE; }
5586 if((seen_1) && (seen_1_then_0) && (!seen_1_then_0_then_1)) { seen_1_then_0_then_1 = TRUE; }
5587 }
5588 else { ESL_FAIL(eslEINVAL, errbuf, "character %d of mask file is invalid: %c (must be a '1' or a '0')\n", n, mask[n]); }
5589
5590 mask[n] = tok[n];
5591 }
5592 mask[n] = '\0';
5593
5594 *ret_mask = mask;
5595 *ret_masklen= toklen;
5596 *ret_mask_has_internal_zeroes = seen_1_then_0_then_1;
5597
5598 esl_fileparser_Close(efp);
5599
5600 return eslOK;
5601
5602 ERROR:
5603 return eslEMEM;
5604 }
5605
5606
5607 /* Function: mutual_information_sspostscript()
5608 *
5609 * Purpose: Fill a postscript data structure with 1 new page, colored squares indicating
5610 * the mutual information of each base paired consensus column.
5611 * mutual information is the extra information gained from modelling
5612 * the pair together (info of vector of bps, size 16) versus separately (sum
5613 * of info of the two independent vector of singlets, size 4).
5614 *
5615 * Return: eslOK on success.
5616 */
5617 static int
5618 mutual_information_sspostscript(const ESL_GETOPTS *go, ESL_ALPHABET *abc, char *errbuf, SSPostscript_t *ps, double ***bp_ct, int msa_nseq, float ***hc_scheme, int hc_scheme_idx, int hc_nbins, float **hc_onecell, int ss_idx, int zerores_idx, FILE *tabfp)
5619 {
5620 int status;
5621 int p, pp, c, i;
5622 int rfpos, apos;
5623 int orig_npage = ps->npage;
5624 double *bg, *bg_pair;
5625 double bg_ent, bg_pair_ent;
5626 double *obs_left, *obs_right, *obs_pair;
5627 double ent_left, ent_right, ent_pair;
5628 int j;
5629 ESL_DSQ lres;
5630 ESL_DSQ rres;
5631 double nres;
5632 int nss = 0;
5633 int nzerores = 0;
5634 int nss_masked = 0;
5635 int nzerores_masked = 0;
5636 int i_within_mask, j_within_mask;
5637 int i_bi, j_bi;
5638 float *limits;
5639 int l;
5640 int idx =1;
5641
5642 if(ps->mask == NULL) nss_masked = nzerores_masked = -1; /* special flag */
5643 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
5644
5645 for(p = orig_npage; p < ps->npage; p++) {
5646 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
5647 //ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1); This is unnecessary because create_scheme_colorlegend allocates
5648 //the SchemeColorLegend_t object
5649 ESL_ALLOC(ps->occlAAA[p], sizeof(OneCellColorLegend_t *) * 2);
5650 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5651 ESL_ALLOC(ps->rAA[p], sizeof(char) * ps->rflen);
5652 ESL_ALLOC(ps->rcolAAA[p], sizeof(float *) * ps->rflen);
5653 ESL_ALLOC(ps->tlAAA[p], sizeof(TextLegend_t *) * 1);
5654 }
5655 for(c = 0; c < ps->rflen; c++) {
5656 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5657 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5658 ESL_ALLOC(ps->rcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
5659 }
5660 }
5661 }
5662 pp = orig_npage;
5663
5664 /* add color legend */
5665 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
5666 limits[0] = 0.0;
5667 limits[1] = 0.167;
5668 limits[2] = 0.333;
5669 limits[3] = 0.500;
5670 limits[4] = 0.667;
5671 limits[5] = 0.833;
5672 limits[6] = 1.000;
5673 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, TRUE, TRUE, TRUE);
5674
5675 if(tabfp != NULL) {
5676 fprintf(tabfp, "# -----------------------\n");
5677 fprintf(tabfp, "# Mutual information data\n");
5678 fprintf(tabfp, "# -----------------------\n");
5679 fprintf(tabfp, "# This section includes %d non #-prefixed lines, one for each consensus position\n", ps->rflen);
5680 fprintf(tabfp, "# in the alignment and corresponding template.\n");
5681 fprintf(tabfp, "# Each line includes %d tokens, separated by whitespace:\n", ps->mask == NULL ? 9 : 11);
5682 fprintf(tabfp, "# \ttoken 1: 'mutualinfo' (tag defining line type to ease parsing)\n");
5683 fprintf(tabfp, "# \ttoken 2: base pair index\n");
5684 fprintf(tabfp, "# \ttoken 3: 5' consensus position of base pair (starting at 1)\n");
5685 fprintf(tabfp, "# \ttoken 4: 3' consensus position of base pair (starting at 1)\n");
5686 fprintf(tabfp, "# \ttoken 5: sequence information content at 5' position (bits)\n");
5687 fprintf(tabfp, "# \ttoken 6: sequence information content at 3' position (bits)\n");
5688 fprintf(tabfp, "# \ttoken 7: mutual information of the base pair (bits)\n");
5689 fprintf(tabfp, "# \ttoken 8: number of sequences with non-gap at 5' and 3' posn (max possible is %d)\n", msa_nseq);
5690 fprintf(tabfp, "# \ttoken 8: number of sequences with non-gap at 5' and 3' position\n");
5691 fprintf(tabfp, "# \ttoken 9: bin index this positions falls in (see bin values below).\n");
5692 if(ps->mask != NULL) {
5693 fprintf(tabfp, "# \ttoken 10: '1' if 5' position is included by mask, '0' if not\n");
5694 fprintf(tabfp, "# \ttoken 11: '1' if 3' position is included by mask, '0' if not\n");
5695 }
5696 fprintf(tabfp, "#\n");
5697 fprintf(tabfp, "# Information content is calculated as 2.0 - H, where\n");
5698 fprintf(tabfp, "# H = - \\sum_x p_x \\log_2 p_x, for x in {A, C, G, U}\n");
5699 fprintf(tabfp, "# p_x is the frequency of x for *non-gap* nucleotides at the position.\n");
5700 fprintf(tabfp, "# Only nucleotides for sequences which have a non-gap nucleotide at both\n");
5701 fprintf(tabfp, "# the 5' and 3' positions of the pair are counted.\n");
5702 fprintf(tabfp, "# Mutual information is calculated as\n");
5703 fprintf(tabfp, "# \\sum_{x,y} p_{x,y} \\log_2 ((p_x * p_y) / p_{x,y}\n");
5704 fprintf(tabfp, "# Value ranges for bins:\n");
5705 fprintf(tabfp, "# \tbin 0: special case, 0 sequences have non-gaps at both 5' and 3' position of pair\n");
5706 for(l = 0; l < hc_nbins; l++) {
5707 fprintf(tabfp, "# \tbin %2d: [%.3f-%.3f%s mutual information per position (bits)\n", l+1, limits[l], limits[l+1], (l == hc_nbins-1) ? "]" : ")");
5708 }
5709 fprintf(tabfp, "#\n");
5710 fprintf(tabfp, "# %10s %4s %5s %5s %8s %8s %9s %10s %3s", "type", "idx", "5'pos", "3'pos", "5'info", "3'info", "mutinfo/2", "nongap", "bin");
5711 if(ps->mask != NULL) fprintf(tabfp, " %6s %6s", "5'mask", "3'mask");
5712 fprintf(tabfp, "\n");
5713 fprintf(tabfp, "# %10s %4s %5s %5s %8s %8s %9s %10s %3s", "----------", "----", "-----", "-----", "--------", "--------", "---------", "----------", "---");
5714 if(ps->mask != NULL) fprintf(tabfp, " %6s %6s", "------", "------");
5715 fprintf(tabfp, "\n");
5716 }
5717
5718 /* determine background entropy */
5719 ESL_ALLOC(bg, sizeof(double) * abc->K);
5720 ESL_ALLOC(bg_pair, sizeof(double) * abc->K*abc->K);
5721 esl_vec_DSet(bg, abc->K, 1./(abc->K));
5722 esl_vec_DSet(bg_pair, abc->K*abc->K, 1./(abc->K*abc->K));
5723 bg_pair_ent = esl_vec_DEntropy(bg_pair, abc->K*abc->K);
5724 bg_ent = esl_vec_DEntropy(bg, abc->K);
5725 free(bg);
5726 free(bg_pair);
5727
5728 ESL_ALLOC(obs_left, sizeof(double) * (abc->K));
5729 ESL_ALLOC(obs_right, sizeof(double) * (abc->K));
5730 ESL_ALLOC(obs_pair, sizeof(double) * (abc->K*abc->K));
5731
5732 /* get observed nucleotides and base pairs at each rfpos */
5733 for(rfpos = 0; rfpos < ps->rflen; rfpos++) {
5734 esl_vec_DSet(obs_left, abc->K, 0.);
5735 esl_vec_DSet(obs_right, abc->K, 0.);
5736 esl_vec_DSet(obs_pair, abc->K*abc->K, 0.);
5737 i = rfpos;
5738 nres = 0.;
5739 apos = ps->msa_rf2a_map[rfpos];
5740 /* check if we're base paired */
5741 if(ps->msa_ct[rfpos+1] != 0) {
5742 /* printf("msa_ct rfpos+1: %d %d\n", rfpos+1, ps->msa_ct[rfpos+1]); */
5743 if(ps->msa_ct[rfpos+1] > (rfpos+1)) { /* rfpos is left half of base pair */
5744 /* add up contributions of all possible base pairs,
5745 * we have to be careful to skip nucleotides that are gaps, missing or nonnucleotides in
5746 * EITHER left or right half of the bp.
5747 * We use the raw counts from msa_count() as the wt, (bp_ct[apos][lres][rres]) */
5748 j = ps->msa_ct[i+1] - 1;
5749 for(lres = 0; lres < abc->K; lres++) {
5750 for(rres = 0; rres < abc->K; rres++) {
5751 /* printf("apos: %d rfpos: %d lres: %d rres: %d bp_ct[][][]: %.5f\n", apos, rfpos, lres, rres, bp_ct[apos][lres][rres]);*/
5752 esl_abc_DCount(abc, obs_left, lres, bp_ct[apos][lres][rres]);
5753 esl_abc_DCount(abc, obs_right, rres, bp_ct[apos][lres][rres]);
5754 PairCount(abc, obs_pair, lres, rres, bp_ct[apos][lres][rres]);
5755 nres += bp_ct[apos][lres][rres];
5756 }
5757 }
5758 for(lres = abc->K+1; lres < abc->Kp-2; lres++) { /* do all degenerates and 'any' (N) */
5759 for(rres = abc->K+1; rres < abc->Kp-2; rres++) { /* do all degenerates and 'any' (N) */
5760 esl_abc_DCount(abc, obs_left, lres, bp_ct[apos][lres][rres]);
5761 esl_abc_DCount(abc, obs_right, rres, bp_ct[apos][lres][rres]);
5762 PairCount(abc, obs_pair, lres, rres, bp_ct[apos][lres][rres]);
5763 nres += bp_ct[apos][lres][rres];
5764 }
5765 }
5766 /* esl_vec_DDump(stdout, obs_left, abc->K, NULL);
5767 esl_vec_DDump(stdout, obs_right, abc->K, NULL);
5768 esl_vec_DDump(stdout, obs_pair, abc->K*abc->K, NULL);
5769 */
5770 esl_vec_DNorm(obs_left, abc->K);
5771 esl_vec_DNorm(obs_right, abc->K);
5772 esl_vec_DNorm(obs_pair, abc->K*abc->K);
5773 ent_left = bg_ent - esl_vec_DEntropy(obs_left, abc->K);
5774 ent_right = bg_ent - esl_vec_DEntropy(obs_right, abc->K);
5775 ent_pair = bg_pair_ent - esl_vec_DEntropy(obs_pair, abc->K*abc->K);
5776 /* printf("lpos: %5d rpos: %5d entP: %8.3f entL: %8.3f entR: %8.3f nres: %.2f ", i+1, j+1, ent_pair, ent_left, ent_right, nres); */
5777 ent_pair -= ent_left + ent_right;
5778 ent_pair /= 2.;
5779 /* printf("Final: %8.3f\n", ent_pair); */
5780
5781 /* To verify that the ent_pair is mutual information, calculated a different way, uncomment the following block */
5782 /* double mi = 0;
5783 for(lres = 0; lres < abc->K; lres++) {
5784 for(rres = 0; rres < abc->K; rres++) {
5785 if(obs_pair[lres*abc->K+rres] > eslSMALLX1) {
5786 mi += obs_pair[lres*abc->K+rres] * (1.44269504 * log((obs_pair[lres*abc->K+rres])/(obs_left[lres] * obs_right[rres])));
5787 printf("mi: %.2f obs_left %.2f obs_right: %.2f obs_pair %.2f \n", mi, obs_left[lres], obs_right[rres], obs_pair[lres*abc->K+rres]);
5788 }
5789 }
5790 }
5791 printf("MI/2: %.2f EP: %.2f %.2f\n", mi/2., ent_pair, (mi/2.) - ent_pair);
5792 */
5793
5794 if(ent_pair < (-1. * eslSMALLX1)) {
5795 ESL_FAIL(eslEINCONCEIVABLE, errbuf, "pair information < 0.: %f (lpos: %d rpos: %d)\n", ent_pair, i, j);
5796 }
5797 if(esl_DCompare_old(nres, 0., eslSMALLX1) == eslOK) { /* nres is 0 */
5798 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][i], NCMYK, hc_onecell[zerores_idx])) != eslOK) return status;
5799 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][j], NCMYK, hc_onecell[zerores_idx])) != eslOK) return status;
5800 nzerores += 2;
5801 if(ps->mask != NULL && ps->mask[i] == '1') nzerores_masked++;
5802 if(ps->mask != NULL && ps->mask[j] == '1') nzerores_masked++;
5803 if(tabfp != NULL) {
5804 fprintf(tabfp, " mutualinfo %4d %5d %5d %8.5f %8.5f %9.5f %10d %3d", idx++, i+1, j+1, 0., 0., 0., 0, 0);
5805 if(ps->mask != NULL) fprintf(tabfp, " %6d %6d", ((ps->mask == NULL || ps->mask[i] == '1') ? 1 : 0), ((ps->mask == NULL || ps->mask[j] == '1') ? 1 : 0));
5806 fprintf(tabfp, "\n");
5807 }
5808 }
5809 else {
5810 i_within_mask = (ps->mask != NULL && ps->mask[i] == '1') ? TRUE : FALSE;
5811 j_within_mask = (ps->mask != NULL && ps->mask[j] == '1') ? TRUE : FALSE;
5812 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][i], NCMYK, hc_scheme[hc_scheme_idx], ent_pair, ps->sclAA[pp], i_within_mask, &i_bi)) != eslOK) return status;
5813 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][j], NCMYK, hc_scheme[hc_scheme_idx], ent_pair, ps->sclAA[pp], j_within_mask, &j_bi)) != eslOK) return status;
5814 if(tabfp != NULL) {
5815 fprintf(tabfp, " mutualinfo %4d %5d %5d %8.5f %8.5f %9.5f %10d %3d",
5816 idx++, i+1, j+1,
5817 ent_left,
5818 ent_right,
5819 ent_pair,
5820 (int) nres,
5821 i_bi+1);
5822 if(ps->mask != NULL) fprintf(tabfp, " %6d %6d", ((ps->mask == NULL || ps->mask[i] == '1') ? 1 : 0), ((ps->mask == NULL || ps->mask[j] == '1') ? 1 : 0));
5823 fprintf(tabfp, "\n");
5824 }
5825 }
5826 } /* end of if(ps->msa_ct[rfpos+1] > (rfpos+1)) { */
5827 }
5828 else { /* single stranded, paint grey */
5829 nss++;
5830 if(ps->mask != NULL && ps->mask[rfpos] == '1') nss_masked++;
5831 if((status = set_onecell_values(errbuf, ps->bcolAAA[pp][rfpos], NCMYK, hc_onecell[ss_idx])) != eslOK) return status;
5832 }
5833 /* fill in info for the consensus nucleotide */
5834 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5835 if(ps->mask == NULL || ps->mask[rfpos] == '1') {
5836 ps->rAA[pp][rfpos] = esl_opt_GetBoolean(go, "--cambig") ? ps->msa_cseq_amb[rfpos] : ps->msa_cseq_maj[rfpos];
5837 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5838 }
5839 else { /* position is a '0' in the mask, leave it blank (' ') */
5840 ps->rAA[pp][rfpos] = ' ';
5841 if((status = set_onecell_values(errbuf, ps->rcolAAA[pp][rfpos], NCMYK, hc_onecell[WHITEOC])) != eslOK) return status;
5842 }
5843 }
5844 }
5845
5846 /* add text to the one cell legend */
5847 ps->occlAAA[pp][0] = create_onecell_colorlegend(hc_onecell[ss_idx], nss, nss_masked, FALSE, FALSE);
5848 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][0], "single-stranded", ps->legx_max_chars, errbuf)) != eslOK) return status;
5849
5850 /* add text to the second one cell legend */
5851 ps->occlAAA[pp][1] = create_onecell_colorlegend(hc_onecell[zerores_idx], nzerores, nzerores_masked, FALSE, FALSE);
5852 if((status = add_text_to_onecell_colorlegend(ps, ps->occlAAA[pp][1], "0 complete basepairs", ps->legx_max_chars, errbuf)) != eslOK) return status;
5853 ps->nocclA[pp] = 2;
5854
5855 /* add text to the scheme legend */
5856 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], "mutual information per position (bits)", ps->legx_max_chars, errbuf)) != eslOK) return status;
5857
5858 /* add the consensus nucleotide explanation text section to the legend */
5859 if(! esl_opt_GetBoolean(go, "--no-cnt")) {
5860 ps->tlAAA[pp][0] = create_text_legend_for_consensus_sequence(go, FALSE);
5861 ps->ntlA[pp] = 1;
5862 }
5863
5864 /* add description to ps */
5865 if((status = add_page_desc_to_sspostscript(ps, pp, "mutual information per basepaired position", errbuf)) != eslOK) return status;
5866
5867 free(limits);
5868 free(obs_left);
5869 free(obs_right);
5870 free(obs_pair);
5871
5872 if(tabfp != NULL) fprintf(tabfp, "//\n");
5873 return eslOK;
5874
5875 ERROR: ESL_FAIL(status, errbuf, "mutual_information_sspostscript(): memory allocation error.");
5876 return status; /* NEVERREACHED */
5877 }
5878
5879 /* Function: PairCount()
5880 * Date: SRE, Tue Aug 1 10:34:20 2000 [St. Louis]
5881 *
5882 * Purpose: Given a possibly degenerate symbol code for left
5883 * and right symbols in a pair, increment a symbol
5884 * counter array appropriately.
5885 *
5886 * Args: abc - pointer to the internal alphabet
5887 * counters - vector to count into [0..abc->K^2-1]
5888 * syml - index of left symbol [0..abc->sym_iupac-1]
5889 * symr - index of right symbol [0..abc->sym_iupac-1]
5890 * wt - weight to use for the count (often 1.0).
5891 *
5892 * Returns: void
5893 */
5894 static void
5895 PairCount(const ESL_ALPHABET *abc, double *counters, ESL_DSQ syml, ESL_DSQ symr, double wt)
5896 {
5897 int status;
5898 if (syml < abc->K && symr < abc->K) {
5899 counters[(int) (syml * abc->K + symr)] += wt;
5900 return;
5901 }
5902 else {
5903 int l,r;
5904 double *left = NULL;
5905 double *right = NULL;
5906 ESL_ALLOC(left, sizeof(double) * abc->K);
5907 ESL_ALLOC(right, sizeof(double) * abc->K);
5908
5909 esl_vec_DSet(left, abc->K, 0.);
5910 esl_vec_DSet(right, abc->K, 0.);
5911 esl_abc_DCount(abc, left, syml, 1.0);
5912 esl_abc_DCount(abc, right, symr, 1.0);
5913
5914 for (l = 0; l < abc->K; l++)
5915 for (r = 0; r < abc->K; r++)
5916 counters[l*abc->K +r] += left[l] * right[r] * wt;
5917 free(left);
5918 free(right);
5919 }
5920 return;
5921
5922 ERROR:
5923 esl_fatal("Memory error");
5924 }
5925
5926 /* Function: get_command
5927 * Date: EPN, Fri Jan 25 13:56:10 2008
5928 *
5929 * Purpose: Return the command used to call esl-ssdraw
5930 * in <ret_command>.
5931 *
5932 * Returns: eslOK on success; eslEMEM on allocation failure.
5933 */
5934 static int
5935 get_command(const ESL_GETOPTS *go, char *errbuf, char **ret_command)
5936 {
5937 int status;
5938 int i;
5939 char *command = NULL;
5940
5941 for (i = 0; i < go->argc; i++) { /* copy all command line options and args */
5942 if((status = esl_strcat(&(command), -1, go->argv[i], -1)) != eslOK) goto ERROR;
5943 if(i < (go->argc-1)) if((status = esl_strcat(&(command), -1, " ", 1)) != eslOK) goto ERROR;
5944 }
5945 *ret_command = command;
5946
5947 return eslOK;
5948
5949 ERROR:
5950 ESL_FAIL(status, errbuf, "get_command(): memory allocation error.");
5951 return status;
5952 }
5953
5954 /* Function: get_date
5955 * Date: EPN, Fri Jan 25 13:59:22 2008
5956 *
5957 * Purpose: Return a string that gives the current date.
5958 *
5959 * Returns: eslOK on success; eslEMEM on allocation failure.
5960 */
5961 static int
5962 get_date(char *errbuf, char **ret_date)
5963 {
5964 int status;
5965 time_t date = time(NULL);
5966 char *sdate = NULL;
5967
5968 if((status = esl_strdup(ctime(&date), -1, &sdate)) != eslOK) goto ERROR;
5969 esl_strchop(sdate, -1); /* doesn't return anything but eslOK */
5970
5971 *ret_date = sdate;
5972 return eslOK;
5973
5974 ERROR:
5975 ESL_FAIL(status, errbuf, "get_date() error status: %d, probably out of memory.", status);
5976 return status;
5977 }
5978
5979 /* Function: set_scheme_values()
5980 *
5981 * Purpose: Set color values from a predefined scheme given min, max,
5982 * value and number of colors.
5983 */
5984 static int
5985 set_scheme_values(char *errbuf, float *vec, int ncolvals, float **scheme, float val, SchemeColorLegend_t *scl, int within_mask, int *ret_bi)
5986 {
5987 float min, max;
5988 int ci, bi;
5989 min = scl->limits[0];
5990 max = scl->limits[scl->nbins];
5991 if((min-val) > eslSMALLX1) { ESL_FAIL(eslEINVAL, errbuf, "set_scheme_values(), val: %.4f < min: %.4f\n", val, min); }
5992 if((val-max) > eslSMALLX1) { ESL_FAIL(eslEINVAL, errbuf, "set_scheme_values(), val: %.4f > max: %.4f\n", val, max); }
5993
5994 bi = 0;
5995 while((bi < (scl->nbins-1)) &&
5996 ((val > scl->limits[bi+1]) || /* val exceeds limits[bi+1] OR */
5997 (esl_FCompare_old(val, scl->limits[bi+1], eslSMALLX1) == eslOK))) { /* val equals limits[bi+1] */
5998 bi++;
5999 }
6000 /* printf("%.3f %d (%.3f)\n", val, bi, scl->limits[bi+1]); */
6001 scl->counts[bi]++;
6002 if(within_mask) scl->counts_masked[bi]++;
6003 for(ci = 0; ci < ncolvals; ci++) {
6004 vec[ci] = scheme[bi][ci];
6005 }
6006
6007 if(ret_bi != NULL) *ret_bi = bi;
6008 return eslOK;
6009 }
6010
6011 /* Function: set_onecell_values()
6012 *
6013 * Purpose: Set color values as a predefined single
6014 * color.
6015 */
6016 static int
6017 set_onecell_values(char *errbuf, float *vec, int ncolvals, float *onecolor)
6018 {
6019 int ci;
6020 for(ci = 0; ci < ncolvals; ci++) { vec[ci] = onecolor[ci]; }
6021 return eslOK;
6022 }
6023
6024
6025
6026 /* Function: draw_masked_block()
6027 *
6028 * Purpose: Given coords, color, and mask style options draw a masked block.
6029 */
6030 static int
6031 draw_masked_block(FILE *fp, float x, float y, float *colvec, int do_circle_mask, int do_square_mask, int do_x_mask, int do_border, float cellsize)
6032 {
6033 if (do_circle_mask) {
6034 if(do_border) {
6035 fprintf(fp, "newpath\n");
6036 fprintf(fp, " %.2f %.2f %.1f 0 360 arc closepath\n", x + (cellsize/2.), y + (cellsize/2.), cellsize * (3./8.));
6037 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6038 fprintf(fp, " stroke\n");
6039 }
6040 else {
6041 fprintf(fp, "newpath\n");
6042 fprintf(fp, " %.2f %.2f %.1f 0 360 arc closepath\n", x + (cellsize/2.), y + (cellsize/2.), cellsize * (3./8.));
6043 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6044 fprintf(fp, " fill\n");
6045 }
6046 }
6047 else if(do_square_mask) {
6048 if(do_border) {
6049 fprintf(fp, "newpath\n");
6050 fprintf(fp, " %.2f %.2f moveto", x +1., y +1.);
6051 fprintf(fp, " 0 %.1f rlineto %.1f 0 rlineto 0 -%.1f rlineto closepath\n", cellsize*0.75, cellsize*0.75, cellsize*0.75);
6052 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6053 fprintf(fp, " stroke\n");
6054 }
6055 else {
6056 fprintf(fp, "newpath\n");
6057 fprintf(fp, " %.2f %.2f moveto", x + 1.5, y + 1.5);
6058 fprintf(fp, " 0 %.1f rlineto %.1f 0 rlineto 0 -%.1f rlineto closepath\n", cellsize*(5./8.), cellsize*(5./8.), cellsize*(5./8.));
6059 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6060 fprintf(fp, " fill\n");
6061 }
6062 }
6063 else if (do_x_mask) {
6064 if(do_border) {
6065 fprintf(fp, "newpath\n");
6066 fprintf(fp, " %.2f %.2f moveto", x, y);
6067 fprintf(fp, " 0 %.1f rlineto %.1f 0 rlineto 0 -%.1f rlineto closepath\n", cellsize, cellsize, cellsize);
6068 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6069 fprintf(fp, " fill\n");
6070
6071 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", 0., 0., 0., 0.);
6072 fprintf(fp, "newpath\n");
6073 fprintf(fp, " %.2f %.2f moveto", x, y);
6074 fprintf(fp, " %.1f %.1f rlineto closepath\n", cellsize, cellsize);
6075 fprintf(fp, " stroke\n");
6076 fprintf(fp, " %.2f %.2f moveto", x + cellsize, y);
6077 fprintf(fp, " -%.1f %.1f rlineto closepath\n", cellsize, cellsize);
6078 fprintf(fp, " stroke\n");
6079 }
6080 else {
6081 fprintf(fp, "newpath\n");
6082 fprintf(fp, " %.4f %.4f %.4f %.4f setcmykcolor\n", colvec[0], colvec[1], colvec[2], colvec[3]);
6083 fprintf(fp, " %.2f %.2f moveto", x, y);
6084 fprintf(fp, " %.1f %.1f rlineto closepath\n", cellsize, cellsize);
6085 fprintf(fp, " stroke\n");
6086 fprintf(fp, "newpath\n");
6087 fprintf(fp, " %.2f %.2f moveto", x + cellsize, y);
6088 fprintf(fp, " -%.1f %.1f rlineto closepath\n", cellsize, cellsize);
6089 fprintf(fp, " stroke\n");
6090 }
6091 }
6092 return eslOK;
6093 }
6094
6095 /* Function: validate_justread_sspostscript()
6096 *
6097 * Purpose: Validate a sspostscript just created by parsing
6098 * a template file. Nothing fancy here, just make
6099 * sure all we've written everything we expect.
6100 */
6101 static int
6102 validate_justread_sspostscript(SSPostscript_t *ps, char *errbuf)
6103 {
6104 if(ps->modelname == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read modelname from template file.");
6105 if(ps->nbp == 0) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read 'lines bpconnects' section from template file.");
6106 if(ps->scale < 0) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read scale from template file.");
6107 if(ps->rflen == 0) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read 'text nucleotides' section from template file.");
6108 if(ps->leg_posn == -1) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read 'legend' section from template file.");
6109 if(ps->leg_cellsize == -1) ESL_FAIL(eslEINVAL, errbuf, "Error, failed to read 'legend' section from template file.");
6110
6111 /* Stuff we don't currently require, but we may want to eventually */
6112 /*if(ps->nposntext == 0) ESL_FAIL(eslEINVAL, errbuf, "validate_justread_sspostscript(), failed to read 'text positiontext' section from template file.");*/
6113 /*if(ps->nticks == 0) ESL_FAIL(eslEINVAL, errbuf, "validate_justread_sspostscript(), failed to read 'lines positionticks' section from template file.");*/
6114 /*if(ps->nbp == 0) ESL_FAIL(eslEINVAL, errbuf, "validate_justread_sspostscript(), failed to read 'lines bpconnects' section from template file.");*/
6115
6116 return eslOK;
6117 }
6118
6119
6120 /* Function: validate_and_update_sspostscript_given_msa()
6121 *
6122 * Purpose: Validate that a sspostscript works with a MSA.
6123 */
6124 static int
6125 validate_and_update_sspostscript_given_msa(const ESL_GETOPTS *go, const ESL_ALPHABET *abc, SSPostscript_t *ps, ESL_MSA *msa, int msa_nseq, char *errbuf)
6126 {
6127 int status;
6128 int *msa_ct;
6129 int msa_nbp = 0;
6130 int *tmp_ct;
6131 int apos, rfpos_i, rfpos_j, rfpos;
6132 int *rf2a_map = NULL;
6133 int *a2rf_map = NULL;
6134 int rflen = 0;
6135
6136 ps->msa = msa;
6137 ps->abc = abc;
6138
6139 /* contract check */
6140 if(msa->rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, msa does not have RF annotation.");
6141 if(msa->ss_cons == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, msa does not have SS_cons annotation.");
6142
6143 /* count non-gap RF columns */
6144 for(apos = 0; apos < msa->alen; apos++) {
6145 if(esl_abc_CIsResidue(abc, msa->rf[apos]))
6146 rflen++;
6147 }
6148 if(ps->rflen != rflen) ESL_FAIL(eslEINVAL, errbuf, "validate_and_update_sspostscript_given_msa(), expected consensus length of %d in MSA, but read %d\n", ps->rflen, rflen);
6149
6150 /* build map of non-gap RF positions to alignment positions and vice versa */
6151 ESL_ALLOC(rf2a_map, sizeof(int) * rflen);
6152 ESL_ALLOC(a2rf_map, sizeof(int) * msa->alen);
6153 esl_vec_ISet(a2rf_map, msa->alen, -1);
6154 rfpos = 0;
6155 for(apos = 0; apos < msa->alen; apos++) {
6156 if(esl_abc_CIsResidue(abc, msa->rf[apos])) {
6157 rf2a_map[rfpos] = apos;
6158 a2rf_map[apos] = rfpos;
6159 rfpos++;
6160 }
6161 }
6162
6163 /* get the CT array for this msa */
6164 ESL_ALLOC(tmp_ct, sizeof(int) * (msa->alen+1));
6165 if (esl_wuss2ct(msa->ss_cons, msa->alen, tmp_ct) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "Problem getting ct from SS_cons, does first alignment of MSA file have SS_cons annotation?");
6166
6167 ESL_ALLOC(msa_ct, sizeof(int) * (rflen+1));
6168 esl_vec_ISet(msa_ct, rflen+1, 0);
6169
6170 /* convert tmp_ct which is in alignment coords [1..alen] to non-gap RF coords [1..rflen]*/
6171 for(apos = 0; apos < msa->alen; apos++) {
6172 if(tmp_ct[apos+1] > (apos+1)) {
6173 rfpos_i = a2rf_map[apos];
6174 rfpos_j = a2rf_map[tmp_ct[apos+1]-1];
6175 if(rfpos_i != -1 && rfpos_j != -1) { /* a consensus basepair */
6176 msa_ct[rfpos_i+1] = rfpos_j+1;
6177 msa_ct[rfpos_j+1] = rfpos_i+1;
6178 msa_nbp++;
6179 }
6180 }
6181 }
6182 free(tmp_ct);
6183 if(ps->nbp != 0 && ps->nbp != msa_nbp) ESL_FAIL(eslEINVAL, errbuf, "validate_and_update_sspostscript_given_msa(), expected %d basepairs in MSA's SS_cons, but read %d\n", ps->nbp, msa_nbp);
6184
6185 /* for(rfpos = 0; rfpos < rflen; rfpos++) printf("ct %5d %5d\n", rfpos, msa_ct[rfpos+1]-1); */
6186
6187 if(ps->msa_ct != NULL) { free(ps->msa_ct); ps->msa_ct = NULL;}
6188 if(ps->msa_rf2a_map != NULL) { free(ps->msa_rf2a_map); ps->msa_rf2a_map = NULL; }
6189 if(ps->msa_a2rf_map != NULL) { free(ps->msa_a2rf_map); ps->msa_a2rf_map = NULL; }
6190 ps->msa_ct = msa_ct;
6191 ps->msa_nbp = msa_nbp;
6192 ps->msa_ct = msa_ct;
6193 ps->msa_rf2a_map = rf2a_map;
6194 ps->msa_a2rf_map = a2rf_map;
6195 ps->msa_nseq = msa_nseq;
6196
6197 return eslOK;
6198
6199 ERROR:
6200 ESL_FAIL(status, errbuf, "validate_and_update_sspostscript_given_msa(), error status %d, probably out of memory.\n", status);
6201 return status;
6202 }
6203
6204
6205 /* Function: draw_header_and_footer()
6206 *
6207 * Purpose: Draw header for a page
6208 */
6209 static int
6210 draw_header_and_footer(FILE *fp, const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, int page, int pageidx2print)
6211 {
6212 int status;
6213 int i, split_idx;
6214 float x, y;
6215 float header_fontsize;
6216 int model_width, desc_width, desc_column_width;
6217 char *model_dashes, *desc_dashes, *desc2print;
6218 char *desc_string = NULL;
6219 float xmodel;
6220 char *model2print = NULL;
6221 float footer_fontsize, footerx_charsize;
6222
6223 header_fontsize = HEADER_FONTSIZE_UNSCALED / ps->scale;
6224
6225 fprintf(fp, "%% begin header section\n");
6226 fprintf(fp, "/%s findfont %.2f scalefont setfont\n", DEFAULT_FONT, header_fontsize);
6227 fprintf(fp, "0.00 0.00 0.00 1.00 setcmykcolor\n"); /* black */
6228
6229 if(! (esl_opt_GetBoolean(go, "--no-head"))) {
6230 model_width = ESL_MAX(strlen("model"), (int) strlen(ps->modelname));
6231 if(model_width > HEADER_MODELNAME_MAXCHARS) {
6232 ESL_ALLOC(model2print, sizeof(char) * (HEADER_MODELNAME_MAXCHARS+1));
6233 for(i = 0; i < (HEADER_MODELNAME_MAXCHARS-3); i++) model2print[i] = ps->modelname[i];
6234 model2print[HEADER_MODELNAME_MAXCHARS-3] = '.';
6235 model2print[HEADER_MODELNAME_MAXCHARS-2] = '.';
6236 model2print[HEADER_MODELNAME_MAXCHARS-1] = '.';
6237 model2print[HEADER_MODELNAME_MAXCHARS] = '\0';
6238 }
6239 else {
6240 if((status = esl_strdup(ps->modelname, -1, &(model2print))) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "draw_header_and_footer(), error copying modelname");
6241 }
6242
6243 model_width = ESL_MIN(model_width, HEADER_MODELNAME_MAXCHARS);
6244 ESL_ALLOC(model_dashes, sizeof(char) * (model_width+1));
6245 for(i = 0; i < model_width; i++) model_dashes[i] = '-';
6246 model_dashes[model_width] = '\0';
6247
6248 if(ps->modeA[page] == ALIMODE || ps->modeA[page] == SIMPLEMASKMODE) {
6249 if((status = esl_strdup("description", -1, &(desc_string))) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "draw_header_and_footer(), error copying description");
6250 }
6251 else if(ps->modeA[page] == INDIMODE) {
6252 if((status = esl_strdup("sequence name", -1, &(desc_string))) != eslOK) ESL_FAIL(eslEINVAL, errbuf, "draw_header_and_footer(), error copying description");
6253 }
6254
6255 xmodel = ps->headerx_desc - (ps->headerx_charsize * (model_width + 6 + 6 + 8 + 2)); /*6,6,8 for #pos,#bps,#seq|seqlen) plus 2 spaces each, first 2 for 2 spaces before desc*/
6256 x = xmodel;
6257 y = ps->headery;
6258
6259 fprintf(fp, "(%-*s %4s %4s) %.2f %.2f moveto show\n", model_width, "model", "#pos", "#bps", x, y);
6260 y -= header_fontsize * 0.75;
6261 fprintf(fp, "(%-*s %4s %4s) %.2f %.2f moveto show\n", model_width, model_dashes, "----", "----", x, y);
6262 y -= header_fontsize * 0.75;
6263 fprintf(fp, "(%-*s %4d %4d) %.2f %.2f moveto show\n", model_width, model2print, ps->rflen, ps->msa_nbp, x, y);
6264 free(model_dashes);
6265 x += (model_width + 6 + 6 +2) * ps->headerx_charsize;
6266
6267 if(ps->modeA[page] == ALIMODE) {
6268 y+= header_fontsize * 1.5;
6269 fprintf(fp, "(%6s) %.2f %.2f moveto show\n", "#seqs", x, y);
6270 y -= header_fontsize * 0.75;
6271 fprintf(fp, "(%6s) %.2f %.2f moveto show\n", "------", x, y);
6272 y -= header_fontsize * 0.75;
6273 fprintf(fp, "(%6d) %.2f %.2f moveto show", ps->msa_nseq, x, y);
6274 }
6275 else if(ps->modeA[page] == INDIMODE && (ps->seqidxA[page] != -1)) { /* ps->seqidxA[page] == -1 if we're printing the consensus sequence */
6276 y+= header_fontsize * 1.5;
6277 fprintf(fp, "(%6s) %.2f %.2f moveto show\n", "seqlen", x, y);
6278 y -= header_fontsize * 0.75;
6279 fprintf(fp, "(%6s) %.2f %.2f moveto show\n", "------", x, y);
6280 y -= header_fontsize * 0.75;
6281 fprintf(fp, "(%6d) %.2f %.2f moveto show", ps->uaseqlenA[ps->seqidxA[page]], x, y);
6282 }
6283
6284 if(ps->descA[page] != NULL) {
6285 x = ps->headerx_desc;
6286 y += 2. * header_fontsize * 0.75;
6287 desc_width = ESL_MAX((int) strlen(desc_string), (int) strlen(ps->descA[page]));
6288 if(desc_width > ps->desc_max_chars) {
6289 /* split into two lines, the add_page_desc_to_sspostscript() function added a '\n' where the split should be */
6290 i = 0;
6291 while(ps->descA[page][i] != '\n') {
6292 i++;
6293 if(i >= desc_width) ESL_FAIL(eslEINVAL, errbuf, "drawing header, failed to find split point from add_page_desc_to_() in two-line description (%s)", ps->descA[page]);
6294 }
6295 split_idx = i;
6296 desc_column_width = split_idx;
6297 }
6298 else desc_column_width = desc_width;
6299
6300 ESL_ALLOC(desc_dashes, sizeof(char) * (desc_column_width+1));
6301 for(i = 0; i < desc_column_width; i++) desc_dashes[i] = '-';
6302 desc_dashes[desc_column_width] = '\0';
6303
6304 fprintf(fp, "(%-*s) %.2f %.2f moveto show\n", desc_column_width, desc_string, x, y);
6305 y -= header_fontsize * 0.75;
6306 fprintf(fp, "(%-*s) %.2f %.2f moveto show\n", desc_column_width, desc_dashes, x, y);
6307 y -= header_fontsize * 0.75;
6308 free(desc_dashes);
6309
6310 if(desc_width > ps->desc_max_chars) {
6311 ESL_ALLOC(desc2print, sizeof(char) * (split_idx+1));
6312 for(i = 0; i < split_idx; i++) desc2print[i] = ps->descA[page][i];
6313 desc2print[split_idx] = '\0';
6314 fprintf(fp, "(%-*s) %.2f %.2f moveto show\n", desc_column_width, desc2print, x, y);
6315 free(desc2print);
6316
6317 x = ps->headerx_desc;
6318 y-= ps->headery_charsize * 1;
6319 ESL_ALLOC(desc2print, sizeof(char) * ((desc_width - split_idx) -1+1));
6320 for(i = split_idx+1; i < desc_width; i++) desc2print[(i-(split_idx+1))] = ps->descA[page][i];
6321 desc2print[(desc_width-(split_idx+1))] = '\0';
6322 fprintf(fp, "(%-*s) %.2f %.2f moveto show\n", desc_column_width, desc2print, x, y);
6323 free(desc2print);
6324 }
6325 else {
6326 fprintf(fp, "(%-*s) %.2f %.2f moveto show\n", desc_width, ps->descA[page], x, y);
6327 }
6328 }
6329 /* masked row of header goes here if desired */
6330 }
6331 fprintf(fp, "%% end header section\n\n");
6332
6333 /* draw footer */
6334 footer_fontsize = LEG_FONTSIZE_UNSCALED / ps->scale;
6335 footerx_charsize = ps->legx_charsize;
6336
6337 if(! (esl_opt_GetBoolean(go, "--no-foot"))) {
6338 fprintf(fp, "%% begin footer section\n");
6339 fprintf(fp, "/%s findfont %.2f scalefont setfont\n", FOOTER_FONT, footer_fontsize);
6340 /* draw alignment file name in lower left hand corner */
6341 if(ps->mask != NULL) {
6342 if(esl_opt_GetString(go, "--mask-diff") != NULL) {
6343 fprintf(fp, "(alignment file: %s; mask 1 file: %s; mask 2 file: %s;) %.2f %.2f moveto show\n", esl_opt_GetArg(go, 1), esl_opt_GetString(go, "--mask"), esl_opt_GetString(go, "--mask-diff"), PAGE_SIDEBUF, PAGE_BOTBUF);
6344 }
6345 else {
6346 fprintf(fp, "(alignment file: %s; mask file: %s;) %.2f %.2f moveto show\n", esl_opt_GetArg(go, 1), esl_opt_GetString(go, "--mask"), PAGE_SIDEBUF, PAGE_BOTBUF);
6347 }
6348 }
6349 else {
6350 fprintf(fp, "(alignment file: %s) %.2f %.2f moveto show\n", esl_opt_GetArg(go, 1), PAGE_SIDEBUF, PAGE_BOTBUF);
6351 }
6352
6353 /* put page number */
6354 /* determine ndigits */
6355 int tmp, ndigits;
6356 tmp = pageidx2print;
6357 ndigits = 1;
6358 while(tmp >= 10) { tmp /= 10; ndigits++; }
6359 x = ps->pagex_max - (PAGE_SIDEBUF) - (footerx_charsize * (5 + ndigits));
6360 fprintf(fp, "(page %d) %.2f %.2f moveto show\n", pageidx2print, x, PAGE_BOTBUF);
6361
6362 fprintf(fp, "%% end footer section\n\n");
6363 }
6364
6365 if(model2print != NULL) free(model2print);
6366 if(desc_string != NULL) free(desc_string);
6367 return eslOK;
6368
6369 ERROR: ESL_FAIL(eslEINVAL, errbuf, "draw_header_and_footer(), memory error.");
6370 }
6371
6372 /* Function: get_insert_info_from_msa
6373 * Date: EPN, Fri Dec 4 13:52:53 2009
6374 *
6375 * Read an MSA with #=GC RF annotation defining
6376 * consensus columns and count how many insertions
6377 * occur after each consensus column for each sequence.
6378 *
6379 * msa - the alignment
6380 * rflen - expected nongap RF length (consensus length)
6381 * ret_nseq_with_ins_ct - [0..rflen] number of sequences with >= 1
6382 * insert after each position. NULL if unwanted.
6383 * ret_nins_ct - [0..rflen] total number of inserted nucleotides
6384 * after each position. NULL if unwanted.
6385 * ret_per_seq_ins_ct - [0..i..msa->nseq-1][0..rflen] number of inserts
6386 * insert after each position per sequence. NULL if unwanted.
6387 *
6388 * Returns void. Dies with an informative error message upon an error.
6389 */
6390 void
6391 get_insert_info_from_msa(const ESL_ALPHABET *abc, ESL_MSA *msa, int rflen, int **ret_nseq_with_ins_ct, int **ret_nins_ct, int ***ret_per_seq_ins_ct)
6392 {
6393 int status;
6394 int i;
6395 int *nseq_with_ins_ct = NULL;
6396 int *nins_ct = NULL;
6397 int **per_seq_ins_ct = NULL;
6398 int rfpos, apos;
6399
6400 /* contract check */
6401 if(msa->rf == NULL) esl_fatal("Error in get_insert_info_from_msa(), msa->rf is NULL.");
6402
6403 /* allocate and initialize */
6404 ESL_ALLOC(nseq_with_ins_ct, sizeof(int) * (rflen+1));
6405 esl_vec_ISet(nseq_with_ins_ct, rflen+1, 0);
6406 ESL_ALLOC(nins_ct, sizeof(int) * (rflen+1));
6407 esl_vec_ISet(nins_ct, rflen+1, 0);
6408 ESL_ALLOC(per_seq_ins_ct, sizeof(int *) * (msa->nseq));
6409 for(i = 0; i < msa->nseq; i++) {
6410 ESL_ALLOC(per_seq_ins_ct[i], sizeof(int) * (rflen+1));
6411 esl_vec_ISet(per_seq_ins_ct[i], (rflen+1), 0);
6412 }
6413
6414 /* fill per_seq_ins_ct, nseq_with_ins_ct */
6415 rfpos = 0;
6416 for(apos = 0; apos < msa->alen; apos++) {
6417 if(esl_abc_CIsResidue(abc, msa->rf[apos])) {
6418 rfpos++;
6419 if(rfpos > rflen) esl_fatal("Error in get_insert_info_from_msa(), expected consensus length (%d) is incorrect.");
6420 }
6421 else {
6422 for(i = 0; i < msa->nseq; i++) {
6423 if(! esl_abc_CIsGap(abc, msa->aseq[i][apos])) {
6424 per_seq_ins_ct[i][rfpos]++;
6425 nins_ct[rfpos]++;
6426 if(per_seq_ins_ct[i][rfpos] == 1) nseq_with_ins_ct[rfpos]++;
6427 }
6428 }
6429 }
6430 }
6431
6432 if(ret_nseq_with_ins_ct != NULL) *ret_nseq_with_ins_ct = nseq_with_ins_ct;
6433 else free(nseq_with_ins_ct);
6434 if(ret_nins_ct != NULL) *ret_nins_ct = nins_ct;
6435 else free(nins_ct);
6436 if(ret_per_seq_ins_ct != NULL) *ret_per_seq_ins_ct = per_seq_ins_ct;
6437 else esl_arr2_Destroy((void **) per_seq_ins_ct, msa->nseq);
6438
6439 return;
6440
6441 ERROR:
6442 esl_fatal("Error in get_insert_info_from_msa(), memory allocation error.");
6443 return; /* NEVERREACHED */
6444 }
6445
6446
6447 /* Function: get_insert_info_from_ifile
6448 * Date: EPN, Fri Dec 4 14:49:12 2009
6449 *
6450 * Read a file output from Infernal's cmalign
6451 * when run with '--matchonly --ifile <f>' and
6452 * fill *ret_ict[0..rflen][0..msa->nseq-1] number
6453 * of inserts after each consensus position for
6454 * each sequence.
6455 *
6456 * We also keep track of cases where a count of
6457 * the first and final nongap positions of all sequence
6458 * per column would be incorrect IF the msa were
6459 * to have all insert columns removed. These corrections
6460 * are stored and returned in srfoff_ct and erfoff_ct
6461 * as explained below.
6462 *
6463 * Format of an insert file (from the commented header of an ifile):
6464 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6465 * This file includes 2+<nseq> non-'#' pre-fixed lines per model used for alignment,
6466 * where <nseq> is the number of sequences in the target file.
6467 * The first non-'#' prefixed line per model includes 2 tokens, separated by a single space (' '):
6468 * The first token is the model name and the second is the consensus length of the model (<clen>).
6469 * The following <nseq> lines include (4+3*<n>) whitespace delimited tokens per line.
6470 * The format for these <nseq> lines is:
6471 * <seqname> <seqlen> <spos> <epos> <c_1> <u_1> <i_1> <c_2> <u_2> <i_2> .... <c_x> <u_x> <i_x> .... <c_n> <u_n> <i_n>
6472 * indicating <seqname> has >= 1 inserted nucleotides after <n> different consensus positions,
6473 * <seqname> is the name of the sequence
6474 * <seqlen> is the unaligned length of the sequence
6475 * <spos> is the first (5'-most) consensus position filled by a nongap for this sequence (-1 if 0 nongap consensus posns)
6476 * <epos> is the final (3'-most) consensus position filled by a nongap for this sequence (-1 if 0 nongap consensus posns)
6477 * <seqlen> is the unaligned length of the sequence
6478 * <c_x> is a consensus position (between 1 and <clen>; if 0 inserts occur before 1st consensus posn)
6479 * <u_x> is the *unaligned* position (b/t 1 and <seqlen>) in <seqname> of the first inserted nucleotide after <c_x>.
6480 * <i_x> is the number of inserted nucleotides after position <c_x> for <seqname>.
6481 * Lines for sequences with 0 inserted nucleotides will include only <seqname> <seqlen>.
6482 * The final non-'#' prefixed line per model includes only '//', indicating the end of info for a model.
6483 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6484 *
6485 * Example:
6486 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
6487 * tRNA 71
6488 * tRNA-1 67 1 71 17 15 1
6489 * tRNA-2 71 1 71 20 20 1
6490 * tRNA-3 70 1 71 45 41 3 46 44 1
6491 * tRNA-4 71 1 71
6492 * tRNA-5 69 1 71 46 44 1
6493 * tRNA-6 70 1 71
6494 * tRNA-7 67 1 71
6495 * tRNA-8 71 1 71 17 16 1
6496 * tRNA-9 67 1 71
6497 * tRNA-10 73 1 71 45 44 4
6498 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
6499 *
6500 * ifile - name of ifile
6501 * rflen - expected nongap RF length (consensus length)
6502 * msa_nseq - expected number of sequences
6503 * useme_keyhash - keyhash with names of sequences for which we will store insert info
6504 * if NULL, we store insert info for all seqs.
6505 * ret_nseq_with_ins_ct - [0..rflen] number of sequences with >= 1 inserts
6506 * after each RF position.
6507 * Note [0] is before first posn, [1] is after first posn, [rflen] is after last one
6508 * ret_nins_ct - [0..rflen] total number of inserted nucleotides (over all seqs) after each RF position.
6509 * Note [0] is before first posn, [1] is after first posn, [rflen] is after last one
6510 * ret_per_seq_ins_ct - [0..i..msa->nseq-1][0..rflen number of inserts
6511 * after each position for each sequence.
6512 * Filled here.
6513 *
6514 * ret_srfoff_ct - [0..rfpos..rflen-1] count adjustment to make to a spos_ct[rfpos] if it
6515 * were obtained from an alignment with inserts removed, this value is <a>+<b>,
6516 * where <a> is : -1 times the number of sequences that have their first nongap
6517 * RF position as rfpos, but include an insert before an rfpos2 < rfpos.
6518 * and <b> is : +1 times the number of sequences that have their first nongap
6519 * RF position as rfpos3, but have an insert before rfpos < rfpos3.
6520 * We can only determine these cases when we read the ifile, since the
6521 * msa has had inserts removed.
6522 * NOTE a nasty off-by-one: RF (consensus) positions in the ifile are indexed 1..rflen
6523 *
6524 * ret_erfoff_ct - [0..rfpos..rflen-1] count adjustment to make to a epos_ct[rfpos] if it
6525 * were obtained from an alignment with inserts removed, this value is <a>+<b>,
6526 * where <a> is : -1 times the number of sequences that have their final nongap
6527 * RF position as rfpos, but include an insert after an rfpos2 > rfpos.
6528 * and <b> is : +1 times the number of sequences that have their final nongap
6529 * RF position as rfpos3, but have an insert after rfpos > rfpos3.
6530 * We can only determine these cases when we read the ifile, since the
6531 * msa has had inserts removed.
6532 * NOTE a nasty off-by-one: RF (consensus) positions in the ifile are indexed 1..rflen
6533 *
6534 * Returns void. Dies with an informative error message on an error.
6535 */
6536 void
6537 get_insert_info_from_ifile(char *ifile, int rflen, int msa_nseq, ESL_KEYHASH *useme_keyhash, int **ret_nseq_with_ins_ct, int **ret_nins_ct, int ***ret_per_seq_ins_ct, int **ret_srfoff_ct, int **ret_erfoff_ct)
6538 {
6539 int status;
6540 ESL_FILEPARSER *efp;
6541 char *tok;
6542 int nseq_read = 0;
6543 int nseq_stored = 0;
6544 int **per_seq_ins_ct = NULL;
6545 int *nins_ct = NULL;
6546 int *nseq_with_ins_ct = NULL;
6547 int nins;
6548 int i;
6549 int rfpos; /* current nongap RF position */
6550 int uapos; /* unaligned position for current sequence */
6551
6552 int seen_model_name_line = FALSE;
6553 int seen_end_of_model_line = FALSE;
6554 int nseq2store; /* number of seqs we'll store insert info on */
6555 int seqlen; /* sequence length for current sequence, read from ifile */
6556 int spos; /* first (5'-most) nongap consensus position for current sequence, read from ifile */
6557 int epos; /* final (3'-most) nongap consensus position for current sequence, read from ifile */
6558 int *srfoff_ct = NULL; /* [0..rfpos..rflen-1] correction for spos_ct[rfpos], add this value to spos_ct
6559 * to correct the miscounting that occurs if the msa has had all inserts removed, but
6560 * an insert file with insert info has been supplied and is read in this function */
6561 int *erfoff_ct = NULL; /* [0..rfpos..rflen-1] correction for epos_ct[rfpos] (analagous to srfoff_ct) */
6562 int already_handled_special_spos = FALSE;
6563 int prv_e_increment, prv_e_decrement;
6564
6565 if (esl_fileparser_Open(ifile, NULL, &efp) != eslOK) esl_fatal("Error: failed to open insert file %s\n", ifile);
6566 esl_fileparser_SetCommentChar(efp, '#');
6567
6568 /* determine how many sequences we'll be storing info for */
6569 nseq2store = (useme_keyhash == NULL) ? msa_nseq : esl_keyhash_GetNumber(useme_keyhash);
6570
6571 /* allocate and initialize */
6572 ESL_ALLOC(nseq_with_ins_ct, sizeof(int) * (rflen+1));
6573 esl_vec_ISet(nseq_with_ins_ct, rflen+1, 0);
6574
6575 ESL_ALLOC(nins_ct, sizeof(int) * (rflen+1));
6576 esl_vec_ISet(nins_ct, rflen+1, 0);
6577
6578 if(ret_srfoff_ct != NULL) {
6579 ESL_ALLOC(srfoff_ct, sizeof(int) * rflen);
6580 esl_vec_ISet(srfoff_ct, rflen, 0);
6581 }
6582 if(ret_erfoff_ct != NULL) {
6583 ESL_ALLOC(erfoff_ct, sizeof(int) * rflen);
6584 esl_vec_ISet(erfoff_ct, rflen, 0);
6585 }
6586 if(ret_per_seq_ins_ct != NULL) {
6587 ESL_ALLOC(per_seq_ins_ct, sizeof(int *) * (nseq2store));
6588 for(i = 0; i < nseq2store; i++) {
6589 ESL_ALLOC(per_seq_ins_ct[i], sizeof(int) * (rflen+1));
6590 esl_vec_ISet(per_seq_ins_ct[i], (rflen+1), 0);
6591 }
6592 }
6593
6594 /* Read the file, verify that it contains the correct number of sequences and the
6595 * consensus length(s) listed in the file agrees with expected rflen.
6596 * Special care is taken to allow concatenated ifiles, so we may see more than
6597 * one // lines, but the total number of seqs should match what we expect.
6598 */
6599 i = 0;
6600 while (esl_fileparser_NextLine(efp) == eslOK) {
6601 if (esl_fileparser_GetTokenOnLine(efp, &tok, NULL) != eslOK) {
6602 if(seen_model_name_line) esl_fatal("Error reading insert file, failed to read seq name on line %d of file %s\n", efp->linenumber, ifile);
6603 else esl_fatal("Error reading insert file, failed to read model name on line %d of file %s\n", efp->linenumber, ifile);
6604 }
6605 if(! seen_model_name_line) { /* this should be a special line, 2 tokens: <cmname> <rflen>, verify that <rflen> is what we expect */
6606 seen_model_name_line = TRUE;
6607 seen_end_of_model_line = FALSE;
6608 if (esl_fileparser_GetTokenOnLine(efp, &tok, NULL) != eslOK) {
6609 esl_fatal("Error reading insert file, failed to read consensus length on line %d of file %s\n", efp->linenumber, ifile);
6610 }
6611 if(rflen != atoi(tok)) {
6612 esl_fatal("Error reading insert file, read consensus length of %d on line %d of file %s, but expected length %d\n", atoi(tok), efp->linenumber, ifile, rflen);
6613 }
6614 }
6615 else if (strncmp(tok, "//", 2) == 0) { /* end of data for an ifile, but we may have concatenated them, so we keep going */
6616 seen_model_name_line = FALSE;
6617 seen_end_of_model_line = TRUE;
6618 }
6619 else { /* should be a seq line with 1+3*n tokens, <seqlen> <rfpos_0> <uapos_0> <nins_0> ... <rfpos_n> <uapos_n> <nins_n>, n can be 0 */
6620 /* determine if we're using this sequence */
6621 i++;
6622 if(useme_keyhash == NULL || (esl_keyhash_Lookup(useme_keyhash, tok, -1, NULL) == eslOK)) {
6623 already_handled_special_spos = FALSE; /* initialize */
6624 prv_e_decrement = prv_e_increment = -1; /* initialize */
6625
6626 if(esl_fileparser_GetTokenOnLine(efp, &tok, NULL) != eslOK) esl_fatal("Error reading insert file, failed to read unaligned length for sequence on line %d of file %s.\n", efp->linenumber, ifile);
6627 seqlen = atoi(tok);
6628
6629 if(esl_fileparser_GetTokenOnLine(efp, &tok, NULL) != eslOK) esl_fatal("Error reading insert file, failed to read first nongap consensus position for sequence on line %d of file %s.\n", efp->linenumber, ifile);
6630 spos = atoi(tok);
6631 if(spos > rflen) esl_fatal("Error reading insert file, read spos of %d that exceeds expected consensus length %d on line %d of file %s.\n", spos, rflen, efp->linenumber, ifile);
6632
6633 if(esl_fileparser_GetTokenOnLine(efp, &tok, NULL) != eslOK) esl_fatal("Error reading insert file, failed to read final nongap consensus position for sequence on line %d of file %s.\n", efp->linenumber, ifile);
6634 epos = atoi(tok);
6635 if(epos > rflen) esl_fatal("Error reading insert file, read epos of %d that exceeds expected consensus length %d on line %d of file %s.\n", epos, rflen, efp->linenumber, ifile);
6636
6637 if(spos == -1 && epos != -1) esl_fatal("insert file is corrupt, spos is -1 but epos is not -1, on line %d\n", efp->linenumber);
6638 if(spos != -1 && epos == -1) esl_fatal("insert file is corrupt, spos is not -1 but epos is -1, on line %d\n", efp->linenumber);
6639
6640 while(esl_fileparser_GetTokenOnLine(efp, &tok, NULL) == eslOK) {
6641 /* rfpos */
6642 rfpos = atoi(tok);
6643 if(rfpos > rflen) esl_fatal("Error reading insert file, read insert info for position %d that exceeds expected consensus length %d on line %d of file %s.\n", rfpos, rflen, efp->linenumber, ifile);
6644
6645 /* uapos */
6646 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, NULL)) != eslOK) esl_fatal("Error reading insert file, didn't read unaligned sequence position for rfpos %d on line %d of file %s.\n", rfpos, efp->linenumber, ifile);
6647 uapos = atoi(tok);
6648 if(uapos > seqlen) esl_fatal("Error reading insert file, read insert info for position %d that exceeds expected sequence length %d on line %d of file %s.\n", rfpos, seqlen, efp->linenumber, ifile);
6649
6650 /* nins */
6651 if((status = esl_fileparser_GetTokenOnLine(efp, &tok, NULL)) != eslOK) esl_fatal("Error reading insert file, didn't read number of inserts for position %d on line %d of file %s.\n", rfpos, efp->linenumber, ifile);
6652 nins = atoi(tok);
6653 nins_ct[rfpos] += nins;
6654 if(per_seq_ins_ct != NULL) per_seq_ins_ct[nseq_stored][rfpos] = nins;
6655 if(nins > 0) nseq_with_ins_ct[rfpos]++; /* nins should always be > 0, but why not check? */
6656
6657 /* check for special cases where a spos_ct and epos_ct read from a de-inserted msa would be incorrect */
6658 if(spos != -1) {
6659 /* Note, if spos == -1 (then epos also == -1, we checked above), then there are 0 nongap consensus positions for
6660 * this sequence, and the spos and epos counts were never incremented for this sequence, so they don't need to be corrected. */
6661
6662 /* First check if this insert is before spos-1 */
6663 if(srfoff_ct != NULL) {
6664 if((rfpos < (spos-1)) && (! already_handled_special_spos)) { /* we've got an insert *after* a position rfpos < (spos-1) for minimal rfpos */
6665 /* Any function that counted the first RF position for this sequence if inserts were
6666 * removed thought it was spos, but an insert exists after rfpos<(spos-1), which means there
6667 * was an insert after rfpos, then a gap in at least 1 consensus position before the
6668 * first nongap consensus nucleotide at cpos.
6669 * NOTE a nasty off-by-one: RF (consensus) positions in the ifile are indexed 1..rflen, whereas srfoff_ct is 0..rflen-1 */
6670 srfoff_ct[(spos-1)]--; /* this position was overcounted */
6671 srfoff_ct[(rfpos-1)+1]++; /* this position was undercounted, we do +1 because insert occured after rfpos */
6672 /* printf("decremented srfoff_ct[%d] for seq %d\n", spos-1, i);
6673 printf("incremented srfoff_ct[%d] for seq %d\n", rfpos-1+1, i);
6674 */
6675 already_handled_special_spos = TRUE; /* if another rfpos is less than (spos-1) we don't care, we already fixed s_rfoff_ct */
6676 }
6677 }
6678
6679 /* Now check if this insert is after epos */
6680 if(erfoff_ct != NULL) {
6681 if(rfpos > epos) { /* we've got an insert *after* a position rfpos > epos */
6682 /* Any function that counted the final RF position for this sequence if inserts were
6683 * removed thought it was epos, but an insert exists after rfpos>epos, which means there
6684 * was a gap in at least one consensus position cpos > epos (cpos <= rfpos) and then inserts
6685 * after rfpos.
6686 * NOTE a nasty off-by-one: RF (consensus) positions in the ifile are indexed 1..rflen, whereas srfoff_ct is 0..rflen-1 */
6687 erfoff_ct[(epos-1)]--; /* this position was overcounted */
6688 erfoff_ct[(rfpos-1)]++; /* this position was undercounted */
6689
6690 /* BUT, be careful, we could have already entered this 'if' for this sequence, if so we need to negate
6691 * the previous time we incremented and decremented.
6692 * NOTE this case and corresponding code only applies to epos (not spos) b/c with spos
6693 * its the first case only that we want to handle (which is why we use the already_handled_special_spos FLAG) */
6694 if(prv_e_decrement != -1 && prv_e_increment != -1) {
6695 erfoff_ct[prv_e_increment]--; /* undo increment with a decrement */
6696 erfoff_ct[prv_e_decrement]++; /* undo decrement with an increment */
6697 prv_e_decrement = (epos-1);
6698 prv_e_increment = (rfpos-1);
6699 }
6700 }
6701 }
6702 }
6703 }
6704 nseq_stored++;
6705 }
6706 nseq_read++;
6707 if(nseq_read > msa_nseq) esl_fatal("Error reading insert file, read info for more sequences than expected (%d) at line %d of file %s.", msa_nseq, efp->linenumber, ifile);
6708 }
6709 }
6710 esl_fileparser_Close(efp);
6711
6712 /* end of file, make sure we read a '//' at the end of it */
6713 if(! seen_end_of_model_line) esl_fatal("Error reading insert file, didn't read the special '//' line at the end of file %s.\n", rfpos, efp->linenumber, ifile);
6714
6715 /* if useme_keyhash != NULL, make sure we read all the seqs we wanted to */
6716 if((useme_keyhash != NULL) && (nseq_stored != nseq2store)) {
6717 esl_fatal("Error reading insert file, wanted to read insert info on %d seqs, but only found %d of them in the insert file %s\n", nseq2store, nseq_stored, ifile);
6718 }
6719 if(nseq_read != msa_nseq) esl_fatal("Error reading insert file, expected to read info on %d seqs, but only found %d in the insert file %s\n", msa_nseq, nseq_read, ifile);
6720
6721 if (ret_nins_ct) *ret_nins_ct = nins_ct; else esl_free(nins_ct);
6722 if (ret_nseq_with_ins_ct) *ret_nseq_with_ins_ct = nseq_with_ins_ct; else esl_free(nseq_with_ins_ct);
6723 if (ret_srfoff_ct) *ret_srfoff_ct = srfoff_ct; else esl_free(srfoff_ct);
6724 if (ret_erfoff_ct) *ret_erfoff_ct = erfoff_ct; else esl_free(erfoff_ct);
6725 if (ret_per_seq_ins_ct) *ret_per_seq_ins_ct = per_seq_ins_ct; else esl_arr2_Destroy((void **) per_seq_ins_ct, msa_nseq);
6726 return;
6727
6728 ERROR:
6729 esl_fatal("Memory allocation error while reading insert file %s.", ifile);
6730 return;; /* NEVERREACHED */
6731 }
6732
6733 /* Function: get_insert_info_from_abc_ct
6734 * Date: EPN, Tue Jan 19 09:32:30 2010
6735 *
6736 * Given an abc_ct array:
6737 * [0..apos..alen-1][0..abc->K]: per position count of
6738 * each symbol in alphabet over all seqs.
6739 *
6740 * Determine the number of sequences with >= 1 inserted
6741 * nucleotides after each RF position.
6742 *
6743 * IMPORTANT NOTE: This is done based on the assumption that inserts
6744 * have been systematically placed in the MSA by a program like
6745 * Infernal or HMMER and that the maximum number of nongap nucleotides in
6746 * any insert position after rfpos is the number of sequences with >=1
6747 * insert nucleotides after rfpos. However, if the alignments were not
6748 * generated from HMMER nor Infernal, or have been modified, the
6749 * inserts may not follow this rule (the rule, in other words is that
6750 * there is always 1 insert column after each nongap RF position rfpos
6751 * that includes a nucleotide from all sequences that have >= 1 inserted
6752 * nucleotides after rfpos).
6753 *
6754 * abc_ct - [0..apos..msa_alen-1][0..abc->K] count of each nucleotide in each position
6755 * abc - the alphabet
6756 * msa_rf - msa->rf, the reference annotation
6757 * msa->alen - length of alignment
6758 * rflen - expected nongap RF length (consensus length)
6759 * ret_nseq_with_ins_ct - [0..rflen] number of sequences with >= 1
6760 * insert after each position. NULL if unwanted.
6761 * ret_nins_ct - [0..rflen] total number of inserted nucleotides (over
6762 * all sequences after each position. NULL if unwanted.
6763 *
6764 * Returns void. Dies with an informative error message upon an error.
6765 */
6766 void
6767 get_insert_info_from_abc_ct(double **abc_ct, ESL_ALPHABET *abc, char *msa_rf, int64_t msa_alen, int rflen, int **ret_nseq_with_ins_ct, int **ret_nins_ct)
6768 {
6769 int status;
6770 int *nins_ct;
6771 int *nseq_with_ins_ct;
6772 int nins, nmaxins;
6773 int rfpos, apos;
6774
6775 /* allocate and initialize */
6776 ESL_ALLOC(nseq_with_ins_ct, sizeof(int) * (rflen+1));
6777 esl_vec_ISet(nseq_with_ins_ct, rflen+1, 0);
6778 ESL_ALLOC(nins_ct, sizeof(int) * (rflen+1));
6779 esl_vec_ISet(nins_ct, rflen+1, 0);
6780
6781 nmaxins = 0;
6782 rfpos = 0;
6783 for(apos = 0; apos < msa_alen; apos++) {
6784 if(esl_abc_CIsResidue(abc, msa_rf[apos])) {
6785 nseq_with_ins_ct[rfpos] = nmaxins;
6786 nmaxins = 0;
6787 rfpos++;
6788 if(rfpos > rflen) esl_fatal("Error in get_insert_info_from_abc_ct(), expected consensus length (%d) is incorrect.");
6789 }
6790 else {
6791 nins = (int) esl_vec_DSum(abc_ct[apos], abc->K);
6792 nins_ct[rfpos] += nins;
6793 nmaxins = ESL_MAX(nmaxins, nins);
6794 }
6795 }
6796 /* get max_nseq with inserts after the final position */
6797 nseq_with_ins_ct[rfpos] = nmaxins;
6798
6799 if(ret_nins_ct != NULL) *ret_nins_ct = nins_ct;
6800 else free(nins_ct);
6801 if(ret_nseq_with_ins_ct != NULL) *ret_nseq_with_ins_ct = nseq_with_ins_ct;
6802 else free(nseq_with_ins_ct);
6803
6804 return;
6805
6806 ERROR:
6807 esl_fatal("Error in get_insert_info_from_abc_ct(), memory allocation error.");
6808 return; /* NEVERREACHED */
6809 }
6810
6811 /* get_pp_idx
6812 *
6813 * Given a #=GR PP or #=GC PP_cons character, return the appropriate index
6814 * in a pp_ct[] vector.
6815 * '0' return 0;
6816 * '1' return 1;
6817 * '2' return 2;
6818 * '3' return 3;
6819 * '4' return 4;
6820 * '5' return 5;
6821 * '6' return 6;
6822 * '7' return 7;
6823 * '8' return 8;
6824 * '9' return 9;
6825 * '*' return 10;
6826 * gap return 11;
6827 *
6828 * Anything else (including missing or nonnucleotide) return -1;
6829 */
6830 int
6831 get_pp_idx(const ESL_ALPHABET *abc, char ppchar)
6832 {
6833 if(esl_abc_CIsGap(abc, ppchar)) return 11;
6834 if(ppchar == '*') return 10;
6835 if(ppchar == '9') return 9;
6836 if(ppchar == '8') return 8;
6837 if(ppchar == '7') return 7;
6838 if(ppchar == '6') return 6;
6839 if(ppchar == '5') return 5;
6840 if(ppchar == '4') return 4;
6841 if(ppchar == '3') return 3;
6842 if(ppchar == '2') return 2;
6843 if(ppchar == '1') return 1;
6844 if(ppchar == '0') return 0;
6845 return -1;
6846 }
6847
6848 /* spos_and_epos2span_ct
6849 *
6850 * Given two arrays, spos_ct[0..apos..alen-1] and epos_ct[0..apos..alen-1],
6851 * specifying the number of sequences for which the first and last non-gap
6852 * position is apos, calculate the number of sequences that 'span' each RF
6853 * position. A sequence spans position rfpos which is actually alignment
6854 * position apos, it has at least one nongap nucleotide in a position x <= apos,
6855 * and at least one nucleotide in a position y, y >= apos.
6856 *
6857 * As a special case, if alen == rflen and srfoff_ct != NULL and erfoff_ct != NULL,
6858 * then we've read an insert file (with --ifile) which has given us information
6859 * that we'll use to update spos_ct and epos_ct. In this case, the inserts
6860 * have been removed from the alignment but it is possible that spos_ct and
6861 * epos_ct have miscounted some positions (see get_insert_info_from_ifile()
6862 * for more info).
6863 *
6864 * span_ct: [0..rfpos..ps->msa->rflen-1] number of sequences that 'span' each position rfpos
6865 * have >= 1 nucleotide at a position apos before rf2a_map[rfpos] and >= 1 nucleotide at
6866 * any position apos >= rf2a_map[rfpos],
6867 */
6868 int
6869 get_span_ct(int *msa_rf2a_map, int64_t alen, int rflen, int nseq, int *spos_ct, int *epos_ct, int *srfoff_ct, int *erfoff_ct, int **ret_span_ct)
6870 {
6871 int status;
6872 int *nseq_start_after_rfpos = NULL;
6873 int *nseq_end_before_rfpos = NULL;
6874 int *span_ct = NULL;
6875 int rfpos;
6876 int do_correction;
6877 int apos, nxt_apos, prv_apos;
6878
6879 ESL_ALLOC(span_ct, sizeof(int) * rflen);
6880 ESL_ALLOC(nseq_start_after_rfpos, sizeof(int) * rflen);
6881 ESL_ALLOC(nseq_end_before_rfpos, sizeof(int) * rflen);
6882
6883 esl_vec_ISet(nseq_start_after_rfpos, rflen, 0);
6884 esl_vec_ISet(nseq_end_before_rfpos, rflen, 0);
6885
6886 /* check for special case, when we need to update spos_ct and epos_ct */
6887 if(srfoff_ct != NULL && erfoff_ct == NULL) esl_fatal("Internal error, get_span_ct: srfoff_ct != NULL and erfoff_ct == NULL");
6888 if(srfoff_ct == NULL && erfoff_ct != NULL) esl_fatal("Internal error, get_span_ct: srfoff_ct == NULL and erfoff_ct != NULL");
6889
6890 /* determine if we should correct spos_ct and epos_ct using srfoff_ct and erfoff_ct */
6891 do_correction = (alen == rflen && srfoff_ct != NULL) ? TRUE : FALSE;
6892
6893 /* NOTE: if alen == rflen spos_ct and epos_ct have length rflen, just like srfoff_ct and erfoff_ct */
6894
6895 /* first count number of seqs that start after each position */
6896 nseq_start_after_rfpos[rflen-1] = 0; /* initialize */
6897 nxt_apos = (int) alen - 1;
6898 for(rfpos = rflen-2; rfpos >= 0; rfpos--) {
6899 for(apos = nxt_apos; apos > msa_rf2a_map[rfpos]; apos--) {
6900 nseq_start_after_rfpos[rfpos] += spos_ct[apos];
6901 if(do_correction) nseq_start_after_rfpos[rfpos] += srfoff_ct[apos];
6902 }
6903 nseq_start_after_rfpos[rfpos] += nseq_start_after_rfpos[rfpos+1];
6904 nxt_apos = msa_rf2a_map[rfpos];
6905 }
6906
6907 /* count number of seqs that end before each position */
6908 nseq_end_before_rfpos[0] = 0; /* initialize */
6909 prv_apos = 0;
6910 for(rfpos = 1; rfpos < rflen; rfpos++) {
6911 for(apos = prv_apos; apos < msa_rf2a_map[rfpos]; apos++) {
6912 nseq_end_before_rfpos[rfpos] += epos_ct[apos];
6913 if(do_correction) nseq_end_before_rfpos[rfpos] += erfoff_ct[apos];
6914 }
6915 nseq_end_before_rfpos[rfpos] += nseq_end_before_rfpos[rfpos-1];
6916 prv_apos = msa_rf2a_map[rfpos];
6917 }
6918
6919 /* We now know how many seqs start after each rfpos (a = nseq_start_after_rfpos[rfpos]) and
6920 * how many seqs end before each rfpos (b = nseq_end_before_rfpos[rfpos])
6921 * so c = [nseq-(a+b)] seqs span rfpos (b/c they don't start after it AND don't end before it).
6922 *
6923 * Note: this is really confusing to me, but I've convinced myself it's true, and I think
6924 * it is only true because we know each sequence's end position must be >= its start position
6925 */
6926 for(rfpos = 0; rfpos < rflen; rfpos++) {
6927 span_ct[rfpos] = nseq - (nseq_start_after_rfpos[rfpos] + nseq_end_before_rfpos[rfpos]);
6928 /*printf("span_ct[rfpos: %d]: %d after: %d before: %d nseq: %d\n", rfpos, span_ct[rfpos], nseq_start_after_rfpos[rfpos], nseq_end_before_rfpos[rfpos], nseq);*/
6929 }
6930
6931 *ret_span_ct = span_ct;
6932 free(nseq_start_after_rfpos);
6933 free(nseq_end_before_rfpos);
6934 return eslOK;
6935
6936 ERROR:
6937 return eslEMEM;
6938 }
6939
6940
6941 /* Function: drawfile2sspostscript()
6942 *
6943 * Purpose: Fill a postscript data structure with >= 1 new page(s), with colors described
6944 * in an input 'draw' file, with >= 1 sets of <x> lines of data, each set
6945 * is separated by a line with only "//". <x> must be equal to the consensus
6946 * ps->rflen. Each line contains a single real number between 0 and 1,
6947 * these are converted into 1 of 6 CMYK colors based on their values, using
6948 * the same color scheme used for frequency of inserts.
6949 *
6950 * Return: eslOK on success.
6951 */
6952 static int
6953 drawfile2sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps, float ***hc_scheme, int hc_scheme_idx, int hc_nbins)
6954 {
6955 int status;
6956 int p, pp;
6957 int rfpos, c;
6958 int orig_npage = ps->npage;
6959 ESL_FILEPARSER *efp;
6960 char *s;
6961 char *dfile = esl_opt_GetString(go, "--dfile");
6962 float *limits;
6963 float value;
6964 int l;
6965 int bi, within_mask;
6966 char *desc = NULL;
6967 char *legheader = NULL;
6968
6969 /* allocate for limits, but don't fill it yet */
6970 ESL_ALLOC(limits, sizeof(float) * (hc_nbins+1));
6971
6972 if (esl_fileparser_Open(dfile, NULL, &efp) != eslOK) ESL_FAIL(eslFAIL, errbuf, "failed to open %s in draw_file2sspostscript\n", dfile);
6973 esl_fileparser_SetCommentChar(efp, '#');
6974
6975 pp = orig_npage - 1;
6976
6977 /* Format of dfile:
6978 * For each page:
6979 * line 1: description of page (max is ps->desc_max_chars*2 chars)
6980 * line 2: header for legend (max is ps->legx_max_chars chars)
6981 * line 3: limits for color bins, must be 7 tokens, all numbers, each greater than the last
6982 * lines 4-N: single tokens, numerical values for each position, N is CLEN-3.
6983 * line N+1: single token, only:"//\n" signifying end of page.
6984 *
6985 * '#' prefixed lines are considered comments and skipped.
6986 *
6987 * Example:
6988 */
6989
6990 rfpos = -1;
6991 while (esl_fileparser_NextLine(efp) == eslOK)
6992 {
6993 if(rfpos == -1) { /* new page, first add a new page */
6994 /* next 3 lines must be a specific format */
6995 /* first line is description */
6996 if(desc != NULL) { free(desc); desc = NULL; }
6997 while((status = esl_fileparser_GetTokenOnLine(efp, &s, NULL)) == eslOK) {
6998 if((status = esl_strcat(&desc, -1, s, -1)) != eslOK) esl_fatal("Out of memory");
6999 if((status = esl_strcat(&desc, -1, " ", -1)) != eslOK) esl_fatal("Out of memory");
7000 }
7001 if(strlen(desc) > (ps->desc_max_chars*2.)) esl_fatal("Error reading --dfile, description length (%d) exceeds max allowed (%d)", strlen(desc), (ps->desc_max_chars*2));
7002 if(esl_fileparser_NextLine(efp) != eslOK) esl_fatal("Error reading --dfile, expected legend header line at line %d\n", efp->linenumber);
7003
7004 /* second line is legend header */
7005 if(legheader != NULL) { free(legheader); legheader = NULL; }
7006 while((status = esl_fileparser_GetTokenOnLine(efp, &s, NULL)) == eslOK) {
7007 if((status = esl_strcat(&legheader, -1, s, -1)) != eslOK) esl_fatal("Out of memory");
7008 if((status = esl_strcat(&legheader, -1, " ", -1)) != eslOK) esl_fatal("Out of memory");
7009 }
7010 if(strlen(legheader) > ps->legx_max_chars) esl_fatal("Error reading --dfile, legend header length (%d) exceeds max allowed (%d)", strlen(legheader), ps->legx_max_chars);
7011 if(esl_fileparser_NextLine(efp) != eslOK) esl_fatal("Error reading --dfile, expected limits line at line %d\n", efp->linenumber);
7012
7013 /* third line is bin limits for the colors, must be 7 numbers, we read them as floats */
7014 for(l = 0; l < hc_nbins+1; l++) {
7015 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK) esl_fatal("Error reading --dfile, expected limits line at line %d to have %d limits (numbers) in increasing order, it doesn't", efp->linenumber, hc_nbins+1);
7016 limits[l] = atof(s);
7017 if(l > 0 && limits[l] < limits[l-1]) esl_fatal("Error reading --dfile, expected limits line at line %d with %d limits (numbers) in increasing order", efp->linenumber, hc_nbins+1);
7018 }
7019 rfpos++; /* rfpos will now be 0 */
7020 }
7021 else if(rfpos == (ps->rflen)) { /* end of page, should be a single token, a "\\" on this line */
7022 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7023 esl_fatal("Failed to read a final token at the end of description of page %d on line %d of drawfile %s\n", (pp - orig_npage + 1), efp->linenumber, dfile);
7024 if (strcmp(s, "//") != 0)
7025 esl_fatal("Failed to read a final \"//\" token (read %s) at the end of description of draw page %d on line %d of drawfile %s\n", s, (pp - orig_npage + 1), efp->linenumber, dfile);
7026 rfpos = -1;
7027 /* add color legend */
7028 if((status = add_text_to_scheme_colorlegend(ps, ps->sclAA[pp], legheader, ps->legx_max_chars, errbuf)) != eslOK) return status;
7029 if((status = add_page_desc_to_sspostscript(ps, ps->npage-1, desc, errbuf)) != eslOK) return status;
7030 }
7031 else { /* a normal line, should either contain a single float or the \\ marking end of this page */
7032 rfpos++;
7033 if(rfpos == 1) { /* add a new page, (we now have limits for legend, from if(rfpos == -1) loop above) */
7034 if((status = add_pages_sspostscript(ps, 1, ALIMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
7035 for(p = (ps->npage-1); p < ps->npage; p++) {
7036 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
7037 ESL_ALLOC(ps->sclAA[p], sizeof(SchemeColorLegend_t) * 1);
7038 for(c = 0; c < ps->rflen; c++) {
7039 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
7040 }
7041 }
7042 pp++; /* if first page, pp == orig_npage now */
7043 if(ps->sclAA[pp]!= NULL){ // prevent memory leak
7044 free(ps->sclAA[pp]);
7045 }
7046 ps->sclAA[pp] = create_scheme_colorlegend(hc_scheme_idx, hc_nbins, limits, FALSE, TRUE, TRUE, TRUE);
7047 }
7048 /* now parse the line, it should have a single number, a numerical value for a position */
7049 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK) esl_fatal("Failed to read value for position %d for page %d on line %d of dfile\n", rfpos, (pp - orig_npage + 1), efp->linenumber);
7050 value = atof(s);
7051 if(value < limits[0] || value > limits[hc_nbins]) esl_fatal("--dfile value %.4f out of allowed range [%.3f-%.3f] on line %d\n", value, limits[0], limits[hc_nbins], efp->linenumber, dfile);
7052 within_mask = (ps->mask != NULL && ps->mask[rfpos-1] == '1') ? TRUE : FALSE;
7053 if((status = set_scheme_values(errbuf, ps->bcolAAA[pp][rfpos-1], NCMYK, hc_scheme[hc_scheme_idx], value, ps->sclAA[pp], within_mask, &bi)) != eslOK) return status;
7054 }
7055 }
7056 if(pp == (orig_npage - 1)) { /* no new pages were read, this is an error */
7057 esl_fatal("Failed to read a single page from drawfile %s\n", dfile);
7058 }
7059 esl_fileparser_Close(efp);
7060
7061 free(limits);
7062 if(desc != NULL) { free(desc); desc = NULL; }
7063 if(legheader != NULL) { free(legheader); legheader = NULL; }
7064
7065 return eslOK;
7066
7067 ERROR: ESL_FAIL(status, errbuf, "drawfile2sspostscript(): memory allocation error.");
7068 return status; /* NEVERREACHED */
7069 }
7070
7071
7072 /* Function: expertfile2sspostscript()
7073 *
7074 * Purpose: Fill a postscript data structure with >= 1 new page(s), with colors described
7075 * in an input 'expert draw' file, with >= 1 sets of <x> lines of data, each set
7076 * is separated by a line with only "//". <x> must be equal to the consensus
7077 * ps->rflen. Each line has at least 4 floats explaining
7078 * the CMYK values for the color to use at each position of the SS diagram,
7079 * and optionally contains an extra single character which is the nucleotide
7080 * to put at that position.
7081 *
7082 * Return: eslOK on success.
7083 */
7084 static int
7085 expertfile2sspostscript(const ESL_GETOPTS *go, char *errbuf, SSPostscript_t *ps)
7086 {
7087 int status;
7088 int p, pp;
7089 int cpos, c;
7090 int orig_npage = ps->npage;
7091 ESL_FILEPARSER *efp;
7092 char *s;
7093 char *efile = esl_opt_GetString(go, "--efile");
7094
7095 if (esl_fileparser_Open(efile, NULL, &efp) != eslOK) ESL_FAIL(eslFAIL, errbuf, "failed to open %s in draw_file2sspostscript\n", efile);
7096 esl_fileparser_SetCommentChar(efp, '#');
7097
7098 pp = orig_npage - 1;
7099 cpos = 0;
7100
7101 while (esl_fileparser_NextLine(efp) == eslOK)
7102 {
7103 /* example line without nucleotide markup:
7104 * 0.000 0.000 0.000 0.500
7105 *
7106 * example line with nucleotide markup:
7107 * 0.000 0.000 0.000 0.500 A
7108 */
7109
7110 cpos++;
7111 if(cpos == 1) { /* add a new page */
7112 if((status = add_pages_sspostscript(ps, 1, SIMPLEMASKMODE)) != eslOK) ESL_FAIL(status, errbuf, "memory error adding pages to the postscript object.");
7113
7114 for(p = (ps->npage-1); p < ps->npage; p++) {
7115 ESL_ALLOC(ps->rAA[p], sizeof(char) * (ps->rflen+1));
7116 ESL_ALLOC(ps->bcolAAA[p], sizeof(float *) * ps->rflen);
7117 for(c = 0; c < ps->rflen; c++) {
7118 ESL_ALLOC(ps->bcolAAA[p][c], sizeof(float) * NCMYK); /* CMYK colors */
7119 }
7120 }
7121 pp++; /* if first page, pp == orig_npage now */
7122 }
7123 if(cpos == (ps->rflen+1)) { /* should be a single token, a "\\" on this line */
7124 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7125 esl_fatal("Failed to read a final token at the end of description of draw page %d on line %d of expertfile %s\n", (pp - orig_npage + 1), efp->linenumber, efile);
7126 if (strcmp(s, "//") != 0)
7127 esl_fatal("Failed to read a final \"//\" token (read %s) at the end of description of draw page %d on line %d of expertfile %s\n", s, (pp - orig_npage + 1), efp->linenumber, efile);
7128 cpos = 0;
7129 }
7130 else {
7131 /* get C value */
7132 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7133 esl_fatal("Failed to read C of CMYK value on line %d of expertfile %s\n", efp->linenumber, efile);
7134 ps->bcolAAA[pp][(cpos-1)][0] = atof(s);
7135
7136 /* get M value */
7137 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7138 esl_fatal("Failed to read M of CMYK value on line %d of expertfile %s\n", efp->linenumber, efile);
7139 ps->bcolAAA[pp][(cpos-1)][1] = atof(s);
7140
7141 /* get Y value */
7142 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7143 esl_fatal("Failed to read Y of CMYK value on line %d of expertfile %s\n", efp->linenumber, efile);
7144 ps->bcolAAA[pp][(cpos-1)][2] = atof(s);
7145
7146 /* get K value */
7147 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) != eslOK)
7148 esl_fatal("Failed to read K of CMYK value on line %d of expertfile %s\n", efp->linenumber, efile);
7149 ps->bcolAAA[pp][(cpos-1)][3] = atof(s);
7150
7151 /* optionally read a nucleotide value */
7152 if (esl_fileparser_GetTokenOnLine(efp, &s, NULL) == eslOK) {
7153 if(((int) strlen(s)) != 1) esl_fatal("Read multi-character string (%s) for consensus nucleotide %d on line %d of expertfile %s\n", s, cpos, efp->linenumber, efile);
7154 ps->rAA[pp][(cpos-1)] = s[0];
7155 }
7156 else ps->rAA[pp][(cpos-1)] = ' ';
7157 }
7158 }
7159 if(pp == (orig_npage - 1)) { /* no new pages were read, this is an error */
7160 esl_fatal("Failed to read a single page from expertfile %s\n", efile);
7161 }
7162
7163 esl_fileparser_Close(efp);
7164 return eslOK;
7165
7166 ERROR: ESL_FAIL(status, errbuf, "expertfile2sspostscript(): memory allocation error.");
7167 return status; /* NEVERREACHED */
7168 }
7169
7170
7171 /* is_watson_crick_bp(i,j): return TRUE if i,j form a watson crick bp, FALSE if not
7172 * is_gu_or_ug_bp(i,j): return TRUE if i,j form a G-U or U-G bp, FALSE if not
7173 */
7174 static int
7175 is_watson_crick_bp(char i, char j)
7176 {
7177 switch (toupper(i)) {
7178 case 'A':
7179 switch (toupper(j)) {
7180 case 'U': return TRUE; break;
7181 case 'T': return TRUE; break;
7182 default: break;
7183 }
7184 break;
7185 case 'C':
7186 switch (toupper(j)) {
7187 case 'G': return TRUE; break;
7188 default: break;
7189 }
7190 break;
7191 case 'G':
7192 switch (toupper(j)) {
7193 case 'C': return TRUE; break;
7194 default: break;
7195 }
7196 break;
7197 case 'U':
7198 switch (toupper(j)) {
7199 case 'A': return TRUE; break;
7200 default: break;
7201 }
7202 break;
7203 case 'T':
7204 switch (toupper(j)) {
7205 case 'A': return TRUE; break;
7206 default: break;
7207 }
7208 break;
7209 default: break;
7210 }
7211
7212 return FALSE;
7213 }
7214
7215 static int
7216 is_gu_or_ug_bp(char i, char j)
7217 {
7218 switch (toupper(i)) {
7219 case 'G':
7220 switch (toupper(j)) {
7221 case 'U': return TRUE; break;
7222 case 'T': return TRUE; break;
7223 default: break;
7224 }
7225 break;
7226 case 'U':
7227 switch (toupper(j)) {
7228 case 'G': return TRUE; break;
7229 default: break;
7230 }
7231 break;
7232 case 'T':
7233 switch (toupper(j)) {
7234 case 'G': return TRUE; break;
7235 default: break;
7236 }
7237 break;
7238 default: break;
7239 }
7240
7241 return FALSE;
7242 }
7243
7244 /* Function: get_consensus_seqs_from_abc_ct()
7245 * Date: EPN, Wed Apr 7 07:39:57 2010
7246 *
7247 * Given an abc_ct array:
7248 * [0..apos..alen-1][0..abc->K]: per position count of
7249 * each symbol in alphabet over all seqs.
7250 *
7251 * Determine the consensus sequence for the msa that abc_ct
7252 * corresponds to, one nucleotide per nongap RF position, the
7253 * most informative IUPAC nucleotide (including ambiguous nucleotides)
7254 * that represents >= <x> fraction of the sequences in the msa
7255 * that have a nongap at that position.
7256 *
7257 * Alternatively, if --majrule is enabled, define consensus nucleotide
7258 * as the most common nucleotide at each position. Uppercase
7259 * if frequency >= 0.5 of nongap nts, else it is lowercase.
7260 *
7261 * ps - the postscript object
7262 * errbuf - informative error message, returned upon failure
7263 * abc_ct - count of nucleotides per alignment position (see above)
7264 * abc - the alphabet
7265 * cthresh - consensus nucleotide must explain >= <cthresh> fraction
7266 * of nucleotides in msa (ex: if cthresh = 0.8, and
7267 * A=0.6, C=0.01, G=0.25, U=0.14, consensus nucleotide = R (A|G))
7268 * ret_cseq - RETURN: the consensus sequence [0..rflen-1]
7269 *
7270 * Returns eslOK on success.
7271 */
7272 typedef struct nt2sort_s {
7273 double ntfreq; /* frequency of this nucleotide */
7274 int ntidx; /* index of this nucleotide in alphabet (ex: 1 for 'C' in eslRNA) */
7275 } nt2sort_t;
7276
7277
7278 int
7279 compare_by_ntfreq(const void *a_void, const void *b_void)
7280 {
7281 nt2sort_t *a, *b;
7282
7283 a = (nt2sort_t *) a_void;
7284 b = (nt2sort_t *) b_void;
7285
7286 if (a->ntfreq > b->ntfreq) return -1;
7287 else if (a->ntfreq < b->ntfreq) return 1;
7288 else return 0;
7289 }
7290
7291
7292
7293 /* Function: get_consensus_nucleotide()
7294 * Date: EPN, Wed Apr 7 07:39:57 2010
7295 *
7296 * Given a useme array:
7297 * [0..i..abc->K]: TRUE if nucleotide i is used, FALSE if not
7298 * Return the consensus nucleotide - the least ambiguous iupac
7299 * nucleotide that represents all TRUEs. For ex: [1,0,1,0] --> R
7300 * If <do_dna> is TRUE, return 'T' instead of 'U' when appropriate.
7301 *
7302 * Returns the consensus nucleotide.
7303 *
7304 * Degeneracies:
7305 * R = A|G
7306 * Y = C|U
7307 * M = A|C
7308 * K = G|U
7309 * S = C|G
7310 * W = A|U
7311 * H = A|C|U
7312 * B = C|G|U
7313 * V = A|C|G
7314 * D = A|G|U
7315 */
7316 int get_consensus_nucleotide(int *useme, int K)
7317 {
7318 /* You brute! */
7319 if((! useme[0]) && (! useme[1]) && (! useme[2]) && ( useme[3])) { return 'U'; } /* 0001 */
7320 if((! useme[0]) && (! useme[1]) && ( useme[2]) && (! useme[3])) { return 'G'; } /* 0010 */
7321 if((! useme[0]) && ( useme[1]) && (! useme[2]) && (! useme[3])) { return 'C'; } /* 0100 */
7322 if(( useme[0]) && (! useme[1]) && (! useme[2]) && (! useme[3])) { return 'A'; } /* 1000 */
7323
7324 if(( useme[0]) && ( useme[1]) && ( useme[2]) && ( useme[3])) { return 'N'; } /* 1111 */
7325
7326 if((! useme[0]) && ( useme[1]) && ( useme[2]) && ( useme[3])) { return 'B'; } /* 0111 */
7327 if(( useme[0]) && (! useme[1]) && ( useme[2]) && ( useme[3])) { return 'D'; } /* 1011 */
7328 if(( useme[0]) && ( useme[1]) && ( useme[2]) && (! useme[3])) { return 'V'; } /* 1110 */
7329 if(( useme[0]) && ( useme[1]) && (! useme[2]) && ( useme[3])) { return 'H'; } /* 1101 */
7330
7331 if((! useme[0]) && (! useme[1]) && ( useme[2]) && ( useme[3])) { return 'K'; } /* 0011 */
7332 if((! useme[0]) && ( useme[1]) && ( useme[2]) && (! useme[3])) { return 'S'; } /* 0110 */
7333 if(( useme[0]) && ( useme[1]) && (! useme[2]) && (! useme[3])) { return 'M'; } /* 1100 */
7334 if((! useme[0]) && ( useme[1]) && (! useme[2]) && ( useme[3])) { return 'Y'; } /* 0101 */
7335 if(( useme[0]) && (! useme[1]) && (! useme[2]) && ( useme[3])) { return 'W'; } /* 1001 */
7336 if(( useme[0]) && (! useme[1]) && ( useme[2]) && (! useme[3])) { return 'R'; } /* 1010 */
7337
7338 if((! useme[0]) && (! useme[1]) && (! useme[2]) && (! useme[3])) { return '-'; } /* 0000 */
7339 esl_fatal("coding error in consensus_nucleotide");
7340 return ' '; /* NEVERREACHED */
7341 }
7342
7343 int
7344 get_consensus_seqs_from_abc_ct(const ESL_GETOPTS *go, SSPostscript_t *ps, char *errbuf, double **abc_ct, ESL_ALPHABET *abc, int64_t msa_alen)
7345 {
7346 int status; /* the Easel return status */
7347 char *cseq_maj = NULL; /* the consensus sequence string based on majority rule that we're building */
7348 char *cseq_amb = NULL; /* the consensus sequence string including ambiguities that we're building */
7349 int rfpos, apos; /* counter over nongap RF, alignment positions */
7350 float nongap_freq; /* for current position, fraction of nongap nucleotides from abc_ct */
7351 float covered; /* fraction of nongap nucleotides currently covered while
7352 * calculating the consensus nucleotide */
7353 int *nt_is_used; /* [0..a..abc->K-1] TRUE if nucleotide a is included for
7354 * current consensus nucleotide, FALSE if not */
7355 nt2sort_t *sorted_ntfreq; /* [0..abc->K-1] sorted array of nt2sort structures */
7356 int sorted_idx; /* index in sorted_ntfreq */
7357 int a; /* counter over nucleotides in abc */
7358 float cthresh = esl_opt_GetReal(go, "--cthresh");
7359 float athresh = esl_opt_GetReal(go, "--athresh");
7360
7361 /* allocate and initialize */
7362 ESL_ALLOC(cseq_maj, sizeof(char) * (ps->rflen+1));
7363 ESL_ALLOC(cseq_amb, sizeof(char) * (ps->rflen+1));
7364 cseq_maj[ps->rflen] = '\0';
7365 cseq_amb[ps->rflen] = '\0';
7366 ESL_ALLOC(sorted_ntfreq, sizeof(nt2sort_t) * (abc->K));
7367 ESL_ALLOC(nt_is_used, sizeof(int) * (abc->K));
7368
7369 rfpos = 0;
7370 for(apos = 0; apos < msa_alen; apos++) {
7371 if(esl_abc_CIsResidue(abc, ps->msa->rf[apos])) {
7372 nongap_freq = esl_vec_DSum(abc_ct[apos], abc->K);
7373 if(esl_DCompare_old(nongap_freq, 0., eslSMALLX1) == eslOK) { /* all nucleotides are gaps */
7374 cseq_maj[rfpos] = cseq_amb[rfpos] = '-';
7375 }
7376 else { /* at least one sequence has a nongap at rfpos, calculate the consensus nucleotide */
7377 for(a = 0; a < abc->K; a++) {
7378 sorted_ntfreq[a].ntfreq = abc_ct[apos][a] / nongap_freq;
7379 sorted_ntfreq[a].ntidx = a;
7380 }
7381 /* quicksort */
7382 qsort(sorted_ntfreq, abc->K, sizeof(nt2sort_t), compare_by_ntfreq);
7383 covered = 0.;
7384 esl_vec_ISet(nt_is_used, abc->K, FALSE);
7385 sorted_idx = 0;
7386 /* determine majority rule nucleotide */
7387 cseq_maj[rfpos] = (sorted_ntfreq[sorted_idx].ntfreq >= cthresh) ?
7388 toupper(abc->sym[sorted_ntfreq[sorted_idx].ntidx]) :
7389 tolower(abc->sym[sorted_ntfreq[sorted_idx].ntidx]);
7390 /* determine ambiguous nucleotide that covers > --athresh fraction of seqs */
7391 while(covered < athresh) {
7392 nt_is_used[sorted_ntfreq[sorted_idx].ntidx] = TRUE;
7393 covered += sorted_ntfreq[sorted_idx].ntfreq;
7394 /*printf("RF: %4d SIDX: %d AIDX: %d FREQ: %.3f COVERED: %.3f\n",
7395 rfpos, sorted_idx,
7396 sorted_ntfreq[sorted_idx].ntidx,
7397 sorted_ntfreq[sorted_idx].ntfreq,
7398 covered);*/
7399 sorted_idx++;
7400 }
7401 cseq_amb[rfpos] = get_consensus_nucleotide(nt_is_used, abc->K);
7402 }
7403 rfpos++;
7404 }
7405 }
7406 /* printf("\n"); */
7407 ps->msa_cseq_maj = cseq_maj;
7408 ps->msa_cseq_amb = cseq_amb;
7409
7410 free(sorted_ntfreq);
7411 free(nt_is_used);
7412
7413 return eslOK;
7414
7415 ERROR:
7416 ESL_FAIL(eslEMEM, errbuf, "Ran out of memory in get_consensus_seqs_from_abc_ct.");
7417 return eslEMEM; /* NEVERREACHED */
7418 }
7419
7420 /* Function: compare_two_cmyk_colors()
7421 * Date: EPN, Fri Apr 23 05:47:54 2010
7422 *
7423 * Given four CMYK values defining a color <a>
7424 * and four CMYK values defining a color <b>, check if
7425 * <a> and <b> are the same color. Return TRUE if they
7426 * are and FALSE if not.
7427 *
7428 * acol_C - first CMYK value for color <a> (Cyan)
7429 * acol_M - second CMYK value for color <a> (Magenta)
7430 * acol_Y - third CMYK value for color <a> (Yellow)
7431 * acol_K - fourth CMYK value for color <a> (Key)
7432 * bcol_C - first CMYK value for color <b> (Cyan)
7433 * bcol_M - second CMYK value for color <b> (Magenta)
7434 * bcol_Y - third CMYK value for color <b> (Yellow)
7435 * bcol_K - fourth CMYK value for color <b> (Key)
7436 *
7437 * Returns TRUE if colors a and b are identical, FALSE if not.
7438 */
7439 int compare_two_cmyk_colors(float acol_C, float acol_M, float acol_Y, float acol_K, float bcol_C, float bcol_M, float bcol_Y, float bcol_K)
7440 {
7441 if(esl_FCompare_old(acol_C, bcol_C, eslSMALLX1) != eslOK) return FALSE;
7442 if(esl_FCompare_old(acol_M, bcol_M, eslSMALLX1) != eslOK) return FALSE;
7443 if(esl_FCompare_old(acol_Y, bcol_Y, eslSMALLX1) != eslOK) return FALSE;
7444 if(esl_FCompare_old(acol_K, bcol_K, eslSMALLX1) != eslOK) return FALSE;
7445 return TRUE;
7446 }
7447
7448
7449 /* Function: define_outline_procedures()
7450 * Date: EPN, Sun May 2 10:36:32 2010
7451 *
7452 * Given an open output postscript file, define
7453 * the three procedures for drawing outlines of
7454 * cells.
7455 *
7456 * Returns void.
7457 */
7458 void define_outline_procedure(FILE *fp)
7459 {
7460 fprintf(fp, "\n/%s %% stack: x y cellsize linewidth (empty)\n", OUTLINE_PROCEDURE);
7461 fprintf(fp, "{\n");;
7462 fprintf(fp, " 0. 0. 0. 1. setcmykcolor\n");/* set linewidth as top value on stack */
7463 fprintf(fp, " setlinewidth\n"); /* set linewidth as top value on stack */
7464 fprintf(fp, " /cellsize exch def\n"); /* push /cellsize onto stack, invert top 2 els, and define cellsize */
7465 fprintf(fp, " newpath moveto\n"); /* move to x, y, which are now on top of stack */
7466 fprintf(fp, " currentlinewidth 2. div currentlinewidth 2. div rmoveto\n");
7467 fprintf(fp, " 0 cellsize currentlinewidth sub rlineto\n");
7468 fprintf(fp, " cellsize currentlinewidth sub 0 rlineto\n");
7469 fprintf(fp, " 0 cellsize currentlinewidth sub -1 mul rlineto\n");
7470 fprintf(fp, " closepath stroke\n");
7471 fprintf(fp, "} def\n\n");
7472
7473 /* Old definitions, kept here for reference, note these only work with OUTLINE_LINEWIDTH of 1.0 */
7474 /*
7475
7476 //printing basic box outline
7477 fprintf(fp, "\n/%s %% stack: x y => ---\n", OUTLINE_MAX_PROCEDURE);
7478 fprintf(fp, "{ %.2f setlinewidth\n", OUTLINE_LINEWIDTH_MAX);
7479 fprintf(fp, " newpath moveto\n");
7480 fprintf(fp, " %.2f %.2f rmoveto\n",
7481 OUTLINE_LINEWIDTH_MAX / 2., OUTLINE_LINEWIDTH_MAX / 2.);
7482 fprintf(fp, " 0 %.2f rlineto %.2f 0 rlineto 0 %.2f rlineto\n",
7483 CELLSIZE - OUTLINE_LINEWIDTH_MAX, CELLSIZE - OUTLINE_LINEWIDTH_MAX, -1 * (CELLSIZE - OUTLINE_LINEWIDTH_MAX));
7484 fprintf(fp, " closepath gsave stroke grestore \n");
7485 fprintf(fp, "} def\n\n");
7486
7487 //printing a double-line outline
7488 fprintf(fp, "\n/%s %% stack: x y => ---\n", OUTLINE_MAX_PROCEDURE);
7489 fprintf(fp, "{ %s\n", OUTLINE_MIN_PROCEDURE);
7490 fprintf(fp, " %.2f %.2f rmoveto\n",
7491 OUTLINE_LINEWIDTH * 2., OUTLINE_LINEWIDTH * 2.);
7492 fprintf(fp, " 0 %.2f rlineto %.2f 0 rlineto 0 %.2f rlineto\n",
7493 CELLSIZE - (5. * OUTLINE_LINEWIDTH), CELLSIZE - (5. * OUTLINE_LINEWIDTH), -1 * (CELLSIZE - (5. * OUTLINE_LINEWIDTH)));
7494 fprintf(fp, " closepath gsave stroke grestore \n");
7495 fprintf(fp, "} def\n\n");
7496
7497 //drawing corners only for the outline
7498 fprintf(fp, "\n/outlinemin %% stack: x y => ---\n");
7499 fprintf(fp, "{ newpath moveto\n");
7500 fprintf(fp, " %.2f 0 rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7501 OUTLINE_LINEWIDTH/2, CELLSIZE / 4.);
7502 fprintf(fp, " grestore 0 %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7503 3 * CELLSIZE / 4., CELLSIZE / 4. );
7504
7505 fprintf(fp, " grestore %.2f %.2f rmoveto %.2f 0 rlineto closepath gsave stroke\n",
7506 -1 * OUTLINE_LINEWIDTH / 2., 3 * OUTLINE_LINEWIDTH / 2., CELLSIZE / 4.);
7507 fprintf(fp, " grestore %.2f 0 rmoveto %.2f 0 rlineto closepath gsave stroke\n",
7508 3 * CELLSIZE / 4., CELLSIZE / 4. );
7509
7510 fprintf(fp, " grestore %.2f %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7511 3 * OUTLINE_LINEWIDTH / 2., -3 * OUTLINE_LINEWIDTH/2, CELLSIZE / 4.);
7512 fprintf(fp, " grestore 0 %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7513 -3 * CELLSIZE / 4., CELLSIZE / 4.);
7514
7515 fprintf(fp, " grestore %.2f %.2f rmoveto %.2f 0 rlineto closepath gsave stroke\n",
7516 -3 * OUTLINE_LINEWIDTH / 2., OUTLINE_LINEWIDTH / 2., CELLSIZE / 4.);
7517 fprintf(fp, " grestore %.2f 0 rmoveto %.2f 0 rlineto closepath gsave stroke\n",
7518 -3 * CELLSIZE / 4., CELLSIZE / 4.);
7519 fprintf(fp, "} def\n");
7520
7521 //drawing corners plus a dash in the middle of each side for the outline
7522 fprintf(fp, "/outlinemid %% stack: x y => ---\n");
7523 fprintf(fp, "{ \n");
7524 fprintf(fp, " outlinemin\n");
7525 fprintf(fp, " grestore %.2f %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7526 OUTLINE_LINEWIDTH / 2., CELLSIZE / 4. + OUTLINE_LINEWIDTH / 2., CELLSIZE / 4.);
7527 fprintf(fp, " grestore %.2f %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7528 CELLSIZE / 4. + OUTLINE_LINEWIDTH / 2., CELLSIZE / 2. + OUTLINE_LINEWIDTH / 2., CELLSIZE / 4.);
7529 fprintf(fp, " grestore %.2f %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7530 CELLSIZE / 2. + OUTLINE_LINEWIDTH / 2., -1 * (CELLSIZE / 2. + OUTLINE_LINEWIDTH / 2.), CELLSIZE / 4.);
7531 fprintf(fp, " grestore %.2f %.2f rmoveto 0 %.2f rlineto closepath gsave stroke\n",
7532 -1 * (CELLSIZE / 2. + OUTLINE_LINEWIDTH / 2.), -1 * CELLSIZE / 4. + OUTLINE_LINEWIDTH / 2., CELLSIZE / 4.);
7533 fprintf(fp, " gsave\n");
7534 fprintf(fp, "} def\n");
7535 */
7536 return;
7537 }
7538
7539