3 # Copyright (C) 2007 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines to match sequences
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 use Storable qw( dclone );
37 use Maasha::Berkeley_DB;
38 use vars qw ( @ISA @EXPORT );
40 @ISA = qw( Exporter );
43 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
48 # Martin A. Hansen, June 2007.
50 # Match sequences using MUMmer.
52 my ( $entries1, # FASTA entries
53 $entries2, # FASTA entries
54 $options, # additional MUMmer options - OPTIONAL
55 $tmp_dir, # temporary directory
60 my ( @args, $arg, $file_in1, $file_in2, $cmd, $file_out, $fh, $line, $result, @results );
62 $tmp_dir ||= $ENV{ "BP_TMP" };
64 $options->{ "word_size" } ||= 20;
65 $options->{ "direction" } ||= "both";
70 push @args, "-l $options->{ 'word_size' }";
71 push @args, "-maxmatch";
72 push @args, "-n" if not Maasha::Seq::seq_guess_type( $entries1->[ 0 ]->[ 1 ] ) eq "protein";
73 push @args, "-b" if $options->{ "direction" } =~ /^b/;
74 push @args, "-r" if $options->{ "direction" } =~ /^r/;
76 $arg = join " ", @args;
78 $file_in1 = "$tmp_dir/muscle1.tmp";
79 $file_in2 = "$tmp_dir/muscle2.tmp";
80 $file_out = "$tmp_dir/muscle3.tmp";
82 map { $_->[ 0 ] =~ tr/ /_/ } @{ $entries1 };
83 map { $_->[ 0 ] =~ tr/ /_/ } @{ $entries2 };
85 Maasha::Fasta::put_entries( $entries1, $file_in1 );
86 Maasha::Fasta::put_entries( $entries2, $file_in2 );
88 Maasha::Common::run( "mummer", "$arg $file_in1 $file_in2 > $file_out 2>/dev/null" );
90 $fh = Maasha::Common::read_open( $file_out );
92 while ( $line = <$fh> )
96 if ( $line =~ /^> (.+)Reverse\s+Len = (\d+)$/ )
98 $result->{ "Q_ID" } = $1;
99 $result->{ "Q_LEN" } = $2;
100 $result->{ "DIR" } = "reverse";
102 elsif ( $line =~ /^> (.+)Len = (\d+)$/ )
104 $result->{ "Q_ID" } = $1;
105 $result->{ "Q_LEN" } = $2;
106 $result->{ "DIR" } = "forward";
108 elsif ( $line =~ /^\s*(.\S+)\s+(\d+)\s+(\d+)\s+(\d+)$/ )
110 $result->{ "S_ID" } = $1;
111 $result->{ "S_BEG" } = $2 - 1;
112 $result->{ "Q_BEG" } = $3 - 1;
113 $result->{ "HIT_LEN" } = $4;
114 $result->{ "S_END" } = $result->{ "S_BEG" } + $result->{ "HIT_LEN" } - 1;
115 $result->{ "Q_END" } = $result->{ "Q_BEG" } + $result->{ "HIT_LEN" } - 1;
117 push @results, dclone $result;
126 return wantarray ? @results : \@results;
132 # Martin A. Hansen, April 2008.
134 # Vmatches a list of records against a list of index files and the full
135 # path to the result file is returned.
137 my ( $tmp_dir, # directory in where to save temp files
138 $records, # list of records
139 $index_files, # list of index files
140 $options, # argument hash
145 my ( $query_file, $result_file, @result_files, $fh_in, $fh_out, $line, @fields, $i, $record, $vmatch_args, @index_names, @seq_names, $count_list );
147 $query_file = "$tmp_dir/query.seq";
148 $result_file = "$tmp_dir/vmatch.out";
150 $fh_out = Maasha::Common::write_open( $query_file );
152 foreach $record ( @{ $records } )
154 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
156 next if length $record->{ "SEQ" } < 12; # assuming that the index is created for 12 as minimum length
158 push @seq_names, $record->{ "SEQ_NAME" };
160 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
166 if ( $options->{ 'genome' } ) {
167 $vmatch_args = "-complete -d -p -q $query_file";
169 $vmatch_args = "-complete -d -p -showdesc 100 -q $query_file";
172 $vmatch_args .= " -h " . $options->{ "hamming_dist" } if $options->{ "hamming_dist" };
173 $vmatch_args .= " -e " . $options->{ "edit_dist" } if $options->{ "edit_dist" };
175 for ( $i = 0; $i < @{ $index_files }; $i++ )
177 Maasha::Common::run( "vmatch", "$vmatch_args $index_files->[ $i ] > $result_file.$i" );
179 push @result_files, "$result_file.$i";
184 $count_list = vmatch_count_hits( \@result_files ) if ( $options->{ "count" } );
186 $fh_out = Maasha::Common::write_open( $result_file );
188 for ( $i = 0; $i < @{ $index_files }; $i++ )
190 $index_files->[ $i ] =~ s/.+\/(.+)\.fna$/$1/ if $options->{ 'genome' };
192 $fh_in = Maasha::Common::read_open( "$result_file.$i" );
194 while ( $line = <$fh_in> )
198 next if $line =~ /^#/;
200 @fields = split " ", $line;
202 next if $options->{ "max_hits" } and $count_list->[ $fields[ 5 ] ] > $options->{ 'max_hits' };
204 $fields[ 1 ] = $index_files->[ $i ]; # S_ID
205 $fields[ 9 ] = $count_list->[ $fields[ 5 ] ] if $options->{ "count" }; # SCORE
206 $fields[ 5 ] = $seq_names[ $fields[ 5 ] ]; # Q_ID
208 print $fh_out join( "\t", @fields ), "\n";
213 unlink "$result_file.$i";
222 sub vmatch_count_hits
224 # Martin A. Hansen, April 2008.
226 # Given a list of Vmatch result file, count duplications based
227 # on q_id. The counts are returned in a list where the list index
228 # corresponds to the q_id index in the query file.
230 my ( $files, # vmatch result files
235 my ( $file, $fh_in, $line, @fields, @count_list );
237 foreach $file ( @{ $files } )
239 $fh_in = Maasha::Common::read_open( $file );
241 while ( $line = <$fh_in> )
245 next if $line =~ /^#/;
247 @fields = split " ", $line;
249 $count_list[ $fields[ 5 ] ]++;
255 return wantarray ? @count_list : \@count_list;
259 sub vmatch_count_hits_old
261 # Martin A. Hansen, April 2008.
263 # Given a Vmatch result file, substitute the
264 # score field with the times the query sequence
267 my ( $tmp_dir, # directory in where to save temp files
268 $path, # full path to vmatch file
269 $max_count, # filter too abundant seqs - OPTIONAL
274 my ( $fh_in, $fh_out, $line, @fields, @count_list );
276 $fh_in = Maasha::Common::read_open( $path );
278 while ( $line = <$fh_in> )
282 next if $line =~ /^#/;
284 @fields = split " ", $line;
286 $count_list[ $fields[ 5 ] ]++;
291 $fh_in = Maasha::Common::read_open( $path );
292 $fh_out = Maasha::Common::write_open( "$tmp_dir/vmatch.count" );
294 while ( $line = <$fh_in> )
298 next if $line =~ /^#/;
300 @fields = split " ", $line;
302 $fields[ 9 ] = $count_list[ $fields[ 5 ] ];
305 print $fh_out join( "\t", @fields ), "\n" if $fields[ 9 ] <= $max_count;
307 print $fh_out join( "\t", @fields ), "\n";
314 rename "$tmp_dir/vmatch.count", $path;
318 sub vmatch_count_hits_old
320 # Martin A. Hansen, April 2008.
322 # Given a Vmatch result file, substitute the
323 # score field with the times the query sequence
326 my ( $tmp_dir, # directory in where to save temp files
327 $path, # full path to vmatch file
328 $max_count, # filter too abundant seqs - OPTIONAL
333 my ( $fh_in, $fh_out, $line, @fields, %count_hash );
338 %count_hash = Maasha::Berkeley_DB::db_init( "$tmp_dir/hash.bdb" );
341 $fh_in = Maasha::Common::read_open( $path );
343 while ( $line = <$fh_in> )
347 next if $line =~ /^#/;
349 @fields = split " ", $line;
351 $count_hash{ $fields[ 5 ] }++;
356 $fh_in = Maasha::Common::read_open( $path );
357 $fh_out = Maasha::Common::write_open( "$tmp_dir/vmatch.count" );
359 while ( $line = <$fh_in> )
363 next if $line =~ /^#/;
365 @fields = split " ", $line;
367 $fields[ 9 ] = $count_hash{ $fields[ 5 ] };
370 print $fh_out join( "\t", @fields ), "\n" if $fields[ 9 ] <= $max_count;
372 print $fh_out join( "\t", @fields ), "\n";
379 if ( not $max_count )
382 unlink "$tmp_dir/hash.bdb";
385 rename "$tmp_dir/vmatch.count", $path;
391 # Martin A. Hansen, January 2008.
393 # Parses vmatch output records.
395 my ( $fh, # file handle to vmatch result file.
400 my ( $line, @fields, %record );
402 while ( $line = <$fh> )
406 next if $line =~ /^#/;
408 @fields = split "\t", $line;
410 $record{ "REC_TYPE" } = "VMATCH";
412 $record{ "S_LEN" } = $fields[ 0 ];
413 $record{ "S_ID" } = $fields[ 1 ];
414 $record{ "S_BEG" } = $fields[ 2 ];
416 if ( $fields[ 3 ] eq "D" ) {
417 $record{ "STRAND" } = "+";
419 $record{ "STRAND" } = "-";
422 $record{ "Q_LEN" } = $fields[ 4 ];
423 $record{ "Q_ID" } = $fields[ 5 ];
424 $record{ "Q_BEG" } = $fields[ 6 ];
425 $record{ "MATCH_DIST" } = $fields[ 7 ];
426 $record{ "E_VAL" } = $fields[ 8 ];
427 $record{ "SCORE" } = $fields[ 9 ];
428 $record{ "IDENT" } = $fields[ 10 ];
430 $record{ "Q_END" } = $record{ "Q_BEG" } + $record{ "Q_LEN" } - 1;
431 $record{ "S_END" } = $record{ "S_BEG" } + $record{ "S_LEN" } - 1;
433 return wantarray ? %record : \%record;
438 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<