3 # Copyright (C) 2007-2009 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Vmatch sequences in the stream against a specified database.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, @index_files, $tmp_dir, $query_file, $fh_out, $fh_in, $record, $entry,
42 $vmatch_args, $result_file, $count_hash );
44 $options = Maasha::Biopieces::parse_options(
46 { long => 'genome', short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'index_name', short => 'i', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'count', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'max_hits', short => 'm', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
50 { long => 'hamming_dist', short => 'h', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
51 { long => 'edit_dist', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
55 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
57 Maasha::Common::error( qq(both --index_hame and --genome specified) ) if $options->{ "genome" } and $options->{ "index_name" };
58 Maasha::Common::error( qq(no --index_name or --genome specified) ) if not $options->{ "genome" } and not $options->{ "index_name" };
60 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
61 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63 @index_files = get_index_files( $options );
65 $tmp_dir = Maasha::Biopieces::get_tmpdir();
67 $query_file = "$tmp_dir/query.seq";
68 $result_file = "$tmp_dir/vmatch.out";
70 $fh_out = Maasha::Filesys::file_write_open( $query_file );
72 while ( $record = Maasha::Biopieces::get_record( $in ) )
74 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
76 next if length $record->{ 'SEQ' } < 12; # assuming that the index is created for 12 as minimum length
78 Maasha::Fasta::put_entry( $entry, $fh_out, 100 );
81 Maasha::Biopieces::put_record( $record, $out ); # must this be here?
86 $vmatch_args = vmatch_args( $options, $query_file );
88 map { Maasha::Common::run( "vmatch", "$vmatch_args $_ >> $result_file" ) } @index_files;
92 $fh_in = Maasha::Filesys::file_read_open( $result_file );
94 if ( $options->{ "count" } )
96 $count_hash = vmatch_count_hits( $result_file ) if ( $options->{ "count" } );
98 if ( $options->{ "max_hits" } )
100 while ( $record = vmatch_get_entry( $fh_in ) )
102 $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
104 next if $record->{ 'SCORE' } > $options->{ 'max_hits' };
106 Maasha::Biopieces::put_record( $record, $out );
112 while ( $record = vmatch_get_entry( $fh_in ) )
114 $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
116 Maasha::Biopieces::put_record( $record, $out );
122 while ( $record = vmatch_get_entry( $fh_in ) ) {
123 Maasha::Biopieces::put_record( $record, $out );
132 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
137 # Martin A. Hansen, May 2009.
139 # Get the index_files from the index_name or genome options.
141 my ( $options, # hashref
146 my ( @index_files, %hash );
148 if ( $options->{ "index_name" } )
150 @index_files = $options->{ "index_name" };
154 @index_files = Maasha::Filesys::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
156 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
158 @index_files = sort keys %hash;
161 return wantarray ? @index_files : \@index_files;
167 # Martin A. Hansen, May 2009.
169 # Given an option hashref and query file, compiles the arguments for running
170 # Vmatch on the commandline.
172 my ( $options, # hashref
173 $query_file, # path to query file
180 $vmatch_args = "-complete -d -p -showdesc 100 -q $query_file";
182 $vmatch_args .= " -h " . $options->{ "hamming_dist" } if $options->{ "hamming_dist" };
183 $vmatch_args .= " -e " . $options->{ "edit_dist" } if $options->{ "edit_dist" };
189 sub vmatch_count_hits
191 # Martin A. Hansen, April 2008.
193 # Given a Vmatch result files, count duplications based
194 # on q_id. The counts are returned in a hash.
196 my ( $file, # vmatch result file
201 my ( $fh_in, $line, @fields, %count_hash );
203 $fh_in = Maasha::Filesys::file_read_open( $file );
205 while ( $line = <$fh_in> )
209 next if $line =~ /^#/;
211 @fields = split " ", $line;
213 $count_hash{ $fields[ 5 ] }++;
218 return wantarray ? %count_hash : \%count_hash;
224 # Martin A. Hansen, January 2008.
226 # Parses vmatch output records.
228 my ( $fh, # file handle to vmatch result file.
233 my ( $line, @fields, %record );
235 while ( $line = <$fh> )
239 next if $line =~ /^#/;
242 @fields = split " ", $line;
244 $record{ "REC_TYPE" } = "VMATCH";
246 $record{ "S_LEN" } = $fields[ 0 ];
247 $record{ "S_ID" } = $fields[ 1 ];
248 $record{ "S_BEG" } = $fields[ 2 ];
250 if ( $fields[ 3 ] eq "D" ) {
251 $record{ "STRAND" } = "+";
253 $record{ "STRAND" } = "-";
256 $record{ "Q_LEN" } = $fields[ 4 ];
257 $record{ "Q_ID" } = $fields[ 5 ];
258 $record{ "Q_BEG" } = $fields[ 6 ];
259 $record{ "MATCH_DIST" } = $fields[ 7 ];
260 $record{ "E_VAL" } = $fields[ 8 ];
261 $record{ "SCORE" } = $fields[ 9 ];
262 $record{ "IDENT" } = $fields[ 10 ];
264 $record{ "Q_END" } = $record{ "Q_BEG" } + $record{ "Q_LEN" } - 1;
265 $record{ "S_END" } = $record{ "S_BEG" } + $record{ "S_LEN" } - 1;
267 return wantarray ? %record : \%record;
272 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
277 $run_time_beg = Maasha::Biopieces::run_time();
279 Maasha::Biopieces::log_biopiece();
284 Maasha::Biopieces::close_stream( $in );
285 Maasha::Biopieces::close_stream( $out );
287 $run_time_end = Maasha::Biopieces::run_time();
289 Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
293 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<