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