3 # Copyright (C) 2007-2010 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 # USEARCH sequences in the stream against a specified database or genome.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $TYPE_HASH, $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $q_type, $record, $entry, $fh_in, $fh_out );
53 $options = Maasha::Biopieces::parse_options(
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 },
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" };
64 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
65 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
67 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
69 $tmp_dir = Maasha::Biopieces::get_tmpdir();
70 $tmp_in = "$tmp_dir/usearch_query.seq";
71 $tmp_out = "$tmp_dir/usearch.uc";
73 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
75 while ( $record = Maasha::Biopieces::get_record( $in ) )
77 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
79 $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
81 Maasha::Fasta::put_entry( $entry, $fh_out );
84 Maasha::Biopieces::put_record( $record, $out );
90 if ( $options->{ 'verbose' } )
96 "--lib $options->{ 'database' }",
99 "--id $options->{ 'id' }",
111 "--lib $options->{ 'database' }",
114 "--id $options->{ 'id' }",
124 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
126 while ( $entry = get_tab_entry( $fh_in ) )
128 $record = uclust_tab2biopiece( $entry );
130 Maasha::Biopieces::put_record( $record, $out ) if $record->{ 'TYPE' } eq 'Hit';
137 Maasha::Biopieces::close_stream( $in );
138 Maasha::Biopieces::close_stream( $out );
141 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
146 # Martin A. Hansen, May 2009.
148 # Get the next tabular entry from a filehandle to a USEARCH file.
150 my ( $fh, # filehandle
155 my ( $line, @fields );
157 while ( $line = <$fh> )
159 next if $line =~ /^#/;
161 @fields = split /\s+/, $line;
163 return wantarray ? @fields : \@fields;
170 sub uclust_tab2biopiece
172 # Martin A. Hansen, May 2009.
174 # Get the next USEARCH tabular entry and convert it to
175 # a biopiece record that is returned.
177 my ( $entry, # USEARCH tabular entry
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 ];
196 if ( $record{ 'TYPE' } eq 'Hit' )
198 $record{ "Q_END" } = $record{ "Q_BEG" } + $record{ "ALIGN_LEN" } - 1;
199 $record{ "S_END" } = $record{ "S_BEG" } + $record{ "ALIGN_LEN" } - 1;
202 return wantarray ? %record : \%record;
207 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
212 Maasha::Biopieces::status_set();
218 Maasha::Biopieces::status_log();
222 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<