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