annotate SCValidator.pm @ 1:4f6952e0af48 default tip

CREST - add crest.loc.sample
author Jim Johnson <jj@umn.edu>
date Wed, 08 Feb 2012 16:08:01 -0600
parents acc8d8bfeb9a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
1 package SCValidator;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
2 # this is a singleton class
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
3
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
4 use strict;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
5 use Exporter 'import';
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
6 our @EXPORT_OK = qw($lowqual_cutoff $min_percent_id $min_percent_hq LEFT_CLIP RIGHT_CLIP);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
7 use Bio::DB::Sam;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
8 use constant LEFT_CLIP => 0;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
9 use constant RIGHT_CLIP => 1;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
10
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
11 our $min_percent_hq = 80;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
12 our $lowqual_cutoff = 20;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
13 our $min_percent_id = 90;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
14
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
15 my $SCValidator;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
16 my %default_validators = (
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
17 "quality_validator" => \&_quality_validator,
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
18 "align_validator" => \&_align_validator,
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
19 "strand_validator" => \&_strand_validator,
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
20 );
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
21
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
22 sub new {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
23 if( $SCValidator) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
24 return $SCValidator;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
25 } else {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
26 my $type = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
27 my $this = {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
28 "validator" => {}
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
29 };
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
30 foreach my $v ( keys(%default_validators)) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
31 $this->{validator}{$v} = $default_validators{$v};
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
32 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
33 $SCValidator = bless $this, $type;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
34 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
35 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
36
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
37 sub add_validator {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
38 my $self = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
39 my $name = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
40 $name = lc $name;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
41 if( exists($self->{validator}{$name})) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
42 # print STDERR "Validator: $name is already in the list";
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
43 return;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
44 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
45 if( exists($default_validators{$name})) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
46 $self->{validator}{$name} = $default_validators{$name};
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
47 return;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
48 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
49 my $validator = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
50 die "Validator must be coderef" if(ref($validator) ne "CODE");
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
51 $self->{validator}{$name} = $validator;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
52 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
53
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
54 sub remove_validator {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
55 my ($self, $name) = @_;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
56 $name = lc $name;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
57 if(exists($self->{validator}{$name})) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
58 delete $self->{validator}{$name};
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
59 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
60 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
61
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
62 sub validate {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
63 my $self = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
64 my $a = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
65 my $clip = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
66 die "The method will only validate bam alignment object"
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
67 if( ref($a) ne "Bio::DB::Bam::Aligment" &&
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
68 ref($a) ne "Bio::DB::Bam::AlignWrapper" );
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
69 return undef if($a->unmapped);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
70 foreach my $v (keys(%{$self->{"validator"}})) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
71 return undef if(!$self->{"validator"}{$v}->($a, $clip));
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
72 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
73 return 1;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
74 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
75
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
76 sub _align_validator {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
77 #we only consider high quality part of the alignment
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
78 my $a = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
79 my ($ref, $matches, $query) = $a->padded_alignment;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
80 my @qscores = $a->qscore;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
81 my $n_matches = 0;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
82 my $align_len = 0;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
83 my $j = 0;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
84 my ($start, $end) = (0, length($ref)-1);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
85 my @cigar_array = @{ $a->cigar_array };
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
86 $start = $cigar_array[0]->[1] if($cigar_array[0]->[0] eq 'S');
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
87 $end = $end - $cigar_array[$#cigar_array]->[1] if($cigar_array[$#cigar_array]->[0] eq 'S');
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
88
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
89 for( my $i = $start; $i <= $end; $i++) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
90 my $m = substr($matches, $i, 1);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
91 if($m ne '|') {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
92 if( substr($query, $i, 1) ne '-') {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
93 # $align_len++ if($qscores[$j] >= $lowqual_cutoff);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
94 $j++;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
95 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
96 # else {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
97 # $align_len++ if($qscores[$j] >= $lowqual_cutoff && $qscores[$j+1] >= $lowqual_cutoff);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
98 # }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
99 next;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
100 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
101 if($qscores[$j] >= $lowqual_cutoff) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
102 $n_matches++ if(substr($ref, $i, 1) eq substr($query, $i, 1));
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
103 $align_len++;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
104 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
105 $j++;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
106 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
107 return ($align_len - $n_matches > 1) ? undef : 1 if($align_len < 20);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
108 return ($n_matches * 100 >= $align_len * $min_percent_id) ? 1 : undef;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
109 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
110
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
111 sub _quality_validator {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
112 my $a = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
113 my $clip = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
114 # my $cigar_str = $a->cigar_str;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
115 my @cigar_array = @{$a->cigar_array};
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
116 my @qual = $a->qscore;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
117 if($clip == LEFT_CLIP) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
118 #my($sclip_len) = $cigar_str =~ m/^S(\d+)/;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
119 my $sclip_len = $cigar_array[0]->[1];
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
120 @qual = @qual[0 .. ($sclip_len - 1)];
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
121 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
122 elsif ($clip == RIGHT_CLIP) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
123 #my($sclip_len) = $cigar_str =~ m/S(\d+)$/;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
124 my $sclip_len = $cigar_array[$#cigar_array]->[1];
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
125 @qual = @qual[($a->l_qseq - $sclip_len) .. ($a->l_qseq - 1)];
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
126 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
127 else {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
128 print STDERR "Please specify the soft_clipping position:
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
129 0: LEFT_CLIP or 1: RIGHT_CLIP";
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
130 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
131
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
132 my $n_hq = 0;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
133 for( my $i = 0; $i <= $#qual; $i++) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
134 $n_hq++ if($qual[$i] >= $lowqual_cutoff);
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
135 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
136 return ($n_hq * 100 > $#qual * $min_percent_hq) ? 1 : undef;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
137 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
138
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
139 sub _strand_validator {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
140 my $a = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
141 my $clip = shift;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
142 if($clip == LEFT_CLIP ) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
143 return ($a->paired && ($a->mtid != $a->tid || $a->mate_start < $a->start)) ? undef : 1;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
144 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
145 elsif($clip == RIGHT_CLIP) {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
146 return ($a->paired && ($a->mtid != $a->tid || $a->mate_start > $a->end)) ? undef : 1;
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
147 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
148 else {
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
149 print STDERR "Please specify the soft_clipping position:
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
150 0: LEFT_CLIP or 1: RIGHT_CLIP";
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
151 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
152 }
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
153
acc8d8bfeb9a Uploaded
jjohnson
parents:
diff changeset
154 1;