-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreeOrderedFasta.pl
executable file
·144 lines (124 loc) · 3.27 KB
/
treeOrderedFasta.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
#!/usr/bin/env perl
# Gets a fasta file and orders by tree order.
#
# Samuel S. Shepard (vfn4@cdc.gov)
# 2011, Centers for Disease Control & Prevention
#
# Version 1.0.3
# 1.0.3 + Improved capatibility with MEGA4's header mutilations.
# 1.0.2 + Added a quoted ID option for MEGA5.
# 1.0.2 * Possible warning mix-up.
# 1.0.1 + Gives warnings for fasta headers not found.
# 1.0.1 * Only outputs fasta headers with sequences.
# 1.0.1 * Fixed a problem with apostrophes in headers.
use Getopt::Long;
GetOptions( 'quoted-ids|Q' => \$quoted );
if ( scalar(@ARGV) != 3 ) {
die("Usage:\n\t$0 <tree.nwk> <sequences.fasta> <output.fasta> [-Q|--quoted-ids]\n");
}
open( IN, '<', $ARGV[0] ) or die("Cannot open $ARGV[0].\n");
open( SEQ, '<', $ARGV[1] ) or die("Cannot open $ARGV[1].\n");
open( OUT, '>', $ARGV[2] ) or die("Cannot open $ARGV[2].\n");
$debug = 0;
# Process Newick file.
@ordered = ();
%tree = ();
if ($quoted) {
$/ = "\n";
$line = <IN>;
chomp($line);
#if ( $line =~ s/([^'\d])(A\/.+?)([,:)])/\1'\2'\3/g ) {
# print STDERR "TREEORDER: Warning, sequence adjustment was required!\n";
#}
while ( $line =~ m/'(.+?)':/g ) {
$ordered[$i] = $1;
$tree{$1} = 'T';
$i++;
}
} else {
$/ = ':';
$block = <IN>;
chomp($block);
$block =~ tr/'//d;
$block =~ /\(*(.+)$/;
$tree{$1} = 'T';
$ordered[0] = $1;
$i = 1;
while ( $block = <IN> ) {
chomp($block);
$block =~ tr/'//d;
@parts = split( ',', $block );
if ( scalar(@parts) > 1 ) {
$parts[1] =~ /\(*(.+)$/;
$ordered[$i] = $1;
$tree{$1} = 'T';
$i++;
}
}
}
close(IN);
if ($debug) {
open( DEBUG, '>', 'debugTree.log' );
foreach $item ( sort(@ordered) ) {
print DEBUG $item, "\n";
}
close(DEBUG);
}
# Process Fasta file.
$/ = ">";
%sequences = ();
while ( $record = <SEQ> ) {
chomp($record);
@lines = split( /\r\n|\n|\r/, $record );
$header = shift(@lines);
$sequence = join( '', @lines );
if ( length($sequence) == 0 ) {
next;
}
if ($quoted) {
$header =~ tr/'//d;
$header =~ tr/ /_/;
}
if ( $tree{$header} eq 'T' ) {
$sequences{$header} = $sequence;
} else {
#if( $header =~ /(.*?H\d{1,2}N\d(_NP){0,1}).*$/ ) {
# $key = $1;
#} else {
# $key = $header;
#}
if ( $header =~ /(.*?)_\{.+\}/ ) {
$header = $1;
}
$key = substr( $header, 0, 40 );
$key =~ tr/:)(//d;
if ( substr( $key, -1 ) eq '_' ) {
chop($key);
}
$key =~ s/__/_/g;
$sequences{$key} = $sequence;
}
}
close(SEQ);
if ($debug) {
open( DEBUG, '>', 'debugFasta.log' );
foreach $item ( sort( keys(%sequences) ) ) {
print DEBUG $item, "\n";
}
close(DEBUG);
}
# Process output
$notFound = 0;
foreach $key (@ordered) {
if ( !defined( $sequences{$key} ) ) {
$notFound++;
print("WARNING! Cannot find header in sequence file: $key\n");
} else {
print OUT '>', $key, "\n";
print OUT $sequences{$key}, "\n";
}
}
close(OUT);
if ( $notFound > 1 ) {
print "There were $notFound sequences not found in the fasta sequence file.\n";
}