-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbad_start_stop.pl
executable file
·182 lines (173 loc) · 6.29 KB
/
bad_start_stop.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl
use Getopt::Std;
use lib $ENV{SCRIPTS};
use ENV;
use strict;
my %arg;
&getopts('D:u:p:f:t', \%arg);
if (! $arg{'D'}) { die "bad_start_stop.pl -D db [-u user] -p pswd -f file [-t (nodbwrite)]\n";}
my $dbh = &connect(\%arg);
my $FILE = Bio::SeqIO->new(-file => $arg{'f'});
my $seqq = "select seq_id, seq_length from sequences where iscurrent=1";
my $SEQ = $dbh->selectall_hashref($seqq, 'seq_id');
my $TEST = $arg{'t'};
while (my $seqr = $FILE->next_seq) {
my $id = $seqr->primary_id;
my $seq = $seqr->seq;
my ($bad_start, $bad_stop, $gap);
my ($fid, $feat_obj, $locr);
if ($seq !~ /^[atg]tg/i) {
$bad_start = 1;
}
if ($seq !~ /(taa|tag|tga)$/i) {
$bad_stop = 1;
}
if ($seq =~ /n/i &&
length($seq) % 3 != 0) {
$gap = 1;
}
if ($bad_start || $bad_stop || $gap) {
if ($id =~ /\|(\d+)\|/) {
$fid = $1;
$feat_obj = get_features_by_feature_id($dbh, $fid);
if ($feat_obj->{$fid}->{feat_type} ne "CDS") { next }
$locr = $feat_obj->{$fid}->{'location'};
} else { die "Couldn't parse feature id from " . $id . "\n";}
} else { next }
if ($gap) {
print "$id appears to be frame-shifted due to scaffold or sequence gap\n";
foreach my $set_id (keys %$locr) {
my $strand = $locr->{$set_id}->{'strand'} == 1 ? -1 : 1;
my $seq_id = $locr->{$set_id}->{'seq_id'};
my $q = "UPDATE seq_feat_mappings SET pseudo=1 where feature_id=$fid and seq_id=$seq_id";
if ($TEST) {
print $q, "\n";
} else {
my $r = $dbh->do($q);
if (! defined $r) { warn $dbh->errstr }
next;
}
}
}
if ($seq =~ /^(cta|tta|tca).*(ca[cat])$/i) {
print $id, " appears to be on the wrong strand: ", substr($seq, 0, 3), "..", substr($seq, -3, 3), "\n\n";
foreach my $set_id (keys %$locr) {
my $strand = $locr->{$set_id}->{'strand'} == 1 ? -1 : 1;
my $seq_id = $locr->{$set_id}->{'seq_id'};
my $q = "UPDATE seq_feat_mappings SET strand='$strand' where feature_id=$fid and seq_id=$seq_id";
if ($TEST) {
print $q, "\n";
} else {
my $r = $dbh->do($q);
if (! defined $r) { warn $dbh->errstr }
next;
}
}
} elsif ($seq =~ /^(cta|tta|tca)/i) {
foreach my $set_id (keys %$locr) {
my $seq_id = $locr->{$set_id}->{'seq_id'};
if ($locr->{$set_id}->{'feat_max'} >= $SEQ->{$seq_id}->{'seq_length'} - 2 &&
$locr->{$set_id}->{'strand'} > 0) {
print "$id may be on wrong strand: ", substr($seq, 0, 3), "..", substr($seq, -3, 3), "\n\n";
my $strand = $locr->{$set_id}->{'strand'} == 1 ? -1 : 1;
my $q = "UPDATE seq_feat_mappings SET strand='$strand', max_partial='1'"
. " where feature_id=$fid and seq_id=$seq_id";
$bad_stop = 0;
if ($TEST) {
print $q, "\n";
} else {
my $r = $dbh->do($q);
if (! defined $r) { warn $dbh->errstr }
next;
}
}
}
} elsif ($seq =~ /(ca[cat])$/i) {
foreach my $set_id (keys %$locr) {
my $seq_id = $locr->{$set_id}->{'seq_id'};
if ($locr->{$set_id}->{'feat_min'} <= 3 &&
$locr->{$set_id}->{'strand'} > 0) {
print "$id may be on wrong strand: ", substr($seq, 0, 3), "..", substr($seq, -3, 3), "\n\n";
my $strand = $locr->{$set_id}->{'strand'} == 1 ? -1 : 1;
my $q = "UPDATE seq_feat_mappings SET strand='$strand', min_partial='1'"
. " where feature_id=$fid and seq_id=$seq_id";
$bad_start = 0;
if ($TEST) {
print $q, "\n";
} else {
my $r = $dbh->do($q);
if (! defined $r) { warn $dbh->errstr }
next;
}
}
}
}
if ($bad_start) {
print $id, " has bad start: ", substr($seq, 0, 3), "\n";
foreach my $set_id (keys %$locr) {
my $seq_id = $locr->{$set_id}->{'seq_id'};
print "$id\tseq:$seq_id ($SEQ->{$seq_id}->{seq_length} nt)\t" . $locr->{$set_id}->{feat_min}
. " / " . $locr->{$set_id}->{feat_max}
. "\tstrand:" . $locr->{$set_id}->{strand}
. "\n";
if ($locr->{$set_id}->{'feat_min'} <= 3 &&
$locr->{$set_id}->{'strand'} > 0) {
my $phase = $locr->{$set_id}->{'feat_max'} % 3;
print "START:update min_partial for $fid to T, phase to $phase\n\n";
my $q1 = "update seq_feat_mappings"
. " set feat_min = 1, min_partial=1, phase='$phase'"
. " where seq_id=$seq_id and feature_id=$fid";
if ($TEST) { print "$q1\n";}
else { my $r = $dbh->do($q1);
if (! defined $r) { warn $dbh->errstr }}
} elsif ($locr->{$set_id}->{'feat_max'} >= $SEQ->{$seq_id}->{'seq_length'} - 2 &&
$locr->{$set_id}->{'strand'} < 0) {
my $phase = (abs($locr->{$set_id}->{'feat_min'} - $locr->{$set_id}->{'feat_max'}) + 1) % 3;
print "START:update max_partial for $fid to T, phase to $phase\n\n";
my $q2 = "update seq_feat_mappings"
. " set feat_max = " . $SEQ->{$seq_id}->{'seq_length'} . ", max_partial=1, phase = '$phase'"
. " where seq_id=$seq_id and feature_id=$fid";
if ($TEST) { print "$q2\n" }
else { my $r = $dbh->do($q2);
if (! defined $r) { warn $dbh->errstr } }
}
}
}
if ($bad_stop) {
if (! $feat_obj) {
if ($id =~ /\|(\d+)\|/) {
$fid = $1;
$feat_obj = get_features_by_feature_id($dbh, $fid);
} else { die "Couldn't parse feature id from " . $id . "\n";}
}
if ($feat_obj->{$fid}->{feat_type} ne "CDS") { next }
my $locr = $feat_obj->{$fid}->{'location'};
print $id, " has bad stop: ", substr($seq, -3, 3), "\n";
foreach my $set_id (keys %$locr) {
my $seq_id = $locr->{$set_id}->{'seq_id'};
print "$id\t$seq_id($SEQ->{$seq_id}->{seq_length})\t" . $locr->{$set_id}->{feat_min}
. "\t" . $locr->{$set_id}->{feat_max}
. "\t" . $locr->{$set_id}->{strand}
. "\n";
if ($locr->{$set_id}->{'feat_min'} <= 3 &&
$locr->{$set_id}->{'strand'} < 0) {
print "STOP:update min_partial for $fid to T\n\n";
my $q1 = "update seq_feat_mappings"
. " set feat_min=1, min_partial=1"
. " where seq_id=$seq_id and feature_id=$fid";
my $r = $dbh->do($q1) unless $TEST;
if (!$TEST && ! defined $r) { warn $dbh->errstr }
} elsif ($locr->{$set_id}->{'feat_max'} >= $SEQ->{$seq_id}->{'seq_length'} - 2 &&
$locr->{$set_id}->{'strand'} > 0) {
print "STOP:update max_partial for $fid to T\n\n";
my $q2 = "update seq_feat_mappings"
. " set feat_max = " . $SEQ->{$seq_id}->{'seq_length'} . ", max_partial=1"
. " where seq_id=$seq_id and feature_id=$fid";
if ($TEST) { print "$q2\n" } else {
my $r = $dbh->do($q2);
if (! defined $r) { warn $dbh->errstr } }
}
}
print "\n";
}
}