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 # Use bowtie to map sequences in the stream against a specified genome.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, $index, $tmp_dir, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $entry, $line, @fields, $type, $args );
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'genome', short => 'g', type => 'genome', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
49 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
52 $index = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/bowtie/$options->{ 'genome' }";
54 $tmp_dir = Maasha::Biopieces::get_tmpdir();
55 $tmp_in = "$tmp_dir/bowtie.seq";
56 $tmp_out = "$tmp_dir/bowtie.out";
58 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
60 while ( $record = Maasha::Biopieces::get_record( $in ) )
62 if ( $entry = Maasha::Fastq::biopiece2fastq( $record ) )
64 Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTQ";
65 Maasha::Fastq::put_entry( $entry, $fh_out );
69 elsif ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
71 Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTA";
72 Maasha::Fasta::put_entry( $entry, $fh_out );
77 Maasha::Biopieces::put_record( $record, $out );
83 $args = "-f" if $type eq "FASTA";
85 if ( $options->{ 'verbose' } ) {
86 Maasha::Common::run( "bowtie", "$args $index $tmp_in $tmp_out" );
88 Maasha::Common::run( "bowtie", "$args $index $tmp_in $tmp_out > /dev/null 2>&1" );
93 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
95 while ( $line = <$fh_in> )
99 @fields = split /\t/, $line;
101 $record = bowtie2biopiece( \@fields );
103 Maasha::Biopieces::put_record( $record, $out );
110 Maasha::Biopieces::close_stream( $in );
111 Maasha::Biopieces::close_stream( $out );
114 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
119 # Martin A. Hansen, July 2009
121 # Convert a bowtie entry to a Biopiece record.
123 my ( $entry, # bowtie entry
128 my ( $record, @scores );
130 $record->{ 'Q_ID' } = $entry->[ 0 ];
131 $record->{ 'STRAND' } = $entry->[ 1 ];
132 $record->{ 'S_ID' } = $entry->[ 2 ];
133 $record->{ 'S_BEG' } = $entry->[ 3 ];
134 $record->{ 'SEQ' } = $entry->[ 4 ];
135 $record->{ 'SCORES' } = $entry->[ 5 ];
136 $record->{ 'MISMATCH' } = $entry->[ 6 ];
138 $record->{ 'SEQ_LEN' } = length $entry->[ 4 ];
139 $record->{ 'S_END' } = $record->{ 'S_BEG' } + $record->{ 'SEQ_LEN' } - 1;
140 $record->{ 'SCORES' } =~ s/(.)/ord( $1 ) - 33 . ";"/ge; # http://maq.sourceforge.net/fastq.shtml
141 $record->{ 'SCORE_MEAN' } = sprintf( "%.2f", Maasha::Calc::mean( [ split /;/, $record->{ 'SCORES' } ] ) );
143 $record->{ 'REC_TYPE' } = "BOWTIE";
145 return wantarray ? %{ $record } : $record;
149 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
154 Maasha::Biopieces::status_set();
160 Maasha::Biopieces::status_log();
164 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<