/
protpaleo.pl
executable file
·155 lines (131 loc) · 3.55 KB
/
protpaleo.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env perl
BEGIN {
use FindBin;
use lib $FindBin::Bin;
push @INC, $FindBin::Bin;
}
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Stockholm;
my ($infile, $outfile, $tree, $dna);
my $dartdir = $ENV{'DARTDIR'} || ($FindBin::Bin . "/..");
my $muscle = `which muscle` || "$dartdir/bin/muscle";
chomp $muscle;
my $usage = "\n"
."Usage: protpaleo.pl <infile> [more options...]\n"
."\n"
." <infile> (FASTA or Stockholm format)\n"
." -dna (if input is DNA)\n"
." -tree <tree string> (New Hampshire format)\n"
." -out <outfile>\n"
."\n";
@ARGV || die $usage;
unshift @ARGV, "-in";
GetOptions ("in=s" => \$infile,
"out=s" => \$outfile,
"tree=s" => \$tree,
"dartdir=s" => \$dartdir,
"muscle=s" => \$muscle,
"dna" => \$dna)
or die $usage;
defined($infile) or die $usage;
my $dartbin = "$dartdir/bin";
my $xrate = "$dartbin/xrate";
my $protpal = "$dartbin/protpal";
my $nullgram = "$dartdir/grammars/" . ($dna ? "nullrna.eg" : "nullprot.eg");
my $muscle_opts = $dna ? "-maxiters 1 -diags" : "-maxiters 1 -diags -sv -distance1 kbit20_3";
# Fasta or Stockholm?
my $got_stock = 0;
local *IN;
open IN, "<$infile" or die "Couldn't open input file $infile: $!";
while (<IN>) {
if (/^\s*# STOCKHOLM"/) {
$got_stock = 1;
last;
} elsif (/\S/ && /^\s*[^#]/) {
last;
}
}
close IN;
# Get guide alignment
my $alignfile;
if ($got_stock) {
$alignfile = $infile;
} else {
$alignfile = "$infile.align.stockholm";
my $alignfile_fasta = "$infile.align.fasta";
sysrun ("$muscle $muscle_opts -in $infile -out $alignfile_fasta");
fasta2stockholm ($alignfile_fasta, $alignfile);
}
# Get tree
$tree = $tree || stockholm_tree($alignfile);
if (!defined $tree) {
my $treealignfile = "$alignfile.treealign.stockholm";
sysrun ("$xrate -g $nullgram -noa $alignfile >$treealignfile");
$tree = stockholm_tree ($treealignfile);
}
# Write tree
my $treefile = "$infile.newick";
local *TREE;
open TREE, ">$treefile";
print TREE $tree;
close TREE;
# Run protpal
my $infile_opts = $got_stock ? "-stk $infile" : "-fa $infile";
my $outfile_opts = defined($outfile) ? ">$outfile" : "";
my $dna_opts = $dna ? "-dna" : "";
sysrun ("$protpal -ga $alignfile -tf $treefile -g $nullgram $dna_opts -eri $outfile_opts");
# Helper functions
sub fasta2stockholm {
my ($fasta, $stockholm) = @_;
my %seq;
my @name;
my $name;
local *FASTA;
open FASTA, "<$fasta" or die "Couldn't open FASTA file '$fasta': $!";
while (<FASTA>) {
if (/^\s*>\s*(\S+)/) {
$name = $1;
die "Duplicate name: $name" if defined $seq{$name};
push @name, $name;
} else {
if (/\S/ && !defined $name) {
warn "Ignoring: $_";
} else {
s/\s//g;
$seq{$name} .= $_;
}
}
}
close FASTA;
my $length;
my $lname;
foreach my $name (@name) {
my $l = length $seq{$name};
if (defined $length) {
die "Sequences not all same length ($lname is $length, $name is $l)" unless $length == $l;
} else {
$length = length $seq{$name};
$lname = $name;
}
}
local *STOCK;
open STOCK, ">$stockholm" or die "Couldn't write to file $stockholm: $!";
print STOCK "# STOCKHOLM 1.0\n";
foreach my $name (@name) {
print STOCK $name, " ", $seq{$name}, "\n";
}
print STOCK "//\n";
close STOCK;
}
sub stockholm_tree {
my ($stockfile) = @_;
my $stock = Stockholm->from_file ($stockfile);
return @{$stock->gf_NH} ? $stock->NH : undef;
}
sub sysrun {
my ($cmd) = @_;
warn "Running $cmd\n";
system $cmd;
}