-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodonCorrect.pl
executable file
·159 lines (135 loc) · 5.74 KB
/
codonCorrect.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
#!/usr/bin/env perl
# Codon corrects sequences using calculated statistics.
#
# Samuel S. Shepard ([email protected])
# 2019, Centers for Disease Control & Preventio
use File::Basename;
use Getopt::Long;
GetOptions( 'insertion-table|I=s' => \$insertionTable,
'output-table|O=s' => \$outputTable );
if ( -t STDIN && scalar(@ARGV) != 1 ) {
$message = "Usage:\n\tperl $0 <nts.fasta> [options]\n";
$message .= "\t\t--insertion-table|-I <STR>\tInsertion table for insertion corrections.\n";
$message .= "\t\t--output-table|-O <STR>\t\tOutput file for the insertion table.\n";
die( $message . "\n" );
}
if ($insertionTable) {
$/ = "\n";
%inserts = ();
open( INS, '<', $insertionTable ) or die("Cannot open $insertionTable for reading.\n");
@lines = <INS>;
chomp(@lines);
foreach $line (@lines) {
( $id, $pos, $insert ) = split( "\t", $line );
$inserts{$id}{$pos} = lc($insert);
}
close(INS);
}
if ($outputTable) {
open( TABL, '>', $outputTable ) or die("Cannot open $outputTable for writing.\n");
} else {
*TABL = *STDERR;
}
$PROG = basename( $0, '.pl' );
$firstFile = $ARGV[0];
# Process records.
%codonStats = %sequences = ();
@patterns = ();
$/ = ">";
while ( $record = <> ) {
chomp($record);
@lines = split( /\r\n|\n|\r/, $record );
$id = shift(@lines);
$sequence = lc( join( '', @lines ) );
$length = length($sequence);
if ( $length == 0 ) {
next;
} elsif ( $length % 3 != 0 ) {
die("$PROG:\tNot a codon alignment! See:\n\t$firstFile\n");
} else {
$sequences{$id} = $sequence;
$counts{$sequence}++;
}
}
@patterns = keys(%counts);
foreach $id ( keys(%sequences) ) {
$sequence = $sequences{$id};
$seqLimit = length($sequence) - 2; # TO-DO: what about the second to last insertion opportunity?
if ( defined( $inserts{$id} ) ) {
# position is 1 based
foreach $pos ( keys( %{ $inserts{$id} } ) ) {
if ( $pos > $seqLimit ) {
# should be in frame if at length
if ( $pos == length($sequence) ) { print TABL $id, "\t", $pos, "\t", uc($insert), "\n"; }
next;
}
$insert = $inserts{$id}{$pos};
$iDivis = length($insert) % 3;
$iFrame = $pos % 3;
if ( ( ( $iDivis == 0 ) && ( $iFrame != 0 ) ) ) {
# zero based codon number
$codonNumber = int( ( $pos - 1 ) / 3 );
$codonStart = $codonNumber * 3;
if ( !defined( $codonStats{$codonNumber} ) ) {
foreach $pattern (@patterns) {
$codon = substr( $pattern, $codonStart, 3 );
$codonStats{$codonNumber}{$codon} += $counts{$pattern};
}
}
# A2 insertion
if ( $iFrame == 2 ) {
$A2L2 = substr( $insert, -2 ) . substr( $sequence, $codonStart + 2, 1 );
$A2R1 = substr( $sequence, $codonStart, 2 ) . substr( $insert, 0, 1 );
if ( $codonStats{$codonNumber}{$A2R1} >= $codonStats{$codonNumber}{$A2L2} ) {
#A2::R1 shift
$newInsert = substr( $insert, 1 ) . substr( $sequence, $pos, 1 );
$newCodon = $A2R1;
$newPos = $codonStart + 3; # +2 +1 => +3 given 1-based
} else {
#A2::L2 shift
$newInsert = substr( $sequence, $codonStart, 2 ) . substr( $insert, 0, -2 );
$newCodon = $A2L2;
$newPos = $codonStart; # -1 + 1 => + 0 given 1-based
}
# A1 insertion
} else {
$A1L1 = substr( $insert, -1 ) . substr( $sequence, $codonStart + 1, 2 );
$A1R2 = substr( $sequence, $codonStart, 1 ) . substr( $insert, 0, 2 );
if ( $codonStats{$codonNumber}{$A1L1} >= $codonStats{$codonNumber}{$A1R2} ) {
#A1::L1 shift
$newInsert = substr( $sequence, $codonStart, 1 ) . substr( $insert, 0, -1 );
$newCodon = $A1L1;
$newPos = $codonStart; # -1 + 1 => + 0 given 1-based
} else {
#A1::R2 shift
$newInsert = substr( $insert, 2 ) . substr( $sequence, $pos, 2 );
$newCodon = $A1R2;
$newPos = $codonStart + 3; # +2 +1 => +3 given 1-based
}
}
print TABL $id, "\t", $newPos, "\t", uc($newInsert), "\n";
substr( $sequence, $codonStart, 3 ) = $newCodon;
} else {
print TABL $id, "\t", $pos, "\t", uc($insert), "\n";
}
}
}
# perform after insertion corrections
while ( $sequence =~ /([A-Za-z]{3})((---)+)([A-Za-z]{3})/g ) {
( $left, $gaps, $gapT, $right ) = ( $1, $2, $3, $4 );
$frame = $-[1] % 3;
if ( $frame == 1 ) {
# RIGHT SHIFT
$replacement = substr( $left, 0, -1 ) . $gaps . substr( $left, -1 );
substr( $sequence, $-[1], length($replacement) ) = $replacement;
} elsif ( $frame == 2 ) {
# LEFT SHIFT
$replacement = $left . substr( $right, 0, 1 ) . $gaps;
substr( $sequence, $-[1], length($replacement) ) = $replacement;
}
}
print '>', $id, "\n", $sequence, "\n";
}
if ($outputTable) {
close(TABL);
}