-
Notifications
You must be signed in to change notification settings - Fork 2
/
clusterInformativeSites.pl
219 lines (179 loc) · 7.33 KB
/
clusterInformativeSites.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl
##---------------------------------------------------------------------------##
## File:
## @(#) clusterInformativeSites
## Author:
## Paul Thatcher Edlefsen pedlefse@fredhutch.org
## Description:
## Script for parsing the informativeSites output from the
## InformativeSites DiveIn tool (at the Mullins lab web site) to
## cluster the sequences. By default it writes the output to
## stdout; use -o to write to a file. Note that this creates a
## multiple alignment of just the informative sites and then
## calls out to the R script clusterInformativeSites.R.
##
###******************************************************************************
use Getopt::Std; # for getopts
use Text::Wrap; # for wrap
$Text::Wrap::columns = 72;# TODO: DEHACKIFY MAGIC #
use strict;
use vars qw( $opt_D $opt_V $opt_f );
use vars qw( $VERBOSE $DEBUG );
sub clusterInformativeSites {
@ARGV = @_;
sub clusterInformativeSites_usage {
print "\tclusterInformativeSites [-DVf] <input_fasta_filename> <informativeSites.txt> [<output dir>]\n";
exit;
}
# This means -D, -V, and -f are ok, but nothin' else.
# opt_f means don't actually cluster them; instead put them all into one cluster ("cluster 0").
# opt_D means print debugging output.
# opt_V means be verbose.
# But first reset the opt vars.
( $opt_D, $opt_V, $opt_f ) = ();
if( not getopts('DVf') ) {
clusterInformativeSites_usage();
}
$DEBUG ||= $opt_D;
$VERBOSE ||= $opt_V;
my $force_one_cluster = $opt_f;
my $old_autoflush;
if( $VERBOSE ) {
select STDOUT;
$old_autoflush = $|;
$| = 1; # Autoflush.
}
my $original_fasta_filename = shift @ARGV || clusterInformativeSites_usage();
my $informative_sites_file = shift @ARGV || clusterInformativeSites_usage();
my ( $informative_sites_file_path, $informative_sites_file_short ) =
( $informative_sites_file =~ /^(.*?)\/([^\/]+)$/ );
unless( $informative_sites_file_short ) {
$informative_sites_file_short = $informative_sites_file;
$informative_sites_file_path = ".";
}
my $output_dir = shift @ARGV || $informative_sites_file_path;
# Remove the trailing "/" if any
if( defined( $output_dir ) ) {
( $output_dir ) = ( $output_dir =~ /^(.*[^\/])\/*$/ );
}
if( $VERBOSE ) { print "Output will be written in directory \"$output_dir\".."; }
my ( $line );
my %seqs;
my @fields;
my @seqorder;
my @seq_as_list;
my $seq;
if( -e $informative_sites_file ) {
if( $VERBOSE ) { print "Opening file \"$informative_sites_file\".."; }
unless( open( INFORMATIVE_SITES_FILE_FH, $informative_sites_file ) ) {
warn "Unable to open informative_sites_file file \"$informative_sites_file\": $!";
return 1;
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Reading informative sites file.."; }
while( $line = <INFORMATIVE_SITES_FILE_FH> ) {
#( $line ) = ( $line =~ /^(.+?)\s*$/ ); # Chop and Chomp won't remove ^Ms
chomp( $line ); # Actually we don't want to remove trailing tabs...
if( $DEBUG ) {
print "LINE: $line\n";
}
next unless $line;
next if $line =~ /^\s/;
last if $line =~ /^Alignment/;
if( $VERBOSE ) { print "."; }
@fields = split( "\t", $line, -1 ); # The -1 means no limit, which forces inclusion of flanking empty fields.
## NOTE that there is a bug (as of September, 2015) in the inSites
## code online, in which flanking gaps are not printed in the
## output table. THIS MUST BE FIXED WHEN READING/USING THE FILE.
@fields = map { if( $_ =~ /^\s*$/ ) { '-' } else { $_ } } @fields;
if( $DEBUG ) {
print $fields[ 0 ], ": ";
print join( "\t", @fields[ 4..$#fields ] ), "\n";
}
push @seqorder, $fields[ 0 ];
$seqs{ $fields[ 0 ] } = [ @fields[ 4..$#fields ] ];
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Closing file \"$informative_sites_file\".."; }
close INFORMATIVE_SITES_FILE_FH;
if( $VERBOSE ) { print ".done.\n"; }
} # End if there is a file to read.
# Maybe there are no informative sites. We are just done, then.
my $insites_fasta_file = "";
if( !scalar( @seqorder ) ) {
if( $VERBOSE ) {
print( "No informative sites..\n" );
}
$force_one_cluster = 1;
$insites_fasta_file = $original_fasta_filename;
} else {
# Ok, great. Now, for all entries, replace '.' with the consensus value.
unless( $seqorder[ 0 ] eq 'Consensus' ) {
die( "First sequence is not 'Consensus' it is instead '$seqorder[ 0 ]'" );
}
shift @seqorder; # Remove `Consensus`
if( $DEBUG ) {
print "Sequences: ", join( ", ", @seqorder ), "\n";
}
my @consensus_as_list = @{ $seqs{ "Consensus" } };
my $alignment_length = scalar( @consensus_as_list );
delete $seqs{ "Consensus" };
$insites_fasta_file =
"${output_dir}/${informative_sites_file_short}.fasta";
if( $VERBOSE ) { print "Opening file \"$insites_fasta_file\" for writing.."; }
unless( open INSITES_FASTA_FH, ">$insites_fasta_file" ) {
warn "Unable to open insites_fasta file \"$insites_fasta_file\": $!";
return 1;
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) {
print "Printing alignment of informative sites..";
}
my $seq_wrapped;
foreach my $seq_name ( @seqorder ) {
if( $DEBUG ) {
print $seq_name, "\n";
}
@seq_as_list = @{ $seqs{ $seq_name } };
unless( scalar( @seq_as_list ) == $alignment_length ) {
print( "Alignment length error in inSites file $insites_fasta_file: got " . scalar( @seq_as_list ) . ", expected $alignment_length" );
print( ">Consensus\n", join( "", @consensus_as_list ), "\n" );
print( ">$seq_name\n", join( "", @seq_as_list ), "\n" );
die( "Alignment length error in inSites file $insites_fasta_file: got " . scalar( @seq_as_list ) . ", expected $alignment_length" );
}
for( my $i = 0; $i < scalar( @seq_as_list ); $i++ ) {
if( $seq_as_list[ $i ] eq '.' ) {
$seq_as_list[ $i ] = $consensus_as_list[ $i ];
}
}
$seq = join "", @seq_as_list;
# add newlines / wraparound to $seq
$seq_wrapped = wrap( '', '', $seq );
print INSITES_FASTA_FH "> $seq_name\n$seq_wrapped\n";
} # End foreac $seq_name
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Closing file \"$insites_fasta_file\".."; }
close INSITES_FASTA_FH;
if( $VERBOSE ) { print ".done.\n"; }
} # End if there are no informative sites .. else ..
if( $VERBOSE ) {
print "Calling R to cluster informative sites..";
if( $force_one_cluster ) {
print( "Forcing one cluster..\n" );
} else {
print( "Clustering..\n" );
}
}
my $R_output = `export clusterInformativeSites_forceOneCluster="$force_one_cluster"; export clusterInformativeSites_inputFilename="$insites_fasta_file"; export clusterInformativeSites_originalFastaFilename="$original_fasta_filename"; export clusterInformativeSites_outputDir="$output_dir"; R -f clusterInformativeSites.R --vanilla --slave`;
print( $R_output );
if( $VERBOSE ) {
print ".done.\n";
}
if( $VERBOSE ) {
select STDOUT;
$| = $old_autoflush;
}
return 0;
} # clusterInformativeSites(..)
clusterInformativeSites( @ARGV );
1;