]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/bowtie_seq
added some args to bowtie_seq
[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.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::Fastq;
34 use Maasha::Fasta;
35 use Maasha::Calc;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $in, $out, $index, $tmp_dir, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $entry, $line, @fields, $type, @args, $arg );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'genome',     short => 'g', type => 'genome', mandatory => 'yes', default => undef, allowed => undef,     disallowed => undef },
46         { long => 'mismatches', short => 'm', type => 'uint',   mandatory => 'no',  default => 2,     allowed => "0,1,2,3", disallowed => undef },
47         { long => 'max_hits',   short => 'h', type => 'uint',   mandatory => 'no',  default => undef, allowed => undef,     disallowed => 0     },
48     ]   
49 );
50
51 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
52 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53
54 $index = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/bowtie/$options->{ 'genome' }";
55
56 $tmp_dir = Maasha::Biopieces::get_tmpdir();
57 $tmp_in  = "$tmp_dir/bowtie.seq";
58 $tmp_out = "$tmp_dir/bowtie.out";
59
60 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
61
62 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
63 {
64     if ( $entry = Maasha::Fastq::biopiece2fastq( $record ) )
65     {
66         Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTQ";
67         Maasha::Fastq::put_entry( $entry, $fh_out );
68
69         $type = "FASTQ";
70     }
71     elsif ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
72     {
73         Maasha::Common::error( "Mixed FASTA and FASTQ entries in stream" ) if defined $type and $type ne "FASTA";
74         Maasha::Fasta::put_entry( $entry, $fh_out );
75
76         $type = "FASTA";
77     }
78
79     Maasha::Biopieces::put_record( $record, $out );
80 }
81
82 close $fh_out;
83
84 push @args, "-n $options->{ 'mismatches' }";
85 push @args, "-f" if $type eq "FASTA";
86
87 if ( defined $options->{ 'max_hits' } ) {
88     push @args, "-k $options->{ 'max_hits' }";
89 } else {
90     push @args, "-a";
91 }
92
93 $arg = join " ", @args;
94
95 if ( $options->{ 'verbose' } ) {
96     Maasha::Common::run( "bowtie", "$arg $index $tmp_in $tmp_out" );
97 } else {
98     Maasha::Common::run( "bowtie", "$arg $index $tmp_in $tmp_out > /dev/null 2>&1" );
99 }
100
101 unlink $tmp_in;
102
103 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
104
105 while ( $line = <$fh_in> )
106 {
107     chomp $line;
108
109     @fields = split /\t/, $line;
110
111     $record = bowtie2biopiece( \@fields );
112
113     Maasha::Biopieces::put_record( $record, $out );
114 }
115
116 close $fh_out;
117
118 unlink $tmp_out;
119
120 Maasha::Biopieces::close_stream( $in );
121 Maasha::Biopieces::close_stream( $out );
122
123
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125
126
127 sub bowtie2biopiece
128 {
129     # Martin A. Hansen, July 2009
130
131     # Convert a bowtie entry to a Biopiece record.
132
133     my ( $entry,   # bowtie entry
134        ) = @_;
135
136     # Returns a hash.
137
138     my ( $record, @scores );
139
140     $record->{ 'Q_ID' }     = $entry->[ 0 ];
141     $record->{ 'STRAND' }   = $entry->[ 1 ];
142     $record->{ 'S_ID' }     = $entry->[ 2 ];
143     $record->{ 'S_BEG' }    = $entry->[ 3 ];
144     $record->{ 'SEQ' }      = $entry->[ 4 ];
145     $record->{ 'SCORES' }   = $entry->[ 5 ];
146     $record->{ 'MISMATCH' } = $entry->[ 6 ];
147
148     $record->{ 'SEQ_LEN' }    = length $entry->[ 4 ];
149     $record->{ 'S_END' }      = $record->{ 'S_BEG' } + $record->{ 'SEQ_LEN' } - 1;
150     $record->{ 'SCORES' }     =~ s/(.)/ord( $1 ) - 33 . ";"/ge; # http://maq.sourceforge.net/fastq.shtml
151     $record->{ 'SCORE_MEAN' } = sprintf( "%.2f", Maasha::Calc::mean( [ split /;/, $record->{ 'SCORES' } ] ) );
152
153     $record->{ 'REC_TYPE' } = "BOWTIE";
154
155     return wantarray ? %{ $record } : $record; 
156 }
157
158
159 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
160
161
162 BEGIN
163 {
164     Maasha::Biopieces::status_set();
165 }
166
167
168 END
169 {
170     Maasha::Biopieces::status_log();
171 }
172
173
174 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
175
176
177 __END__