]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/analyze_snp
fixed seq qual length check
[biopieces.git] / bp_bin / analyze_snp
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
25 # Analyze mismatches and indels from ALIGN descriptors in the stream.
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31 use warnings;
32 use strict;
33 use Maasha::Biopieces;
34 use Data::Dumper;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $record, @aligns, $align, $offset, $src, $dst, $data, $s_id, $pos, $count, $new_record );
41
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'min', short => 'm', type => 'uint', mandatory => 'no', default => 1, allowed => undef, disallowed => '0' },
46     ]
47 );
48
49 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
51
52 $data = {};
53
54 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
55 {
56     if ( exists $record->{ 'ALIGN' } )
57     {
58         @aligns = split /,/, $record->{ 'ALIGN' };
59
60         foreach $align ( @aligns )
61         {
62             if ( $align =~ /(\d+):([ATCGN-])>([ATCGN-])/ )
63             {
64                 $offset = $1;
65                 $src    = $2;
66                 $dst    = $3;
67
68                 $data->{ $record->{ 'S_ID' } }->{ $record->{ 'S_BEG' } + $offset }->{ $src }->{ $dst }++;
69             }
70         }
71     }
72
73     Maasha::Biopieces::put_record( $record, $out );
74 }
75
76 foreach $s_id ( sort keys %{ $data } )
77 {
78     foreach $pos ( sort { $a <=> $b } keys %{ $data->{ $s_id } } )
79     {
80         foreach $src ( sort keys %{ $data->{ $s_id }->{ $pos } } )
81         {
82             foreach $dst ( sort keys %{ $data->{ $s_id }->{ $pos }->{ $src } } )
83             {
84                 $count = $data->{ $s_id }->{ $pos }->{ $src }->{ $dst };
85
86                 if ( $count >= $options->{ 'min' } )
87                 {
88                     $new_record->{ 'REC_TYPE' }  = "SNP";
89                     $new_record->{ 'S_ID' }      = $s_id;
90                     $new_record->{ 'S_POS' }     = $pos;
91                     $new_record->{ 'SRC' }       = $src;
92                     $new_record->{ 'DST' }       = $dst;
93                     $new_record->{ 'SNP_COUNT' } = $count;
94
95                     if ( $src eq '-' ) {
96                         $new_record->{ 'TYPE' } = "INSERTION";
97                     } elsif ( $dst eq '-' ) {
98                         $new_record->{ 'TYPE' } = "DELETION";
99                     } else {
100                         $new_record->{ 'TYPE' } = "MISMATCH";
101                     }
102
103                     Maasha::Biopieces::put_record( $new_record, $out );
104                 }
105             }
106         }
107     }
108 }
109
110 Maasha::Biopieces::close_stream( $in );
111 Maasha::Biopieces::close_stream( $out );
112
113
114 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
115
116
117 BEGIN
118 {
119     Maasha::Biopieces::status_set();
120 }
121
122
123 END
124 {
125     Maasha::Biopieces::status_log();
126 }
127
128
129 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
130
131
132 __END__