Debian Codegraph @

cgview_xml_builder/cgview_xml_builder.pl

raw
1 #!/usr/bin/perl
2 #
3 #cgview_xml_builder.pl
4 #
5 #Version 1.0
6 #
7 #This script requires bioperl-1.4 or newer.
8 #
9 #See README
10 #
11 #Written by Paul Stothard, University of Alberta
12 #
13 #[email protected]
14
15 use strict;
16 use warnings;
17 use Bio::SeqIO;
18 use Getopt::Long;
19 use Data::Dumper;
20
21 my $MIN_ORF_SIZE = 25;
22 my $MAX_ORF_SIZE = 1000;
23 my $MIN_STEP = 1;
24 my $MAX_STEP = 100;
25 my $MIN_WINDOW = 1000;
26 my $MAX_WINDOW = 10000;
27 my $MIN_LENGTH = 1000;
28 my $MAX_LENGTH = 20000000;
29
30 my %options = (sequence => undef,
31 output => undef,
32 source_features => 'T',
33 reading_frames => 'F',
34 orfs => 'F',
35 combined_orfs => 'F',
36 orf_size => 100,
37 starts => 'atg|ttg|att|gtg|ctg',
38 stops => 'taa|tag|tga',
39 gc_content => 'T',
40 gc_skew => 'T',
41 at_content => 'F',
42 at_skew => 'F',
43 average => 'T',
44 scale => 'T',
45 step => undef,
46 window => undef,
47 size => "medium",
48 tick_density => 0.5,
49 linear => undef,
50 title => undef,
51 details => 'T',
52 legend => 'T',
53 parse_reading_frame => 'F',
54 show_queries => 'F',
55 condensed => 'F',
56 feature_labels => 'F',
57 gene_labels => 'F',
58 hit_labels => 'F',
59 orf_labels => 'F',
60 global_label => 'T',
61 use_opacity => 'F',
62 show_sequence_features => 'T',
63 draw_divider_rings => 'F',
64 font_size => undef,
65 gene_decoration => undef,
66 genes => undef,
67 analysis => undef,
68 blast => undef,
69 verbose => 'T',
70 log => undef);
71
72 #Feel free to add your own COG letters and colors
73 my %settings = (cogColors => {J => "rgb(128,0,0)", #maroon
74 K => "rgb(0,0,128)", #navy
75 L => "rgb(128,0,128)", #purple
76 D => "rgb(185,148,101)", #light brown
77 O => "rgb(0,255,255)", #aqua
78 M => "rgb(0,128,128)", #teal
79 N => "rgb(0,0,255)", #blue
80 P => "rgb(234,165,42)", #orange
81 T => "rgb(190,152,253)", #light purple
82 C => "rgb(128,128,0)", #olive
83 G => "rgb(0,255,0)", #lime
84 E => "rgb(0,128,0)", #green
85 F => "rgb(255,0,255)", #fuchsia
86 H => "rgb(241,199,200)", #light pink
87 I => "rgb(255,0,0)", #red
88 Q => "rgb(255,255,0)", #yellow
89 R => "rgb(128,128,128)", #gray
90 S => "rgb(192,192,192)", #sliver
91 Unknown => "rgb(0,0,0)"}, #black
92
93 blastColors => ["rgb(0,128,128)", #teal
94 "rgb(0,255,255)", #aqua
95 "rgb(234,165,42)", #orange
96 "rgb(190,152,253)", #light purple
97 "rgb(255,0,0)", #red
98 "rgb(0,0,255)", #blue
99 "rgb(128,128,0)", #olive
100 "rgb(153,153,0)"], #yellow
101
102 analysisColors => ["rgb(0,128,0)", #green
103 "rgb(241,199,200)", #light pink
104 "rgb(255,0,255)", #fuchsia
105 "rgb(0,153,153)", #light blue
106 "rgb(0,153,0)", #green
107 "rgb(153,153,0)"], #yellow
108
109 width => "3000",
110 height => "3000",
111 backboneRadius => "820",
112 backboneColor => "rgb(102,102,102)", #dark gray
113 backboneThickness => "20",
114 featureThickness => "30", #was 40
115 featureThicknessPlot => "50", #was 45
116 featureSlotSpacing => "2",
117 rulerFontSize => "30",
118 rulerFontColor => "rgb(0,0,0)",
119 titleFontSize => "80",
120 labelFontSize => "30",
121 legendFontSize => "30",
122 maxTitleLength => "90",
123 maxLabelLength => "20",
124 maxLegendLength => "30",
125 plotLineThickness => "0.02",
126 proteinColor => "rgb(0,0,153)", #dark blue
127 tRNAColor => "rgb(153,0,0)", #dark red
128 rRNAColor => "rgb(153,0,153)", #dark purple
129 otherColor => "rgb(51,51,51)", #dark gray
130 featureOpacity => "1.0",
131 featureOpacityOther => "0.5", #features of type 'other' are drawn with transparency so that underlying CDS can be seen
132 gcColorPos => "rgb(0,0,0)", #black
133 gcColorNeg => "rgb(0,0,0)", #black
134 atColorPos => "rgb(51,51,51)", #dark gray
135 atColorNeg => "rgb(51,51,51)", #dark gray
136 gcSkewColorPos => "rgb(0,153,0)", #dark green
137 gcSkewColorNeg => "rgb(153,0,153)", #dark purple
138 atSkewColorPos => "rgb(153,153,0)", #dark yellow
139 atSkewColorNeg => "rgb(0,0,153)", #dark blue
140 orfColor => "rgb(204,0,0)", #dark red
141 startColor => "rgb(153,0,153)", #dark purple
142 stopColor => "rgb(204,0,0)", #dark red
143 backgroundColor => "white",
144 foregroundColor => "black",
145 sepColor => "rgb(0,51,0)", #dark green
146 tickColor => "rgb(0,51,0)", #dark green
147 labelLineLength => "200",
148 labelPlacementQuality => "good", #good, better, best
149 labelLineThickness => "4",
150 rulerPadding => "40",
151 tickThickness => "5",
152 arrowheadLength => "6",
153 minimumFeatureLength => "1.0",
154 moveInnerLabelsToOuter => "false",
155 tickLength => "20",
156 useInnerLabels => "true",
157 showBorder => "true",
158 isLinear => "false",
159 autoPlotSlotIncrease => 1,
160 maxFeatureSize => 50000); #maxFeatureSize is used to prevent large features ('source' for example) from obscuring other features
161
162 my %global = (orfCount => 0,
163 ncbiGiLink => "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Text\&db=Protein\&dopt=genpept\&dispmax=20\&uid=",
164 ncbiGeneLink => "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene\&cmd=Retrieve\&dopt=Graphics\&list_uids=",
165 format => undef,
166 length => undef,
167 accession => undef,
168 topology => "circular");
169
170 my %param = (options => \%options,
171 settings => \%settings,
172 global => \%global);
173
174
175 #read user options
176 GetOptions ('sequence=s' => \$options{sequence},
177 'output=s' => \$options{output},
178 'source_features=s' => \$options{source_features},
179 'reading_frames=s' => \$options{reading_frames},
180 'orfs=s' => \$options{orfs},
181 'combined_orfs=s' => \$options{combined_orfs},
182 'orf_size=i' => \$options{orf_size},
183 'starts=s' => \$options{starts},
184 'stops=s' => \$options{stops},
185 'gc_content=s' => \$options{gc_content},
186 'gc_skew=s' => \$options{gc_skew},
187 'at_content=s' => \$options{at_content},
188 'at_skew=s' => \$options{at_skew},
189 'average=s' => \$options{average},
190 'scale=s' => \$options{scale},
191 'step=i' => \$options{step},
192 'window=i' => \$options{window},
193 'size=s' => \$options{size},
194 'tick_density=f' => \$options{tick_density},
195 'linear=s' => \$options{linear},
196 'title=s' => \$options{title},
197 'details=s' => \$options{details},
198 'legend=s' => \$options{legend},
199 'parse_reading_frame=s' =>\$options{parse_reading_frame},
200 'show_queries=s' =>\$options{show_queries},
201 'condensed=s' => \$options{condensed},
202 'feature_labels=s' => \$options{feature_labels},
203 'gene_labels=s' => \$options{gene_labels},
204 'hit_labels=s' => \$options{hit_labels},
205 'orf_labels=s' => \$options{orf_labels},
206 'use_opacity=s' => \$options{use_opacity},
207 'show_sequence_features=s' => \$options{show_sequence_features},
208 'draw_divider_rings=s' => \$options{draw_divider_rings},
209 'global_label=s' => \$options{global_label},
210 'font_size=s' => \$options{font_size},
211 'gene_decoration=s' => \$options{gene_decoration},
212 'genes=s@{,}' => \$options{genes},
213 'analysis=s@{,}' => \$options{analysis},
214 'blast=s@{,}' => \$options{blast},
215 'verbose=s' => \$options{verbose},
216 'log=s' => \$options{log});
217
218
219 #check for required options
220 if (!(defined($param{options}->{sequence}))) {
221 die ("Please specify a sequence using the '-sequence' option.\n");
222 }
223 if (!(defined($param{options}->{output}))) {
224 die ("Please specify an output file using the '-output' option.\n");
225 }
226
227 #start log file
228 if (defined($param{options}->{log})) {
229 _createLog($param{options}->{log});
230 }
231
232 #check some important values
233 #-orf_size
234 if ($options{orf_size} =~ m/(\d+)/) {
235 $options{orf_size} = $1;
236 }
237 else {
238 _message($param{options}, "-orf_size must be an integer value.");
239 die ("-orf_size must be an integer value.");
240 }
241 if ($options{orf_size} < $MIN_ORF_SIZE) {
242 _message($param{options}, "-orf_size must be greater than or equal to $MIN_ORF_SIZE.");
243 die ("-orf_size must be greater than or equal to $MIN_ORF_SIZE.");
244 }
245 if ($options{orf_size} > $MAX_ORF_SIZE) {
246 _message($param{options}, "-orf_size must be less than or equal to $MAX_ORF_SIZE.");
247 die ("-orf_size must be less than or equal to $MAX_ORF_SIZE.");
248 }
249
250 #-step
251 if (defined($options{step})) {
252 if ($options{step} =~ m/(\d+)/) {
253 $options{step} = $1;
254 }
255 else {
256 _message($param{options}, "-step must be an integer value.");
257 die ("-step must be an integer value.");
258 }
259 if ($options{step} < $MIN_STEP) {
260 _message($param{options}, "-step must be greater than or equal to $MIN_STEP.");
261 die ("-step must be greater than or equal to $MIN_STEP.");
262 }
263 if ($options{step} > $MAX_STEP) {
264 _message($param{options}, "-step must be less than or equal to $MAX_STEP.");
265 die ("-step must be less than or equal to $MAX_STEP.");
266 }
267 }
268
269 #-window
270 if (defined($options{window})) {
271 if ($options{window} =~ m/(\d+)/) {
272 $options{window} = $1;
273 }
274 else {
275 _message($param{options}, "-window must be an integer value.");
276 die ("-window must be an integer value.");
277 }
278 if ($options{window} < $MIN_WINDOW) {
279 _message($param{options}, "-window must be greater than or equal to $MIN_WINDOW.");
280 die ("-window must be greater than or equal to $MIN_WINDOW.");
281 }
282 if ($options{window} > $MAX_WINDOW) {
283 _message($param{options}, "-window must be less than or equal to $MAX_WINDOW.");
284 die ("-window must be less than or equal to $MAX_WINDOW.");
285 }
286 }
287
288 #-tick_density
289 if ($options{tick_density} =~ m/([\d\.]+)/) {
290 $options{tick_density} = $1;
291 }
292 else {
293 _message($param{options}, "-tick_density must be a real value.");
294 die ("-tick_density must be a real value.");
295 }
296 if ($options{tick_density} < 0) {
297 _message($param{options}, "-tick_density must be greater than or equal to 0.");
298 die ("-tick_density must be greater than or equal to 0.");
299 }
300 if ($options{tick_density} > 1) {
301 _message($param{options}, "-tick_density must be less than or equal to 1.");
302 die ("-tick_density must be less than or equal to 1.");
303 }
304
305 #-global_label
306 if ($options{global_label} =~ m/auto/i) {
307 $options{global_label} = "auto";
308 }
309 elsif ($options{global_label} =~ m/t/i) {
310 $options{global_label} = "true";
311 }
312 else {
313 $options{global_label} = "false";
314 }
315
316
317 #-starts
318 if (defined($options{starts})) {
319 my @starts = split(/\|/, $options{starts});
320 foreach(@starts) {
321 if (!($_ =~ m/[a-z]{3}/)) {
322 _message($param{options}, "-starts must be given as codons separated by the '|', eg 'atg|ttg|att|gtg|ctg'. Be sure to include single quotes when passing as a command line argument.");
323 die ("-starts must be given as codons separated by the '|', eg 'atg|ttg|att|gtg|ctg'. Be sure to include single quotes when passing as a command line argument.");
324 }
325 }
326 }
327
328 #-stops
329 if (defined($options{stops})) {
330 my @stops = split(/\|/, $options{stops});
331 foreach(@stops) {
332 if (!($_ =~ m/[a-z]{3}/)) {
333 _message($param{options}, "-stops must be given as codons separated by the '|', eg 'taa|tag|tga'. Be sure to include single quotes when passing as a command line argument.");
334 die ("-stops must be given as codons separated by the '|', eg 'taa|tag|tga'. Be sure to include single quotes when passing as a command line argument.");
335 }
336 }
337 }
338
339 #obtain BioPerl Bio::Seq sequence object
340 #_getSeqObject also sets value of $global{format} to 'genbank', 'embl', 'raw', or 'fasta'
341 my $seqObject = _getSeqObject(\%param);
342
343
344 #get feature objects, go through them, find complex features, and divide them into new features.
345 _expand_complex_features ($seqObject);
346
347 #determine the length of the genome
348 $global{length} = $seqObject->length();
349 if (!(defined($global{length}))) {
350 _message($param{options}, "The sequence length could not be determined from the -sequence file $options{sequence}.");
351 die ("The sequence length could not be determined from the -sequence file $options{sequence}.");
352 }
353
354 if ($global{length} < $MIN_LENGTH) {
355 _message($param{options}, "The sequence must be longer than $MIN_LENGTH bases.");
356 die ("The sequence must be longer than $MIN_LENGTH bases.");
357 }
358
359 if ($global{length} > $MAX_LENGTH) {
360 _message($param{options}, "The sequence must be shorter than $MAX_LENGTH bases.");
361 die ("The sequence must be shorter than $MAX_LENGTH bases.");
362 }
363
364 #set window
365 #these values may need to be adjusted
366 if (!(defined($options{window}))) {
367 if ($global{length} < 1000) {
368 $options{window} = 10;
369 }
370 elsif ($global{length} < 10000) {
371 $options{window} = 50;
372 }
373 elsif ($global{length} < 100000) {
374 $options{window} = 500;
375 }
376 elsif ($global{length} < 1000000) {
377 $options{window} = 1000;
378 }
379 elsif ($global{length} < 10000000) {
380 $options{window} = 10000;
381 }
382 else {
383 $options{window} = 10000;
384 }
385 }
386
387 #set step.
388 #these values may need to be adjusted
389 #the step may need to be smaller when a larger map is drawn
390 if (!(defined($options{step}))) {
391 if ($global{length} < 1000) {
392 $options{step} = 1;
393 }
394 elsif ($global{length} < 10000) {
395 $options{step} = 1;
396 }
397 elsif ($global{length} < 100000) {
398 $options{step} = 1;
399 }
400 elsif ($global{length} < 1000000) {
401 $options{step} = 10;
402 }
403 elsif ($global{length} < 10000000) {
404 $options{step} = 100;
405 }
406 else {
407 $options{step} = 100;
408 }
409
410 #adjust based on map size
411 if ($options{size} eq 'x-large') {
412 if ($options{step} == 10) {
413 $options{step} = 1;
414 }
415 elsif ($options{step} == 100) {
416 $options{step} = 10;
417 }
418 }
419
420 if ($options{size} eq 'large') {
421 if ($options{step} == 10) {
422 $options{step} = 5;
423 }
424 elsif ($options{step} == 100) {
425 $options{step} = 50;
426 }
427 }
428
429 }
430
431 #determine some global settings from sequence file
432 if (($global{format} eq "embl") || ($global{format} eq "genbank")) {
433 $global{"accession"} = $seqObject->accession_number;
434 if (!(defined($options{title}))) {
435 $options{title} = $seqObject->description();
436 }
437 }
438 if ($global{format} eq "fasta") {
439 if (!(defined($options{title}))) {
440 $options{title} = $seqObject->description();
441 }
442 }
443
444 #try to determine topology from genbank or embl record
445 if (($global{format} eq "embl") || ($global{format} eq "genbank")) {
446 if ($seqObject->is_circular) {
447 $settings{isLinear} = "false";
448 $global{topology} = "circular";
449 }
450 else {
451 $settings{isLinear} = "true";
452 $global{topology} = "linear";
453 }
454 }
455
456 #user-supplied values take precedence
457 if (_isTrue($options{linear})) {
458 $settings{isLinear} = "true";
459 $global{topology} = "linear";
460 }
461 elsif ((defined($options{linear})) && ($options{linear} =~ m/f/i)) {
462 $settings{isLinear} = "false";
463 $global{topology} = "circular";
464 }
465
466 #adjust settings based on size of map
467 _adjustSettingsBasedOnSize($param{options}, $param{settings}, $param{global});
468
469 #start building XML file
470 _writeHeader(\%options, \%settings, \%global);
471
472 #write title legend
473 if ((defined($options{title})) && ($options{title} =~ m/\S/)) {
474 _writeTitleLegend(\%options, \%settings);
475 }
476
477 #write details legend
478 if (_isTrue($options{details})) {
479 _writeDetailsLegend(\%param);
480 }
481
482 #write features legend
483 if (_isTrue($options{legend})) {
484 _writeFeatureLegend(\%param, $seqObject);
485 }
486
487
488 #forward strand features drawn on outside of backbone circle. Those appearing first in the XML are drawn closest to the backbone.
489 _message(\%options, "Creating XML for feature sets on the outside of the backbone circle.");
490 _message(\%options, "Feature sets written first are drawn closest to the backbone circle.");
491
492 #write starts and stops for three reading frames
493 if (_isTrue($options{reading_frames})) {
494 _message(\%options, "Creating XML for starts and stops that are in reading frame +1.");
495 _writeStopsAndStarts(\%options, \%settings, $seqObject, 1, 1);
496 if (_isTrue($options{draw_divider_rings})) {
497 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
498 }
499 _message(\%options, "Creating XML for starts and stops that are in reading frame +2.");
500 _writeStopsAndStarts(\%options, \%settings, $seqObject, 1, 2);
501 if (_isTrue($options{draw_divider_rings})) {
502 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
503 }
504 _message(\%options, "Creating XML for starts and stops that are in reading frame +3.");
505 _writeStopsAndStarts(\%options, \%settings, $seqObject, 1, 3);
506 if (_isTrue($options{draw_divider_rings})) {
507 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
508 }
509 }
510
511 #write ORFs for three reading frames
512 if (_isTrue($options{orfs})) {
513 _message(\%options, "Creating XML for ORFs that are in reading frame +1.");
514 _writeOrfs(\%options, \%settings, \%global, $seqObject, 1, 1);
515 if (_isTrue($options{draw_divider_rings})) {
516 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
517 }
518 _message(\%options, "Creating XML for ORFs that are in reading frame +2.");
519 _writeOrfs(\%options, \%settings, \%global, $seqObject, 1, 2);
520 if (_isTrue($options{draw_divider_rings})) {
521 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
522 }
523 _message(\%options, "Creating XML for ORFs that are in reading frame +3.");
524 _writeOrfs(\%options, \%settings, \%global, $seqObject, 1, 3);
525 if (_isTrue($options{draw_divider_rings})) {
526 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
527 }
528 }
529
530 #write combined orfs
531 if (_isTrue($options{combined_orfs})) {
532 _message(\%options, "Creating XML for ORFs that are on the plus strand.");
533 _writeOrfs(\%options, \%settings, \%global, $seqObject, 1, "all");
534 if (_isTrue($options{draw_divider_rings})) {
535 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
536 }
537 }
538
539 #write features in GenBank or EMBL file
540 if (_isTrue($options{show_sequence_features})) {
541 if ((defined($global{format})) && ($global{format} eq "genbank")) {
542 _message(\%options, "Creating XML for features in the GenBank file that are on the plus strand.");
543 _writeGenBankGenes(\%options, \%settings, \%global, $seqObject, 1, undef);
544 if (_isTrue($options{draw_divider_rings})) {
545 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
546 }
547 }
548 elsif ((defined($global{format})) && ($global{format} eq "embl")) {
549 _message(\%options, "Creating XML for features in the EMBL file that are on the plus strand.");
550 _writeEmblGenes(\%options, \%settings, \%global, $seqObject, 1, undef);
551 if (_isTrue($options{draw_divider_rings})) {
552 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
553 }
554 }
555 }
556
557 #write features info from -genes files if available
558 if (defined($options{genes})) {
559 foreach(reverse(@{$options{genes}})) {
560 _message(\%options, "Creating XML for features in the genes file $_ that are on the plus strand.");
561 _writeGenes(\%options, \%settings, \%global, $seqObject, 1, undef, undef, undef, $_, undef);
562 if (_isTrue($options{draw_divider_rings})) {
563 _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
564 }
565 }
566 }
567
568 #write info from analysis file if available
569 #commented out on 2007-12-11 so that all analysis results shown on inside of backbone
570 #if (defined($options{"analysis"})) {
571 # my @colors = @{$settings{'analysisColors'}};
572 # foreach(reverse(@{$options{"analysis"}})) {
573 # my $colorPos = shift(@colors);
574 # my $colorNeg = shift(@colors);
575 # push(@colors, $colorPos);
576 # push(@colors, $colorNeg);
577 # _message(\%options, "Creating XML for analysis values from the analysis file $_ that are on the plus strand.");
578 # _writeGenes(\%options, \%settings, \%global, $seqObject, 1, undef, $colorPos, $colorNeg, $_, 1);
579 # if (_isTrue($options{draw_divider_rings})) {
580 # _drawDivider(\%options, \%settings, \%global, $seqObject, 1, 0.25);
581 # }
582 # }
583 #}
584
585
586 #Reverse strand features drawn on inside of backbone circle. Those appearing first
587 #in the XML are drawn closest to the backbone.
588 _message(\%options, "Creating XML for feature sets on the inside of the backbone circle.");
589 _message(\%options, "Feature sets written first are drawn closest to the backbone circle.");
590
591 #write starts and stops for three reading frames
592 if (_isTrue($options{reading_frames})) {
593 _message(\%options, "Creating XML for starts and stops that are in reading frame -1.");
594 _writeStopsAndStarts(\%options, \%settings, $seqObject, -1, 1);
595 if (_isTrue($options{draw_divider_rings})) {
596 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
597 }
598 _message(\%options, "Creating XML for starts and stops that are in reading frame -2.");
599 _writeStopsAndStarts(\%options, \%settings, $seqObject, -1, 2);
600 if (_isTrue($options{draw_divider_rings})) {
601 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
602 }
603 _message(\%options, "Creating XML for starts and stops that are in reading frame -3.");
604 _writeStopsAndStarts(\%options, \%settings, $seqObject, -1, 3);
605 if (_isTrue($options{draw_divider_rings})) {
606 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
607 }
608 }
609
610 #write ORFs for three reading frames
611 if (_isTrue($options{orfs})) {
612 _message(\%options, "Creating XML for ORFs that are in reading frame -1.");
613 _writeOrfs(\%options, \%settings, \%global, $seqObject, -1, 1);
614 if (_isTrue($options{draw_divider_rings})) {
615 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
616 }
617 _message(\%options, "Creating XML for ORFs that are in reading frame -2.");
618 _writeOrfs(\%options, \%settings, \%global, $seqObject, -1, 2);
619 if (_isTrue($options{draw_divider_rings})) {
620 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
621 }
622 _message(\%options, "Creating XML for ORFs that are in reading frame -3.");
623 _writeOrfs(\%options, \%settings, \%global, $seqObject, -1, 3);
624 if (_isTrue($options{draw_divider_rings})) {
625 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
626 }
627 }
628
629 #write combined orfs
630 if (_isTrue($options{combined_orfs})) {
631 _message(\%options, "Creating XML for ORFs that are on the reverse strand.");
632 _writeOrfs(\%options, \%settings, \%global, $seqObject, -1, "all");
633 if (_isTrue($options{draw_divider_rings})) {
634 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
635 }
636 }
637
638 #write features in GenBank or EMBL file
639 if (_isTrue($options{show_sequence_features})) {
640 if ((defined($global{format})) && ($global{format} eq "genbank")) {
641 _message(\%options, "Creating XML for features in the GenBank file that are on the reverse strand.");
642 _writeGenBankGenes(\%options, \%settings, \%global, $seqObject, -1, undef);
643 if (_isTrue($options{draw_divider_rings})) {
644 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
645 }
646 }
647 elsif ((defined($global{format})) && ($global{format} eq "embl")) {
648 _message(\%options, "Creating XML for features in the EMBL file that are on the reverse strand.");
649 _writeEmblGenes(\%options, \%settings, \%global, $seqObject, -1, undef);
650 if (_isTrue($options{draw_divider_rings})) {
651 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
652 }
653 }
654 }
655
656 #write features info from -genes files if available
657 if (defined($options{genes})) {
658 foreach(reverse(@{$options{genes}})) {
659 _message(\%options, "Creating XML for features in the genes file $_ that are on the reverse strand.");
660 _writeGenes(\%options, \%settings, \%global, $seqObject, -1, undef, undef, undef, $_, undef);
661 if (_isTrue($options{draw_divider_rings})) {
662 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
663 }
664 }
665 }
666
667 #write info from analysis file if available
668 if (defined($options{"analysis"})) {
669 my @colors = @{$settings{'analysisColors'}};
670 foreach(reverse(@{$options{"analysis"}})) {
671 my $colorPos = shift(@colors);
672 my $colorNeg = shift(@colors);
673 push(@colors, $colorPos);
674 push(@colors, $colorNeg);
675 _message(\%options, "Creating XML for analysis values from the analysis file $_.");
676 _writeGenes(\%options, \%settings, \%global, $seqObject, -1, undef, $colorPos, $colorNeg, $_, 1);
677 if (_isTrue($options{draw_divider_rings})) {
678 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
679 }
680 }
681 }
682
683 #write blast results
684 if (defined($options{"blast"})) {
685 my @colors = @{$settings{'blastColors'}};
686 foreach(@{$options{"blast"}}) {
687 my $color = shift(@colors);
688 push(@colors, $color);
689 _message(\%options, "Creating XML for BLAST hits from the BLAST file $_.");
690 _writeBlast(\%options, \%settings, \%global, $seqObject, -1, $_, $color, $settings{'featureThickness'}, undef);
691 if (_isTrue($options{draw_divider_rings})) {
692 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
693 }
694 }
695 }
696
697 #draw base content graphs.
698 if (_isTrue($options{'at_content'})) {
699 _message(\%options, "Creating XML for AT content information.");
700 _writeBaseContent(\%options, \%settings, $seqObject, -1, 'at_content');
701 if (_isTrue($options{draw_divider_rings})) {
702 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
703 }
704 }
705 if (_isTrue($options{'at_skew'})) {
706 _message(\%options, "Creating XML for AT skew information.");
707 _writeBaseContent(\%options, \%settings, $seqObject, -1, 'at_skew');
708 if (_isTrue($options{draw_divider_rings})) {
709 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
710 }
711 }
712 if (_isTrue($options{'gc_content'})) {
713 _message(\%options, "Creating XML for GC content information.");
714 _writeBaseContent(\%options, \%settings, $seqObject, -1, 'gc_content');
715 if (_isTrue($options{draw_divider_rings})) {
716 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
717 }
718 }
719 if (_isTrue($options{'gc_skew'})) {
720 _message(\%options, "Creating XML for GC skew information.");
721 _writeBaseContent(\%options, \%settings, $seqObject, -1, 'gc_skew');
722 if (_isTrue($options{draw_divider_rings})) {
723 _drawDivider(\%options, \%settings, \%global, $seqObject, -1, 0.25);
724 }
725 }
726
727
728 #write footer
729 _writeFooter(\%options);
730
731
732 #give information about running CGView and improving XML
733 _message(\%options, "CGView XML file complete.");
734 _message(\%options, "The recommended CGView command is:");
735 _message(\%options, "----------------------------------");
736 _message(\%options, "java -jar -Xmx1500m ../cgview.jar -i $options{output} -o map.png -f png");
737 _message(\%options, "Success!");
738
739 ##############################
740
741
742 sub _createLog {
743 my $file = shift;
744 open(OUTFILE, ">" . $file) or die ("Cannot open file : $!");
745 print(OUTFILE "#Results of cgview_sml_builder.pl run started on " . _getTime() . ".\n");
746 close(OUTFILE) or die ("Cannot close file : $!");
747 }
748
749 sub _getTime {
750 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
751 $year += 1900;
752
753 my @days = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
754 my @months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
755 my $time = $days[$wday] . " " . $months[$mon] . " " . sprintf("%02d", $mday) . " " . sprintf("%02d", $hour) . ":" . sprintf("%02d", $min) . ":" . sprintf("%02d", $sec) . " " . sprintf("%04d", $year);
756 return $time;
757 }
758
759 sub _adjustSettingsBasedOnSize {
760 my $options = shift;
761 my $settings = shift;
762 my $global = shift;
763
764 if ($options->{size} eq "small") {
765 $settings->{width} = "1000";
766 $settings->{height} = "1000";
767 $settings->{featureSlotSpacing} = "4";
768 $settings->{backboneRadius} = "300";
769 $settings->{backboneThickness} = "2";
770 $settings->{featureThickness} = "8";
771 $settings->{featureThicknessPlot} = "15";
772 $settings->{rulerFontSize} = "8";
773 $settings->{titleFontSize} = "30";
774 $settings->{labelFontSize} = "10";
775 $settings->{legendFontSize} = "8";
776 $settings->{maxTitleLength} = "50";
777 $settings->{maxLabelLength} = "20";
778 $settings->{maxLegendLength} = "20";
779 $settings->{plotLineThickness} = "0.02";
780 $settings->{labelLineLength} = "60";
781 $settings->{labelLineThickness} = "1";
782 $settings->{rulerPadding} = "14";
783 $settings->{tickThickness} = "1";
784 $settings->{arrowheadLength} = "4";
785 $settings->{minimumFeatureLength} = "0.2";
786 $settings->{tickLength} = "5";
787 }
788 elsif ($options->{size} eq "medium") {
789 $settings->{width} = "3000";
790 $settings->{height} = "3000";
791 $settings->{featureSlotSpacing} = "6";
792 $settings->{backboneRadius} = "1000";
793 $settings->{backboneThickness} = "8";
794 $settings->{featureThickness} = "30";
795 $settings->{featureThicknessPlot} = "80";
796 $settings->{rulerFontSize} = "30";
797 $settings->{titleFontSize} = "80";
798 $settings->{labelFontSize} = "15";
799 $settings->{legendFontSize} = "20";
800 $settings->{maxTitleLength} = "50";
801 $settings->{maxLabelLength} = "20";
802 $settings->{maxLegendLength} = "30";
803 $settings->{plotLineThickness} = "0.02";
804 $settings->{labelLineLength} = "200";
805 $settings->{labelLineThickness} = "4";
806 $settings->{rulerPadding} = "40";
807 $settings->{tickThickness} = "6";
808 $settings->{arrowheadLength} = "6";
809 $settings->{minimumFeatureLength} = "0.2";
810 $settings->{tickLength} = "15";
811 $options->{tick_density} = $options->{tick_density} / 3.0;
812 }
813 #setting for the CGView server
814 elsif ($options->{size} eq "cgview_server_full") {
815 $settings->{width} = "3000";
816 $settings->{height} = "3000";
817 $settings->{featureSlotSpacing} = "1";
818 $settings->{backboneRadius} = "1000";
819 $settings->{backboneThickness} = "8";
820 $settings->{featureThickness} = "60";
821 $settings->{featureThicknessPlot} = "80";
822 $settings->{rulerFontSize} = "30";
823 $settings->{titleFontSize} = "80";
824 $settings->{labelFontSize} = "15";
825 $settings->{legendFontSize} = "20";
826 $settings->{maxTitleLength} = "90";
827 $settings->{maxLabelLength} = "20";
828 $settings->{maxLegendLength} = "30";
829 $settings->{plotLineThickness} = "0.02";
830 $settings->{labelLineLength} = "200";
831 $settings->{labelLineThickness} = "4";
832 $settings->{rulerPadding} = "40";
833 $settings->{tickThickness} = "6";
834 $settings->{arrowheadLength} = "10";
835 $settings->{minimumFeatureLength} = "0.5"; #was 0.2
836 $settings->{tickLength} = "15";
837 $settings->{moveInnerLabelsToOuter} = "true";
838 $options->{tick_density} = $options->{tick_density} / 3.0;
839
840 #if using labels, make rings thinner for more label space
841 #and don't allow plot slots to grow
842 if (_isTrue($options->{feature_labels})) {
843 $settings->{backboneRadius} = "900";
844 $settings->{featureThickness} = "50";
845 $settings->{featureThicknessPlot} = "80";
846 $settings->{autoPlotSlotIncrease} = 0;
847 }
848 }
849 #setting for the CGView server
850 elsif ($options->{size} eq "cgview_server_zoom") {
851 $settings->{width} = "3000";
852 $settings->{height} = "3000";
853 $settings->{featureSlotSpacing} = "1";
854 $settings->{backboneRadius} = "1000";
855 $settings->{backboneThickness} = "16";
856 $settings->{featureThickness} = "260";
857 $settings->{featureThicknessPlot} = "260";
858 $settings->{rulerFontSize} = "30";
859 $settings->{titleFontSize} = "80";
860 $settings->{labelFontSize} = "15";
861 $settings->{legendFontSize} = "20";
862 $settings->{maxTitleLength} = "90";
863 $settings->{maxLabelLength} = "20";
864 $settings->{maxLegendLength} = "30";
865 $settings->{plotLineThickness} = "0.02";
866 $settings->{labelLineLength} = "200";
867 $settings->{labelLineThickness} = "4";
868 $settings->{rulerPadding} = "40";
869 $settings->{tickThickness} = "6";
870 $settings->{arrowheadLength} = "10";
871 $settings->{minimumFeatureLength} = "0.5"; #was 0.2
872 $settings->{tickLength} = "15";
873 $options->{tick_density} = $options->{tick_density} / 3.0;
874 }
875 elsif ($options->{size} eq "large") {
876 $settings->{width} = "9000";
877 $settings->{height} = "9000";
878 $settings->{featureSlotSpacing} = "6";
879 $settings->{backboneRadius} = "3500";
880 $settings->{backboneThickness} = "8";
881 $settings->{featureThickness} = "60";
882 $settings->{featureThicknessPlot} = "120";
883 $settings->{rulerFontSize} = "60";
884 $settings->{titleFontSize} = "100";
885 $settings->{labelFontSize} = "40";
886 $settings->{legendFontSize} = "50";
887 $settings->{maxTitleLength} = "90";
888 $settings->{maxLabelLength} = "20";
889 $settings->{maxLegendLength} = "30";
890 $settings->{plotLineThickness} = "0.02";
891 $settings->{labelLineLength} = "200";
892 $settings->{labelLineThickness} = "4";
893 $settings->{rulerPadding} = "100";
894 $settings->{tickThickness} = "10";
895 $settings->{arrowheadLength} = "12";
896 $settings->{minimumFeatureLength} = "0.2";
897 $settings->{tickLength} = "25";
898 $settings->{labelPlacementQuality} = "better";
899
900 $options->{tick_density} = $options->{tick_density} / 9.0;
901 }
902 elsif ($options->{size} eq "x-large") {
903 $settings->{width} = "12000";
904 $settings->{height} = "12000";
905 $settings->{featureSlotSpacing} = "2";
906 $settings->{backboneRadius} = "4000";
907 $settings->{featureThickness} = "100";
908 $settings->{featureThicknessPlot} = "150";
909 $settings->{rulerFontSize} = "60";
910 $settings->{titleFontSize} = "80";
911 $settings->{labelFontSize} = "15";
912 $settings->{legendFontSize} = "60";
913 $settings->{maxTitleLength} = "90";
914 $settings->{maxLabelLength} = "20";
915 $settings->{maxLegendLength} = "30";
916 $settings->{plotLineThickness} = "0.02";
917 $settings->{labelLineLength} = "200";
918 $settings->{labelLineThickness} = "4";
919 $settings->{rulerPadding} = "40";
920 $settings->{tickThickness} = "5";
921 $settings->{arrowheadLength} = "6";
922 $settings->{minimumFeatureLength} = "0.2";
923 $settings->{tickLength} = "20";
924 $settings->{labelPlacementQuality} = "better";
925
926 $options->{tick_density} = $options->{tick_density} / 12.0;
927 }
928 else {
929 _message($options, "-size setting $options->{size} not recognized.");
930 die("-size setting $options->{size} not recognized");
931 }
932
933 #override the fontsize settings if -font_size specified
934 #these are for the CGView server
935 if ((defined($options->{font_size})) && ($options->{size} eq "cgview_server_zoom")) {
936 if ($options->{font_size} eq 'xx-small') {
937 $settings->{rulerFontSize} = '8';
938 $settings->{titleFontSize} = '50';
939 $settings->{labelFontSize} = '8';
940 $settings->{legendFontSize} = '12';
941 $settings->{labelLineLength} = "50";
942 $settings->{labelLineThickness} = "1";
943 }
944 elsif ($options->{font_size} eq 'x-small') {
945 $settings->{rulerFontSize} = '10';
946 $settings->{titleFontSize} = '60';
947 $settings->{labelFontSize} = '10';
948 $settings->{legendFontSize} = '16';
949 $settings->{labelLineLength} = "100";
950 $settings->{labelLineThickness} = "2";
951 }
952 elsif ($options->{font_size} eq 'small') {
953 $settings->{rulerFontSize} = '12';
954 $settings->{titleFontSize} = '70';
955 $settings->{labelFontSize} = '12';
956 $settings->{legendFontSize} = '18';
957 $settings->{labelLineLength} = "150";
958 $settings->{labelLineThickness} = "2";
959 }
960 elsif ($options->{font_size} eq 'medium') {
961 $settings->{rulerFontSize} = '15';
962 $settings->{titleFontSize} = '80';
963 $settings->{labelFontSize} = '15';
964 $settings->{legendFontSize} = '20';
965 $settings->{labelLineLength} = "150";
966 $settings->{labelLineThickness} = "2";
967 }
968 elsif ($options->{font_size} eq 'large') {
969 $settings->{rulerFontSize} = '20';
970 $settings->{titleFontSize} = '80';
971 $settings->{labelFontSize} = '20';
972 $settings->{legendFontSize} = '22';
973 $settings->{labelLineLength} = "150";
974 $settings->{labelLineThickness} = "4";
975 }
976 elsif ($options->{font_size} eq 'x-large') {
977 $settings->{rulerFontSize} = '32';
978 $settings->{titleFontSize} = '80';
979 $settings->{labelFontSize} = '32';
980 $settings->{legendFontSize} = '24';
981 $settings->{labelLineLength} = "150";
982 $settings->{labelLineThickness} = "4";
983 }
984 elsif ($options->{font_size} eq 'xx-large') {
985 $settings->{rulerFontSize} = '36';
986 $settings->{titleFontSize} = '80';
987 $settings->{labelFontSize} = '36';
988 $settings->{legendFontSize} = '30';
989 $settings->{labelLineLength} = "150";
990 $settings->{labelLineThickness} = "4";
991 }
992 }
993
994 #these are for the CGView server
995 if ((defined($options->{font_size})) && ($options->{size} eq "cgview_server_full")) {
996 if ($options->{font_size} eq 'xx-small') {
997 $settings->{rulerFontSize} = '8';
998 $settings->{titleFontSize} = '50';
999 $settings->{labelFontSize} = '8';
1000 $settings->{legendFontSize} = '12';
1001 $settings->{labelLineLength} = "50";
1002 $settings->{labelLineThickness} = "1";
1003 }
1004 elsif ($options->{font_size} eq 'x-small') {
1005 $settings->{rulerFontSize} = '10';
1006 $settings->{titleFontSize} = '60';
1007 $settings->{labelFontSize} = '10';
1008 $settings->{legendFontSize} = '16';
1009 $settings->{labelLineLength} = "100";
1010 $settings->{labelLineThickness} = "2";
1011 }
1012 elsif ($options->{font_size} eq 'small') {
1013 $settings->{rulerFontSize} = '12';
1014 $settings->{titleFontSize} = '70';
1015 $settings->{labelFontSize} = '12';
1016 $settings->{legendFontSize} = '18';
1017 $settings->{labelLineLength} = "150";
1018 $settings->{labelLineThickness} = "2";
1019 }
1020 elsif ($options->{font_size} eq 'medium') {
1021 $settings->{rulerFontSize} = '15';
1022 $settings->{titleFontSize} = '80';
1023 $settings->{labelFontSize} = '15';
1024 $settings->{legendFontSize} = '20';
1025 $settings->{labelLineLength} = "150";
1026 $settings->{labelLineThickness} = "2";
1027 }
1028 elsif ($options->{font_size} eq 'large') {
1029 $settings->{rulerFontSize} = '16';
1030 $settings->{titleFontSize} = '80';
1031 $settings->{labelFontSize} = '20';
1032 $settings->{legendFontSize} = '22';
1033 $settings->{labelLineLength} = "150";
1034 $settings->{labelLineThickness} = "4";
1035 }
1036 elsif ($options->{font_size} eq 'x-large') {
1037 $settings->{rulerFontSize} = '17';
1038 $settings->{titleFontSize} = '80';
1039 $settings->{labelFontSize} = '32';
1040 $settings->{legendFontSize} = '24';
1041 $settings->{labelLineLength} = "150";
1042 $settings->{labelLineThickness} = "4";
1043 }
1044 elsif ($options->{font_size} eq 'xx-large') {
1045 $settings->{rulerFontSize} = '18';
1046 $settings->{titleFontSize} = '80';
1047 $settings->{labelFontSize} = '36';
1048 $settings->{legendFontSize} = '30';
1049 $settings->{labelLineLength} = "150";
1050 $settings->{labelLineThickness} = "4";
1051 }
1052
1053 }
1054
1055 #count the number of featureSlots
1056 my $plotSlotsOuter = 0;
1057 my $plotSlotsInner = 0;
1058 my $otherSlotsOuter = 0;
1059 my $otherSlotsInner = 0;
1060
1061 #non plot slots such as genes
1062 if (($global->{format} eq "embl") || ($global->{format} eq "genbank")) {
1063 if ($options->{show_sequence_features}) {
1064 $otherSlotsOuter++;
1065 $otherSlotsInner++;
1066 }
1067 }
1068 if (_isTrue($options->{reading_frames})) {
1069 $otherSlotsOuter = $otherSlotsOuter + 3;
1070 $otherSlotsInner = $otherSlotsInner + 3;
1071 }
1072 if (_isTrue($options->{orfs})) {
1073 $otherSlotsOuter = $otherSlotsOuter + 3;
1074 $otherSlotsInner = $otherSlotsInner + 3;
1075 }
1076 if (_isTrue($options->{combined_orfs})) {
1077 $otherSlotsOuter = $otherSlotsOuter + 3;
1078 $otherSlotsInner = $otherSlotsInner + 3;
1079 }
1080 if (defined($options->{genes})) {
1081 $otherSlotsOuter = $otherSlotsOuter + scalar(@{$options->{genes}});
1082 $otherSlotsInner = $otherSlotsInner + scalar(@{$options->{genes}});
1083 }
1084 if (defined($options->{analysis})) {
1085 #$plotSlotsOuter = $plotSlotsOuter + 2 * (scalar(@{$options->{analysis}}));
1086 $plotSlotsInner = $plotSlotsInner + 2 * (scalar(@{$options->{analysis}}));
1087 }
1088
1089 #This estimation could be improved, since maps can now show some blast results
1090 #in a single track and others in six tracks, depending on whether reading
1091 #frame information is read by _parseBLAST
1092 if (defined($options->{blast})) {
1093 if (_isTrue($options->{parse_reading_frame})) {
1094 foreach(@{$options->{blast}}) {
1095 if (_containsReadingFrameInfo($settings, $global, $_)) {
1096 $otherSlotsInner = $otherSlotsInner + 6;
1097 }
1098 else {
1099 $otherSlotsInner = $otherSlotsInner + 1;
1100 }
1101 }
1102 }
1103 else {
1104 $otherSlotsInner = $otherSlotsInner + scalar(@{$options->{blast}});
1105 }
1106 }
1107
1108 #plot slots such as gc skew
1109 if (_isTrue($options->{at_content})) {
1110 $plotSlotsInner++;
1111 }
1112 if (_isTrue($options->{at_skew})) {
1113 $plotSlotsInner++;
1114 }
1115 if (_isTrue($options->{gc_content})) {
1116 $plotSlotsInner++;
1117 }
1118 if (_isTrue($options->{gc_skew})) {
1119 $plotSlotsInner++;
1120 }
1121
1122 #adjust featureSlotThickness based on number of slots used
1123 #want plotSlots to be six times wider than other slots
1124 my $availableSpace = $settings->{backboneRadius} * 0.60 - $settings->{featureSlotSpacing} * ($plotSlotsInner + $plotSlotsOuter + $otherSlotsInner + $otherSlotsOuter - 1);
1125 my $slotUnits = 6.0 * ($plotSlotsInner + $plotSlotsOuter) + $otherSlotsInner + $otherSlotsOuter;
1126
1127 if ($slotUnits == 0) {
1128 $slotUnits++;
1129 }
1130
1131 my $slotWidths = $availableSpace / $slotUnits;
1132
1133 #changed 2007-01-14 so that feature widths don't get too big
1134 #$settings->{featureThickness} = sprintf("%.2f", $slotWidths);
1135 if (sprintf("%.2f", $slotWidths) < $settings->{featureThickness}) {
1136 $settings->{featureThickness} = sprintf("%.2f", $slotWidths);
1137 }
1138
1139 if (sprintf("%.2f", $slotWidths * 6.0) < $settings->{featureThicknessPlot}) {
1140 $settings->{featureThicknessPlot} = sprintf("%.2f", $slotWidths * 6.0);
1141 }
1142 else {
1143 if ($settings->{autoPlotSlotIncrease}) {
1144 $settings->{featureThicknessPlot} = sprintf("%.2f", $slotWidths * 6.0);
1145 }
1146 }
1147
1148 #check condensed setting.
1149 if (_isTrue($options->{condensed})) {
1150 my $newWidth;
1151 if ($options->{size} eq "small") {
1152 $newWidth = 8;
1153 }
1154 if ($options->{size} eq "medium") {
1155 $newWidth = 10;
1156 }
1157 if ($options->{size} eq "large") {
1158 $newWidth = 10;
1159 }
1160 if ($options->{size} eq "x-large") {
1161 $newWidth = 10;
1162 }
1163
1164 if ($newWidth < $settings->{featureThickness}) {
1165 $settings->{featureThickness} = $newWidth;
1166 $settings->{backboneThickness} = "2";
1167 }
1168 if (($newWidth * 2) < $settings->{featureThicknessPlot}) {
1169 $settings->{featureThicknessPlot} = $newWidth * 2;
1170 $settings->{backboneThickness} = "2";
1171 }
1172 }
1173 }
1174
1175 sub _message {
1176 my $options = shift;
1177 my $message = shift;
1178
1179 if (_isTrue($options->{verbose})) {
1180 print "$message\n";
1181 }
1182
1183 if (defined($options->{log})) {
1184 _writeLog($options->{log}, "$message\n");
1185 }
1186 }
1187
1188 sub _isTrue {
1189 my $string = shift;
1190 if ((defined($string)) && ($string =~ m/t/i)) {
1191 return 1;
1192 }
1193 return 0;
1194 }
1195
1196 sub _writeLog {
1197 my $file = shift;
1198 my $message = shift;
1199 open(OUTFILE, "+>>" . $file) or die ("Cannot open file : $!");
1200 print(OUTFILE $message);
1201 close(OUTFILE) or die ("Cannot close file : $!");
1202 }
1203
1204 sub _getSeqObject {
1205 my $param = shift;
1206 my $file = $param->{options}->{sequence};
1207
1208 open (INFILE, $file) or die( "Cannot open input file: $!" );
1209
1210 while (my $line = <INFILE>) {
1211 if (!($line =~ m/\S/)) {
1212 next;
1213 }
1214
1215 #guess file format from first line
1216 if ($line =~ m/^LOCUS\s+/) {
1217 $param->{global}->{format} = "genbank";
1218 }
1219 elsif ($line =~ m/^ID\s+/) {
1220 $param->{global}->{format} = "embl";
1221 }
1222 elsif ($line =~ m/^>/) {
1223 $param->{global}->{format} = "fasta";
1224 }
1225 else {
1226 $param->{global}->{format} = "raw";
1227 }
1228 last;
1229 }
1230
1231 close (INFILE) or die ("Cannot close input file: $!");
1232
1233 #get seqobj
1234 my $in = Bio::SeqIO->new(-format => $param->{global}->{format}, -file => $file);
1235 my $seq = $in->next_seq();
1236
1237 return $seq;
1238
1239 }
1240
1241
1242 sub _writeHeader {
1243 my $options = shift;
1244 my $settings = shift;
1245 my $global = shift;
1246
1247 my $header = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<cgview backboneRadius=\"$settings->{backboneRadius}\" backboneColor=\"$settings->{backboneColor}\" backboneThickness=\"$settings->{backboneThickness}\" featureSlotSpacing=\"$settings->{featureSlotSpacing}\" labelLineLength=\"$settings->{labelLineLength}\" labelPlacementQuality=\"$settings->{labelPlacementQuality}\" labelLineThickness=\"$settings->{labelLineThickness}\" rulerPadding=\"$settings->{rulerPadding}\" tickThickness=\"$settings->{tickThickness}\" arrowheadLength=\"$settings->{arrowheadLength}\" rulerFont=\"SansSerif, plain, $settings->{rulerFontSize}\" rulerFontColor=\"$settings->{rulerFontColor}\" labelFont=\"SansSerif, plain, $settings->{labelFontSize}\" isLinear=\"$settings->{isLinear}\" minimumFeatureLength=\"$settings->{minimumFeatureLength}\" sequenceLength=\"$global->{length}\" height=\"$settings->{height}\" width=\"$settings->{width}\" globalLabel=\"$options->{global_label}\" moveInnerLabelsToOuter=\"$settings->{moveInnerLabelsToOuter}\" featureThickness=\"$settings->{featureThickness}\" tickLength=\"$settings->{tickLength}\" useInnerLabels=\"$settings->{useInnerLabels}\" shortTickColor=\"$settings->{tickColor}\" longTickColor=\"$settings->{tickColor}\" zeroTickColor=\"$settings->{tickColor}\" showBorder=\"$settings->{showBorder}\" borderColor=\"$settings->{foregroundColor}\" backgroundColor=\"$settings->{backgroundColor}\" tickDensity=\"$options->{tick_density}\">\n";
1248
1249 open (OUTFILE, ">$options->{output}") or die( "Cannot open file : $!" );
1250 print (OUTFILE $header);
1251 close (OUTFILE) or die( "Cannot close file : $!");
1252
1253 }
1254
1255 sub _writeTitleLegend {
1256 my $options = shift;
1257 my $settings = shift;
1258 my $title = $options->{title};
1259 $title =~ s/[\.\,]//g;
1260 if (length($title) > $settings->{maxTitleLength} - 3) {
1261 if ($options->{verbose}) {
1262 _message ($options, "The sequence title was shortened because it is longer than $settings->{maxTitleLength} characters.");
1263 }
1264 $title = substr($title, 0, $settings->{maxTitleLength} - 3) . "...";
1265 }
1266 $title = _escapeText($title);
1267
1268 my $legend = "<legend position=\"lower-center\" backgroundOpacity=\"0.8\">\n<legendItem textAlignment=\"center\" font=\"SansSerif, plain, $settings->{titleFontSize}\" text=\"$title\" />\n</legend>\n";
1269
1270 open (OUTFILE, "+>>" . $options->{output}) or die( "Cannot open file : $!" );
1271 print (OUTFILE "\n\n\n\n");
1272 print (OUTFILE "<!-- title -->\n");
1273 print (OUTFILE $legend);
1274 print (OUTFILE "\n");
1275 close (OUTFILE) or die( "Cannot close file : $!");
1276 }
1277
1278 sub _escapeText {
1279 my $text = shift;
1280 if (!defined($text)) {
1281 return undef;
1282 }
1283 $text =~ s/&/&amp;/g;
1284 $text =~ s/\'/&apos;/g;
1285 $text =~ s/\"/&quot;/g;
1286 $text =~ s/</&lt;/g;
1287 $text =~ s/>/&gt;/g;
1288 return $text;
1289 }
1290
1291 sub _writeDetailsLegend {
1292 my $param = shift;
1293
1294 my $accession = _escapeText($param->{global}->{accession});
1295 my $length = _escapeText($param->{global}->{length});
1296 my $topology = _escapeText($param->{global}->{topology});
1297
1298 my $legend = "<legend position=\"upper-left\" font=\"SansSerif, plain, $param->{settings}->{titleFontSize}\" backgroundOpacity=\"0.8\">\n";
1299 if (defined($accession)) {
1300 $legend = $legend . "<legendItem text=\"Accession: $accession\" />\n";
1301 }
1302
1303 while ($length =~ s/^(-?\d+)(\d\d\d)/$1,$2/) {
1304 1;
1305 }
1306 $length = $length . " bp";
1307 $legend = $legend . "<legendItem text=\"Length: $length\" />\n";
1308
1309 if ($topology =~ m/linear/i) {
1310 $legend = $legend . "<legendItem text=\"" . "Topology: $topology\" />\n";
1311 }
1312
1313 $legend = $legend . "</legend>\n";
1314
1315 open (OUTFILE, "+>>" . $param->{options}->{output}) or die( "Cannot open file : $!" );
1316 print (OUTFILE "\n\n\n\n");
1317 print (OUTFILE "<!-- details legend -->\n");
1318 print (OUTFILE $legend);
1319 print (OUTFILE "\n");
1320 close (OUTFILE) or die( "Cannot close file : $!");
1321
1322 }
1323
1324 sub _writeFeatureLegend {
1325 my $param = shift;
1326 my $options = $param->{options};
1327 my $settings = $param->{settings};
1328 my $global = $param->{global};
1329 my $seqObject = shift;
1330
1331
1332 #check -genes files to see if they contain COG information, or features such as 'other' 'tRNA' 'rRNA'
1333 my $hasCogs = 0;
1334 my $hasOther = 0;
1335 my $hastRNA = 0;
1336 my $hasrRNA = 0;
1337 my $hasCoding = 0;
1338 if (defined($options->{genes})) {
1339 foreach(@{$options->{genes}}) {
1340 my @features = @{_parseGFF($_, $options, $settings, $global, $seqObject)};
1341
1342 foreach(@features) {
1343 my $feat = $_;
1344 my $type = lc($feat->{'feature'});
1345
1346 if ($type eq "cds") {
1347 $hasCoding = 1;
1348 }
1349 elsif ($type eq "rrna") {
1350 $hasrRNA = 1;
1351 }
1352 elsif ($type eq "trna") {
1353 $hastRNA = 1;
1354 }
1355 elsif ($type eq "other") {
1356 $hasOther = 1;
1357 }
1358 elsif (defined($settings->{'cogColors'}->{uc($type)})) {
1359 $hasCogs = 1;
1360 }
1361 }
1362 }
1363 }
1364
1365 my $legend = "<legend position=\"upper-right\" textAlignment=\"left\" backgroundOpacity=\"0.8\" font=\"SansSerif, plain, " . $settings->{'legendFontSize'} . "\">\n";
1366
1367 #legend for COGs if genes file supplied and COGs were found
1368 if ($hasCogs) {
1369 if (defined($options->{'genes'})) {
1370 my @cogs = keys(%{$settings->{'cogColors'}});
1371 @cogs = sort(@cogs);
1372 foreach(@cogs) {
1373 $legend = $legend . "<legendItem text=\"" . $_ . " COG" . "\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'cogColors'}->{$_} . "\" />\n";
1374 }
1375 }
1376 }
1377
1378 #protein encoding genes legend
1379 if (((($global->{'format'} eq "genbank") || ($global->{'format'} eq "embl"))) || ($hasCoding)) {
1380 $legend = $legend . "<legendItem text=\"CDS\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'proteinColor'} . "\" />\n";
1381 }
1382
1383 if (!(defined($global->{'format'}))) {
1384 die ("_writeFeatureLegend requires that the sequence format be set by _getSeqObject.");
1385 }
1386
1387 #legend for tRNA, rRNA and other genes if input file is GenBank or EMBL, or if these features were found in the -genes files
1388 if ((((($global->{'format'} eq "genbank") || ($global->{'format'} eq "embl"))) && ($options->{show_sequence_features})) || ($hastRNA)) {
1389 $legend = $legend . "<legendItem text=\"tRNA\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'tRNAColor'} . "\" />\n";
1390 }
1391 if ((((($global->{'format'} eq "genbank") || ($global->{'format'} eq "embl"))) && ($options->{show_sequence_features})) || ($hasrRNA)) {
1392 $legend = $legend . "<legendItem text=\"rRNA\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'rRNAColor'} . "\" />\n";
1393 }
1394 if ((((($global->{'format'} eq "genbank") || ($global->{'format'} eq "embl"))) && ($options->{show_sequence_features})) || ($hasOther)) {
1395 $legend = $legend . "<legendItem text=\"Other\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacityOther'} . "\" swatchColor=\"" . $settings->{'otherColor'} . "\" />\n";
1396 }
1397
1398 #orfs legend
1399 if ((_isTrue($options->{'orfs'})) || (_isTrue($options->{'combined_orfs'}))) {
1400 $legend = $legend . "<legendItem text=\"ORF\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'orfColor'} . "\" />\n";
1401 }
1402
1403 #reading frames legend
1404 if (_isTrue($options->{'reading_frames'})) {
1405 $legend = $legend . "<legendItem text=\"Start\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'startColor'} . "\" />\n";
1406 $legend = $legend . "<legendItem text=\"Stop\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'stopColor'} . "\" />\n";
1407 }
1408
1409 #analysis legends
1410 if (defined($options->{'analysis'})) {
1411 my @colors = @{$settings->{'analysisColors'}};
1412 my @color_set = ();
1413 foreach(@{$options->{'analysis'}}) {
1414 my $colorPos = shift(@colors);
1415 my $colorNeg = shift(@colors);
1416 push (@color_set, $colorPos);
1417 push (@color_set, $colorNeg);
1418 }
1419 @color_set = reverse(@color_set);
1420
1421 foreach(@{$options->{'analysis'}}) {
1422 my $title = _createLegendName($_);
1423 my $colorNeg = shift(@color_set);
1424 my $colorPos = shift(@color_set);
1425 $legend = $legend . "<legendItem text=\"$title+\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $colorPos . "\" />\n";
1426 $legend = $legend . "<legendItem text=\"$title-\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $colorNeg . "\" />\n";
1427 }
1428 }
1429
1430 #blast legends
1431 if (defined($options->{'blast'})) {
1432 my @colors = @{$settings->{'blastColors'}};
1433 foreach(@{$options->{'blast'}}) {
1434 my $title = "BLAST " . _createLegendName($_);
1435 my $color = shift(@colors);
1436 push(@colors, $color);
1437 $legend = $legend . "<legendItem text=\"$title\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $color . "\" />\n";
1438 }
1439 }
1440
1441 #legend for various graphs
1442 #at
1443 if (_isTrue($options->{'at_content'})) {
1444 if ($settings->{'atColorPos'} eq $settings->{'atColorNeg'}) {
1445 $legend = $legend . "<legendItem text=\"AT content\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atColorPos'} . "\" />\n";
1446 }
1447 else {
1448 $legend = $legend . "<legendItem text=\"AT content+\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atColorPos'} . "\" />\n";
1449 $legend = $legend . "<legendItem text=\"AT content-\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atColorNeg'} . "\" />\n";
1450 }
1451 }
1452 #at_skew
1453 if (_isTrue($options->{'at_skew'})) {
1454 if ($settings->{'atSkewColorPos'} eq $settings->{'atSkewColorNeg'}) {
1455 $legend = $legend . "<legendItem text=\"AT skew\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atSkewColorPos'} . "\" />\n";
1456 }
1457 else {
1458 $legend = $legend . "<legendItem text=\"AT skew+\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atSkewColorPos'} . "\" />\n";
1459 $legend = $legend . "<legendItem text=\"AT skew-\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'atSkewColorNeg'} . "\" />\n";
1460 }
1461 }
1462 #gc
1463 if (_isTrue($options->{'gc_content'})) {
1464 if ($settings->{'gcColorPos'} eq $settings->{'gcColorNeg'}) {
1465 $legend = $legend . "<legendItem text=\"GC content\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcColorPos'} . "\" />\n";
1466 }
1467 else {
1468 $legend = $legend . "<legendItem text=\"GC content+\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcColorPos'} . "\" />\n";
1469 $legend = $legend . "<legendItem text=\"GC content-\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcColorNeg'} . "\" />\n";
1470 }
1471 }
1472 #gc_skew
1473 if (_isTrue($options->{'gc_skew'})) {
1474 if ($settings->{'gcSkewColorPos'} eq $settings->{'gcSkewColorNeg'}) {
1475 $legend = $legend . "<legendItem text=\"GC skew\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcSkewColorPos'} . "\" />\n";
1476 }
1477 else {
1478 $legend = $legend . "<legendItem text=\"GC skew+\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcSkewColorPos'} . "\" />\n";
1479 $legend = $legend . "<legendItem text=\"GC skew-\" drawSwatch=\"true\" swatchOpacity=\"" . $settings->{'featureOpacity'} . "\" swatchColor=\"" . $settings->{'gcSkewColorNeg'} . "\" />\n";
1480 }
1481 }
1482
1483 $legend = $legend . "</legend>\n";
1484
1485 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
1486 print (OUTFILE "\n\n\n\n");
1487 print (OUTFILE "<!-- legend -->\n");
1488 print (OUTFILE $legend);
1489 print (OUTFILE "\n");
1490 close (OUTFILE) or die( "Cannot close file : $!");
1491
1492 }
1493
1494 sub _createLegendName {
1495 my $file = shift;
1496 my $name;
1497 if ($file =~ m/\/([^\/]+)$/) {
1498 $name = $1;
1499 #remove extension
1500 $name =~ s/\.[^\.]*$//;
1501 $name =~ s/_/ /g;
1502 #add underscores back to NCBI accession numbers (NM_181029 for example).
1503 $name =~ s/([A-Z][A-Z])\s(\d{4,})/$1_$2/g;
1504 }
1505 if (defined($name)) {
1506 return $name;
1507 }
1508 return $file;
1509 }
1510
1511 sub _writeStopsAndStarts {
1512 my $options = shift;
1513 my $settings = shift;
1514 my $seqObject = shift;
1515 my $strand = shift; #1 or -1
1516 my $rf = shift; #1,2, or 3
1517
1518 my $opacity = $settings->{'featureOpacity'};
1519 my $startCodons = $options->{'starts'};
1520 my $stopCodons = $options->{'stops'};
1521
1522 my @outputArray = ();
1523 if ($strand == 1) {
1524 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"direct\">\n");
1525 }
1526 else {
1527 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"reverse\">\n");
1528 }
1529
1530 #add start and stop codons
1531 my $dna;
1532 if ($strand == 1) {
1533
1534 if ($rf == 1) {
1535 $dna = substr($seqObject->seq(), 0);
1536 }
1537 elsif ($rf == 2) {
1538 $dna = substr($seqObject->seq(), 1);
1539 }
1540 else {
1541 $dna = substr($seqObject->seq(), 2);
1542 }
1543
1544
1545 my $length = length($dna);
1546 my $codon;
1547 my $start;
1548 my $stop;
1549 my $feature;
1550 my $featureRange;
1551 #for start
1552 for (my $i = 0; $i < $length - 2; $i = $i + 3) {
1553 $codon = substr($dna, $i, 3);
1554 if ($codon =~ m/$startCodons/i) {
1555 $start = $i + $rf;
1556 $stop = $start + 2;
1557 $feature = "<feature color=\"" . $settings->{'startColor'} . "\" decoration=\"arc\" opacity=\"$opacity\">\n";
1558 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" proportionOfThickness=\"0.5\" />\n";
1559 push (@outputArray, $feature . $featureRange . "</feature>\n");
1560 }
1561 }
1562
1563 #for stop
1564 for (my $i = 0; $i < $length - 2; $i = $i + 3) {
1565 $codon = substr($dna, $i, 3);
1566 if ($codon =~ m/$stopCodons/i) {
1567 $start = $i + $rf;
1568 $stop = $start + 2;
1569 $feature = "<feature color=\"" . $settings->{'stopColor'} . "\" decoration=\"arc\" opacity=\"$opacity\">\n";
1570 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" proportionOfThickness=\"1.0\" />\n";
1571 push (@outputArray, $feature . $featureRange . "</feature>\n");
1572 }
1573 }
1574 }
1575 elsif ($strand == -1) {
1576 my $rev = $seqObject->revcom;
1577 $dna = $rev->seq();
1578
1579 if ($rf == 1) {
1580 $dna = substr($dna, 0);
1581 }
1582 elsif ($rf == 2) {
1583 $dna = substr($dna, 1);
1584 }
1585 else {
1586 $dna = substr($dna, 2);
1587 }
1588
1589 my $length = length($dna);
1590 my $codon;
1591 my $start;
1592 my $stop;
1593 my $feature;
1594 my $featureRange;
1595
1596 #for start
1597 for (my $i = 0; $i < $length - 2; $i = $i + 3) {
1598 $codon = substr($dna, $i, 3);
1599 if ($codon =~ m/$startCodons/i) {
1600 #bug fix on 2009-09-01
1601 #$start = $length - $i - 1 - $rf;
1602 $start = $length - $i - 2;
1603 $stop = $start + 2;
1604 $feature = "<feature color=\"" . $settings->{'startColor'} . "\" decoration=\"arc\" opacity=\"$opacity\">\n";
1605 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" proportionOfThickness=\"0.5\" />\n";
1606 push (@outputArray, $feature . $featureRange . "</feature>\n");
1607 }
1608 }
1609
1610 #for stop
1611 for (my $i = 0; $i < $length - 2; $i = $i + 3) {
1612 $codon = substr($dna, $i, 3);
1613 if ($codon =~ m/$stopCodons/i) {
1614 #bug fix on 2009-09-01
1615 #$start = $length - $i - 1 - $rf;
1616 $start = $length - $i - 2;
1617 $stop = $start + 2;
1618 $feature = "<feature color=\"" . $settings->{'stopColor'} . "\" decoration=\"arc\" opacity=\"$opacity\">\n";
1619 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" proportionOfThickness=\"1.0\" />\n";
1620 push (@outputArray, $feature . $featureRange . "</feature>\n");
1621 }
1622 }
1623 }
1624
1625 push (@outputArray, "</featureSlot>\n");
1626 my $strandTerm = undef;
1627 if ($strand == 1) {
1628 $strandTerm = "forward";
1629 }
1630 if ($strand == -1) {
1631 $strandTerm = "reverse";
1632 }
1633
1634 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
1635 print (OUTFILE "\n\n\n\n");
1636 print (OUTFILE "<!-- stops and starts in rf $rf on $strandTerm strand -->\n");
1637 print (OUTFILE join("", @outputArray));
1638 print (OUTFILE "\n");
1639 close (OUTFILE) or die( "Cannot close file : $!");
1640
1641 }
1642
1643
1644 sub _writeOrfs {
1645 my $options = shift;
1646 my $settings = shift;
1647 my $global = shift;
1648 my $seqObject = shift;
1649 my $strand = shift; #1 or -1
1650 my $rf = shift; #1,2, 3, or all
1651
1652 my $opacity = $settings->{'featureOpacity'};
1653 my $startCodons = $options->{'starts'};
1654 my $stopCodons = $options->{'stops'};
1655 my $orfLength = $options->{'orf_size'};
1656 my $color = $settings->{'orfColor'};
1657 my $decoration;
1658
1659 my @outputArray = ();
1660
1661 #for CGView server
1662 my $shading = 'false';
1663 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arrow')) {
1664 $shading = 'true';
1665 }
1666
1667 if ($strand == 1) {
1668 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"direct\">\n");
1669 }
1670 else {
1671 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"reverse\">\n");
1672 }
1673
1674 my @orfs = ();
1675
1676 if ($rf eq "all") {
1677 push (@orfs, @{_getOrfs($options, $settings, $global, $seqObject, $strand, 1)});
1678 push (@orfs, @{_getOrfs($options, $settings, $global, $seqObject, $strand, 2)});
1679 push (@orfs, @{_getOrfs($options, $settings, $global, $seqObject, $strand, 3)});
1680 }
1681 else {
1682 push (@orfs, @{_getOrfs($options, $settings, $global, $seqObject, $strand, $rf)});
1683 }
1684
1685 #sort orfs for visualization
1686 if ($strand == 1) {
1687 @orfs = @{_sortOrfs(\@orfs, 'start')};
1688 }
1689 elsif ($strand == -1) {
1690 @orfs = @{_sortOrfs(\@orfs, 'stop')};
1691 }
1692
1693 push (@outputArray, @orfs);
1694
1695 push (@outputArray, "</featureSlot>\n");
1696 my $strandTerm = undef;
1697 if ($strand == 1) {
1698 $strandTerm = "forward";
1699 }
1700 if ($strand == -1) {
1701 $strandTerm = "reverse";
1702 }
1703
1704 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
1705 print (OUTFILE "\n\n\n\n");
1706 print (OUTFILE "<!-- ORFs in rf $rf on $strandTerm strand -->\n");
1707 print (OUTFILE join("", @outputArray));
1708 print (OUTFILE "\n");
1709 close (OUTFILE) or die( "Cannot close file : $!");
1710
1711 }
1712
1713 sub _sortOrfs {
1714 my $orfs = shift;
1715 my $field = shift;
1716
1717 @$orfs = map { $_->[1] }
1718 sort { $b->[0] <=> $a->[0]}
1719 map { [ _getSortValueOrfs($_, $field), $_] }
1720 @$orfs;
1721
1722 return $orfs;
1723 }
1724
1725 sub _getSortValueOrfs {
1726 my $orf = shift;
1727 my $field = shift;
1728
1729 $orf =~ m/$field=\"(\d+)\"/;
1730 return $1;
1731 }
1732
1733 sub _getOrfs {
1734 my $options = shift;
1735 my $settings = shift;
1736 my $global = shift;
1737 my $seqObject = shift;
1738 my $strand = shift; #1 or -1
1739 my $rf = shift; #1,2, or 3
1740 my $rfForLabel = $rf;
1741
1742 my $opacity = $settings->{'featureOpacity'};
1743 my $startCodons = $options->{'starts'};
1744 my $stopCodons = $options->{'stops'};
1745 my $orfLength = $options->{'orf_size'};
1746 my $color = $settings->{'orfColor'};
1747 my $decoration;
1748
1749 if ($strand == 1) {
1750 $decoration = "clockwise-arrow";
1751 }
1752 else {
1753 $decoration = "counterclockwise-arrow";
1754 }
1755
1756 #for CGView server
1757 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arc')) {
1758 $decoration = "arc";
1759 }
1760
1761 my @orfs = ();
1762
1763 my $dna;
1764 if ($strand == 1) {
1765 $dna = $seqObject->seq();
1766 }
1767 else {
1768 my $rev = $seqObject->revcom;
1769 $dna = $rev->seq();
1770 }
1771 my $length = length($dna);
1772 my $i = 0;
1773 my $codon;
1774 my $foundStart = 0;
1775 my $proteinLength = 0;
1776 my $foundStop = 0;
1777 my $startPos = $rf - 1;
1778 my $feature;
1779 my $featureRange;
1780 my $firstBase;
1781 my $lastBase;
1782 my $temp;
1783
1784 my @dna = ();
1785
1786 while ($i <= $length - 3) {
1787 for ($i = $startPos; $i <= $length - 3; $i = $i + 3) {
1788 $codon = substr($dna,$i,3);
1789 if (($startCodons ne "any") && ($foundStart == 0) && (!($codon =~ m/$startCodons/i))) {
1790 last;
1791 }
1792 $foundStart = 1;
1793
1794 if ($codon =~ m/$stopCodons/i) {
1795 $foundStop = 1;
1796 }
1797
1798 $proteinLength++;
1799 push (@dna, $codon);
1800
1801 if (($foundStop) && ($proteinLength < $orfLength)) {
1802 last;
1803 }
1804 if ((($foundStop) && ($proteinLength >= $orfLength)) || (($i >= $length - 5) && ($proteinLength >= $orfLength))) {
1805 $firstBase = $startPos + 1;
1806 $lastBase = $i + 3;
1807
1808 if ($strand == -1) {
1809 $temp = $length - $lastBase + 1;
1810 $lastBase = $length - $firstBase + 1;
1811 $firstBase = $temp;
1812 }
1813
1814 $global->{orfCount}++;
1815
1816 $feature = "<feature color=\"$color\" decoration=\"$decoration\" opacity=\"$opacity\" ";
1817
1818 if (_isTrue($options->{orf_labels})) {
1819 $feature = $feature . "label=\"orf_$global->{orfCount}\" ";
1820 $feature = $feature . "mouseover=\"" . _escapeText("orf_$global->{orfCount}; $firstBase to $lastBase; strand=$strand; rf=$rfForLabel") . "\" ";
1821 }
1822 $feature = $feature . ">\n";
1823
1824 $featureRange = "<featureRange start=\"$firstBase\" stop=\"$lastBase\" />\n";
1825 push (@orfs, $feature . $featureRange . "</feature>\n");
1826
1827 last;
1828 }
1829 }
1830 $startPos = $i + 3;
1831 $i = $startPos;
1832 $foundStart = 0;
1833 $foundStop = 0;
1834 $proteinLength = 0;
1835 @dna = ();
1836 }
1837
1838 return \@orfs;
1839
1840 }
1841
1842
1843 sub _expand_complex_features {
1844 my $seq_object = shift ();
1845 my @features = $seq_object->get_SeqFeatures();
1846 for my $feature (@features) {
1847
1848 #check to see of the location is split (ie. complex feature)
1849 if ($feature->location->isa('Bio::Location::SplitLocationI')) {
1850 for my $sub_location ($feature->location->sub_Location) {
1851
1852 #create a new split feature based on the original feature
1853 my $split_feature = new Bio::SeqFeature::Generic (
1854 -display_name => $feature->display_name,
1855 -strand => $feature->strand,
1856 -primary_tag => $feature->primary_tag,
1857 -frame => $feature->frame,);
1858 #copy all the tags
1859 for my $tag ($feature->all_tags) {
1860 for my $value ($feature->each_tag_value($tag)) {
1861 $split_feature->set_attributes (-tag => {$tag => $value});
1862 }
1863 }
1864
1865 #set the location of the split feature to the sub location
1866 $split_feature->location($sub_location);
1867 $seq_object->add_SeqFeature($split_feature);
1868 }
1869 #change original feature type to 'del_feat' which will be excluded later
1870 #therefore the original complex feature will be excluded from drawing
1871 $feature->primary_tag('del_feat');
1872 }
1873 }
1874 }
1875
1876
1877 sub _writeEmblGenes {
1878 my $options = shift;
1879 my $settings = shift;
1880 my $global = shift;
1881 my $seqObject = shift;
1882 my $strand = shift; #1 or -1
1883 my $rf = shift; #1,2,3, or undefined for all reading frames
1884 _writeGenBankGenes($options, $settings, $global, $seqObject, $strand, $rf);
1885 }
1886
1887 sub _writeGenBankGenes {
1888
1889 my $options = shift;
1890 my $settings = shift;
1891 my $global = shift;
1892 my $seqObject = shift;
1893 my $strand = shift; #1 or -1
1894 my $rf = shift; #1,2,3, or undefined for all reading frames
1895
1896 my $opacity;
1897
1898 #make rf
1899 if ((defined($rf)) && ($rf == 3)) {
1900 $rf = 0;
1901 }
1902
1903 my @outputArray = ();
1904
1905 #for CGView server
1906 my $shading = 'false';
1907 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arrow')) {
1908 $shading = 'true';
1909 }
1910
1911 my $decoration;
1912 if ($strand == 1) {
1913 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"direct\">\n");
1914 $decoration = "clockwise-arrow";
1915 }
1916 else {
1917 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"reverse\">\n");
1918 $decoration = "counterclockwise-arrow";
1919 }
1920
1921 #for CGView server
1922 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arc')) {
1923 $decoration = "arc";
1924 }
1925
1926 #need to get the features from from the GenBank record.
1927 my @features = $seqObject->get_SeqFeatures();
1928 @features = @{_sortFeaturesByStart(\@features)};
1929
1930 if ($strand == 1) {
1931 @features = reverse(@features);
1932 }
1933
1934 foreach(@features) {
1935 my $feat = $_;
1936 my $type = lc($feat->primary_tag);
1937 my $color;
1938
1939 if ($type eq "cds") {
1940 $color = $settings->{'proteinColor'};
1941 $opacity = $settings->{'featureOpacity'};
1942 }
1943 # elsif ($type eq "exon") {
1944 # $color = $settings->{'proteinColor'};
1945 # $opacity = $settings->{'featureOpacity'};
1946 # }
1947
1948 elsif ($type eq "rrna") {
1949 $color = $settings->{'rRNAColor'};
1950 $opacity = $settings->{'featureOpacity'};
1951 }
1952 elsif ($type eq "trna") {
1953 $color = $settings->{'tRNAColor'};
1954 $opacity = $settings->{'featureOpacity'};
1955 }
1956 else {
1957 $color = $settings->{'otherColor'};
1958 $opacity = $settings->{'featureOpacityOther'};
1959 # $decoration = "arc";
1960 }
1961
1962 #skip certain feature types
1963 if ($type eq "source") {
1964 next;
1965 }
1966 if ($type eq "gene") {
1967 next;
1968 }
1969 if ($type eq "exon") {
1970 next;
1971 }
1972 if ($type eq "del_feat") {
1973 next;
1974 }
1975 # if ($type eq "misc_feature") {
1976 # next;
1977 # }
1978
1979 my $st = $feat->strand;
1980 unless($st == $strand) {
1981 next;
1982 }
1983
1984 my $start = $feat->start;
1985 my $stop = $feat->end;
1986
1987 #this handles feature that spans start/stop
1988 #UPDATE: all features with more than 1 entry in @loc are skipped below
1989
1990 my $location = $feat->location;
1991 my $locString = $location->to_FTstring;
1992 my @loc = split(/,/, $locString);
1993
1994 if ($loc[0] =~ m/(\d+)\.\.(\d+)/) {
1995 $start = $1;
1996 }
1997
1998 if ($loc[scalar(@loc) - 1] =~ m/(\d+)\.\.(\d+)/) {
1999 $stop = $2;
2000 }
2001
2002 if (defined($rf)) {
2003 if ($strand == 1) {
2004 unless ($rf == $start % 3) {
2005 next;
2006 }
2007 }
2008 elsif ($strand == -1) {
2009 unless ($rf == ($seqObject->length() - $stop + 1)% 3) {
2010 next;
2011 }
2012 }
2013 }
2014
2015 my $label;
2016 if ($feat->has_tag('gene')) {
2017 $label = join("",$feat->get_tag_values('gene'));
2018 }
2019 elsif ($feat->has_tag('locus_tag')) {
2020 $label = join("",$feat->get_tag_values('locus_tag'));
2021 }
2022 elsif ($feat->has_tag('note')) {
2023 $label = join("",$feat->get_tag_values('note'));
2024 }
2025 else {
2026 $label = $feat->primary_tag;
2027 }
2028
2029 if ($feat->primary_tag eq 'intron') {
2030 $label .= " (intron)";
2031 }
2032
2033 if (length($label) > $settings->{'maxLabelLength'} - 3) {
2034 $label = substr($label, 0, $settings->{'maxLabelLength'} - 3) . "...";
2035 }
2036 $label = _escapeText($label);
2037
2038 #/db_xref="GI:11497049"
2039 my $hyperlink;
2040 if ($feat->has_tag('db_xref')) {
2041 my $dbXref = join("",$feat->get_tag_values('db_xref'));
2042 if ($dbXref =~ m/GI\:(\d+)/) {
2043 $hyperlink = $global->{'ncbiGiLink'} . $1;
2044 }
2045 #/db_xref="GeneID:1132092"
2046 elsif ($dbXref =~ m/GeneID\:(\d+)/) {
2047 $hyperlink = $global->{'ncbiGeneLink'} . $1;
2048 }
2049 }
2050
2051 #/product="conserved hypothetical protein"
2052 my $mouseover = $label . "; " . $start . " to " . $stop;
2053 if ($feat->has_tag('product')) {
2054 my $product = join("",$feat->get_tag_values('product'));
2055 $mouseover = $mouseover . "; " . $product;
2056 }
2057 if ($feat->has_tag('db_xref')) {
2058 my $dbXref = join("",$feat->get_tag_values('db_xref'));
2059 if ($dbXref =~ m/(GI\:\d+)/) {
2060 $mouseover = $mouseover . "; " . $1;
2061 }
2062 #/db_xref="GeneID:1132092"
2063 elsif ($dbXref =~ m/(GeneID\:\d+)/) {
2064 $mouseover = $mouseover . "; " . $1;
2065 }
2066 }
2067 $mouseover = _escapeText($mouseover);
2068
2069 #check length of feature
2070 my $featLength = $stop - $start + 1;
2071 if (scalar(@loc) > 1) {
2072 _message($options, "The following feature of type '$type' has a complex location string and will be ignored: label: $label");
2073 next;
2074 }
2075 if ($featLength == 0) {
2076 _message($options, "The following feature of type '$type' has length equal to zero and will be ignored: label: $label; start: $start; end: $stop");
2077 next;
2078 }
2079 if ($featLength < 0) {
2080 _message($options, "The following feature of type '$type' has 'start' less than 'end' and will be ignored: label: $label; start: $start; end: $stop");
2081 next;
2082 }
2083 if ($featLength > $settings->{maxFeatureSize}) {
2084 _message($options, "The following feature of type '$type' has length greater than $settings->{maxFeatureSize} and will be ignored: label: $label; start: $start; end: $stop");
2085 next;
2086 }
2087 if ($featLength == $seqObject->length()) {
2088 _message($options, "The following feature of type '$type' has length equal to the length of the entire sequence and will be ignored: label: $label; start: $start; end: $stop");
2089 next;
2090 }
2091
2092 #now check entries and make tags
2093 my $feature = "<feature color=\"$color\" decoration=\"$decoration\" opacity=\"$opacity\" ";
2094
2095 if (_isTrue($options->{feature_labels})) {
2096 if (_containsText($label)) {
2097 $feature = $feature . "label=\"$label\" ";
2098 }
2099 if (_containsText($hyperlink)) {
2100 $feature = $feature . "hyperlink=\"$hyperlink\" ";
2101 }
2102 if (_containsText($mouseover)) {
2103 $feature = $feature . "mouseover=\"$mouseover\" ";
2104 }
2105 }
2106
2107 $feature = $feature . ">\n";
2108
2109 my $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" />\n";
2110
2111 #now add $feature and $featureRange to @outputArray
2112 push (@outputArray, $feature . $featureRange . "</feature>\n");
2113
2114
2115 }
2116
2117
2118 push (@outputArray, "</featureSlot>\n");
2119 my $strandTerm = undef;
2120 if ($strand == 1) {
2121 $strandTerm = "forward";
2122 }
2123 if ($strand == -1) {
2124 $strandTerm = "reverse";
2125 }
2126
2127 my $rfTerm = undef;
2128 if (!(defined($rf))) {
2129 $rfTerm = "1,2,3";
2130 }
2131 else {
2132 $rfTerm = $rf;
2133 }
2134
2135 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
2136 print (OUTFILE "\n\n\n\n");
2137 print (OUTFILE "<!-- GenBank or EMBL genes on strand $strandTerm in rf $rfTerm -->\n");
2138 print (OUTFILE join("", @outputArray));
2139 print (OUTFILE "\n");
2140 close (OUTFILE) or die( "Cannot close file : $!");
2141
2142 }
2143
2144 sub _sortFeaturesByStart {
2145 my $features = shift;
2146
2147 @$features = map { $_->[1] }
2148 sort { $a->[0] <=> $b->[0]}
2149 map { [ _getSortValueFeature($_), $_] }
2150 @$features;
2151
2152 return $features;
2153 }
2154
2155 sub _getSortValueFeature {
2156 my $feature = shift;
2157 return $feature->start;
2158 }
2159
2160 sub _containsText {
2161 my $text = shift;
2162
2163 if (!(defined($text))) {
2164 return 0;
2165 }
2166 if ($text =~ m/[A-Za-z0-9]/g) {
2167 return 1;
2168 }
2169 else {
2170 return 0;
2171 }
2172 }
2173
2174 sub _writeGenes {
2175
2176 my $options = shift;
2177 my $settings = shift;
2178 my $global = shift;
2179 my $seqObject = shift;
2180 my $strand = shift; #1 or -1
2181 my $rf = shift; #1,2,3, or undefined for all reading frames
2182 my $analysisColorPos = shift;
2183 my $analysisColorNeg = shift;
2184 my $file = shift;
2185 my $useScore = shift;
2186 my $standardBarGraph = 0;
2187
2188 my $opacity = $settings->{'featureOpacity'};
2189
2190
2191 if ((defined($rf)) && ($rf == 3)) {
2192 $rf = 0;
2193 }
2194
2195 my @outputArray = ();
2196
2197 #for CGView server
2198 my $shading = 'false';
2199 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arrow')) {
2200 $shading = 'true';
2201 }
2202
2203 my $decoration;
2204 if (!($useScore)) {
2205 if ($strand == 1) {
2206 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"direct\">\n");
2207 $decoration = "clockwise-arrow";
2208 }
2209 else {
2210 push (@outputArray, "<featureSlot showShading=\"$shading\" strand=\"reverse\">\n");
2211 $decoration = "counterclockwise-arrow";
2212 }
2213 }
2214 else {
2215 if ($strand == 1) {
2216 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"direct\" featureThickness=\"" . $settings->{'featureThicknessPlot'} . "\">\n");
2217 }
2218 else {
2219 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"reverse\" featureThickness=\"" . $settings->{'featureThicknessPlot'} . "\">\n");
2220 }
2221 $decoration = "arc";
2222 }
2223
2224 #for CGView server
2225 if ((defined($options->{gene_decoration})) && ($options->{gene_decoration} eq 'arc')) {
2226 $decoration = "arc";
2227 }
2228
2229 #need to get the features from the file.
2230 my @features = ();
2231 @features = @{_parseGFF($file, $options, $settings, $global, $seqObject)};
2232
2233 @features = @{_sortGFFByStart(\@features)};
2234
2235 if ($strand == 1) {
2236 @features = reverse(@features);
2237 }
2238
2239 #This section was added 2007-05-30 to normalize score values for plotting
2240 #
2241 if (defined($useScore)) {
2242 my $min = undef;
2243 my $max = undef;
2244 my $average = undef;
2245 my $sum = 0;
2246 my $count = 0;
2247 foreach(@features) {
2248 my $score;
2249 if ($_->{score} =~ m/(\-?[\d\.e\-]+)/) {
2250 $score = $1;
2251 $sum = $sum + $score;
2252 if ((!(defined($max))) || ($score > $max)) {
2253 $max = $score;
2254 }
2255 if ((!(defined($min))) || ($score < $min)) {
2256 $min = $score;
2257 }
2258 $count++;
2259 }
2260 }
2261
2262 #Added 2009-02-22 to allow data sets consisting of all positive values to be plotted as a standard bar graph.
2263 if ((defined($min)) && ($min >= 0)) {
2264 $standardBarGraph = 1;
2265 $max = sprintf("%.4f", $max);
2266 $min = sprintf("%.4f", $min);
2267
2268 _message($options, "Scaling score values in the file $file to between 0 and 1.");
2269 _message($options, "The maximum score value is $max.");
2270 _message($options, "The minimum score value is $min.");
2271
2272 #adjust the score values so that the full width of the featureSlot is used.
2273 foreach(@features) {
2274 my $score;
2275 if ($_->{score} =~ m/(\-?[\d\.e\-]+)/) {
2276 $score = $1;
2277 $_->{score} = sprintf("%.4f", $score / $max);
2278 }
2279 }
2280 }
2281
2282 #If there are negative values, the x-axis is positioned in the middle of the featureSlot
2283 elsif ((defined($min)) && ($min < 0)) {
2284 $average = $sum / $count;
2285
2286 $average = sprintf("%.4f", $average);
2287 $max = sprintf("%.4f", $max);
2288 $min = sprintf("%.4f", $min);
2289
2290 _message($options, "Scaling score values in the file $file to between 1 and -1.");
2291 _message($options, "The maximum score value is $max.");
2292 _message($options, "The minimum score value is $min.");
2293 _message($options, "The average score value is $average.");
2294
2295 my $maxDeviation;
2296 if (($max - $average) > ($average - $min)) {
2297 $maxDeviation = $max - $average;
2298 }
2299 else {
2300 $maxDeviation = $average - $min;
2301 }
2302
2303 #adjust the score values so that the full width of the featureSlot is used.
2304 foreach(@features) {
2305 my $score;
2306 if ($_->{score} =~ m/(\-?[\d\.e\-]+)/) {
2307 $score = $1;
2308 $_->{score} = sprintf("%.4f", ($score - $average) / $maxDeviation);
2309 }
2310 }
2311 }
2312
2313
2314 }
2315
2316
2317 foreach(@features) {
2318 my $feat = $_;
2319 my $type = lc($feat->{'feature'});
2320
2321 #the genes in the GFF file can be coloured by the 'feature' column, based on the
2322 #type of gene (CDS, rRNA, tRNA, or other) or by COG (J, K, L etc).
2323 #colour based on feature type first
2324 my $color;
2325 if ((defined($analysisColorPos)) && (defined($analysisColorNeg))) {
2326 $color = undef;
2327 }
2328 elsif ($type eq "cds") {
2329 $color = $settings->{'proteinColor'};
2330 $opacity = $settings->{'featureOpacity'};
2331 }
2332 elsif ($type eq "rrna") {
2333 $color = $settings->{'rRNAColor'};
2334 $opacity = $settings->{'featureOpacity'};
2335 }
2336 elsif ($type eq "trna") {
2337 $color = $settings->{'tRNAColor'};
2338 $opacity = $settings->{'featureOpacity'};
2339 }
2340 elsif ($type eq "other") {
2341 $color = $settings->{'otherColor'};
2342 $opacity = $settings->{'featureOpacityOther'};
2343 }
2344 elsif (defined($settings->{'cogColors'}->{uc($type)})) {
2345 $color = $settings->{'cogColors'}->{uc($type)};
2346 $opacity = $settings->{'featureOpacity'};
2347 }
2348 else {
2349 $color = $settings->{'otherColor'};
2350 $opacity = $settings->{'featureOpacityOther'};
2351 }
2352
2353 my $st = $feat->{'strand'};
2354 if ($st eq "+") {
2355 $st = 1;
2356 }
2357 elsif ($st eq "-") {
2358 $st = -1;
2359 }
2360
2361 #when using score keep all features regardless of strand
2362 if (($st ne $strand) && (!$useScore)) {
2363 next;
2364 }
2365
2366 my $start = $feat->{'start'};
2367 my $stop = $feat->{'end'};
2368
2369 if (defined($rf)) {
2370 if ($strand == 1) {
2371 unless ($rf == $start % 3) {
2372 next;
2373 }
2374 }
2375 elsif ($strand == -1) {
2376 unless ($rf == ($seqObject->length() - $stop + 1)% 3) {
2377 next;
2378 }
2379 }
2380 }
2381
2382 my $label;
2383 if (_containsText($feat->{'seqname'})) {
2384 $label = $feat->{'seqname'};
2385 if (length($label) > $settings->{'maxLabelLength'} - 3) {
2386 $label = substr($label, 0, $settings->{'maxLabelLength'} - 3) . "...";
2387 }
2388 $label = _escapeText($label);
2389 }
2390
2391 my $mouseover;
2392 if (_containsText($label)) {
2393 $mouseover = $label . "; " . $start . " to " . $stop;
2394 }
2395 else {
2396 $mouseover = $start . " to " . $stop;
2397 }
2398 if (_containsText($feat->{'feature'})) {
2399 $mouseover = $mouseover . "; " . $feat->{'feature'};
2400 $mouseover = _escapeText($mouseover);
2401 }
2402
2403 #now check entries and make tags
2404 my $feature;
2405 if (defined($color)) {
2406 $feature = "<feature color=\"$color\" decoration=\"$decoration\" opacity=\"$opacity\" ";
2407 }
2408 else {
2409 $feature = "<feature decoration=\"$decoration\" opacity=\"$opacity\" ";
2410 }
2411
2412 #don't label genes when $useScore defined
2413 if ((_isTrue($options->{gene_labels})) && (!$useScore)) {
2414 if (_containsText($label)) {
2415 $feature = $feature . "label=\"$label\" ";
2416 }
2417 if (_containsText($mouseover)) {
2418 $feature = $feature . "mouseover=\"$mouseover\" ";
2419 }
2420 }
2421
2422 $feature = $feature . ">\n";
2423
2424 my $featureRange;
2425 if (defined($useScore)) {
2426 #score should be between -1 and 1 at this point
2427 my $score;
2428 if (defined($feat->{'score'})) {
2429 if ($feat->{'score'} =~ m/(\-?[\d\.e\-]+)/) {
2430 $score = $1;
2431 }
2432 }
2433 if (defined($score)) {
2434 if ($score > 1) {
2435 $score = 1;
2436 }
2437 if ($score < -1) {
2438 $score = -1;
2439 }
2440 }
2441 else {
2442 $score = 0;
2443 }
2444
2445 #Want to draw as bars with positive values extending outwards and negative values extending inwards.
2446 #If $standardBarGraph == 1, draw as bars with all values extending outwards.
2447 my $barHeight;
2448 my $radiusShift;
2449
2450 if ($standardBarGraph) {
2451 $barHeight = $score;
2452 $radiusShift = $barHeight / 2.0;
2453 $color = $analysisColorPos;
2454 }
2455 elsif ($score > 0) {
2456 $barHeight = $score;
2457 $barHeight = $barHeight * 0.5;
2458 $radiusShift = 0.5 + $barHeight / 2.0;
2459 $color = $analysisColorPos;
2460 }
2461 elsif ($score < 0) {
2462 $barHeight = 0 - $score;
2463 $barHeight = $barHeight * 0.5;
2464 $radiusShift = 0.5 - $barHeight / 2;
2465 $color = $analysisColorNeg;
2466 }
2467 else {
2468 $radiusShift = 0.5;
2469 $barHeight = $settings->{'plotLineThickness'};
2470 $color = $analysisColorPos;
2471 }
2472
2473 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" radiusAdjustment=\"$radiusShift\" proportionOfThickness=\"$barHeight\" color=\"$color\" />\n";
2474 }
2475 else {
2476 $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" />\n";
2477 }
2478
2479 #now add $feature and $featureRange to @outputArray
2480 push (@outputArray, $feature . $featureRange . "</feature>\n");
2481 }
2482
2483 #Create dashed line to indicate zero
2484 if (defined($useScore)) {
2485 #here a dash is a dash and the space following it.
2486 # if (_isTrue($options->{draw_divider_rings})) {
2487 my $length = $seqObject->length();
2488 my $number_of_dashes = 500;
2489 my $bases_per_dash = $length / $number_of_dashes;
2490 $bases_per_dash = sprintf("%.0f", $bases_per_dash);
2491 if ($bases_per_dash < 2) {
2492 $bases_per_dash = 2;
2493 }
2494 my $i = 1;
2495
2496 #make the dashed line the same thickness as the divider_ring
2497 my $dashed_line_proportion = ($settings->{backboneThickness} * 0.25) / $settings->{featureThicknessPlot};
2498 if ($dashed_line_proportion > 0.05) {
2499 $dashed_line_proportion = 0.05;
2500 }
2501 my $dashed_line_shift = 0.5;
2502
2503 push (@outputArray, "<feature radiusAdjustment=\"$dashed_line_shift\" proportionOfThickness=\"$dashed_line_proportion\" color=\"$settings->{backboneColor}\">\n");
2504
2505 while ($i < $length) {
2506 my $start = $i;
2507 my $stop = $start + sprintf("%.0f", $bases_per_dash / 2);
2508 if ($stop > $length) {
2509 $stop = $length;
2510 }
2511 push (@outputArray, "<featureRange start=\"$start\" stop=\"$stop\" />\n");
2512 $i = $i + $bases_per_dash;
2513 }
2514
2515 push (@outputArray, "</feature>\n");
2516 # }
2517 }
2518
2519 push (@outputArray, "</featureSlot>\n");
2520 my $strandTerm = undef;
2521 if ($strand == 1) {
2522 $strandTerm = "forward";
2523 }
2524 if ($strand == -1) {
2525 $strandTerm = "reverse";
2526 }
2527
2528 my $rfTerm = undef;
2529 if (!(defined($rf))) {
2530 $rfTerm = "1,2,3";
2531 }
2532 else {
2533 $rfTerm = $rf;
2534 }
2535
2536 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
2537 print (OUTFILE "\n\n\n\n");
2538 print (OUTFILE "<!-- genes or analysis on $strandTerm strand in rf $rfTerm -->\n");
2539 print (OUTFILE join("", @outputArray));
2540 print (OUTFILE "\n");
2541 close (OUTFILE) or die( "Cannot close file : $!");
2542
2543 }
2544
2545 sub _parseGFF {
2546 my $file = shift;
2547 my $options = shift;
2548 my $settings = shift;
2549 my $global = shift;
2550 my $seqObject = shift;
2551 my $lineCount = 0;
2552 my @columnTitles = ();
2553 my $columnsRead = 0;
2554 open (INFILE, $file) or die ("Cannot open the GFF file $file");
2555
2556 #check for column titles
2557 while (my $line = <INFILE>) {
2558 $line =~ s/\cM|\n//g;
2559 $lineCount++;
2560 if ($line =~ m/^\#/) {
2561 next;
2562 }
2563 if ($line =~ m/\S/) {
2564 $columnsRead = 1;
2565 @columnTitles = @{_split($line)};
2566 last;
2567 }
2568 }
2569 #print Dumper(@columnTitles);
2570 my @gffColumns = ("seqname", "source", "feature", "start", "end", "score", "strand", "frame");
2571
2572 for (my $i = 0; $i < scalar(@gffColumns); $i++) {
2573 if (!(defined($columnTitles[$i]))) {
2574 _message($options, "Column $i in GFF file $file was not defined - must be titled \"$gffColumns[$i]\".");
2575 die ("Column $i in GFF file $file was not defined - must be titled \"$gffColumns[$i]\"");
2576 }
2577 elsif ($gffColumns[$i] ne lc($columnTitles[$i])) {
2578 _message($options, "Column $i in GFF file $file was titled $columnTitles[$i] - must be titled \"$gffColumns[$i]\".");
2579 die ("Column $i in GFF file $file was titled $columnTitles[$i] - must be titled \"$gffColumns[$i]\"");
2580 }
2581 }
2582
2583 #To allow for 'start', 'end', 'strand', and 'frame' values to be read from the '-sequence' file
2584 #build a hash of genes.
2585 my $sequenceGenes = _getFeatures($global, $seqObject);
2586
2587 #print (Dumper($sequenceGenes));
2588
2589 my @entries = ();
2590 while (my $line = <INFILE>) {
2591 $line =~ s/\cM|\n//g;
2592 $lineCount++;
2593 if ($line =~ m/\S/) {
2594 my @values = @{_split($line)};
2595 my %entry = ();
2596 for (my $i = 0; $i < scalar(@gffColumns); $i++) {
2597 $entry{$gffColumns[$i]} = $values[$i];
2598 }
2599
2600 #try to add to entry if needed
2601 _addToEntry(\%entry, $sequenceGenes);
2602
2603 if (!(defined($entry{'start'}))) {
2604 if (defined($entry{'seqname'})) {
2605 _message($options, "Warning: unable to obtain 'start' value for seqname $entry{seqname} in $file line $lineCount.");
2606 }
2607 else {
2608 _message($options, "Warning: unable to obtain 'start' value for $file line $lineCount.");
2609 }
2610 next;
2611 }
2612
2613 if (!(defined($entry{'end'}))) {
2614 if (defined($entry{'seqname'})) {
2615 _message($options, "Warning: unable to obtain 'end' value for seqname $entry{seqname} in $file line $lineCount.");
2616 }
2617 else {
2618 _message($options, "Warning: unable to obtain 'end' value for $file line $lineCount.");
2619 }
2620 next;
2621 }
2622
2623 if (!(defined($entry{'strand'}))) {
2624 if (defined($entry{'seqname'})) {
2625 _message($options, "Warning: unable to obtain 'strand' value for seqname $entry{seqname} in $file line $lineCount.");
2626 }
2627 else {
2628 _message($options, "Warning: unable to obtain 'strand' value for $file line $lineCount.");
2629 }
2630 next;
2631 }
2632
2633 if (!($entry{'start'} =~ m/^\d+$/)) {
2634 _message($options, "Warning: value in 'start' column must be a positive integer in $file line $lineCount.");
2635 next;
2636 #die ("Value in 'start' column must be a positive integer in $file line $lineCount");
2637 }
2638 if (!($entry{'end'} =~ m/^\d+$/)) {
2639 _message($options, "Warning: value in 'end' column must be a positive integer in $file line $lineCount.");
2640 next;
2641 #die ("Value in 'end' column must be a positive integer in $file line $lineCount");
2642 }
2643 if (!($entry{'strand'} =~ m/^(\+|\-|\.|\s)$/)) {
2644 _message($options, "Warning: value in 'strand' column must be '+', '-', or '.' in $file line $lineCount.");
2645 next;
2646 #die ("Value in 'strand' column must be '+', '-', or '.' in $file line $lineCount");
2647 }
2648
2649 #check start and end
2650 if ($entry{'start'} > $global->{length}) {
2651 _message($options, "Warning: value in 'start' column must be less that the sequence length in $file line $lineCount.");
2652 next;
2653 #die ("Value in 'start' column must be less that the sequence length in $file line $lineCount");
2654 }
2655 if ($entry{'end'} > $global->{length}) {
2656 _message($options, "Warning: value in 'end' column must be less that the sequence length in $file line $lineCount.");
2657 next;
2658 #die ("Value in 'end' column must be less that the sequence length in $file line $lineCount");
2659 }
2660
2661 #Added 2008_07_31
2662 #users often swap the start and end values for features located on the negative strand.
2663 #check for start > end and swap the values if it makes the feature shorter
2664 if ($entry{'start'} > $entry{'end'}) {
2665 my $around_origin_length = ($entry{'end'} - 1 + 1) + ($global->{length} - $entry{'start'} + 1);
2666 my $direct_length = $entry{'start'} - $entry{'end'} + 1;
2667 if ($direct_length < $around_origin_length) {
2668 ($entry{'start'}, $entry{'end'}) = ($entry{'end'}, $entry{'start'});
2669 _message($options, "Warning: swapping values in 'start' and 'end' column so that 'start' is less than 'end' in $file line $lineCount.");
2670 }
2671 }
2672
2673 push (@entries, \%entry);
2674 }
2675 }
2676 close (INFILE) or die( "Cannot close file : $!");
2677 return \@entries;
2678 }
2679
2680 sub _addToEntry {
2681 my $entry = shift;
2682 my $genBankFeatures = shift;
2683
2684 if ((defined($entry->{'seqname'})) && (defined($genBankFeatures->{$entry->{'seqname'}})) && (!(defined($genBankFeatures->{$entry->{'seqname'}}->{multiples})))) {
2685 #will try to determine start, end, strand
2686 if ((!(defined($entry->{'start'}))) || (!($entry->{'start'} =~ m/^\d+$/))) {
2687 $entry->{'start'} = $genBankFeatures->{$entry->{'seqname'}}->{start};
2688 }
2689 if ((!(defined($entry->{'end'}))) || (!($entry->{'end'} =~ m/^\d+$/))) {
2690 $entry->{'end'} = $genBankFeatures->{$entry->{'seqname'}}->{end};
2691 }
2692 if ((!(defined($entry->{'strand'}))) || (!($entry->{'strand'} =~ m/^(\+|\-|\.)$/))) {
2693 $entry->{'strand'} = $genBankFeatures->{$entry->{'seqname'}}->{strand};
2694 }
2695 }
2696 }
2697
2698 sub _getFeatures {
2699 my $global = shift;
2700 my $seqObject = shift;
2701
2702 if (($global->{format} ne "genbank") && ($global->{format} ne "embl")) {
2703 return undef;
2704 }
2705
2706 #need to get the features from from the GenBank record.
2707 my @features = $seqObject->get_SeqFeatures();
2708 @features = @{_sortFeaturesByStart(\@features)};
2709
2710 my %featureHash = ();
2711 foreach(@features) {
2712 my $feat = $_;
2713
2714 my $type = lc($feat->primary_tag);
2715
2716
2717 ################
2718 #added 2007-10-10
2719 #
2720 #Some GenBank files may contain entries like the following:
2721 #
2722 # gene complement(1646..1774)
2723 # /locus_tag="MM1_0006"
2724 # CDS complement(1646..1774)
2725 # /locus_tag="MM1_0006"
2726 #
2727 #These features share the same /locus_tag,
2728 #which was causing this script to declare
2729 #the /locus_tag as having multiple positions.
2730 #This new code block skips several feature types
2731 #that are not drawn on the map anyway, and
2732 #this avoids the problem of having multiple
2733 #positions in many cases.
2734 if ($type eq "source") {
2735 next;
2736 }
2737 if ($type eq "gene") {
2738 next;
2739 }
2740 if ($type eq "exon") {
2741 next;
2742 }
2743 if ($type eq "del_feat") {
2744 next;
2745 }
2746 #################
2747
2748 my $strand = $feat->strand;
2749 my $start = $feat->start;
2750 my $stop = $feat->end;
2751
2752 #this handles feature that spans start/stop
2753 my $location = $feat->location;
2754 my $locString = $location->to_FTstring;
2755 my @loc = split(/,/, $locString);
2756
2757 if ($loc[0] =~ m/(\d+)\.\.(\d+)/) {
2758 $start = $1;
2759 }
2760
2761 if ($loc[scalar(@loc) - 1] =~ m/(\d+)\.\.(\d+)/) {
2762 $stop = $2;
2763 }
2764
2765 my $rf;
2766 if ($strand == 1) {
2767 $rf = $start % 3;
2768 }
2769 elsif ($strand == -1) {
2770 $rf = ($seqObject->length() - $stop + 1) % 3;
2771 }
2772
2773 if ($rf == 0) {
2774 $rf = 3;
2775 }
2776
2777 my $geneName;
2778 if ($feat->has_tag('locus_tag')) {
2779 $geneName = join("",$feat->get_tag_values('locus_tag'));
2780 }
2781
2782 if (!(defined($geneName))) {
2783 next;
2784 }
2785
2786 #change some values
2787 if ($strand == -1) {
2788 $strand = "-";
2789 }
2790 else {
2791 $strand = "+";
2792 }
2793
2794 #add this info to the geneHash
2795 my %geneHash = (start => undef,
2796 end => undef,
2797 strand => undef,
2798 rf => undef);
2799
2800 $geneHash{start} = $start;
2801 $geneHash{end} = $stop;
2802 $geneHash{rf} = $rf;
2803 $geneHash{strand} = $strand;
2804
2805 if (defined($featureHash{$geneName})) {
2806 #if multiple genes have the same tag, mark this gene with the multiples key
2807 $featureHash{$geneName}->{multiples} = 1;
2808 }
2809 else {
2810 $featureHash{$geneName} = \%geneHash;
2811 }
2812 }
2813 return \%featureHash;
2814 }
2815
2816 sub _split {
2817 my $line = shift;
2818 my @values = ();
2819 if ($line =~ m/\t/) {
2820 @values = split(/\t/, $line);
2821 }
2822 elsif ($line =~ m/\,/) {
2823 @values = split(/\,/, $line);
2824 }
2825 else {
2826 @values = split(/\s/, $line);
2827 }
2828 foreach(@values) {
2829 $_ = _cleanValue($_);
2830 }
2831 return \@values;
2832 }
2833
2834 sub _sortGFFByStart {
2835 my $features = shift;
2836
2837 @$features = map { $_->[1] }
2838 sort { $a->[0] <=> $b->[0]}
2839 map { [ _getSortValueGFF($_), $_] }
2840 @$features;
2841
2842 return $features;
2843 }
2844
2845 sub _getSortValueGFF {
2846 my $feature = shift;
2847 return $feature->{'start'};
2848 }
2849
2850 sub _cleanValue {
2851 my $value = shift;
2852 if (!defined($value)) {
2853 return ".";
2854 }
2855 if ($value =~ m/^\s*$/) {
2856 return ".";
2857 }
2858 $value =~ s/^\s+//g;
2859 $value =~ s/\s+$//g;
2860 $value =~ s/\"|\'//g;
2861 return $value;
2862 }
2863
2864
2865
2866 sub _writeFooter {
2867 my $options = shift;
2868 my $footer = "</cgview>\n";
2869 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
2870 print (OUTFILE $footer);
2871 close (OUTFILE) or die( "Cannot close file : $!");
2872 }
2873
2874
2875 sub _writeBlast {
2876 my $options = shift;
2877 my $settings = shift;
2878 my $global = shift;
2879 my $seqObject = shift;
2880 my $strand = shift;
2881 my $file = shift;
2882 my $color = shift;
2883 my $thickness = shift;
2884 my $rf = shift; #1,2,3, or undefined for all reading frames
2885
2886 if ((_isTrue($options->{parse_reading_frame})) && (_containsReadingFrameInfo($settings, $global, $file))) {
2887 #The order needs to be 3, 2, 1 because BLAST results are always drawn on the inside
2888 #of the backbone, and the slots are drawn closet to the backbone first.
2889 _writeBlastResults($options, $settings, $global, $seqObject, 1, $file, $color, $thickness, 3);
2890 if (_isTrue($options->{draw_divider_rings})) {
2891 _drawDivider($options, $settings, $global, $seqObject, $strand, 0.25);
2892 }
2893 _writeBlastResults($options, $settings, $global, $seqObject, 1, $file, $color, $thickness, 2);
2894 if (_isTrue($options->{draw_divider_rings})) {
2895 _drawDivider($options, $settings, $global, $seqObject, $strand, 0.25);
2896 }
2897 _writeBlastResults($options, $settings, $global, $seqObject, 1, $file, $color, $thickness, 1);
2898 _drawDivider($options, $settings, $global, $seqObject, $strand, 1);
2899 _writeBlastResults($options, $settings, $global, $seqObject, -1, $file, $color, $thickness, 1);
2900 if (_isTrue($options->{draw_divider_rings})) {
2901 _drawDivider($options, $settings, $global, $seqObject, $strand, 0.25);
2902 }
2903 _writeBlastResults($options, $settings, $global, $seqObject, -1, $file, $color, $thickness, 2);
2904 if (_isTrue($options->{draw_divider_rings})) {
2905 _drawDivider($options, $settings, $global, $seqObject, $strand, 0.25);
2906 }
2907 _writeBlastResults($options, $settings, $global, $seqObject, -1, $file, $color, $thickness, 3);
2908 }
2909 else {
2910 _writeBlastResults($options, $settings, $global, $seqObject, 1, $file, $color, $thickness, undef);
2911 }
2912 }
2913
2914
2915 sub _drawDivider {
2916
2917 my $options = shift;
2918 my $settings = shift;
2919 my $global = shift;
2920 my $seqObject = shift;
2921 my $strand = shift; #1 or -1
2922 my $proportion_of_backbone = shift;
2923
2924 my $opacity = $settings->{'featureOpacity'};
2925 my @outputArray = ();
2926
2927 my $decoration = "arc";
2928 if ($strand == 1) {
2929 push (@outputArray, "<featureSlot featureThickness=\"" . $settings->{backboneThickness} * $proportion_of_backbone . "\" showShading=\"true\" strand=\"direct\">\n");
2930 }
2931 else {
2932 push (@outputArray, "<featureSlot featureThickness=\"" . $settings->{backboneThickness} * $proportion_of_backbone . "\" showShading=\"true\" strand=\"reverse\">\n");
2933 }
2934
2935 push (@outputArray, "<feature color=\"$settings->{backboneColor}\" decoration=\"$decoration\">\n");
2936 push (@outputArray, "<featureRange start=\"1\" stop=\"$global->{length}\">\n");
2937 push (@outputArray, "</featureRange>\n");
2938 push (@outputArray, "</feature>\n");
2939
2940 push (@outputArray, "</featureSlot>\n");
2941 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
2942 print (OUTFILE "\n\n\n\n");
2943 print (OUTFILE "<!-- BLAST results divider -->\n");
2944 print (OUTFILE join("", @outputArray));
2945 print (OUTFILE "\n");
2946 close (OUTFILE) or die( "Cannot close file : $!");
2947 }
2948
2949 sub _writeBlastResults {
2950 my $options = shift;
2951 my $settings = shift;
2952 my $global = shift;
2953 my $seqObject = shift;
2954 my $strand = shift;
2955 my $file = shift;
2956 my $color = shift;
2957 my $thickness = shift;
2958 my $rf = shift; #1,2,3, or undefined for all reading frames
2959
2960
2961 my $opacity = $settings->{'featureOpacity'};
2962
2963 my @outputArray = ();
2964
2965 #blast results are always drawn on the inside of the backbone. Strand is used when parse_reading_frame is specified
2966 my $decoration = "arc";
2967 if ($strand == 1) {
2968 push (@outputArray, "<featureSlot strand=\"reverse\" showShading=\"false\" featureThickness=\"" . $thickness . "\">\n");
2969 }
2970 else {
2971 push (@outputArray, "<featureSlot strand=\"reverse\" showShading=\"false\" featureThickness=\"" . $thickness . "\">\n");
2972 }
2973
2974 #may want to mark query positions on the map, regardless of whether they
2975 #produced hits.
2976 if (_isTrue($options->{'show_queries'})) {
2977 my @queries = ();
2978 @queries = @{_parseBlastQueries($file, $settings)};
2979 @queries = @{_sortBLASTByStart(\@queries)};
2980
2981 if ($strand == 1) {
2982 @queries = reverse(@queries);
2983 }
2984 #draw queries as faint features
2985 push(@outputArray, "<feature>\n");
2986 foreach(@queries) {
2987 #if a reading frame is specified, only want to draw
2988 #queries from this reading frame and strand
2989 if ((defined($rf)) && (defined($_->{q_rf}))) {
2990 unless (($rf == $_->{q_rf}) && ($strand == $_->{q_strand})) {
2991 next;
2992 }
2993 }
2994 push(@outputArray, "<featureRange start=\"$_->{q_start}\" stop=\"$_->{q_end}\" opacity=\"0.1\" radiusAdjustment=\"0.0\" proportionOfThickness=\"1.0\" color=\"$settings->{orfColor}\" />\n");
2995 }
2996 push(@outputArray, "</feature>\n");
2997 }
2998
2999 my @features = ();
3000 @features = @{_parseBLAST($file, $settings, $global)};
3001 @features = @{_sortBLASTByStart(\@features)};
3002
3003 if ($strand == 1) {
3004 @features = reverse(@features);
3005 }
3006
3007
3008 foreach(@features) {
3009 my $feat = $_;
3010
3011 my $start = $feat->{'q_start'};
3012 my $stop = $feat->{'q_end'};
3013
3014 if ((defined($rf)) && (defined($feat->{'q_rf'}))) {
3015
3016 my $queryFrame = $feat->{'q_rf'};
3017 my $queryStrand = $feat->{'q_strand'};
3018
3019 unless (($rf == $queryFrame) && ($strand == $queryStrand)) {
3020 next;
3021 }
3022 }
3023
3024 my $label;
3025 if (_containsText($feat->{'match_id'})) {
3026 $label = $feat->{'match_id'};
3027 if (length($label) > $settings->{'maxLabelLength'} - 3) {
3028 $label = substr($label, 0, $settings->{'maxLabelLength'} - 3) . "...";
3029 }
3030 $label = _escapeText($label);
3031 }
3032
3033 #gi|15678261
3034 my $hyperlink;
3035 if (_containsText($feat->{'match_id'})) {
3036 if ($feat->{'match_id'} =~ m/gi\|(\d+)/) {
3037 $hyperlink = $global->{'ncbiGiLink'} . $1;
3038 }
3039 #GeneID:1132092"
3040 elsif ($feat->{'match_id'} =~ m/geneid\|(\d+)/) {
3041 $hyperlink = $global->{'ncbiGeneLink'} . $1;
3042 }
3043 }
3044
3045 my $mouseover;
3046 if (_containsText($label)) {
3047 $mouseover = $label . "; " . $start . " to " . $stop;
3048 }
3049 else {
3050 $mouseover = $start . " to " . $stop;
3051 }
3052 if (_containsText($feat->{'match_description'})) {
3053 $mouseover = $mouseover . " " . $feat->{'match_description'};
3054 }
3055
3056 $mouseover = $mouseover . "; percent identity=" . $feat->{'%_identity'} . "; alignment length=" . $feat->{alignment_length} . "; evalue=" . $feat->{'evalue'};
3057 $mouseover = _escapeText($mouseover);
3058
3059 #now check entries and make tags
3060 if (_isTrue($options->{use_opacity})) {
3061 $opacity = ".20";
3062 }
3063 else {
3064 $opacity = "1.0";
3065 }
3066
3067 my $feature = "<feature color=\"$color\" decoration=\"$decoration\" opacity=\"$opacity\" ";
3068
3069 if (_isTrue($options->{hit_labels})) {
3070 if (_containsText($label)) {
3071 $feature = $feature . "label=\"$label\" ";
3072 }
3073 if (_containsText($hyperlink)) {
3074 $feature = $feature . "hyperlink=\"$hyperlink\" ";
3075 }
3076 if (_containsText($mouseover)) {
3077 $feature = $feature . "mouseover=\"$mouseover\" ";
3078 }
3079 }
3080
3081 $feature = $feature . ">\n";
3082
3083
3084 #use %_identity to determine thickness of drawn feature
3085 my $thickness = $feat->{'%_identity'} / 100.0;
3086 $thickness = sprintf("%.10f", ($thickness));
3087
3088 my $featureRange = "<featureRange start=\"$start\" stop=\"$stop\" radiusAdjustment=\"0.0\" proportionOfThickness=\"$thickness\" />\n";
3089 #now add $feature and $featureRange to @outputArray
3090 push (@outputArray, $feature . $featureRange . "</feature>\n");
3091 }
3092
3093 push (@outputArray, "</featureSlot>\n");
3094 my $strandTerm = undef;
3095 if ($strand == 1) {
3096 $strandTerm = "forward";
3097 }
3098 if ($strand == -1) {
3099 $strandTerm = "reverse";
3100 }
3101
3102 my $rfTerm = undef;
3103 if (!(defined($rf))) {
3104 $rfTerm = "1,2,3";
3105 }
3106 else {
3107 $rfTerm = $rf;
3108 }
3109
3110 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
3111 print (OUTFILE "\n\n\n\n");
3112 print (OUTFILE "<!-- BLAST results on $strandTerm strand in rf $rfTerm -->\n");
3113 print (OUTFILE join("", @outputArray));
3114 print (OUTFILE "\n");
3115 close (OUTFILE) or die( "Cannot close file : $!");
3116 }
3117
3118 sub _containsReadingFrameInfo {
3119 my $settings = shift;
3120 my $global = shift;
3121 my $file = shift;
3122
3123 return _parseBLAST($file, $settings, $global, 1);
3124 }
3125
3126 sub _parseBLAST {
3127 my $file = shift;
3128 my $settings = shift;
3129 my $global = shift;
3130 my $check_for_reading_frame_info = shift;
3131
3132 #The file can contain comments starting with'#'
3133 #The file must have a line beginning with a 'query_id' and indicating the column names:
3134 #query_id match_id match_description %_identity alignment_length mismatches gap_openings q_start q_end s_start s_end evalue bit_score
3135
3136 my @required = ('query_id', '%_identity', 'q_start', 'q_end', 'alignment_length', 'evalue');
3137
3138 my $lineCount = 0;
3139 my @columnTitles = ();
3140 my $columnsRead = 0;
3141
3142 #program will be used to store the value of #PROGRAM in the blast results header.
3143 #if it is blastp or tblastn then q_start and q_end are in residues and need to
3144 #be converted to bases.
3145 my $program = undef;
3146
3147 open (INFILE, $file) or die ("Cannot open the BLAST results file $file");
3148
3149 #check for column titles
3150 while (my $line = <INFILE>) {
3151 $line =~ s/\cM|\n//g;
3152 $lineCount++;
3153 if ($line =~ m/^\#PROGRAM\s*=\s*([^\s]+)/) {
3154 $program = $1;
3155 }
3156 if ($line =~ m/^\#/) {
3157 next;
3158 }
3159 if ($line =~ m/^query_id/) {
3160 $columnsRead = 1;
3161 @columnTitles = @{_split($line)};
3162 last;
3163 }
3164 }
3165
3166 if (!(defined($program))) {
3167 die ("Cannot parse the #PROGRAM field in the BLAST results file $file");
3168 }
3169
3170 #print Dumper(@columnTitles);
3171
3172 #now check for required columns
3173 foreach(@required) {
3174 my $req = $_;
3175 my $match = 0;
3176 foreach(@columnTitles) {
3177 my $columnTitle = $_;
3178 if ($columnTitle eq $req) {
3179 $match = 1;
3180 last;
3181 }
3182 }
3183 if (!($match)) {
3184 die ("The BLAST results in $file do not contain a column labeled $req");
3185 }
3186 }
3187
3188 #factor to convert amino acid scales to base scales
3189 my $scale;
3190 if (($program =~ m/^blastp$/) || ($program =~ m/^tblastn$/)) {
3191 $scale = 3;
3192 }
3193 else {
3194 $scale = 1;
3195 }
3196
3197 #read the remaining entries
3198 my @entries = ();
3199 while (my $line = <INFILE>) {
3200 $line =~ s/\cM|\n//g;
3201 $lineCount++;
3202 if ($line =~ m/^\#/) {
3203 next;
3204 }
3205 if ($line =~ m/\S/) {
3206 my @values = @{_split($line)};
3207 #skip lines with missing values
3208 if (scalar(@values) != scalar(@columnTitles)) {
3209 next;
3210 }
3211
3212 my %entry = ();
3213 for (my $i = 0; $i < scalar(@columnTitles); $i++) {
3214 $entry{$columnTitles[$i]} = $values[$i];
3215 }
3216
3217 #do some error checking of values
3218 #check query_id, %_identity, q_start, and q_end
3219 #skip if no identity value
3220 if (!($entry{'%_identity'} =~ m/\d/)) {
3221 die ("No \%_identity value BLAST results $file line $lineCount");
3222 }
3223
3224 if (!($entry{'q_start'} =~ m/\d/)) {
3225 die ("No q_start value BLAST results $file line $lineCount");
3226 }
3227 if (!($entry{'q_end'} =~ m/\d/)) {
3228 die ("No q_end value BLAST results $file line $lineCount");
3229 }
3230
3231 #try to add reading frame and strand information using information
3232 #in the query_id:
3233 #orf3_start=3691;end=3858;strand=1;rf=1
3234 #orf16_start=8095;end=8178;strand=1;rf=1
3235 #
3236 #The q_start and q_end values are region of the query that matched
3237 #the hit. Depending on the search type, these can be in amino acids
3238 #or bases. The values in the query_id are always in bases. The
3239 #scale factor is used to convert the q_start and q_end values
3240 #so that they can be used to adjust the values in the query_id.
3241 #This allows hits to be mapped to the genomic sequence
3242 if ($entry{'query_id'} =~ m/start=(\d+);end=\d+;strand=(\-*\d+);rf=(\d+)\s*$/) {
3243 $entry{'q_start'} = $entry{'q_start'} * $scale + $1 - 1 * $scale;
3244 $entry{'q_end'} = $entry{'q_end'} * $scale + $1 - 1 * $scale;
3245 $entry{'q_strand'} = $2;
3246 $entry{'q_rf'} = $3;
3247 }
3248
3249 #try to determine reading frame of query even if not explicitly given in title,
3250 #by using the position of the hit relative to the source sequence.
3251 #sequence_start=286;end=385;length=100;source_length=1000
3252 #start and end refer to direct strand
3253 #
3254 if ($entry{'query_id'} =~ m/start=(\d+);end=\d+;length=\d+;source_length=(\d+)\s*$/) {
3255 $entry{'q_start'} = $entry{'q_start'} * $scale + $1 - 1 * $scale;
3256 $entry{'q_end'} = $entry{'q_end'} * $scale + $1 - 1 * $scale;
3257 my $source_length = $2;
3258
3259 #for blastn results the s_start and s_end reversed to indicate reverse strand
3260 if ($program =~ m/^blastn$/) {
3261 if ($entry{'s_start'} > $entry{'s_end'}) {
3262 $entry{'q_strand'} = -1;
3263 }
3264 else {
3265 $entry{'q_strand'} = 1;
3266 }
3267 }
3268 #for tblastx and blastx results the q_start and q_end are reversed to indicate reverse strand
3269 elsif (($program =~ m/^tblastx$/) || ($program =~ m/^blastx$/)) {
3270 if ($entry{'q_start'} > $entry{'q_end'}) {
3271 $entry{'q_strand'} = -1;
3272 }
3273 else {
3274 $entry{'q_strand'} = 1;
3275 }
3276 }
3277
3278
3279 if (($program =~ m/^blastx$/) || ($program =~ m/^tblastx$/)) {
3280
3281 if ($entry{'q_strand'} == 1) {
3282 $entry{'q_rf'} = $entry{'q_start'} % 3;
3283 }
3284 elsif ($entry{'q_strand'} == -1) {
3285 my $end = $entry{'q_end'};
3286 if ($entry{'q_start'} > $entry{'q_end'}) {
3287 $end = $entry{'q_start'};
3288 }
3289 $entry{'q_rf'} = ($source_length - $end + 1) % 3;
3290 }
3291
3292 if ($entry{'q_rf'} == 0) {
3293 $entry{'q_rf'} = 3;
3294 }
3295 }
3296 else {
3297 $entry{'q_rf'} = undef;
3298 }
3299
3300 }
3301
3302 #try to adjust q_start and q_end using information in title when rf and strand not specified
3303 #start=X;end=Y
3304 if ($entry{'query_id'} =~ m/start=(\d+);end=\d+\s*$/) {
3305 $entry{'q_start'} = $entry{'q_start'} * $scale + $1 - 1 * $scale;
3306 $entry{'q_end'} = $entry{'q_end'} * $scale + $1 - 1 * $scale;
3307 }
3308
3309 #check q_start and q_end to make sure they are in range
3310 #depending on the blast search, the q_start may be larger than the q_end,
3311 #if this is the case, swap them
3312 if ($entry{'q_start'} > $entry{'q_end'}) {
3313 my $temp = $entry{'q_start'};
3314 $entry{'q_start'} = $entry{'q_end'};
3315 $entry{'q_end'} = $temp;
3316 }
3317
3318 if ($entry{'q_start'} < 1) {
3319 die ("q_start value " . $entry{'q_start'} . " is less than 1 BLAST results $file line $lineCount");
3320 }
3321 if ($entry{'q_end'} < 1) {
3322 die ("q_end value " . $entry{'q_end'} . " is less than 1 BLAST results $file line $lineCount");
3323 }
3324 if ($entry{'q_start'} > $global->{'length'}) {
3325 die ("q_start value " . $entry{'q_start'} . " is greater than sequence length " . $global->{'length'} . " BLAST results $file line $lineCount");
3326 }
3327 if ($entry{'q_end'} > $global->{'length'}) {
3328 die ("q_end value " . $entry{'q_end'} . " is greater than sequence length " . $global->{'length'} . " BLAST results $file line $lineCount");
3329 }
3330 if ($check_for_reading_frame_info) {
3331 if (defined($entry{'q_rf'})) {
3332 return 1;
3333 }
3334 else {
3335 return 0;
3336 }
3337 }
3338 push (@entries, \%entry);
3339 }
3340 }
3341 close (INFILE) or die("Cannot close file : $!");
3342 return \@entries;
3343 }
3344
3345
3346 sub _sortBLASTByStart {
3347 my $features = shift;
3348
3349 @$features = map { $_->[1] }
3350 sort { $a->[0] <=> $b->[0]}
3351 map { [ _getSortValueBLAST($_), $_] }
3352 @$features;
3353
3354 return $features;
3355 }
3356
3357 sub _getSortValueBLAST {
3358 my $feature = shift;
3359 return $feature->{'q_start'};
3360 }
3361
3362 sub _parseBlastQueries {
3363 my $file = shift;
3364 my $lineCount = 0;
3365
3366 open (INFILE, $file) or die ("Cannot open the BLAST results file $file");
3367 #won't bother checking columns.
3368 my %entries = ();
3369 while (my $line = <INFILE>) {
3370 $line =~ s/\cM|\n//g;
3371 $lineCount++;
3372 #orf1_start=142;end=255;strand=1;rf=1
3373 #orf_900_start=142;end=255;strand=1;rf=1
3374 #some gene_start=142;end=255;strand=1;rf=1
3375 if ($line =~ m/^([^\t]+)_start=(\d+);end=(\d+);strand=(\-*\d+);rf=(\d+)/) {
3376 my %entry = ();
3377 $entry{'q_start'} = $2;
3378 $entry{'q_end'} = $3;
3379 $entry{'q_strand'} = $4;
3380 $entry{'q_rf'} = $5;
3381 $entries{$1 . $2 . $3 . $4} = \%entry;
3382 }
3383 #some gene_start=142;end=255
3384 elsif ($line =~ m/^([^\t]+)_start=(\d+);end=(\d+)/) {
3385 my %entry = ();
3386 $entry{'q_start'} = $2;
3387 $entry{'q_end'} = $3;
3388 $entry{'q_strand'} = "1";
3389 $entry{'q_rf'} = undef;
3390 $entries{$1 . $2 . $3} = \%entry;
3391 }
3392 }
3393 close (INFILE) or die( "Cannot close file : $!");
3394 my @values = values(%entries);
3395 return \@values;
3396 }
3397
3398 sub _writeBaseContent {
3399 my $options = shift;
3400 my $settings = shift;
3401 my $seqObject = shift;
3402
3403 #1 or -1. Strand here only determines whether graph is drawn on outside or inside of backbone.
3404 my $strand = shift;
3405
3406 #type should be gc_content, at_content, gc_skew, or at_skew.
3407 my $type = shift;
3408
3409 my $plotTerm;
3410 if ($type eq 'gc_content') {
3411 $plotTerm = "GC content";
3412 }
3413 elsif ($type eq 'gc_skew') {
3414 $plotTerm = "GC skew";
3415 }
3416 elsif ($type eq 'at_content') {
3417 $plotTerm = "AT content";
3418 }
3419 elsif ($type eq 'at_skew') {
3420 $plotTerm = "AT skew";
3421 }
3422
3423 my $globalBaseContentInfo = _getGlobalBaseContentInfo($options, $settings, $seqObject, $type);
3424
3425 #The max, min, and average have been scaled for some plot types to facilatate CGView plotting.
3426 #When returning these to the user, adjust the values so that they are in the standard ranges.
3427 my $actualMax;
3428 my $actualMin;
3429 my $actualAverage;
3430
3431 if (($type eq 'gc_content') || ($type eq 'at_content')) {
3432 $actualMax = $globalBaseContentInfo->{'max'};
3433 $actualMin = $globalBaseContentInfo->{'min'};
3434 $actualAverage = $globalBaseContentInfo->{'average'};
3435 }
3436 elsif (($type eq 'gc_skew') || ($type eq 'at_skew')) {
3437 $actualMax = _skewCorrect($globalBaseContentInfo->{'max'});
3438 $actualMin = _skewCorrect($globalBaseContentInfo->{'min'});
3439 $actualAverage = _skewCorrect($globalBaseContentInfo->{'average'});
3440 }
3441
3442 $actualMax = sprintf("%.4f", $actualMax);
3443 $actualMin = sprintf("%.4f", $actualMin);
3444 $actualAverage = sprintf("%.4f", $actualAverage);
3445
3446 _message($options, "Plotting $plotTerm using a window size of $options->{window} and a step of $options->{step}.");
3447 _message($options, "The maximum $plotTerm value is $actualMax.");
3448 _message($options, "The minimum $plotTerm value is $actualMin.");
3449 _message($options, "The average $plotTerm value is $actualAverage.");
3450
3451 if (_isTrue($options->{scale})) {
3452 _message($options, "$plotTerm will be scaled based on the maximum and minimum values.");
3453 }
3454
3455 if (_isTrue($options->{average})) {
3456 _message($options, "$plotTerm will be plotted as the deviation from the average value.");
3457 }
3458
3459 my $opacity = $settings->{'featureOpacity'};
3460 my $decoration = 'arc';
3461 my $positiveColor;
3462 my $negativeColor;
3463 if ($type eq 'gc_content') {
3464 $positiveColor = $settings->{'gcColorPos'};
3465 $negativeColor = $settings->{'gcColorNeg'};
3466 }
3467 elsif ($type eq 'at_content') {
3468 $positiveColor = $settings->{'atColorPos'};
3469 $negativeColor = $settings->{'atColorNeg'};
3470 }
3471 elsif ($type eq 'gc_skew') {
3472 $positiveColor = $settings->{'gcSkewColorPos'};
3473 $negativeColor = $settings->{'gcSkewColorNeg'};
3474 }
3475 elsif ($type eq 'at_skew') {
3476 $positiveColor = $settings->{'atSkewColorPos'};
3477 $negativeColor = $settings->{'atSkewColorNeg'};
3478 }
3479
3480 my $upstreamLength = sprintf("%.f", $options->{'window'} / 2);
3481 my $downstreamLength = $options->{'window'} - $upstreamLength;
3482 my $step = $options->{'step'};
3483 my $isLinear = undef;
3484 if ($settings->{'isLinear'} eq "true") {
3485 $isLinear = 1;
3486 }
3487 else {
3488 $isLinear = 0;
3489 }
3490
3491 my $dna = $seqObject->seq();
3492 my $originalLength = length($dna);
3493 my $subseq;
3494 my $value;
3495 my $positionCorrection = 0;
3496 my $firstBase;
3497 my $lastBase;
3498
3499 if (!($isLinear)) {
3500 my $prefix = substr($dna, length($dna) - $upstreamLength, $upstreamLength);
3501 my $suffix = substr($dna, 0, $downstreamLength);
3502 $dna = $prefix . $dna . $suffix;
3503 $positionCorrection = length($prefix);
3504 }
3505
3506 my $length = length($dna);
3507 my $maxDeviationUp = $globalBaseContentInfo->{'max'} - $globalBaseContentInfo->{'average'};
3508 my $maxDeviationDown = $globalBaseContentInfo->{'average'} - $globalBaseContentInfo->{'min'};
3509 my $average = $globalBaseContentInfo->{'average'};
3510 my $maxDeviation;
3511 if ($maxDeviationUp > $maxDeviationDown) {
3512 $maxDeviation = $maxDeviationUp;
3513 }
3514 else {
3515 $maxDeviation = $maxDeviationDown;
3516 }
3517
3518 my @outputArray = ();
3519 if ($strand == 1) {
3520 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"direct\" featureThickness=\"" . $settings->{'featureThicknessPlot'} . "\">\n");
3521 }
3522 else {
3523 push (@outputArray, "<featureSlot showShading=\"false\" strand=\"reverse\" featureThickness=\"" . $settings->{'featureThicknessPlot'} . "\">\n");
3524 }
3525
3526 push (@outputArray, "<feature decoration=\"$decoration\" opacity=\"$opacity\">\n");
3527
3528 for (my $i = 1 + $upstreamLength; $i <= $length - $downstreamLength; $i = $i + $step) {
3529
3530 $subseq = substr($dna, $i - $upstreamLength - 1, ($i + $downstreamLength) - ($i - $upstreamLength - 1));
3531
3532 #These set the width and position of the "point" on the map.
3533 #They are not the actual first base and last base in the sliding window.
3534 $firstBase = $i - $positionCorrection;
3535 $lastBase = $firstBase + $step;
3536
3537 ####2007-01-14
3538 ####This hack should prevent background from appearing in plots.
3539 if (($originalLength > 10000) || ($settings->{backboneRadius} > 1000)) {
3540 $lastBase = $lastBase + sprintf("%.f", (2.0 * $step));
3541 $firstBase = $firstBase - sprintf("%.f", (2.0 * $step));
3542 }
3543 ###
3544
3545 if ($firstBase < 1) {
3546 $firstBase = 1;
3547 }
3548
3549 if ($lastBase > $originalLength) {
3550 $lastBase = $originalLength;
3551 }
3552
3553 $value = _calc($type, $subseq);
3554 #want bars above middle line for values > 0.5 and below middle line for values < 0.5
3555
3556 my $barHeight;
3557 my $radiusShift;
3558 my $color;
3559
3560 if ($value > $average) {
3561 $color = $positiveColor;
3562 $barHeight = $value - $average;
3563 $barHeight = $barHeight * 0.5 / $maxDeviation;
3564 $radiusShift = 0.5 + $barHeight / 2.0;
3565 }
3566 elsif ($value < $average) {
3567 $color = $negativeColor;
3568 $barHeight = $average - $value;
3569 $barHeight = $barHeight * 0.5 / $maxDeviation;
3570 $radiusShift = 0.5 - $barHeight / 2;
3571 }
3572 else {
3573 $color = $positiveColor;
3574 $radiusShift = 0.5;
3575 $barHeight = $settings->{'plotLineThickness'};
3576 }
3577
3578 push (@outputArray, "<featureRange color=\"" . $color . "\" start=\"$firstBase\" stop=\"$lastBase\" proportionOfThickness=\"" . $barHeight . "\" radiusAdjustment=\"$radiusShift\" />\n");
3579 }
3580
3581 push (@outputArray, "</feature>\n");
3582
3583 push (@outputArray, "</featureSlot>\n");
3584 my $strandTerm = undef;
3585 if ($strand == 1) {
3586 $strandTerm = "forward";
3587 }
3588 if ($strand == -1) {
3589 $strandTerm = "reverse";
3590 }
3591
3592 open (OUTFILE, "+>>" . $options->{"output"}) or die( "Cannot open file : $!" );
3593 print (OUTFILE "\n\n\n\n");
3594 # print (OUTFILE "<!-- $plotTerm for $strandTerm strand -->\n");
3595 print (OUTFILE "<!-- $plotTerm -->\n");
3596 print (OUTFILE join("", @outputArray));
3597 print (OUTFILE "\n");
3598 close (OUTFILE) or die( "Cannot close file : $!");
3599
3600 }
3601
3602 sub _getGlobalBaseContentInfo {
3603 my $options = shift;
3604 my $settings = shift;
3605 my $seqObject = shift;
3606
3607 #type should be gc_content, at_content, gc_skew, or at_skew.
3608 my $type = shift;
3609
3610 my %globalBaseContentInfo = (min => 1,
3611 max => 0,
3612 average => 0);
3613
3614 my $upstreamLength = sprintf("%.f", $options->{'window'} / 2);
3615 my $downstreamLength = $options->{'window'} - $upstreamLength;
3616 my $step = $options->{'step'};
3617 my $isLinear = undef;
3618 if ($settings->{'isLinear'} eq "true") {
3619 $isLinear = 1;
3620 }
3621 else {
3622 $isLinear = 0;
3623 }
3624
3625 my $dna = $seqObject->seq();
3626
3627 if (_isTrue($options->{'average'})) {
3628 $globalBaseContentInfo{'average'} = _calc($type, $dna);
3629 }
3630 else {
3631 $globalBaseContentInfo{'average'} = 0.5;
3632 }
3633
3634 if (!(_isTrue($options->{'scale'}))) {
3635 $globalBaseContentInfo{'min'} = 0;
3636 $globalBaseContentInfo{'max'} = 1;
3637 return \%globalBaseContentInfo;
3638 }
3639
3640 my $subseq;
3641 my $value;
3642
3643 if (!($isLinear)) {
3644 my $prefix = substr($dna, length($dna) - $upstreamLength, $upstreamLength);
3645 my $suffix = substr($dna, 0, $downstreamLength);
3646 $dna = $prefix . $dna . $suffix;
3647 }
3648
3649 my $length = length($dna);
3650
3651 for (my $i = 1 + $upstreamLength; $i <= $length - $downstreamLength; $i = $i + $step) {
3652
3653 $subseq = substr($dna, $i - $upstreamLength - 1, ($i + $downstreamLength) - ($i - $upstreamLength - 1));
3654
3655 $value = _calc($type, $subseq);
3656
3657 if ($value > $globalBaseContentInfo{'max'}) {
3658 $globalBaseContentInfo{'max'} = $value;
3659 }
3660
3661 if ($value < $globalBaseContentInfo{'min'}) {
3662 $globalBaseContentInfo{'min'} = $value;
3663 }
3664 }
3665
3666 return \%globalBaseContentInfo;
3667
3668 }
3669
3670 sub _calc {
3671 my $type = shift;
3672 my $dna = shift;
3673
3674 if ($type eq "gc_content") {
3675 return _calcGCContent($dna);
3676 }
3677 elsif ($type eq "at_content") {
3678 return _calcATContent($dna);
3679 }
3680 elsif ($type eq "gc_skew") {
3681 return _calcGCSkew($dna);
3682 }
3683 elsif ($type eq "at_skew") {
3684 return _calcATSkew($dna);
3685 }
3686 else {
3687 die ("unknown calc type");
3688 }
3689 }
3690
3691 sub _calcGCContent {
3692 my $dna = lc(shift);
3693 my $total = length($dna);
3694 my $g = ($dna =~ tr/g/G/);
3695 my $c = ($dna =~ tr/c/C/);
3696
3697 if ($total == 0) {
3698 return 0.5;
3699 }
3700
3701 return sprintf("%.5f", (($g + $c) / $total));
3702 }
3703
3704 sub _calcATContent {
3705 my $dna = lc(shift);
3706 my $total = length($dna);
3707 my $a = ($dna =~ tr/a/A/);
3708 my $t = ($dna =~ tr/t/T/);
3709
3710 if ($total == 0) {
3711 return 0.5;
3712 }
3713
3714 return sprintf("%.5f", (($a + $t) / $total));
3715 }
3716
3717 sub _calcGCSkew {
3718 my $dna = lc(shift);
3719 my $c = ($dna =~ tr/c/C/);
3720 my $g = ($dna =~ tr/g/G/);
3721
3722 if (($g + $c) == 0) {
3723 return 0.5;
3724 }
3725
3726 #gives value between -1 and 1
3727 my $value = ($g - $c) / ($g + $c);
3728
3729 #scale to a value between 0 and 1
3730 $value = 0.5 + $value / 2.0;
3731
3732 return sprintf("%.5f", ($value));
3733 }
3734
3735 sub _calcATSkew {
3736 my $dna = lc(shift);
3737 my $a = ($dna =~ tr/a/A/);
3738 my $t = ($dna =~ tr/t/T/);
3739
3740 if (($a + $t) == 0) {
3741 return 0.5;
3742 }
3743
3744 #gives value between -1 and 1
3745 my $value = ($a - $t) / ($a + $t);
3746
3747 #scale to a value between 0 and 1
3748 $value = 0.5 + $value / 2.0;
3749
3750 return sprintf("%.5f", ($value));
3751 }
3752
3753 #for reporting values to user
3754 sub _skewCorrect {
3755 my $value = shift;
3756 return (2*($value - 0.5));
3757 }