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