]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/usearch_seq
added usearch_seq biopiece
[biopieces.git] / bp_bin / usearch_seq
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2010 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 # USEARCH sequences in the stream against a specified database or genome.
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::Seq;
35 use Maasha::Fasta;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $TYPE_HASH, $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $q_type, $record, $entry, $fh_in, $fh_out );
42
43 $TYPE_HASH = {
44     'L' => 'LibSeed',
45     'S' => 'NewSeed',
46     'H' => 'Hit',
47     'R' => 'Reject',
48     'D' => 'LibCluster',
49     'C' => 'NewCluster',
50     'N' => 'NoHit'
51 };
52
53 $options = Maasha::Biopieces::parse_options(
54     [
55         { long => 'database', short => 'd', type => 'file',   mandatory => 'no', default => undef,  allowed => undef, disallowed => undef },
56         { long => 'genome',   short => 'g', type => 'genome', mandatory => 'no', default => undef,  allowed => undef, disallowed => undef },
57         { long => 'id',       short => 'i', type => 'float',  mandatory => 'no', default => '0.90', allowed => undef, disallowed => undef },
58     ]   
59 );
60
61 Maasha::Common::error( qq(both --database and --genome specified) ) if     $options->{ "genome" } and     $options->{ "database" };
62 Maasha::Common::error( qq(no --database or --genome specified) )    if not $options->{ "genome" } and not $options->{ "database" };
63
64 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
65 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
66
67 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
68
69 $tmp_dir = Maasha::Biopieces::get_tmpdir();
70 $tmp_in  = "$tmp_dir/usearch_query.seq";
71 $tmp_out = "$tmp_dir/usearch.uc";
72
73 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
74
75 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
76 {
77     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
78     {
79         $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
80
81         Maasha::Fasta::put_entry( $entry, $fh_out );
82     }
83
84     Maasha::Biopieces::put_record( $record, $out );
85 }
86
87 close $fh_out;
88
89
90 if ( $options->{ 'verbose' } )
91 {
92     Maasha::Common::run(
93         "uclust",
94         join( " ",
95             "--input $tmp_in",
96             "--lib $options->{ 'database' }",
97             "--libonly",
98             "--uc $tmp_out",
99             "--id $options->{ 'id' }",
100             "--rev",
101         ),
102         1
103     );
104 }
105 else
106 {
107     Maasha::Common::run(
108         "uclust",
109         join( " ",
110             "--input $tmp_in",
111             "--lib $options->{ 'database' }",
112             "--libonly",
113             "--uc $tmp_out",
114             "--id $options->{ 'id' }",
115             "--rev",
116             "> /dev/null 2>&1"
117         ),
118         1
119     );
120 }
121
122 unlink $tmp_in;
123
124 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
125
126 while ( $entry = get_tab_entry( $fh_in ) )
127 {
128     $record = uclust_tab2biopiece( $entry );
129
130     Maasha::Biopieces::put_record( $record, $out ) if $record->{ 'TYPE' } eq 'Hit';
131 }
132
133 close $fh_out;
134
135 unlink $tmp_out;
136
137 Maasha::Biopieces::close_stream( $in );
138 Maasha::Biopieces::close_stream( $out );
139
140
141 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
142
143
144 sub get_tab_entry
145 {
146     # Martin A. Hansen, May 2009.
147
148     # Get the next tabular entry from a filehandle to a USEARCH file.
149
150     my ( $fh,   # filehandle
151        ) = @_;
152
153     # Returns a list
154
155     my ( $line, @fields );
156
157     while ( $line = <$fh> )
158     {
159         next if $line =~ /^#/;
160     
161         @fields = split /\s+/, $line;
162
163         return wantarray ? @fields : \@fields;
164     }
165
166     return undef;
167 }
168
169
170 sub uclust_tab2biopiece
171 {
172     # Martin A. Hansen, May 2009.
173
174     # Get the next USEARCH tabular entry and convert it to
175     # a biopiece record that is returned.
176
177     my ( $entry,    # USEARCH tabular entry
178        ) = @_;
179
180     # Returns a hashref.
181
182     my ( %record );
183
184     $record{ "REC_TYPE" }  = "USEARCH";
185     $record{ "TYPE" }      = $TYPE_HASH->{ $entry->[ 0 ] };
186     $record{ "CLUSTER" }   = $entry->[ 1 ];
187     $record{ "ALIGN_LEN" } = $entry->[ 2 ];
188     $record{ "ID" }        = $entry->[ 3 ];
189     $record{ "STRAND" }    = $entry->[ 4 ];
190     $record{ "Q_BEG" }     = $entry->[ 5 ];
191     $record{ "S_BEG" }     = $entry->[ 6 ];
192     $record{ "CIGAR" }     = $entry->[ 7 ];
193     $record{ "Q_ID" }      = $entry->[ 8 ];
194     $record{ "S_ID" }      = $entry->[ 9 ];
195
196     if ( $record{ 'TYPE' } eq 'Hit' )
197     {
198         $record{ "Q_END" } = $record{ "Q_BEG" } + $record{ "ALIGN_LEN" } - 1;
199         $record{ "S_END" } = $record{ "S_BEG" } + $record{ "ALIGN_LEN" } - 1;
200     }
201
202     return wantarray ? %record : \%record;
203 }
204
205
206
207 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
208
209
210 BEGIN
211 {
212     Maasha::Biopieces::status_set();
213 }
214
215
216 END
217 {
218     Maasha::Biopieces::status_log();
219 }
220
221
222 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
223
224
225 __END__