-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta2nexus.pl
109 lines (85 loc) · 2.04 KB
/
fasta2nexus.pl
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Path;
use File::Basename;
# Declare variables
our $input;
parseArgs();
my $line;
my $taxa;
my $nchar;
my @data;
my @names;
my @loci;
my @fasta;
my $name;
my ( $filepath, $dirpath ) = fileparse($input);
#print $filepath, "\n";
#Iterate through files
#print $dirpath, "\n";
#opendir(DIR, "$dirpath") or die "Couldn't open the directory $dirpath\n";
#my @files = readdir(DIR);
#closedir(DIR);
#my @files = glob "$input";
#print $files[0], "\n";
my @files = <$dirpath/*.fasta>;
#print @files, "\n";
foreach my $file ( @files ){
#next if ($file =~ /^\.$/);
#next if ($file =~ /^\.\.$/);
#print $file, "\n";
undef @data;
undef @names;
$taxa = 0;
open ( FILE, "$file" ) || die "\nCan't open $file: $!\n";
while ( <FILE> ){
chomp;
if( $_ =~ /^\>/ ){
$taxa++;
$_ =~ /^\>(\S+)/;
$name = "$1";
push @names, "$name";
}elsif( $_ =~ /^\s*$/ ){
next;
}elsif( $_ =~ /^\s*#/ ){
next;
}else{
push @data, $_ ;
$nchar = length;
}
}
close FILE;
#Capture taxa name to use as identifier
my $filepath = fileparse("$file");
$filepath =~ /(\w+)\.\w/;
my $ID = $1;
open( OUT, '>', "$dirpath$ID.nex" ) || die "\nCan't write to $ID.nex\n";
print OUT "#NEXUS\n\n";
print OUT "BEGIN DATA;
DIMENSIONS NTAX=$taxa NCHAR=$nchar;
FORMAT DATATYPE=DNA MISSING=? GAP=- ;
MATRIX\n";
for ( my $i = 0; $i<scalar @names; $i++ ){
print OUT "$names[$i]\t$data[$i]\n";
}
print OUT ";\n";
print OUT "END;\n\n";
}
close OUT;
exit;
###########################SUBROUTINES###################################
sub parseArgs{
#Message to print if mandatory variables not declared
my $usage ="\nUsage: $0 --i /path/to/input/directory/*.fasta
Mandatory
--i - path to the input files in fasta format
\n";
my $options = GetOptions
(
'input|i=s' => \$input,
);
$input or die "\n\nInput not specified!\n\n$usage\n";
}
#########################################################################