]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/soap_seq
removed variables bulk type
[biopieces.git] / bp_bin / soap_seq
1 #!/usr/bin/env perl -w
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 soap to match short nucleotide sequences in the stream against a specified genome.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Biopieces;
31 use Maasha::Common;
32 use Maasha::Filesys;
33 use Maasha::Fasta;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $fh_out, $record, $entry, $count, $args, $line, @fields );
40
41 $options = Maasha::Biopieces::parse_options(
42     [
43         { long => 'in_file',    short => 'i', type => 'file',   mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
44         { long => 'genome',     short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45         { long => 'seed_size',  short => 's', type => 'uint',   mandatory => 'no', default => 10,    allowed => undef, disallowed => undef },
46         { long => 'mismatches', short => 'm', type => 'uint',   mandatory => 'no', default => 2,     allowed => undef, disallowed => undef },
47         { long => 'gap_size',   short => 'G', type => 'uint',   mandatory => 'no', default => 0,     allowed => undef, disallowed => undef },
48         { long => 'cpus',       short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef, disallowed => 0 },
49     ]   
50 );
51
52 Maasha::Common::error( qq(both --in_file and --genome specified) ) if     $options->{ "genome" } and     $options->{ "in_file" };
53 Maasha::Common::error( qq(no --in_file or --genome specified) )    if not $options->{ "genome" } and not $options->{ "in_file" };
54
55 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
56 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
57
58 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
59
60 $tmp_dir = Maasha::Biopieces::get_tmpdir();
61 $tmp_in  = "$tmp_dir/soap_query.seq";
62 $tmp_out = "$tmp_dir/soap.result";
63
64 $fh_out = Maasha::Common::write_open( $tmp_in );
65
66 $count = 0;
67
68 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
69 {
70     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
71     {
72         Maasha::Fasta::put_entry( $entry, $fh_out );
73
74         $count++;
75     }
76
77     Maasha::Biopieces::put_record( $record, $out );
78 }
79
80 close $fh_out;
81
82 if ( $count > 0 )
83 {
84     $args = join( " ",
85         "-s $options->{ 'seed_size' }",
86         "-r 2",
87         "-a $tmp_in",
88         "-v $options->{ 'mismatches' }",
89         "-g $options->{ 'gap_size' }",
90         "-p $options->{ 'cpus' }",
91         "-d $options->{ 'in_file' }",
92         "-o $tmp_out",
93     );
94
95     $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
96
97     Maasha::Common::run( "soap", $args, 1 );
98
99     unlink $tmp_in;
100
101     $fh_out = Maasha::Filesys::file_read_open( $tmp_out );
102
103     undef $record;
104
105     while ( $line = <$fh_out> )
106     {
107         chomp $line;
108
109         @fields = split /\t/, $line;
110
111         $record->{ "REC_TYPE" }   = "SOAP";
112         $record->{ "Q_ID" }       = $fields[ 0 ];
113         $record->{ "SCORE" }      = $fields[ 3 ];
114         $record->{ "STRAND" }     = $fields[ 6 ];
115         $record->{ "S_ID" }       = $fields[ 7 ];
116         $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is 1-based
117         $record->{ "S_END" }      = $fields[ 8 ] + $fields[ 5 ] - 2;
118
119         Maasha::Biopieces::put_record( $record, $out );
120     }
121
122     close $fh_out;
123 }
124
125 unlink $tmp_out;
126
127 Maasha::Filesys::dir_remove( $tmp_dir );
128
129
130 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
131
132
133 BEGIN
134 {
135     $run_time_beg = Maasha::Biopieces::run_time();
136
137     Maasha::Biopieces::log_biopiece();
138 }
139
140 END
141 {
142     Maasha::Biopieces::close_stream( $in );
143     Maasha::Biopieces::close_stream( $out );
144
145     $run_time_end = Maasha::Biopieces::run_time();
146
147     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
148 }
149
150
151 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
152
153
154 __END__
155