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