-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfasta2nexus.pl
executable file
·103 lines (79 loc) · 1.92 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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Path;
use File::Basename;
# Declare variables
my @input;
#our $infiletype=1;
parseArgs();
my ( $filepath, $dirpath ) = fileparse($input[0]);
#Iterate through files
@input = glob "@input";
foreach my $file ( @input ){
#Initialize variables within each daughter process
my %data;
my $taxa = 0;
my @fasta;
my @loci;
my $nchar=0;
my $line;
my $name = "";
my $seq = "";
open ( FILE, "$file" ) || die "Error\nCan't open $file: $!\n";
while ( <FILE> ){
chomp;
if( $_ =~ /^\>/ ){
$taxa++;
if ($name =~ ""){
$_ =~ /^\>(\S+)/;
$name = "$1";
}else{
$data{$name} = $seq;
$seq = "";
$nchar = length($seq);
$_ =~ /^\>(\S+)/;
$name = "$1";
}
}elsif( $_ =~ /^\s*$/ ){
next;
}elsif( $_ =~ /^\s*#/ ){
next;
}else{
$seq .= $_; #append sequence to line; accounts for multi line fasta
}
}
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 "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";
foreach my $key (keys %data){
print OUT "$key\t$data{$key}\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, --input - path to the input files in fasta format
\n";
my $options = GetOptions
(
'input|i=s{1,}' => \@input,
);
@input or die "\n\nError: Input not specified!\n\n$usage\n";
}
#########################################################################