-
Notifications
You must be signed in to change notification settings - Fork 66
/
tab2fasta
executable file
·69 lines (52 loc) · 1.45 KB
/
tab2fasta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env perl
# https://github.com/shenwei356/bio_scripts
use strict;
use Getopt::Long;
use BioUtil::Util;
use BioUtil::Seq;
my $usage = q(
tab2fasta - transfrom column table to fasta fromat
Usage: $0 [options] [tablefile...]
Options:
-l, --linelength Output line length
-h, --help Show this help information
This script is usually used in pair with fasta2tab.
https://github.com/shenwei356/bio_scripts
);
my $para = {};
GetOptions(
'help|h' => \$$para{help},
'linelength|l=i' => \$$para{linelength},
) or die $usage;
die $usage if $$para{help};
my @files = file_list_from_argv(@ARGV);
for my $file (@files) {
my $fh = undef;
my $is_stdin = 0;
if ( $file eq 'STDIN' ) {
$fh = *STDIN;
$is_stdin = 1;
}
else {
open $fh, "<", $file
or die "fail to open file: $file\n";
}
my ( $header, $seq ) = ( "", "" );
while (<$fh>) {
s/\r?\n//g;
s/^\s+|\s+$//g;
next if $_ eq '' # blank line
or /^#/; # annotation
# first column as header, and second column as sequence,
# ignore others
( $header, $seq ) = split /\t/, $_;
$header =~ s/__tab__/\t/g;
if ( $$para{linelength} ) {
print ">$header\n", format_seq( $seq, $$para{linelength} );
}
else {
print ">$header\n$seq\n";
}
}
close $fh unless $is_stdin;
}