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