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