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

Annotation of /FigKernelScripts/run_glimmer3.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (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 : gdpusch 1.12
193 :     if ($rc) {
194 :     if ($longorfs_err) {
195 :     if ($longorfs_err =~ m/WHAT: ERROR: No valid orfs found below entropy cutoff/so) {
196 :     #...Error is harmless --- ignore it
197 :     }
198 :     else {
199 :     die (qq(Could not extract training ORFs from contig $contig_id --- return-code $rc:\n), $longorfs_err);
200 :     }
201 : gdpusch 1.11 }
202 :     else {
203 : gdpusch 1.12 die qq(Could not extract training ORFs from contig $contig_id --- return-code $rc);
204 : gdpusch 1.11 }
205 :     }
206 : gdpusch 1.3
207 :     system("$glimmer_bin/extract -t $tmp_contig $tmp_coords >> $tmp_train")
208 : overbeek 1.1 && die "Could not extract training sequences from contig $contig_id";
209 : gdpusch 1.2
210 :     #... Translate GLIMMER $tmp_coords into SEED $training_tbl
211 :     open(TMP_COORDS, "<$tmp_coords") || die "Could not read-open $tmp_coords";
212 :     print TRAINING map { ++$orf_num;
213 :     chomp $_;
214 :     my (undef, $beg, $end) = split /\s+/o, $_;
215 :     die "Bad coords in entry: $_" unless ($beg && $end);
216 :     my $fid = qq(orf) . (qq(0)x(5-length($orf_num))) . $orf_num;
217 :     my $loc = join(qq(_), ($contig_id, $beg, $end));
218 :     $_ = qq($fid\t$loc\n)
219 :     } <TMP_COORDS>;
220 :     }
221 :     close(TRAINING) || die "Could not close $training_tbl";
222 :     close(CONTIGS) || die "Could not close $contigs_file";
223 :     }
224 :    
225 :    
226 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
227 :     print STDERR ("\n",
228 :     "Training using:\n",
229 :     " contigs --- $training_contigs\n",
230 :     " ORFs --- $training_tbl\n"
231 :     )
232 :     if $ENV{VERBOSE};
233 :     #-----------------------------------------------------------------------
234 :     open(CONTIGS, "<$training_contigs")
235 :     || die "Could not read-open $training_contigs";
236 :    
237 :     my ($len_of, $seq_of);
238 :     while (($contig_id, $seqP) = &read_fasta_record(\*CONTIGS)) {
239 :     $len_of->{$contig_id} = length($$seqP);
240 :     $seq_of->{$contig_id} = $$seqP;
241 :     }
242 :     close(CONTIGS) || die "Could not close $contigs_file";
243 :    
244 :     my $entry;
245 :     my $orf_num;
246 :     my $max_orf_num = 0;
247 :     my %training_tbl;
248 :     open(TBL, "<$training_tbl") || die "Could not read-open $training_tbl";
249 :     open(TRAIN, ">$tmp_train") || die "Could not write-open $tmp_train";
250 :     while (defined($entry = <TBL>)) {
251 :     chomp $entry;
252 :     my ($fid, $locus) = split /\t/, $entry;
253 :    
254 :     if ($fid =~ m/(\d+)$/) {
255 :     $orf_num = $1;
256 :     $max_orf_num = ($orf_num > $max_orf_num) ? $orf_num : $max_orf_num;
257 :     }
258 :     else {
259 :     die "Could not parse FID $fid for training entry $.: $entry";
260 :     }
261 :    
262 :     #...If -skip_called option was selected, record which contigs are in the training set
263 :     if ($skip_called) { $skip{$contig_id} = 1; }
264 :    
265 :     my $training_seq = qq();
266 :     my @exons = split /,/, $locus;
267 :     foreach my $exon (@exons) {
268 :     if ($exon =~ m/^(\S+)_(\d+)_(\d+)/) {
269 :     my ($contig, $beg, $end) = ($1, $2, $3);
270 :    
271 :     my $contig_seq = $seq_of->{$contig};
272 :    
273 :     my $dna = &get_dna_seq($contig, $beg, $end, $len_of, $seq_of);
274 :    
275 :     $training_seq .= lc($dna);
276 :     }
277 :     else {
278 :     die "Could not parse exon $exon for training entry $.: $entry";
279 :     }
280 :    
281 :     my $training_ID = qq(orf) . (qq(0) x (5-length($orf_num))) . $orf_num;
282 :     &display_id_and_seq($training_ID, \$training_seq, \*TRAIN);
283 : overbeek 1.1 }
284 :     }
285 : gdpusch 1.2 close(TRAIN) || die "Could not close $tmp_train";
286 :     close(TBL) || die "Could not close $training_tbl";
287 : overbeek 1.1
288 : gdpusch 1.2 if (($_ = `grep -c "^>" $tmp_train`) && ($_ =~ m/^\s*(\d+)/)) {
289 :     print STDERR "\nExtracted $1 training sequences\n\n" if $ENV{VERBOSE};
290 : overbeek 1.1 }
291 : gdpusch 1.2 else {
292 : overbeek 1.1 die "\nCould not extract any training sequences";
293 :     }
294 :    
295 :    
296 : gdpusch 1.2
297 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
298 :     #... Build ICM (interpolated context model)
299 :     #-----------------------------------------------------------------------
300 :     print STDERR ("Building interpolated context model ---\n",
301 :     " output in $tmp_model\n\n"
302 :     ) if $ENV{VERBOSE};
303 :    
304 :     if (-s "$tmp_model") {
305 : overbeek 1.1 system("rm -f $tmp_model") && die "Could not remove $tmp_model";
306 :     }
307 :    
308 : gdpusch 1.3 $fig->run("$glimmer_bin/build-icm -r $tmp_model < $tmp_train");
309 : gdpusch 1.2
310 :    
311 :    
312 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
313 :     #... First GLIMMER pass
314 :     #-----------------------------------------------------------------------
315 :     print STDERR "Running first GLIMMER pass\n" if $ENV{VERBOSE};
316 : gdpusch 1.8 $fig->run("$glimmer_bin/glimmer3 $glimmeropts $genetic_code_switch $contigs_file $tmp_model $tmp_prefix.pass_1");
317 : gdpusch 1.2
318 : overbeek 1.1
319 : gdpusch 1.2
320 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
321 :     #... Extract upstream regions and START codon counts
322 :     #-----------------------------------------------------------------------
323 : gdpusch 1.3 my $tmp_predictions_pass_1 = "$tmp_prefix.pass_1.predict";
324 : gdpusch 1.2 print STDERR "\nExtracting upstream regions and START counts from $tmp_predictions_pass_1\n"
325 :     if $ENV{VERBOSE};
326 :    
327 :     open(PREDICT, "<$tmp_predictions_pass_1")
328 :     || die "Could not read-open $tmp_predictions_pass_1";
329 :    
330 : gdpusch 1.3 my $tmp_upstream = "$tmp_prefix.upstream";
331 : gdpusch 1.2 open(UPSTREAM, ">$tmp_upstream")
332 :     || die "Could not write-open $tmp_upstream";
333 :    
334 :     my %start_counts; #...Hash to hold START codon counts
335 :    
336 :     $contig_id = qq();
337 :     while(defined($entry = <PREDICT>)) {
338 :     chomp $entry;
339 :    
340 :     if ($entry =~ m/^>(\S+)/) {
341 :     $contig_id = $1;
342 : overbeek 1.1 next;
343 :     }
344 : gdpusch 1.2 elsif ($entry =~ m/^(\S+)\s+(\d+)\s+(\d+)/) {
345 :     my ($id, $beg, $end) = ($1, $2, $3);
346 :    
347 :     my $sign = ($beg < $end) ? +1 : -1;
348 :    
349 :     my $end_start = $beg + $sign * 2;
350 :     my $start_codon = &get_dna_seq($contig_id, $beg, $end_start, $len_of, $seq_of);
351 :    
352 :     ++$start_counts{0};
353 :     ++$start_counts{$start_codon};
354 :    
355 :     my $up_id = $id;
356 :     $up_id =~ s/^orf/ups/o;
357 :     my $up_beg = $beg - $sign * 25;
358 :     my $up_end = $beg - $sign;
359 :    
360 :     my $up_seq;
361 :     if ($up_seq = &get_dna_seq($contig_id, $up_beg, $up_end, $len_of, $seq_of)) {
362 :     &display_id_and_seq($id, \$up_seq, \*UPSTREAM);
363 :     }
364 :     }
365 :     else {
366 :     die "Could not parse prediction $.: $entry";
367 :     }
368 :     }
369 :    
370 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
371 :     #... Compute pseudocount-stabilized START codon frequencies
372 :     #-----------------------------------------------------------------------
373 :     my $atg_freq = ($start_counts{atg} + 80) / ($start_counts{0} + 100);
374 :     my $gtg_freq = ($start_counts{gtg} + 15) / ($start_counts{0} + 100);
375 :     my $ttg_freq = ($start_counts{ttg} + 5) / ($start_counts{0} + 100);
376 :     my $start_freqs = "$atg_freq,$gtg_freq,$ttg_freq";
377 :    
378 :     print STDERR "start_freqs = $start_freqs\n\n" if $ENV{VERBOSE};
379 :    
380 :    
381 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
382 :     #... Build Position Weight Matrix for upstream regions using ELPH
383 :     #-----------------------------------------------------------------------
384 : gdpusch 1.4 print STDERR "\nExtracting upstream motifs from $tmp_predictions_pass_1\n";
385 :    
386 :     my $motif_width = 6;
387 :     my $tmp_motifs = "$tmp_prefix.motifs";
388 :     system("$elph_bin/elph $tmp_upstream LEN=$motif_width > $tmp_motifs")
389 :     && ((-s $tmp_motifs)
390 :     || die("Could not extract upstream motifs")
391 :     );
392 :    
393 :    
394 :     print STDERR "\nBuilding PWM from upstream motifs in $tmp_motifs\n";
395 :    
396 :     open( MOTIFS, "<$tmp_motifs") || die "Could not read-open $tmp_motifs";
397 :     my @motifs = <MOTIFS>;
398 :     close(MOTIFS) || die "Could not close $tmp_motifs";
399 :     while ($motifs[0] !~ m/^Motif counts/) { shift @motifs };
400 :     shift @motifs;
401 :     my $last = 0;
402 :     for (my $i=0; $i < @motifs; ++$i) {
403 :     last unless ($motifs[$i] =~ m/\S+/);
404 :     if ($motifs[$i] =~ m/^[acgt]:((\s+\d+){$motif_width})/) {
405 :     $last = $i;
406 :     }
407 :     }
408 :     $#motifs = $last;
409 : gdpusch 1.2
410 : gdpusch 1.3 my $tmp_matrix = "$tmp_prefix.pwm";
411 : gdpusch 1.4 open( MATRIX, ">$tmp_matrix") || die "Could not write-open $tmp_matrix";
412 :     print MATRIX "$motif_width\n";
413 :     foreach my $line (@motifs) {
414 :     chomp $line;
415 :     my @fields = split /\s+/, $line;
416 :     my $base = substr((shift @fields), 0, 1);
417 :     print MATRIX ($base, (map { sprintf(qq(%7u), $_) } @fields), qq(\n));
418 :     }
419 :     close(MATRIX) || die "Could not close $tmp_matrix";
420 :    
421 :     (-s $tmp_matrix) || die "Could not construct PWM --- empty file $tmp_matrix";
422 : gdpusch 1.2
423 :    
424 :    
425 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
426 :     #... Re-call PEGs using ICM and PWM
427 :     #-----------------------------------------------------------------------
428 :     print STDERR "Re-calling PEGs using trained ICM\n" if $ENV{VERBOSE};
429 :    
430 : gdpusch 1.8 $fig->run("$glimmer_bin/glimmer3 $glimmeropts $genetic_code_switch -b $tmp_matrix -P $start_freqs $contigs_file $tmp_model $tmp_prefix");
431 : gdpusch 1.3
432 : gdpusch 1.2
433 :    
434 :     #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
435 :     #... Parse and write final predictions
436 :     # NOTE: Does not yet handle '-skip' option
437 :     #-----------------------------------------------------------------------
438 :     my $tmp_predictions = "$tmp_prefix.predict";
439 :     print STDERR "\nParsing $tmp_predictions\n"
440 :     if $ENV{VERBOSE};
441 :    
442 :     open(PREDICT, "<$tmp_predictions")
443 :     || die "Could not read-open $tmp_predictions";
444 :    
445 :     my $fid_num = 0;
446 :    
447 :     $contig_id = qq();
448 :     while(defined($entry = <PREDICT>)) {
449 :     chomp $entry;
450 : overbeek 1.1
451 : gdpusch 1.2 if ($entry =~ m/^>(\S+)/) {
452 :     $contig_id = $1;
453 :     next;
454 :     }
455 :     elsif ($entry =~ m/^(\S+)\s+(\d+)\s+(\d+)/) {
456 :     my ($id, $beg, $end) = ($1, $2, $3);
457 : overbeek 1.1
458 : gdpusch 1.2 if ($beg && $end) {
459 :     ++$fid_num;
460 :     my $fid = qq(fig|$taxon_ID.peg.) . $fid_num;
461 :     print STDOUT (qq($fid\t), join(qq(_), ($contig_id, $beg, $end)), qq(\n));
462 : overbeek 1.1 }
463 : gdpusch 1.2 else {
464 :     die "Error in $tmp_predictions line $.: $entry";
465 : overbeek 1.1 }
466 :     }
467 : gdpusch 1.2 else {
468 :     die "Could not parse prediction $.: $entry";
469 :     }
470 : overbeek 1.1 }
471 :    
472 : gdpusch 1.4 if (not $ENV{VERBOSE}) {
473 : gdpusch 1.3 $fig->run("rm -f $tmp_prefix.*");
474 :     }
475 : gdpusch 1.4 else {
476 :     print STDERR "\nKeeping tmp files\n\n";
477 :     }
478 : overbeek 1.1
479 :     exit 0;
480 :    
481 :    
482 : gdpusch 1.2 ########################################################################
483 :     sub get_dna_seq {
484 :     my ($contig, $beg, $end, $len_of, $seq_of) = @_;
485 :     my $dna = qq();
486 :    
487 :     my $contig_len = $len_of->{$contig};
488 :     my $contig_seq = $seq_of->{$contig};
489 :    
490 :     return undef if (&max($beg, $end) > $contig_len);
491 :     return undef if (&min($beg, $end) < 1);
492 :    
493 :     if ($beg < $end) {
494 :     $dna = substr($contig_seq, ($beg-1), ($end+1-$beg));
495 :     }
496 :     else {
497 :     $dna = substr($contig_seq, ($end-1), ($beg+1-$end));
498 :     $dna = $ { &rev_comp(\$dna) };
499 :     }
500 :    
501 :     return lc($dna);
502 :     }
503 :    
504 : overbeek 1.1
505 :     sub read_fasta_record
506 :     {
507 :     my ($file_handle) = @_;
508 :     my ( $old_end_of_record, $fasta_record, @lines, $head, $sequence, $seq_id, $comment, @parsed_fasta_record );
509 :    
510 :     if (not defined($file_handle)) { $file_handle = \*STDIN; }
511 :    
512 :     $old_end_of_record = $/;
513 :     $/ = "\n>";
514 :    
515 :     if (defined($fasta_record = <$file_handle>))
516 :     {
517 :     chomp $fasta_record;
518 :     @lines = split( /\n/, $fasta_record );
519 :     $head = shift @lines;
520 :     $head =~ s/^>?//;
521 :     $head =~ m/^(\S+)/;
522 :     $seq_id = $1;
523 :    
524 :     if ($head =~ m/^\S+\s+(.*)$/) { $comment = $1; } else { $comment = ""; }
525 :    
526 :     $sequence = join( "", @lines );
527 :    
528 :     @parsed_fasta_record = ( $seq_id, \$sequence, $comment );
529 :     }
530 :     else
531 :     {
532 :     @parsed_fasta_record = ();
533 :     }
534 :    
535 :     $/ = $old_end_of_record;
536 :    
537 :     return @parsed_fasta_record;
538 :     }
539 :    
540 :     sub display_id_and_seq
541 :     {
542 :     my( $id, $seq, $fh ) = @_;
543 :    
544 :     if (! defined($fh) ) { $fh = \*STDOUT; }
545 :    
546 :     print $fh ">$id\n";
547 :     &display_seq($seq, $fh);
548 :     }
549 :    
550 :     sub display_seq
551 :     {
552 :     my ( $seq, $fh ) = @_;
553 :     my ( $i, $n, $ln );
554 :    
555 :     if (! defined($fh) ) { $fh = \*STDOUT; }
556 :    
557 :     $n = length($$seq);
558 :     # confess "zero-length sequence ???" if ( (! defined($n)) || ($n == 0) );
559 :     for ($i=0; ($i < $n); $i += 60)
560 :     {
561 :     if (($i + 60) <= $n)
562 :     {
563 :     $ln = substr($$seq,$i,60);
564 :     }
565 :     else
566 :     {
567 :     $ln = substr($$seq,$i,($n-$i));
568 :     }
569 :     print $fh "$ln\n";
570 :     }
571 :     }
572 :    
573 :     sub rev_comp
574 :     {
575 :     my( $seqP ) = @_;
576 :     my( $rev );
577 :    
578 :     $rev = reverse( $$seqP );
579 :     $rev =~ tr/a-z/A-Z/;
580 :     $rev =~ tr/ACGTUMRWSYKBDHV/TGCAAKYWSRMVHDB/;
581 :     return \$rev;
582 :     }
583 :    
584 :     sub min { my ($x, $y) = @_; return (($x < $y) ? $x : $y); }
585 :    
586 :     sub max { my ($x, $y) = @_; return (($x > $y) ? $x : $y); }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3