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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
39 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
42 my ( $options, $in, $out, @index_files, $tmp_dir, $query_file, $fh_out, $fh_in, $record, $entry,
43 $vmatch_args, $result_file, $count_hash );
45 $options = Maasha::Biopieces::parse_options(
47 { long => 'genome', short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'index_name', short => 'i', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'count', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 { long => 'max_hits', short => 'm', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
51 { long => 'hamming_dist', short => 'h', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
52 { long => 'edit_dist', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
56 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
58 Maasha::Common::error( qq(both --index_hame and --genome specified) ) if $options->{ "genome" } and $options->{ "index_name" };
59 Maasha::Common::error( qq(no --index_name or --genome specified) ) if not $options->{ "genome" } and not $options->{ "index_name" };
61 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
64 @index_files = get_index_files( $options );
66 $tmp_dir = Maasha::Biopieces::get_tmpdir();
68 $query_file = "$tmp_dir/query.seq";
69 $result_file = "$tmp_dir/vmatch.out";
71 $fh_out = Maasha::Filesys::file_write_open( $query_file );
73 while ( $record = Maasha::Biopieces::get_record( $in ) )
75 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
77 next if length $record->{ 'SEQ' } < 12; # assuming that the index is created for 12 as minimum length
79 Maasha::Fasta::put_entry( $entry, $fh_out, 100 );
82 Maasha::Biopieces::put_record( $record, $out ); # must this be here?
87 $vmatch_args = vmatch_args( $options, $query_file );
89 map { Maasha::Common::run( "vmatch", "$vmatch_args $_ >> $result_file" ) } @index_files;
93 $fh_in = Maasha::Filesys::file_read_open( $result_file );
95 if ( $options->{ "count" } )
97 $count_hash = vmatch_count_hits( $result_file ) if ( $options->{ "count" } );
99 if ( $options->{ "max_hits" } )
101 while ( $record = vmatch_get_entry( $fh_in ) )
103 $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
105 next if $record->{ 'SCORE' } > $options->{ 'max_hits' };
107 Maasha::Biopieces::put_record( $record, $out );
113 while ( $record = vmatch_get_entry( $fh_in ) )
115 $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
117 Maasha::Biopieces::put_record( $record, $out );
123 while ( $record = vmatch_get_entry( $fh_in ) ) {
124 Maasha::Biopieces::put_record( $record, $out );
133 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
138 # Martin A. Hansen, May 2009.
140 # Get the index_files from the index_name or genome options.
142 my ( $options, # hashref
147 my ( @index_files, %hash );
149 if ( $options->{ "index_name" } )
151 @index_files = $options->{ "index_name" };
155 @index_files = Maasha::Filesys::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
157 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
159 @index_files = sort keys %hash;
162 return wantarray ? @index_files : \@index_files;
168 # Martin A. Hansen, May 2009.
170 # Given an option hashref and query file, compiles the arguments for running
171 # Vmatch on the commandline.
173 my ( $options, # hashref
174 $query_file, # path to query file
181 $vmatch_args = "-complete -d -p -showdesc 100 -q $query_file";
183 $vmatch_args .= " -h " . $options->{ "hamming_dist" } if $options->{ "hamming_dist" };
184 $vmatch_args .= " -e " . $options->{ "edit_dist" } if $options->{ "edit_dist" };
190 sub vmatch_count_hits
192 # Martin A. Hansen, April 2008.
194 # Given a Vmatch result files, count duplications based
195 # on q_id. The counts are returned in a hash.
197 my ( $file, # vmatch result file
202 my ( $fh_in, $line, @fields, %count_hash );
204 $fh_in = Maasha::Filesys::file_read_open( $file );
206 while ( $line = <$fh_in> )
210 next if $line =~ /^#/;
212 @fields = split " ", $line;
214 $count_hash{ $fields[ 5 ] }++;
219 return wantarray ? %count_hash : \%count_hash;
225 # Martin A. Hansen, January 2008.
227 # Parses vmatch output records.
229 my ( $fh, # file handle to vmatch result file.
234 my ( $line, @fields, %record );
236 while ( $line = <$fh> )
240 next if $line =~ /^#/;
243 @fields = split " ", $line;
245 $record{ "REC_TYPE" } = "VMATCH";
247 $record{ "S_LEN" } = $fields[ 0 ];
248 $record{ "S_ID" } = $fields[ 1 ];
249 $record{ "S_BEG" } = $fields[ 2 ];
251 if ( $fields[ 3 ] eq "D" ) {
252 $record{ "STRAND" } = "+";
254 $record{ "STRAND" } = "-";
257 $record{ "Q_LEN" } = $fields[ 4 ];
258 $record{ "Q_ID" } = $fields[ 5 ];
259 $record{ "Q_BEG" } = $fields[ 6 ];
260 $record{ "MATCH_DIST" } = $fields[ 7 ];
261 $record{ "E_VAL" } = $fields[ 8 ];
262 $record{ "SCORE" } = $fields[ 9 ];
263 $record{ "IDENT" } = $fields[ 10 ];
265 $record{ "Q_END" } = $record{ "Q_BEG" } + $record{ "Q_LEN" } - 1;
266 $record{ "S_END" } = $record{ "S_BEG" } + $record{ "S_LEN" } - 1;
268 return wantarray ? %record : \%record;
272 Maasha::Biopieces::close_stream( $in );
273 Maasha::Biopieces::close_stream( $out );
276 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
281 Maasha::Biopieces::status_set();
287 Maasha::Biopieces::status_log();
291 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<