]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/patscan_seq
removed variables bulk type
[biopieces.git] / bp_bin / patscan_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 # Scan sequences in the stream or a specified genome for patterns using patscan.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Biopieces;
31 use Maasha::Fasta;
32 use Maasha::Filesys;
33 use Maasha::Patscan;
34
35 use constant {
36     SEQ_NAME => 0,
37     SEQ      => 1,
38 };
39
40
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
42
43
44 my ( $options, $in, $out, $record, @args, $arg, $type, $tmp_dir,
45      $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $patterns, $pattern, $entry, $result, %head_hash, $i );
46
47 $options = Maasha::Biopieces::parse_options(
48     [
49         { long => 'patterns',    short => 'p', type => 'string',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50         { long => 'patterns_in', short => 'P', type => 'file!',   mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
51         { long => 'comp',        short => 'c', type => 'flag',    mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
52         { long => 'max_hits',    short => 'h', type => 'uint',    mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
53         { long => 'max_misses',  short => 'm', type => 'uint',    mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
54         { long => 'genome',      short => 'g', type => 'genome',  mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
55     ]   
56 );
57
58 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
59 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
60
61 $tmp_dir = Maasha::Biopieces::get_tmpdir();
62
63 $pat_file = "$tmp_dir/patscan.pat";
64 $out_file = "$tmp_dir/patscan.out";
65
66 $patterns = parse_patterns( $options );
67 $arg      = parse_args( $options );
68
69 if ( defined $options->{ 'genome' } )
70 {
71     $seq_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
72 }
73 else
74 {
75     $seq_file = "$tmp_dir/patscan.seq";
76
77     $fh_out = Maasha::Filesys::file_write_open( $seq_file );
78
79     $i = 0;
80
81     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
82     {
83         if ( $entry = Maasha::Biopieces::biopiece2fasta( $record ) )
84         {
85             $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
86         
87             $entry->[ SEQ_NAME ] = $i;
88
89             Maasha::Fasta::put_entry( $entry, $fh_out );
90         
91             $head_hash{ $i } = $record->{ "SEQ_NAME" };
92
93             $i++;
94         }
95     }
96
97     close $fh_out;
98
99     $arg .= " -p" if $type eq "protein";
100 }
101
102 foreach $pattern ( @{ $patterns } )
103 {
104     pat_write( $pat_file, $pattern );
105
106     `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
107      # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
108
109     $fh_in = Maasha::Filesys::file_read_open( $out_file );
110
111     while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
112     {
113         $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
114
115         if ( $options->{ 'genome' } )
116         {
117             $result->{ "CHR" }     = $result->{ "S_ID" };
118             $result->{ "CHR_BEG" } = $result->{ "S_BEG" }; 
119             $result->{ "CHR_END" } = $result->{ "S_END" }; 
120
121             delete $result->{ "S_ID" };
122             delete $result->{ "S_BEG" };
123             delete $result->{ "S_END" };
124         }
125         else
126         {
127             $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
128         }
129
130         Maasha::Biopieces::put_record( $result, $out );
131     }
132
133     close $fh_in;
134 }
135
136 unlink $pat_file;
137 unlink $out_file;
138 unlink $seq_file if not $options->{ 'genome' };
139
140 Maasha::Filesys::dir_remove( $tmp_dir );
141
142
143 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
144
145
146 sub parse_patterns
147 {
148     # Martin A. Hansen, May 2009.
149
150     # Parse pattern arguments based on information in the 
151     # options hash and returns a list of patterns.
152     # Raises an error if no info.
153
154     my ( $options,   # options hash
155        ) = @_;
156
157     # Returns a list.
158
159     my ( $patterns );
160
161     if ( $options->{ "patterns" } ) {
162         $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
163     } elsif ( -f $options->{ "patterns_in" } ) {
164         $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
165     } else {
166         Maasha::Common::error( qq(no patterns specified.) );
167     }
168
169     return wantarray ? @{ $patterns } : $patterns;
170 }
171
172
173 sub parse_args
174 {
175     # Martin A. Hansen, May 2009.
176
177     # Generate an argument string for executing scan_for_matches based
178     # on information in the option hash.
179
180     my ( $options,   # options hash
181        ) = @_;
182
183     # Returns a string.
184
185     my ( @args );
186
187     push @args, "-c"                            if $options->{ "comp" };
188     push @args, "-m $options->{ 'max_hits' }"   if $options->{ 'max_hits' };
189     push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
190
191     return join " ", @args;
192 }
193
194
195 sub pat_write
196 {
197     # Martin A. Hansen, May 2009.
198
199     # Write a scan_for_matches pattern to file.
200
201     my ( $file,      # target file to write pattern to.
202          $pattern,   # pattern to write.
203        ) = @_;
204
205     # Returns nothing.
206
207     my ( $fh_out );
208
209     $fh_out = Maasha::Common::write_open( $pat_file );
210
211     print $fh_out "$pattern\n";
212
213     close $fh_out;
214 }
215
216
217 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
218
219
220 BEGIN
221 {
222     $run_time_beg = Maasha::Biopieces::run_time();
223
224     Maasha::Biopieces::log_biopiece();
225 }
226
227
228 END
229 {
230     Maasha::Biopieces::close_stream( $in );
231     Maasha::Biopieces::close_stream( $out );
232
233     $run_time_end = Maasha::Biopieces::run_time();
234
235     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
236 }
237
238
239 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
240
241
242 __END__
243