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