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