-
Notifications
You must be signed in to change notification settings - Fork 15
/
psl_culler.pl
executable file
·203 lines (176 loc) · 5.76 KB
/
psl_culler.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/perl
use warnings;
use strict;
use English;
use Pod::Usage; ## uses pod documentation in usage code
use Getopt::Long qw(:config auto_version auto_help pass_through);
our $VERSION = "1.00";
=head1 NAME
psl_culler.pl - remove contigs that are substantially covered by other contigs
=head1 SYNOPSIS
./psl_culler.pl -query <file> [options] <self_mapping.psl>
=cut
sub min {
($a, $b) = @_;
return( ($a < $b) ? $a : $b);
}
sub max {
($a, $b) = @_;
return( ($a > $b) ? $a : $b);
}
sub rc {
my ($seq) = @_;
$seq =~ tr/ACGTUYRSWMKDVHBXN-/TGCAARYSWKMHBDVXN-/;
# work on masked sequences as well
$seq =~ tr/acgtuyrswmkdvhbxn/tgcaaryswkmhbdvxn/;
return(scalar(reverse($seq)));
}
sub getConsensus {
my ($b1, $b2) = @_;
if(($b1 eq $b2) || ($b1 eq " ") || ($b2 eq " ")){
## equal bases, or absent bases, so consensus is easy
return($b1);
}
# if different, convert to upper case to simplify lookup
my $bc = uc(($b1 cmp $b2) ? $b1.$b2 : $b2.$b1);
my %consensusLookup =
(AC => "M", AM => "A", CM => "C",
GT => "K", GK => "G", KT => "T",
AG => "R", AR => "A", GR => "G",
CT => "Y", CY => "C", TY => "T",
AT => "W", AW => "A", TW => "T",
);
# if "simple" ambiguity can be found, return that, otherwise return N
# (i.e. GT => K, -A => N, YM -> N)
return( ($consensusLookup{$bc}) ? $consensusLookup{$bc} : "N");
}
sub getMatch {
my ($b1, $b2) = @_;
return((($b1 eq $b2) || ($b1 eq " ") || ($b2 eq " ") ||
($b1 eq "N") || ($b2 eq "N")) ? " " : "*");
}
############### Program starts here
# set default options
my @pslFiles = ();
my $projOpts =
{
"query" => 0, # contig file for query sequences
"prefix" => "psl_scaffold_", # prefix for contig names
"pid" => 90, # percent ID threshold
"capfrac" => 0.9, # fraction captured to exclude sequence
};
GetOptions($projOpts, 'query=s', 'pid=i', 'capfrac=f', 'prefix=s');
# process remaining command line arguments (hopefully only PSL files)
while (@ARGV) {
my $argument = shift @ARGV;
if(-f $argument){
push (@pslFiles, $argument);
} else {
pod2usage({-exitVal => 1,
-message => "Error: Unknown command-line option or ".
"non-existent file, '$argument'\n", -verbose => 0});
}
}
@ARGV = @pslFiles;
if(!$projOpts->{"query"}){
pod2usage({-exitVal => 1,
-message => "Error: No query assembly file provided",
-verbose => 0});
}
if(!(-f $projOpts->{"query"})){
pod2usage({-exitVal => 1,
-message => sprintf("Error: query file '%s' doesn't exist",
$projOpts->{"query"}),
-verbose => 0});
}
print(STDERR "Loading query sequences into memory...");
open(my $queryFile, "<", $projOpts->{"query"});
my $seqID = "";
my %querySeqs = ();
while(<$queryFile>){
chomp;
if(/^>((.+?)( .*?\s*)?)$/){
## line is sequence header
$seqID = $2;
$querySeqs{$seqID}{fullName} = $1;
$querySeqs{$seqID}{sequence} = "";
$querySeqs{$seqID}{length} = 0;
$querySeqs{$seqID}{captured} = 0;
} else {
if(!$seqID){
pod2usage({-exitVal => 1,
-message => sprintf(" Error: query file '%s' doesn't look ".
"like a FASTA file (no initial ID header)",
$projOpts->{"query"}),
-verbose => 0});
}
## line is sequence
$querySeqs{$seqID}{"sequence"} .= $_;
$querySeqs{$seqID}{"length"} += length($_);
}
}
close($queryFile);
my %targetSeqs = %querySeqs;
my $nextScaffoldID = 1;
my %replacementSeqs = ();
printf(STDERR " loaded in %d sequences (last ID %s)\n",
scalar(keys(%querySeqs)), $seqID);
print(STDERR "Processing results...");
while(<>){
chomp;
if(!$_){
next;
}
my @fields = split(/\t/);
my ($matches, $misMatches, $repMatches, $nCount, $qNumInsert,
$qBaseInsert, $tNumInsert, $tBaseInsert, $strand, $qName,
$qSize, $qStart, $qEnd, $tName, $tSize,
$tStart, $tEnd, $blockCount, $blockSizes, $qStarts,
$tStarts, @rest) = @fields;
if(!$tStarts){
pod2usage({-exitVal => 1,
-message => sprintf(" Error: mapping file doesn't look ".
"like a PSL file (expecting".
">=21 tab-separated values, got %d)",
scalar(@fields)),
-verbose => 0});
}
## calculate percent identity
my $qAliSize = $qEnd - $qStart;
my $tAliSize = $tEnd - $tStart;
my $sizeDif = abs($qAliSize - $tAliSize);
my $pid = 100 * ($matches + $repMatches -
($qNumInsert + $tNumInsert + 3*log(1+$sizeDif))) /
($matches + $repMatches + $misMatches);
if(($pid >= $projOpts->{"pid"}) &&
$querySeqs{$qName} && $targetSeqs{$tName}){
if($querySeqs{$qName}{length} > $targetSeqs{$tName}{length}){
## only remove larger sequences; this prevents a larger sequence
## from being removed, and also removing its sub-sequences
$querySeqs{$qName}{captured} += $qEnd - $qStart;
}
} elsif($pid < $projOpts->{"pid"}){
# printf(STDERR "Rejecting match '%s' vs '%s': identity (%f) too low\n",
# $qName, $tName, $pid);
}
}
printf(STDERR " done\n");
my %displayed = ();
foreach my $seqID (sort(keys(%querySeqs))){
if(!$seqID){
next;
}
if($querySeqs{$seqID}{length} == 0){
printf(STDERR "Length of %s is 0\n", $seqID);
}
my $fullName = $querySeqs{$seqID}{fullName};
my $sequence = $querySeqs{$seqID}{sequence};
my $capProp = $querySeqs{$seqID}{captured} ?
($querySeqs{$seqID}{captured} / $querySeqs{$seqID}{length}) : 0;
if($capProp && ($capProp > $projOpts->{capfrac})){
printf(STDERR "culling $seqID; %0.1f%% captured\n", $capProp * 100);
} else {
printf(">%s\n%s\n", $fullName, $sequence);
$displayed{$fullName} = 1;
}
}