]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/vmatch_seq
fixed seq qual length check
[biopieces.git] / bp_bin / vmatch_seq
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Vmatch sequences in the stream against a specified database.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Biopieces;
33 use Maasha::Common;
34 use Maasha::Filesys;
35 use Maasha::Seq;
36 use Maasha::Fasta;
37
38
39 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40
41
42 my ( $options, $in, $out, @index_files, $tmp_dir, $query_file, $fh_out, $fh_in, $record, $entry,
43      $vmatch_args, $result_file, $count_hash );
44
45 $options = Maasha::Biopieces::parse_options(
46     [
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 },
53     ]   
54 );
55
56 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
57
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" };
60
61 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63
64 @index_files = get_index_files( $options );
65
66 $tmp_dir = Maasha::Biopieces::get_tmpdir();
67
68 $query_file  = "$tmp_dir/query.seq";
69 $result_file = "$tmp_dir/vmatch.out";
70
71 $fh_out = Maasha::Filesys::file_write_open( $query_file );
72
73 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
74 {
75     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
76     {
77         next if length $record->{ 'SEQ' } < 12; # assuming that the index is created for 12 as minimum length
78
79         Maasha::Fasta::put_entry( $entry, $fh_out, 100 );
80     }
81
82     Maasha::Biopieces::put_record( $record, $out ); # must this be here?
83 }
84
85 close $fh_out;
86
87 $vmatch_args = vmatch_args( $options, $query_file );
88
89 map { Maasha::Common::run( "vmatch", "$vmatch_args $_ >> $result_file" ) } @index_files;
90
91 unlink $query_file;
92
93 $fh_in = Maasha::Filesys::file_read_open( $result_file );
94
95 if ( $options->{ "count" } )
96 {
97     $count_hash = vmatch_count_hits( $result_file ) if ( $options->{ "count" } );
98
99     if ( $options->{ "max_hits" } )
100     {
101         while ( $record = vmatch_get_entry( $fh_in ) )
102         {
103             $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
104
105             next if $record->{ 'SCORE' } > $options->{ 'max_hits' };
106
107             Maasha::Biopieces::put_record( $record, $out );
108         }
109     
110     }
111     else
112     {
113         while ( $record = vmatch_get_entry( $fh_in ) )
114         {
115             $record->{ 'SCORE' } = $count_hash->{ $record->{ 'Q_ID' } };
116
117             Maasha::Biopieces::put_record( $record, $out );
118         }
119     }
120 }
121 else
122 {
123     while ( $record = vmatch_get_entry( $fh_in ) ) {
124         Maasha::Biopieces::put_record( $record, $out );
125     }
126 }
127
128 close $fh_in;
129
130 unlink $result_file;
131
132
133 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
134
135
136 sub get_index_files
137 {
138     # Martin A. Hansen, May 2009.
139
140     # Get the index_files from the index_name or genome options.
141
142     my ( $options,   # hashref
143        ) = @_;
144
145     # Returns a list.
146
147     my ( @index_files, %hash );
148
149     if ( $options->{ "index_name" } )
150     {
151         @index_files = $options->{ "index_name" };
152     }
153     else
154     {
155         @index_files = Maasha::Filesys::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
156
157         map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
158
159         @index_files = sort keys %hash;
160     }
161
162     return wantarray ? @index_files : \@index_files;
163 }
164
165
166 sub vmatch_args
167 {
168     # Martin A. Hansen, May 2009.
169
170     # Given an option hashref and query file, compiles the arguments for running
171     # Vmatch on the commandline.
172
173     my ( $options,      # hashref
174          $query_file,   # path to query file
175        ) = @_;
176
177     # Returns a string.
178
179     my ( $vmatch_args );
180
181     $vmatch_args  = "-complete -d -p -showdesc 100 -q $query_file";
182     
183     $vmatch_args .= " -h " . $options->{ "hamming_dist" } if $options->{ "hamming_dist" };
184     $vmatch_args .= " -e " . $options->{ "edit_dist" }    if $options->{ "edit_dist" };
185
186     return $vmatch_args;
187 }
188
189
190 sub vmatch_count_hits
191 {
192     # Martin A. Hansen, April 2008.
193
194     # Given a Vmatch result files, count duplications based
195     # on q_id. The counts are returned in a hash.
196
197     my ( $file,   # vmatch result file
198        ) = @_;
199
200     # Returns a list.
201
202     my ( $fh_in, $line, @fields, %count_hash );
203
204     $fh_in = Maasha::Filesys::file_read_open( $file );
205
206     while ( $line = <$fh_in> )
207     {
208         chomp $line;
209
210         next if $line =~ /^#/;
211
212         @fields = split " ", $line;
213
214         $count_hash{ $fields[ 5 ] }++;
215     }
216
217     close $fh_in;
218
219     return wantarray ? %count_hash : \%count_hash;
220 }
221
222
223 sub vmatch_get_entry
224 {
225     # Martin A. Hansen, January 2008.
226
227     # Parses vmatch output records.
228
229     my ( $fh,   # file handle to vmatch result file.
230        ) = @_;
231
232     # Returns a hash.
233
234     my ( $line, @fields, %record );
235
236     while ( $line = <$fh> )
237     {
238         chomp $line;
239
240         next if $line =~ /^#/;
241         $line =~ s/^\s+//;
242
243         @fields = split " ", $line;
244
245         $record{ "REC_TYPE" } = "VMATCH";
246
247         $record{ "S_LEN" }      = $fields[ 0 ];
248         $record{ "S_ID" }       = $fields[ 1 ];
249         $record{ "S_BEG" }      = $fields[ 2 ];
250
251         if ( $fields[ 3 ] eq "D" ) {
252             $record{ "STRAND" } = "+";
253         } else {
254             $record{ "STRAND" } = "-";
255         }
256
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 ];
264
265         $record{ "Q_END" }      = $record{ "Q_BEG" } + $record{ "Q_LEN" } - 1;
266         $record{ "S_END" }      = $record{ "S_BEG" } + $record{ "S_LEN" } - 1;
267
268         return wantarray ? %record : \%record;
269     }
270 }
271
272 Maasha::Biopieces::close_stream( $in );
273 Maasha::Biopieces::close_stream( $out );
274
275
276 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
277
278
279 BEGIN
280 {
281     Maasha::Biopieces::status_set();
282 }
283
284
285 END
286 {
287     Maasha::Biopieces::status_log();
288 }
289
290
291 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
292
293
294 __END__