-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphylip2nexus.pl
executable file
·91 lines (72 loc) · 1.94 KB
/
phylip2nexus.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
#!/usr/bin/perl
#Modified from fasta2nexus.pl written by BTM
#-TKC
use strict;
use warnings;
use Getopt::Long;
use File::Path;
use File::Basename;
# Declare variables
our $input;
#our $infiletype=1;
parseArgs();
#Initialize variables within each daughter process
my @data;
my @names;
my $taxa = 0;
my $name;
my @fasta;
my @loci;
my $nchar;
my $line=0;
my @linedata;
open ( FILE, "$input" ) || die "Error\nCan't open $input: $!\n";
while ( <FILE> ){
chomp;
$line++;
@linedata = split /\s+/, $_;
s/\s+//g;
length($_) or next;
$line == 1 and next;
$taxa++;
$name = $linedata[0];
push @names, "$name";
push @data, $linedata[1];
if ($nchar){
length($linedata[1]) != $nchar and print "Error: Line beginning with $name has a different sequence length.\n";
}else{
$nchar = length($linedata[1]);
}
}
close FILE;
#Capture to use as identifier
my ($filepath, $dirpath) = fileparse("$input");
$filepath =~ /(\w+)\.\w/;
my $ID = $1;
open( OUT, '>', "$dirpath$ID.nex" ) || die "Error\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/*.phylip
Mandatory
-i, --input - path to the input files in phylip format
\n";
my $options = GetOptions
(
'input|i=s{1,}' => \$input
);
$input or die "\n\nError: Input not specified!\n\n$usage\n";
}
#########################################################################