-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremoveGapColumns.pl
executable file
·151 lines (134 loc) · 4.26 KB
/
removeGapColumns.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
#!/usr/bin/env perl
# removeGapColumns.pl - Version 1.0
# Removes gap columns (100% gaps) from FASTA alignments.
# Warning, this program overwrites the source file.
#
# Samuel S. Shepard (vfn4@cdc.gov)
# 2012, Centers for Disease Control & Prevention
use Getopt::Long;
GetOptions(
'display-k-tons|D=i' => \$ktons,
'remove-k-tons|R' => \$removeKtons,
'tab-formatted|T' => \$tabDelimited,
'show-column-support|S' => \$showSupport,
'a2m-insertions-only|A' => \$a2mInsertionsOnly
);
if ( scalar(@ARGV) != 1 ) {
$message = "Usage:\n\tperl $0 <file.fasta> [options]\n";
$message .= "\t\t-D|-display-k-tons\tDisplays sequences where for columns with 0 < bases <= K.\n";
$message .= "\t\t-T|-tab-formatted\tPrint out tab-delimited data.\n";
$message .= "\t\t-S|-show-column-support\tShow column support.\n";
die($message);
}
open( IN, '<', $ARGV[0] ) or die("Cannot open $ARGV[0].\n");
# PROCESS fasta data
$/ = ">";
$i = 0;
my $first = 1;
my @allowed = ();
my $del = '-';
while ( $record = <IN> ) {
chomp($record);
@lines = split( /\r\n|\n|\r/, $record );
$header[$i] = shift(@lines);
$sequence[$i] = join( '', @lines );
$length = length( $sequence[$i] );
if ( $length == 0 ) {
next;
}
if ( defined($a2mInsertionsOnly) ) {
if ($first) {
my @C = split( '', $sequence[$i] );
foreach my $j ( 0 .. $#C ) {
if ( $C[$j] =~ /[a-z.]/ ) {
push( @allowed, $j );
}
}
$first = 0;
$del = '.';
}
foreach my $j (@allowed) {
if ( substr( $sequence[$i], $j, 1 ) eq '.' ) {
$gaps{$j}++;
}
}
} else {
# unify gap characters
$sequence[$i] =~ tr/:~./-/;
$R = index( $sequence[$i], '-', 0 );
while ( $R != -1 ) {
$gaps{$R}++;
$R = index( $sequence[$i], '-', $R + 1 );
}
}
$i++;
}
close(IN);
$totalLength = length( $sequence[0] );
$numSeqs = $i;
if ( $ktons > 0 ) {
# Find and display sequences associated with k-ton support for each column.
%ktons = ();
@sorted = sort { $a <=> $b } ( keys(%gaps) );
foreach $g (@sorted) {
$support = $numSeqs - $gaps{$g};
if ( $support <= $ktons ) {
print STDERR "$g:$support\n";
$i = 0;
while ( $support > 0 && $i < $numSeqs ) {
$base = substr( $sequence[$i], $g, 1 );
if ( $del ne $base ) {
print STDERR "\t", uc($base), " ", $header[$i], "\n";
if ($tabDelimited) {
print $g, "\t", $support, "\t", uc($base), "\t", $header[$i], "\n";
}
$support--;
}
$i++;
}
} else {
delete( $gaps{$g} );
}
}
if ( defined($removeKtons) ) {
# Update file using an overwrite
open( OUT, '>', $ARGV[0] ) or die("Cannot open $ARGV[0] for writing.\n");
@sorted = sort { $b <=> $a } ( keys(%gaps) );
for ( $i = 0; $i < $numSeqs; $i++ ) {
foreach $R (@sorted) {
substr( $sequence[$i], $R, 1, '' );
}
print OUT '>', $header[$i], "\n", $sequence[$i], "\n";
}
close(OUT);
}
} else {
# Find 100% gap columns
foreach $g ( keys(%gaps) ) {
if ( $gaps{$g} != $numSeqs ) {
delete( $gaps{$g} );
}
}
# Update file using an overwrite
open( OUT, '>', $ARGV[0] ) or die("Cannot open $ARGV[0] for writing.\n");
@sorted = sort { $b <=> $a } ( keys(%gaps) );
for ( $i = 0; $i < $numSeqs; $i++ ) {
foreach $R (@sorted) {
substr( $sequence[$i], $R, 1, '' );
}
print OUT '>', $header[$i], "\n", $sequence[$i], "\n";
}
close(OUT);
}
$percentage = 0;
if ($showSupport) {
for ( $i = 0; $i < $totalLength; $i++ ) {
if ( defined( $gaps{$i} ) ) {
$percentage = 1 - $gaps{$i} / $numSeqs;
$percentage *= 100;
} else {
$percentage = 100;
}
print sprintf( "%d\t%5.1f\n", ( $i + 1 ), $percentage );
}
}