Skip to content

Commit

Permalink
fix sequence/fasta_trim_aligned_fasta.pl
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Dec 4, 2014
1 parent ed061be commit 967018f
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions sequence/fasta_trim_aligned_fasta.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
local $| = 1;

my @GAPS = ( '-', '.' );
my $tmpfile_prefix = "fasta_trim_aligned_fasta_tmpfile_";

my $usage = <<USAGE;
Expand Down Expand Up @@ -57,7 +58,9 @@
}
if ( @files == 0 ) {
push @files, 'STDIN';
( $tmp_file_fh, $tmp_file ) = tempfile();
( $tmp_file_fh, $tmp_file )
= tempfile( $tmpfile_prefix . "XXXXXX", DIR => ".", SUFFIX => '.fa' );

$use_stdin = 1;
}

Expand All @@ -67,13 +70,13 @@
my $gaploc = {}; # store the gap location
my $do_once = 1;
my ( $header, $seq, $len, $i, $base ) = (undef) x 5;
my ($sum, $n) = (0) x 2;
my ( $sum, $n ) = (0) x 2;
for my $file (@files) {
my $next_seq = FastaReader($file);
while ( my $fa = &$next_seq() ) {
( $header, $seq ) = @$fa;
$sum++;
print STDERR "\r$sum";
print STDERR "\rsum: $sum";
if ($do_once) {
$len = length $seq;
$$gaploc{$_} = 1 for 0 .. ( $len - 1 );
Expand All @@ -87,14 +90,19 @@
}
}

die "no gap to trim.\n" if scalar keys %$gaploc == 0;
if ( scalar keys %$gaploc == 0 ) {
close $tmp_file_fh if $use_stdin;
remove_tmpfile() if $use_stdin;
die "no gap to trim\n";
}

print $tmp_file_fh ">$header\n$seq\n" if $use_stdin;
}
}

close $tmp_file_fh if $use_stdin;

print STDERR scalar keys %$gaploc, " gaps to trim\n";
print STDERR "\nextract sequences...\n";

@files = ($tmp_file) if $use_stdin;
Expand All @@ -114,7 +122,11 @@

print STDERR "\n";

if ($use_stdin) {
print STDERR "remove $tmp_file\n";
unlink $tmp_file or die "fail to remove $tmp_file\n";
remove_tmpfile() if $use_stdin;

sub remove_tmpfile {
print STDERR "\nremove temporary files\n";
for ( glob "$tmpfile_prefix*" ) {
unlink $_ or die "fail to remove $_\n";
}
}

0 comments on commit 967018f

Please sign in to comment.