]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/bwa_seq
fixed bug in bwa_seq
[biopieces.git] / bp_bin / bwa_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 BWA 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::SAM;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $in, $out, $index, $tmp_dir, $tmp_fq, $tmp_sai, $tmp_sam, $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 => 'cpus',        short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef,     disallowed => 0     },
50     ]   
51 );
52
53 Maasha::Common::error( qq(both --index_name and --genome specified) ) if     $options->{ "genome" } and     $options->{ "index_name" };
54 Maasha::Common::error( qq(no --index_name or --genome specified) )    if not $options->{ "genome" } and not $options->{ "index_name" };
55
56 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
57 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
58
59 if ( defined $options->{ 'genome' } ) {
60     $index = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/bwa/$options->{ 'genome' }";
61 } elsif (defined $options->{ 'index_name' } ) {
62     $index = $options->{ 'index_name' };
63 }
64
65 $tmp_dir = Maasha::Biopieces::get_tmpdir();
66 $tmp_fq  = "$tmp_dir/bwa.fq";
67 $tmp_sai = "$tmp_dir/bwa.fq.sai";
68 $tmp_sam = "$tmp_dir/bwa.fq.sam";
69
70 $fh_out = Maasha::Filesys::file_write_open( $tmp_fq );
71
72 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
73 {
74     if ( $entry = Maasha::Fastq::biopiece2fastq( $record ) )
75     {
76         $entry->[ 2 ] =~ s/(.)/chr( ( ord( $1 ) - 64 ) + 33 )/ge; # convert scores from phred-64 to phred-33
77         Maasha::Fastq::put_entry( $entry, $fh_out );
78     }
79
80     Maasha::Biopieces::put_record( $record, $out );
81 }
82
83 close $fh_out;
84
85 push @args, "-t $options->{ 'cpus' }";
86 push @args, "-R $options->{ 'max_hits' }" if $options->{ 'max_hits' };
87
88 $arg = join " ", @args;
89
90 if ( $options->{ 'verbose' } )
91 {
92     print STDERR qq(Running: bwa aln $arg $index $tmp_fq > $tmp_sai\n);
93     Maasha::Common::run( "bwa", "aln $arg $index $tmp_fq > $tmp_sai" );
94     print STDERR qq(Running: bwa samse $index $tmp_sai $tmp_fq > $tmp_sam\n);
95     Maasha::Common::run( "bwa", "samse $index $tmp_sai $tmp_fq > $tmp_sam" )
96 }
97 else
98 {
99     Maasha::Common::run( "bwa", "aln $arg $index $tmp_fq       > $tmp_sai 2> /dev/null" );
100     Maasha::Common::run( "bwa", "samse $index $tmp_sai $tmp_fq > $tmp_sam 2> /dev/null" );
101 }
102
103 $fh_in = Maasha::Filesys::file_read_open( $tmp_sam );
104
105 while ( $entry = Maasha::SAM::sam_entry_get( $fh_in ) )
106 {
107     if ( $record = Maasha::SAM::sam2biopiece( $entry ) ) {
108         Maasha::Biopieces::put_record( $record, $out );
109     }
110 }
111
112 close $fh_in;
113
114 unlink $tmp_fq;
115 unlink $tmp_sai;
116 unlink $tmp_sam;
117
118 Maasha::Biopieces::close_stream( $in );
119 Maasha::Biopieces::close_stream( $out );
120
121
122 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
123
124
125 BEGIN
126 {
127     Maasha::Biopieces::status_set();
128 }
129
130
131 END
132 {
133     Maasha::Biopieces::status_log();
134 }
135
136
137 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
138
139
140 __END__