]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/bowtie_seq
19368f84452db6ffb3def6777d1ac646e64c9d1e
[biopieces.git] / bp_bin / bowtie_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 # Use bowtie to map sequences in the stream against a specified genome or index.
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::Fastq;
35 use Maasha::Fasta;
36 use Maasha::Calc;
37
38 use constant {
39     SEQ_NAME => 0,
40     SEQ      => 1,
41     SCORES   => 2,
42 };
43
44 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
45
46
47 my ( $options, $in, $out, $index, $tmp_dir, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $entry, $line, @fields, $type, @args, $arg );
48
49 $options = Maasha::Biopieces::parse_options(
50     [
51         { long => 'genome',      short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
52         { long => 'index_name',  short => 'i', type => 'string', mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
53         { long => 'mismatches',  short => 'm', type => 'uint',   mandatory => 'no', default => 0,     allowed => "0,1,2,3", disallowed => undef },
54         { long => 'max_hits',    short => 'h', type => 'uint',   mandatory => 'no', default => undef, allowed => undef,     disallowed => 0     },
55         { long => 'seed_length', short => 's', type => 'uint',   mandatory => 'no', default => 28,    allowed => undef,     disallowed => 0     },
56         { long => 'cpus',        short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef,     disallowed => 0     },
57     ]   
58 );
59
60 Maasha::Common::error( qq(both --index_name and --genome specified) ) if     $options->{ "genome" } and     $options->{ "index_name" };
61 Maasha::Common::error( qq(no --index_name or --genome specified) )    if not $options->{ "genome" } and not $options->{ "index_name" };
62
63 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
64 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
65
66 if ( defined $options->{ 'genome' } ) {
67     $index = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/bowtie/$options->{ 'genome' }";
68 } elsif (defined $options->{ 'index_name' } ) {
69     $index = $options->{ 'index_name' };
70 }
71
72 $tmp_dir = Maasha::Biopieces::get_tmpdir();
73 $tmp_in  = "$tmp_dir/bowtie.seq";
74 $tmp_out = "$tmp_dir/bowtie.out";
75
76 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
77
78 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
79 {
80     if ( $entry = Maasha::Fastq::biopiece2fastq( $record ) )
81     {
82         Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTQ";
83         Maasha::Common::error( "Sequence longer than 1024 not allowed")    if length( $entry->[ SEQ ] ) > 1024;
84         Maasha::Fastq::put_entry( $entry, $fh_out );
85
86         $type = "FASTQ";
87     }
88     elsif ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
89     {
90         Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTA";
91         Maasha::Common::error( "Sequence longer than 1024 not allowed")    if length( $entry->[ SEQ ] ) > 1024;
92         Maasha::Fasta::put_entry( $entry, $fh_out );
93
94         $type = "FASTA";
95     }
96
97     Maasha::Biopieces::put_record( $record, $out );
98 }
99
100 close $fh_out;
101
102 push @args, "-n $options->{ 'mismatches' }";
103 push @args, "-v $options->{ 'mismatches' }";  # DANGER: using seed mismatches as alignment mismatches - may work, may not!
104 push @args, "-f" if $type eq "FASTA";
105 push @args, "-p $options->{ 'cpus' }";
106 push @args, "--phred64-quals" unless $type eq "FASTA";
107
108 if ( defined $options->{ 'max_hits' } ) {
109     push @args, "-k $options->{ 'max_hits' }";
110 } else {
111     push @args, "-a";
112 }
113
114 $arg = join " ", @args;
115
116 if ( $options->{ 'verbose' } )
117 {
118     print STDERR qq(Running: bowtie $arg $index $tmp_in $tmp_out\n);
119     Maasha::Common::run( "bowtie", "$arg $index $tmp_in $tmp_out" );
120 }
121 else
122 {
123     Maasha::Common::run( "bowtie", "$arg $index $tmp_in $tmp_out > /dev/null 2>&1" );
124 }
125
126 unlink $tmp_in;
127
128 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
129
130 while ( $line = <$fh_in> )
131 {
132     chomp $line;
133
134     @fields = split /\t/, $line;
135     $record = bowtie2biopiece( \@fields );
136
137     Maasha::Biopieces::put_record( $record, $out );
138 }
139
140 close $fh_out;
141
142 unlink $tmp_out;
143
144 Maasha::Biopieces::close_stream( $in );
145 Maasha::Biopieces::close_stream( $out );
146
147
148 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
149
150
151 sub bowtie2biopiece
152 {
153     # Martin A. Hansen, July 2009
154
155     # Convert a bowtie entry to a Biopiece record.
156
157     my ( $entry,   # bowtie entry
158        ) = @_;
159
160     # Returns a hash.
161
162     my ( $record, $s_id, $s_len, $hits );
163
164     $record->{ 'Q_ID' }       = $entry->[ 0 ];
165     $record->{ 'STRAND' }     = $entry->[ 1 ];
166     $record->{ 'S_ID' }       = $entry->[ 2 ];
167     $record->{ 'S_BEG' }      = $entry->[ 3 ];
168     $record->{ 'SEQ' }        = $entry->[ 4 ];
169     $record->{ 'SCORES' }     = $entry->[ 5 ];
170     $record->{ 'SCORE' }      = $entry->[ 6 ] + 1;
171     $record->{ 'ALIGN' }      = $entry->[ 7 ] || '.';
172     $record->{ 'S_LEN' }      = length $entry->[ 4 ];
173     $record->{ 'SEQ_LEN' }    = length $entry->[ 4 ];
174     $record->{ 'S_END' }      = $record->{ 'S_BEG' } + $record->{ 'SEQ_LEN' } - 1;
175     $record->{ 'SCORES' }     =~ s/(.)/chr( ( ord( $1 ) - 33 ) + 64 )/ge; # convert phred scores to illumina scores
176
177     $record->{ 'HITS' }       = '.';
178     $record->{ 'REC_TYPE' } = "BOWTIE";
179
180     return wantarray ? %{ $record } : $record; 
181 }
182
183
184 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
185
186
187 BEGIN
188 {
189     Maasha::Biopieces::status_set();
190 }
191
192
193 END
194 {
195     Maasha::Biopieces::status_log();
196 }
197
198
199 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
200
201
202 __END__