[Bio] / FigKernelScripts / run_glimmer3.pl Repository:
ViewVC logotype

Annotation of /FigKernelScripts/run_glimmer3.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (view) (download) (as text)

1 : overbeek 1.1 # -*- perl -*-
2 :    
3 : gdpusch 1.2 use strict;
4 :     use warnings;
5 : overbeek 1.1
6 : gdpusch 1.2 use FIG;
7 :     my $fig = FIG->new();
8 : overbeek 1.1
9 : gdpusch 1.2 $0 =~ m/([^\/]+)$/;
10 :     my $self = $1;
11 : gdpusch 1.11 my $usage = "$self [-code=genetic_code_num] [-minlen=min_training_len] [-train=training_tbl[,training_contigs]] [-skip_called] Taxon_ID contigs";
12 : gdpusch 1.2
13 : gdpusch 1.3
14 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 :     #...PATH environment variables
16 :     #-----------------------------------------------------------------------
17 : gdpusch 1.4 my $elph_bin = $ENV{ELPH_BIN} || $FIG_Config::ext_bin;
18 :     my $glimmer_bin = $ENV{GLIMMER_3_BIN} || "$FIG_Config::ext_bin/../apps/glimmer3/bin";
19 :     my $glimmer_scripts = $ENV{GLIMMER_3_SCRIPTS} || "$FIG_Config::ext_bin/../apps/glimmer3/bin";
20 : gdpusch 1.3
21 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 :     #...Defaults for vars set during argument parsing
23 :     #-----------------------------------------------------------------------
24 :     my $genetic_code = 11; #...Default to the "standard" code
25 : gdpusch 1.2
26 :     my $train = qq(); #...Flag associated with '-train' switch
27 :     my $training_tbl = qq();
28 :     my $training_contigs = qq();
29 :    
30 : gdpusch 1.11 my $min_training_len = 2000; #...Shortest contig used for self-training
31 :    
32 : gdpusch 1.9 my $glimmeropts = qq(-o50 -g110 -t30 -l); #...NOTE: Make these the default for a switch
33 : gdpusch 1.2
34 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
35 :     # NOTE: '-skip' is an obscure option introduced to handle one job
36 :     # for which new contigs had been added to a set of existing contigs,
37 :     # and it was required that we not re-call the existing contigs,
38 :     # but instead use their calls as the 'training set' for the new contigs.
39 :     # It seems quite unlikely that this option will ever be used again.
40 :     #-----------------------------------------------------------------------
41 :     my %skip; #...Hash storing already-called contig IDs to be skipped.
42 :     my $skip_called = qq(); #...Flag indicating whether '-skip' switch is present.
43 :    
44 :    
45 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
46 :     #... Parse argument list
47 :     #-----------------------------------------------------------------------
48 :     my $trouble = 0;
49 : overbeek 1.5 for (my $arg=$#ARGV; ($arg >= 0); $arg--)
50 : overbeek 1.1 {
51 : gdpusch 1.10 if ($ARGV[$arg] =~ m/^-{1,2}help/) {
52 :     warn "\n\tusage: $usage\n\n";
53 :     exit(0);
54 :     }
55 :     elsif ($ARGV[$arg] =~ m/^-{1,2}code=(\d+)/) {
56 : gdpusch 1.2 $genetic_code = $1;
57 : gdpusch 1.7 splice(@ARGV, $arg, 1);
58 : overbeek 1.1 }
59 : gdpusch 1.11 elsif ($ARGV[$arg] =~ m/^-{1,2}minlen=(\d+)/) {
60 :     $min_training_len = $1;
61 :     splice(@ARGV, $arg, 1);
62 :     }
63 : overbeek 1.5 elsif ($ARGV[$arg] =~ m/^-{1,2}train=(\S+)/) {
64 : gdpusch 1.2 $train = $1;
65 :     ($training_tbl, $training_contigs) = split /,/, $train;
66 :    
67 :     if (-s $training_tbl) {
68 :     print STDERR "Training features will be taken from $training_tbl\n"
69 :     if $ENV{VERBOSE};
70 :     }
71 :     else {
72 :     $trouble = 1;
73 :     warn "Training tbl $training_tbl does not exist";
74 :     }
75 :    
76 :     if ($training_contigs) {
77 :     if (-s $training_contigs) {
78 :     print STDERR "Training set will be extracted from $training_contigs\n"
79 :     if $ENV{VERBOSE};
80 : overbeek 1.1 }
81 : gdpusch 1.2 else {
82 : overbeek 1.1 $trouble = 1;
83 : gdpusch 1.2 warn "Training contigs $training_contigs do not exist";
84 : overbeek 1.1 }
85 :     }
86 : gdpusch 1.7 splice(@ARGV, $arg, 1);
87 : overbeek 1.1 }
88 : overbeek 1.5 elsif ($ARGV[$arg] =~ m/^-{1,2}skip_called/) {
89 :     $skip_called = $ARGV[$arg];
90 : gdpusch 1.2 print STDERR "Skip option is set\n" if $ENV{VERBOSE};
91 : overbeek 1.5 splice(@ARGV,$arg,1);
92 : overbeek 1.1 }
93 : overbeek 1.5 elsif ($ARGV[$arg] =~ /^-/) {
94 : gdpusch 1.2 $trouble = 1;
95 : overbeek 1.5 warn "Unrecognized argument $ARGV[$arg]";
96 : overbeek 1.1 }
97 :     }
98 :    
99 : gdpusch 1.2 my $genetic_code_switch = qq(-z $genetic_code);
100 : overbeek 1.1
101 :    
102 : gdpusch 1.2 my $taxon_ID = qq();
103 :     my $contigs_file = qq();
104 : overbeek 1.1
105 : gdpusch 1.2 if (@ARGV == 2) {
106 :     ($taxon_ID, $contigs_file) = @ARGV;
107 : overbeek 1.6 if (! $training_contigs) { $training_contigs = $contigs_file }
108 :    
109 : gdpusch 1.2 if ($taxon_ID && ($taxon_ID =~ m/^\d+\.\d+$/)) {
110 :     print STDERR "Taxon-ID is $taxon_ID\n" if $ENV{VERBOSE};
111 :     } else {
112 :     $trouble = 1;
113 :     warn "Bad Taxon-ID $taxon_ID\n";
114 : overbeek 1.1 }
115 :    
116 : gdpusch 1.2 if (-s $contigs_file) {
117 :     print STDERR "Contigs file is $contigs_file\n" if $ENV{VERBOSE};
118 :     } else {
119 :     $trouble = 1;
120 :     warn "Missing or zero-size contigs file $contigs_file";
121 : overbeek 1.1 }
122 :     }
123 : gdpusch 1.7 else {
124 :     $trouble = 1;
125 :     print STDERR
126 :     qq(\nWrong number of mandatory arguments: ),
127 :     &FIG::flatten_dumper(@ARGV),
128 :     qq(\n);
129 :     }
130 : gdpusch 1.2 die "\n\tusage: $usage\n\n" if $trouble;
131 :    
132 :    
133 :    
134 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135 :     # ... Initial Training Pass
136 :     #-----------------------------------------------------------------------
137 :     if (not $training_contigs) {
138 :     $training_contigs = $contigs_file;
139 :     }
140 :    
141 : gdpusch 1.3 my $tmp_prefix = "$FIG_Config::temp/tmp$$";
142 :     $fig->run("rm -f $tmp_prefix.*");
143 :    
144 : gdpusch 1.11 my $tmp_contig = "$tmp_prefix.contig";
145 :     my $tmp_coords = "$tmp_prefix.coords";
146 :     my $tmp_train = "$tmp_prefix.train";
147 :     my $tmp_model = "$tmp_prefix.icm";
148 : gdpusch 1.2
149 :     my ($contig_id, $seqP);
150 :    
151 :     if (not $train) {
152 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
153 :     print STDERR "\nFinding training ORFs using GLIMMER default procedure\n"
154 :     if $ENV{VERBOSE};
155 :     #-----------------------------------------------------------------------
156 : overbeek 1.1 if (-s "$tmp_train") {
157 :     system("rm -f $tmp_train")
158 :     && die "Could not remove $tmp_train";
159 :     }
160 :    
161 :     open(CONTIGS, "<$training_contigs")
162 : gdpusch 1.7 || die "could not read-open training contigs file: $training_contigs";
163 : overbeek 1.1
164 : gdpusch 1.3 $training_tbl = "$tmp_prefix.train.tbl";
165 : gdpusch 1.7 open(TRAINING, ">$training_tbl") || die "Could not write-open training_tbl file: $training_tbl";
166 : gdpusch 1.2
167 :     my $orf_num = 0;
168 :     my %len_of; #...Hash storing training contig lens
169 : overbeek 1.1 while (($contig_id, $seqP) = &read_fasta_record(\*CONTIGS))
170 :     {
171 : gdpusch 1.10 my $len = $len_of{$contig_id} = length($$seqP);
172 : overbeek 1.1
173 :     open( TMP, ">$tmp_contig") || die "Could not write-open $tmp_contig";
174 :     &display_id_and_seq($contig_id, $seqP, \*TMP);
175 :     close(TMP) || die "Could not close $tmp_contig";
176 :    
177 : gdpusch 1.11 if ($len >= $min_training_len) {
178 : gdpusch 1.10 print STDERR "\nScanning contig $contig_id for long orfs\n" if $ENV{VERBOSE};
179 :     }
180 :     else {
181 :     print STDERR "\nSkipping contig $contig_id --- too short (len=$len)\n" if $ENV{VERBOSE};
182 :     next;
183 :     }
184 : gdpusch 1.2
185 : gdpusch 1.11 my $longorfs_err = qq();
186 :     my $tmp_longorfs_err = "$tmp_prefix.longorfs.err";
187 :     my $rc = system("$glimmer_bin/long-orfs -l -n -t 1.15 $genetic_code_switch $tmp_contig $tmp_coords > $tmp_longorfs_err 2>&1");
188 :     if (-s $tmp_longorfs_err) {
189 :     $longorfs_err = &FIG::file_read($tmp_longorfs_err);
190 :     print STDERR $longorfs_err if $ENV{VERBOSE};
191 :     }
192 :     if ($rc && $longorfs_err) {
193 :     if ($longorfs_err =~ m/WHAT: ERROR: No valid orfs found below entropy cutoff/so) {
194 :     #...Error is harmless --- ignore it
195 :     }
196 :     else {
197 :     die "Could not extract training ORFs from contig $contig_id";
198 :     }
199 :     }
200 : gdpusch 1.3
201 :     system("$glimmer_bin/extract -t $tmp_contig $tmp_coords >> $tmp_train")
202 : overbeek 1.1 && die "Could not extract training sequences from contig $contig_id";
203 : gdpusch 1.2
204 :     #... Translate GLIMMER $tmp_coords into SEED $training_tbl
205 :     open(TMP_COORDS, "<$tmp_coords") || die "Could not read-open $tmp_coords";
206 :     print TRAINING map { ++$orf_num;
207 :     chomp $_;
208 :     my (undef, $beg, $end) = split /\s+/o, $_;
209 :     die "Bad coords in entry: $_" unless ($beg && $end);
210 :     my $fid = qq(orf) . (qq(0)x(5-length($orf_num))) . $orf_num;
211 :     my $loc = join(qq(_), ($contig_id, $beg, $end));
212 :     $_ = qq($fid\t$loc\n)
213 :     } <TMP_COORDS>;
214 :     }
215 :     close(TRAINING) || die "Could not close $training_tbl";
216 :     close(CONTIGS) || die "Could not close $contigs_file";
217 :     }
218 :    
219 :    
220 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
221 :     print STDERR ("\n",
222 :     "Training using:\n",
223 :     " contigs --- $training_contigs\n",
224 :     " ORFs --- $training_tbl\n"
225 :     )
226 :     if $ENV{VERBOSE};
227 :     #-----------------------------------------------------------------------
228 :     open(CONTIGS, "<$training_contigs")
229 :     || die "Could not read-open $training_contigs";
230 :    
231 :     my ($len_of, $seq_of);
232 :     while (($contig_id, $seqP) = &read_fasta_record(\*CONTIGS)) {
233 :     $len_of->{$contig_id} = length($$seqP);
234 :     $seq_of->{$contig_id} = $$seqP;
235 :     }
236 :     close(CONTIGS) || die "Could not close $contigs_file";
237 :    
238 :     my $entry;
239 :     my $orf_num;
240 :     my $max_orf_num = 0;
241 :     my %training_tbl;
242 :     open(TBL, "<$training_tbl") || die "Could not read-open $training_tbl";
243 :     open(TRAIN, ">$tmp_train") || die "Could not write-open $tmp_train";
244 :     while (defined($entry = <TBL>)) {
245 :     chomp $entry;
246 :     my ($fid, $locus) = split /\t/, $entry;
247 :    
248 :     if ($fid =~ m/(\d+)$/) {
249 :     $orf_num = $1;
250 :     $max_orf_num = ($orf_num > $max_orf_num) ? $orf_num : $max_orf_num;
251 :     }
252 :     else {
253 :     die "Could not parse FID $fid for training entry $.: $entry";
254 :     }
255 :    
256 :     #...If -skip_called option was selected, record which contigs are in the training set
257 :     if ($skip_called) { $skip{$contig_id} = 1; }
258 :    
259 :     my $training_seq = qq();
260 :     my @exons = split /,/, $locus;
261 :     foreach my $exon (@exons) {
262 :     if ($exon =~ m/^(\S+)_(\d+)_(\d+)/) {
263 :     my ($contig, $beg, $end) = ($1, $2, $3);
264 :    
265 :     my $contig_seq = $seq_of->{$contig};
266 :    
267 :     my $dna = &get_dna_seq($contig, $beg, $end, $len_of, $seq_of);
268 :    
269 :     $training_seq .= lc($dna);
270 :     }
271 :     else {
272 :     die "Could not parse exon $exon for training entry $.: $entry";
273 :     }
274 :    
275 :     my $training_ID = qq(orf) . (qq(0) x (5-length($orf_num))) . $orf_num;
276 :     &display_id_and_seq($training_ID, \$training_seq, \*TRAIN);
277 : overbeek 1.1 }
278 :     }
279 : gdpusch 1.2 close(TRAIN) || die "Could not close $tmp_train";
280 :     close(TBL) || die "Could not close $training_tbl";
281 : overbeek 1.1
282 : gdpusch 1.2 if (($_ = `grep -c "^>" $tmp_train`) && ($_ =~ m/^\s*(\d+)/)) {
283 :     print STDERR "\nExtracted $1 training sequences\n\n" if $ENV{VERBOSE};
284 : overbeek 1.1 }
285 : gdpusch 1.2 else {
286 : overbeek 1.1 die "\nCould not extract any training sequences";
287 :     }
288 :    
289 :    
290 : gdpusch 1.2
291 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
292 :     #... Build ICM (interpolated context model)
293 :     #-----------------------------------------------------------------------
294 :     print STDERR ("Building interpolated context model ---\n",
295 :     " output in $tmp_model\n\n"
296 :     ) if $ENV{VERBOSE};
297 :    
298 :     if (-s "$tmp_model") {
299 : overbeek 1.1 system("rm -f $tmp_model") && die "Could not remove $tmp_model";
300 :     }
301 :    
302 : gdpusch 1.3 $fig->run("$glimmer_bin/build-icm -r $tmp_model < $tmp_train");
303 : gdpusch 1.2
304 :    
305 :    
306 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
307 :     #... First GLIMMER pass
308 :     #-----------------------------------------------------------------------
309 :     print STDERR "Running first GLIMMER pass\n" if $ENV{VERBOSE};
310 : gdpusch 1.8 $fig->run("$glimmer_bin/glimmer3 $glimmeropts $genetic_code_switch $contigs_file $tmp_model $tmp_prefix.pass_1");
311 : gdpusch 1.2
312 : overbeek 1.1
313 : gdpusch 1.2
314 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
315 :     #... Extract upstream regions and START codon counts
316 :     #-----------------------------------------------------------------------
317 : gdpusch 1.3 my $tmp_predictions_pass_1 = "$tmp_prefix.pass_1.predict";
318 : gdpusch 1.2 print STDERR "\nExtracting upstream regions and START counts from $tmp_predictions_pass_1\n"
319 :     if $ENV{VERBOSE};
320 :    
321 :     open(PREDICT, "<$tmp_predictions_pass_1")
322 :     || die "Could not read-open $tmp_predictions_pass_1";
323 :    
324 : gdpusch 1.3 my $tmp_upstream = "$tmp_prefix.upstream";
325 : gdpusch 1.2 open(UPSTREAM, ">$tmp_upstream")
326 :     || die "Could not write-open $tmp_upstream";
327 :    
328 :     my %start_counts; #...Hash to hold START codon counts
329 :    
330 :     $contig_id = qq();
331 :     while(defined($entry = <PREDICT>)) {
332 :     chomp $entry;
333 :    
334 :     if ($entry =~ m/^>(\S+)/) {
335 :     $contig_id = $1;
336 : overbeek 1.1 next;
337 :     }
338 : gdpusch 1.2 elsif ($entry =~ m/^(\S+)\s+(\d+)\s+(\d+)/) {
339 :     my ($id, $beg, $end) = ($1, $2, $3);
340 :    
341 :     my $sign = ($beg < $end) ? +1 : -1;
342 :    
343 :     my $end_start = $beg + $sign * 2;
344 :     my $start_codon = &get_dna_seq($contig_id, $beg, $end_start, $len_of, $seq_of);
345 :    
346 :     ++$start_counts{0};
347 :     ++$start_counts{$start_codon};
348 :    
349 :     my $up_id = $id;
350 :     $up_id =~ s/^orf/ups/o;
351 :     my $up_beg = $beg - $sign * 25;
352 :     my $up_end = $beg - $sign;
353 :    
354 :     my $up_seq;
355 :     if ($up_seq = &get_dna_seq($contig_id, $up_beg, $up_end, $len_of, $seq_of)) {
356 :     &display_id_and_seq($id, \$up_seq, \*UPSTREAM);
357 :     }
358 :     }
359 :     else {
360 :     die "Could not parse prediction $.: $entry";
361 :     }
362 :     }
363 :    
364 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
365 :     #... Compute pseudocount-stabilized START codon frequencies
366 :     #-----------------------------------------------------------------------
367 :     my $atg_freq = ($start_counts{atg} + 80) / ($start_counts{0} + 100);
368 :     my $gtg_freq = ($start_counts{gtg} + 15) / ($start_counts{0} + 100);
369 :     my $ttg_freq = ($start_counts{ttg} + 5) / ($start_counts{0} + 100);
370 :     my $start_freqs = "$atg_freq,$gtg_freq,$ttg_freq";
371 :    
372 :     print STDERR "start_freqs = $start_freqs\n\n" if $ENV{VERBOSE};
373 :    
374 :    
375 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
376 :     #... Build Position Weight Matrix for upstream regions using ELPH
377 :     #-----------------------------------------------------------------------
378 : gdpusch 1.4 print STDERR "\nExtracting upstream motifs from $tmp_predictions_pass_1\n";
379 :    
380 :     my $motif_width = 6;
381 :     my $tmp_motifs = "$tmp_prefix.motifs";
382 :     system("$elph_bin/elph $tmp_upstream LEN=$motif_width > $tmp_motifs")
383 :     && ((-s $tmp_motifs)
384 :     || die("Could not extract upstream motifs")
385 :     );
386 :    
387 :    
388 :     print STDERR "\nBuilding PWM from upstream motifs in $tmp_motifs\n";
389 :    
390 :     open( MOTIFS, "<$tmp_motifs") || die "Could not read-open $tmp_motifs";
391 :     my @motifs = <MOTIFS>;
392 :     close(MOTIFS) || die "Could not close $tmp_motifs";
393 :     while ($motifs[0] !~ m/^Motif counts/) { shift @motifs };
394 :     shift @motifs;
395 :     my $last = 0;
396 :     for (my $i=0; $i < @motifs; ++$i) {
397 :     last unless ($motifs[$i] =~ m/\S+/);
398 :     if ($motifs[$i] =~ m/^[acgt]:((\s+\d+){$motif_width})/) {
399 :     $last = $i;
400 :     }
401 :     }
402 :     $#motifs = $last;
403 : gdpusch 1.2
404 : gdpusch 1.3 my $tmp_matrix = "$tmp_prefix.pwm";
405 : gdpusch 1.4 open( MATRIX, ">$tmp_matrix") || die "Could not write-open $tmp_matrix";
406 :     print MATRIX "$motif_width\n";
407 :     foreach my $line (@motifs) {
408 :     chomp $line;
409 :     my @fields = split /\s+/, $line;
410 :     my $base = substr((shift @fields), 0, 1);
411 :     print MATRIX ($base, (map { sprintf(qq(%7u), $_) } @fields), qq(\n));
412 :     }
413 :     close(MATRIX) || die "Could not close $tmp_matrix";
414 :    
415 :     (-s $tmp_matrix) || die "Could not construct PWM --- empty file $tmp_matrix";
416 : gdpusch 1.2
417 :    
418 :    
419 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
420 :     #... Re-call PEGs using ICM and PWM
421 :     #-----------------------------------------------------------------------
422 :     print STDERR "Re-calling PEGs using trained ICM\n" if $ENV{VERBOSE};
423 :    
424 : gdpusch 1.8 $fig->run("$glimmer_bin/glimmer3 $glimmeropts $genetic_code_switch -b $tmp_matrix -P $start_freqs $contigs_file $tmp_model $tmp_prefix");
425 : gdpusch 1.3
426 : gdpusch 1.2
427 :    
428 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
429 :     #... Parse and write final predictions
430 :     # NOTE: Does not yet handle '-skip' option
431 :     #-----------------------------------------------------------------------
432 :     my $tmp_predictions = "$tmp_prefix.predict";
433 :     print STDERR "\nParsing $tmp_predictions\n"
434 :     if $ENV{VERBOSE};
435 :    
436 :     open(PREDICT, "<$tmp_predictions")
437 :     || die "Could not read-open $tmp_predictions";
438 :    
439 :     my $fid_num = 0;
440 :    
441 :     $contig_id = qq();
442 :     while(defined($entry = <PREDICT>)) {
443 :     chomp $entry;
444 : overbeek 1.1
445 : gdpusch 1.2 if ($entry =~ m/^>(\S+)/) {
446 :     $contig_id = $1;
447 :     next;
448 :     }
449 :     elsif ($entry =~ m/^(\S+)\s+(\d+)\s+(\d+)/) {
450 :     my ($id, $beg, $end) = ($1, $2, $3);
451 : overbeek 1.1
452 : gdpusch 1.2 if ($beg && $end) {
453 :     ++$fid_num;
454 :     my $fid = qq(fig|$taxon_ID.peg.) . $fid_num;
455 :     print STDOUT (qq($fid\t), join(qq(_), ($contig_id, $beg, $end)), qq(\n));
456 : overbeek 1.1 }
457 : gdpusch 1.2 else {
458 :     die "Error in $tmp_predictions line $.: $entry";
459 : overbeek 1.1 }
460 :     }
461 : gdpusch 1.2 else {
462 :     die "Could not parse prediction $.: $entry";
463 :     }
464 : overbeek 1.1 }
465 :    
466 : gdpusch 1.4 if (not $ENV{VERBOSE}) {
467 : gdpusch 1.3 $fig->run("rm -f $tmp_prefix.*");
468 :     }
469 : gdpusch 1.4 else {
470 :     print STDERR "\nKeeping tmp files\n\n";
471 :     }
472 : overbeek 1.1
473 :     exit 0;
474 :    
475 :    
476 : gdpusch 1.2 ########################################################################
477 :     sub get_dna_seq {
478 :     my ($contig, $beg, $end, $len_of, $seq_of) = @_;
479 :     my $dna = qq();
480 :    
481 :     my $contig_len = $len_of->{$contig};
482 :     my $contig_seq = $seq_of->{$contig};
483 :    
484 :     return undef if (&max($beg, $end) > $contig_len);
485 :     return undef if (&min($beg, $end) < 1);
486 :    
487 :     if ($beg < $end) {
488 :     $dna = substr($contig_seq, ($beg-1), ($end+1-$beg));
489 :     }
490 :     else {
491 :     $dna = substr($contig_seq, ($end-1), ($beg+1-$end));
492 :     $dna = $ { &rev_comp(\$dna) };
493 :     }
494 :    
495 :     return lc($dna);
496 :     }
497 :    
498 : overbeek 1.1
499 :     sub read_fasta_record
500 :     {
501 :     my ($file_handle) = @_;
502 :     my ( $old_end_of_record, $fasta_record, @lines, $head, $sequence, $seq_id, $comment, @parsed_fasta_record );
503 :    
504 :     if (not defined($file_handle)) { $file_handle = \*STDIN; }
505 :    
506 :     $old_end_of_record = $/;
507 :     $/ = "\n>";
508 :    
509 :     if (defined($fasta_record = <$file_handle>))
510 :     {
511 :     chomp $fasta_record;
512 :     @lines = split( /\n/, $fasta_record );
513 :     $head = shift @lines;
514 :     $head =~ s/^>?//;
515 :     $head =~ m/^(\S+)/;
516 :     $seq_id = $1;
517 :    
518 :     if ($head =~ m/^\S+\s+(.*)$/) { $comment = $1; } else { $comment = ""; }
519 :    
520 :     $sequence = join( "", @lines );
521 :    
522 :     @parsed_fasta_record = ( $seq_id, \$sequence, $comment );
523 :     }
524 :     else
525 :     {
526 :     @parsed_fasta_record = ();
527 :     }
528 :    
529 :     $/ = $old_end_of_record;
530 :    
531 :     return @parsed_fasta_record;
532 :     }
533 :    
534 :     sub display_id_and_seq
535 :     {
536 :     my( $id, $seq, $fh ) = @_;
537 :    
538 :     if (! defined($fh) ) { $fh = \*STDOUT; }
539 :    
540 :     print $fh ">$id\n";
541 :     &display_seq($seq, $fh);
542 :     }
543 :    
544 :     sub display_seq
545 :     {
546 :     my ( $seq, $fh ) = @_;
547 :     my ( $i, $n, $ln );
548 :    
549 :     if (! defined($fh) ) { $fh = \*STDOUT; }
550 :    
551 :     $n = length($$seq);
552 :     # confess "zero-length sequence ???" if ( (! defined($n)) || ($n == 0) );
553 :     for ($i=0; ($i < $n); $i += 60)
554 :     {
555 :     if (($i + 60) <= $n)
556 :     {
557 :     $ln = substr($$seq,$i,60);
558 :     }
559 :     else
560 :     {
561 :     $ln = substr($$seq,$i,($n-$i));
562 :     }
563 :     print $fh "$ln\n";
564 :     }
565 :     }
566 :    
567 :     sub rev_comp
568 :     {
569 :     my( $seqP ) = @_;
570 :     my( $rev );
571 :    
572 :     $rev = reverse( $$seqP );
573 :     $rev =~ tr/a-z/A-Z/;
574 :     $rev =~ tr/ACGTUMRWSYKBDHV/TGCAAKYWSRMVHDB/;
575 :     return \$rev;
576 :     }
577 :    
578 :     sub min { my ($x, $y) = @_; return (($x < $y) ? $x : $y); }
579 :    
580 :     sub max { my ($x, $y) = @_; return (($x > $y) ? $x : $y); }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3