]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/find_genes
added full switch to find_genes
[biopieces.git] / bp_bin / find_genes
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 # Find genes in sequences in stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Common;
33 use Maasha::Biopieces;
34 use Maasha::Filesys;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $record, $tmp_dir, $tmp_file, $fh_out, $fh_in, $entry, $chunk, @lines, $line, $s_id, $type, $s_beg, $s_end, $strand, @fields, $def,
41     @commands, $command );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'full', short => 'f', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46     ]   
47 );
48
49 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
51
52 $tmp_dir  = Maasha::Biopieces::get_tmpdir();
53 $tmp_file = "$tmp_dir/tmp.seq";
54 $fh_out   = Maasha::Filesys::file_write_open( $tmp_file );
55
56 while ( $record = Maasha::Biopieces::get_record( $in ) )
57 {
58     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
59         Maasha::Fasta::put_entry( $entry, $fh_out );
60     }
61
62     Maasha::Biopieces::put_record( $record, $out );
63 }
64
65 close $fh_out;
66
67 push @commands, "-c" if $options->{ 'full' };
68 push @commands, "< $tmp_file > $tmp_file.out";
69 push @commands, "2> /dev/null" unless $options->{ 'verbose' };
70
71 $command = join " ",  @commands;
72
73 Maasha::Common::run( "prodigal", $command );
74
75 $fh_in = Maasha::Filesys::file_read_open( "$tmp_file.out" );
76
77 $/ = "//\n";
78
79 while ( $chunk = <$fh_in> )
80 {
81     chomp $chunk;
82
83     @lines = split /\n/, $chunk;
84
85     $line = shift @lines;
86
87     if ( $line =~ /^DEFINITION\s+(.+)/ )
88     {
89         $def  = $1;
90
91         if ( $def =~ /seqhdr="([^"]+)"/ ) {
92             $s_id = $1;
93         } else {
94             Maasha::Common::error( qq(BAD sequence header: $def) );
95         }
96
97         $line = shift @lines;
98
99         if ( $line =~ /^FEATURES/ )
100         {
101             foreach $line ( @lines )
102             {
103                 next if $line =~ /.+\//;
104
105                 @fields = split " ", $line;
106
107                 $type   = $fields[ 0 ];
108
109                 if ( $fields[ 1 ] =~ /complement/ ) {
110                     $strand = "-"; 
111                 } else {
112                     $strand = "+"; 
113                 }
114
115                 if ( $fields[ 1 ] =~ /(\d+)\.\.>?(\d+)/ )
116                 {
117                     $s_beg = $1;
118                     $s_end = $2;
119                 }
120                 else
121                 {
122                     Maasha::Common::error( qq(BAD locator: $line) );
123                 }
124
125                 $record = {
126                     S_ID   => $s_id,
127                     S_BEG  => $s_beg - 1,
128                     S_END  => $s_end - 1,
129                     Q_ID   => $type,
130                     STRAND => $strand,
131                 };
132
133                 Maasha::Biopieces::put_record( $record, $out );
134             }
135         }
136         else
137         {
138             Maasha::Common::error( qq(BAD feature: $line) );
139         }
140     }
141     else
142     {
143         Maasha::Common::error( qq(BAD definition: $line) );
144     }
145 }
146
147 close $fh_in;
148
149 $/ = "\n";
150
151 Maasha::Biopieces::close_stream( $in );
152 Maasha::Biopieces::close_stream( $out );
153
154
155 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
156
157
158 BEGIN
159 {
160     Maasha::Biopieces::status_set();
161 }
162
163
164 END
165 {
166     Maasha::Biopieces::status_log();
167 }
168
169
170 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
171
172
173 __END__