-
Notifications
You must be signed in to change notification settings - Fork 7
/
descriptive_matches.pl
executable file
·224 lines (196 loc) · 5.59 KB
/
descriptive_matches.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/perl
## Pombert Lab 2020
my $version = '0.9';
my $name = 'descriptive_matches.pl';
my $updated = '2022-05-12';
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use File::Basename;
use PerlIO::gzip;
## Usage definition
my $USAGE = <<"OPTIONS";
NAME ${name}
VERSION ${version}
UPDATED ${updated}
SYNOPSIS Adds descriptive information from PDB headers to the foldseek/gesamt matches;
Parses results by quality scores, and concatenates the output into a single file
EXAMPLE ${name} \\
-r \$TDFI_/DB/RCSB_PDB_titles.tsv \\
-m *.gesamt.gz \\
-q 0.3 \\
-b 5 \\
-o GESAMT.matches
OPTIONS:
-r (--rcsb) Tab-delimited list of RCSB structures and their titles ## see PDB_headers.pl
-m (--matches) Results from homology searches ## Supports GZIPPEd files; see run_GESAMT.pl/run_foldseek.pl
-q (--qscore) Quality score cut-off [Default: 0.3]
-b (--best) Keep the best match(es) only (top X hits)
-o (--output) Output name [Default: Gesamt.matches]
-n (--nobar) Turn off the progress bar
-x (--regex) Regex to parse filenames: word (\\w+) or nonspace (\\S+) [Default: nonspace]
OPTIONS
die "\n$USAGE\n" unless @ARGV;
## Defining options
my $rcsb;
my @matches;
my $qthreshold = 0.3;
my $best;
my $output = 'GESAMT.matches';
my $nobar;
my $regex = 'nonspace';
GetOptions(
'r|rcsb=s' => \$rcsb,
'm|matches=s@{1,}' => \@matches,
'q|qscore=s' => \$qthreshold,
'b|best=i' => \$best,
'o|output=s' => \$output,
'n|nobar' => \$nobar,
'x|regex=s' => \$regex
);
my $aligner;
my %rcsb_titles;
if ($rcsb){
## Creating a database of RSCB stuctures and their descriptions; PDB 4-letter code => description
open DB, "<", "$rcsb" or die "Can't open tab-delimited file $rcsb: $!\n";
while (my $line = <DB>){
chomp $line;
my @columns = split ("\t", $line);
my $pdb_locus = $columns[0];
my $chain_or_title = $columns[1];
my $description = $columns[2];
$rcsb_titles{$pdb_locus}{$chain_or_title} = $description;
}
}
## Iterating through foldseek/Gesamt matches
open OUT, ">", "$output" or die "Can't create output file $output: $!\n";
my $total_matches = scalar(@matches);
while (my $match = shift@matches){
my $rgx = '\S+';
$regex = lc($regex);
if ($regex eq 'word'){ $rgx = '\w+'; }
my ($basename, $path) = fileparse($match);
my $query;
my $mode;
if ($basename =~ /\.gesamt(\.gz)?$/){
($query, $mode) = $basename =~ /^($rgx).*\.(normal|high)\.gesamt(\.gz)?$/;
$aligner = 'gesamt';
}
elsif ($basename =~ /\.fseek(.gz)?$/){
($query) = $basename =~ /^($rgx).*\.fseek(\.gz)?$/;
$aligner = 'foldseek';
}
unless ($nobar) { ## Progress bar
system "clear";
my $remaining = "." x (int((scalar(@matches)/$total_matches)*100));
my $progress = "|" x (100-int((scalar(@matches)/$total_matches)*100));
my $status = "[".$progress.$remaining."]";
print "Getting match descriptions from $path\n";
print "\n\t$status\t".($total_matches-scalar(@matches))."/$total_matches\n";
}
## Working on foldseek / GESAMT file
my $gzip = '';
if ($match =~ /.gz$/){
$gzip = ':gzip';
}
open MA, "<$gzip", "$match" or die "Can't read file $match: $!\n";
## Header per protein
if ($aligner eq 'gesamt'){
print OUT '### '."$query"."; Query mode = $mode\n";
}
elsif ($aligner eq 'foldseek'){
print OUT '### '."$query"."\n";
}
if ($nobar) { print " Getting descriptive matches from $match\n"; }
my $hit_number = 0;
while (my $line = <MA>){
chomp $line;
## Skipping comments
if ($line =~ /^#/){ next; }
else { $hit_number++; }
## Working on matches
my @data = split(/\s+/, $line);
my $pdb_code;
my $chain;
my $qscore;
if ($aligner eq 'gesamt'){
# Data columns for GESAMT files are:
# Hit number, PDB code, Chain ID, Q-score, RMSD, Sequence identity,
# number of aligned residues, number of residues, file
$pdb_code = lc($data[2]);
$chain = $data[3];
$qscore = $data[4];
}
elsif ($aligner eq 'foldseek'){
## Data columns for foldseek files are:
# query, target, fident, alnlen, mismatch, gapopen, qstart, qend,
# tstart, tend, evalue, bits
if ($data[1] =~ /^pdb(\w{4}).ent.gz_(\S+)$/){
$pdb_code = $1;
$chain = $2;
}
elsif ($data[1] =~ /^pdb(\w{4}).ent.gz$/){
$pdb_code = $1;
## Unique chains can have names other than A in RCSB PBDs
## e.g. A [Auth C] => C
## Grabbing the chain name from our RCSB_PDB_titles.tsv
my @keys = keys %{$rcsb_titles{$pdb_code}};
my $key;
for my $item (@keys){
unless ($item eq 'TITLE'){
$key = $item;
last;
}
}
$chain = $key;
}
$qscore = $data[-1];
}
## Printing information
if ($qscore >= $qthreshold){
if ($best){
if ($best >= $hit_number){
if ($rcsb){
print OUT "$query\t";
if ($aligner eq 'gesamt'){
print OUT $data[1]."\t";
}
else{
print OUT "pdb".$pdb_code.".ent.gz_".$chain."\t";
}
for my $datum (@data[2..$#data]){
print OUT "$datum\t";
}
if ($rcsb_titles{$pdb_code}){
print OUT "$rcsb_titles{$pdb_code}{$chain}\n";
}
else {
print OUT "No PDB entry. Verify if obsolete PDB ID...\n";
}
}
}
}
else {
if ($rcsb){
print OUT "$query\t";
if ($aligner eq 'gesamt'){
print OUT $data[1]."\t";
}
else{
print OUT "pdb".$pdb_code.".ent.gz_".$chain."\t";
}
for my $datum (@data[2..$#data]){
print OUT "$datum\t";
}
if ($rcsb_titles{$pdb_code}){
print OUT "$rcsb_titles{$pdb_code}{$chain}\n";
}
else {
print OUT "No PDB entry. Verify if obsolete PDB ID...\n";
}
}
}
}
}
if ($gzip eq ':gzip'){ binmode MA, ":gzip(none)"; }
}