-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_fasta_to_phylip_paml.pl
56 lines (36 loc) · 1.29 KB
/
convert_fasta_to_phylip_paml.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
#!/usr/bin/env perl
##############################################################################################################################################
#Convert fasta supermatrix in phylip format for paml (aa alignment). Ambiguous characters and gaps (X, -) are replaced with ?
###############################################################################################################################################
use strict;
use warnings;
my $in_supermatrix=shift @ARGV;
my $outfile=shift @ARGV;
#generate files
open my $fh, '<', $in_supermatrix or die "Could not open file\"$in_supermatrix\":$!\n";
open my $fh_out, '>', $outfile or die "Could not open file\"$outfile\":$!\n";
my %hash=();
my $header;
my $no_species;
my $length=0;
my $linecounter=0;
while (my $line=<$fh>){
chomp $line;
++$linecounter;
#Store fasta file in a hash
if ($line=~m/^>(.+)/) {
++$no_species;
$header=$1;
}
else{
$line=~s/X/\?/g; #substitute X with ?
$line=~s/\-/\?/g; #substitute - with ?
$hash{$header}=$line;
}
if ($linecounter==2){$length=length $line;}
}
#print no of species and length for the alignment
print {$fh_out} "$no_species $length\n";
foreach (sort (keys %hash)){
printf {$fh_out} "%-40s%s\n", $_, $hash{$_};
}