-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_3D_homology_results.pl
executable file
·193 lines (173 loc) · 6.68 KB
/
parse_3D_homology_results.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
#!/usr/bin/perl
## Pombert Lab 2022
my $name = "parse_3D_homology_results.pl";
my $version = "0.2.3";
my $updated = "2022-08-31";
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use File::Basename;
use File::Path qw(make_path);
use PerlIO::gzip;
my $usage = <<"EXIT";
NAME ${name}
VERSION ${version}
UPDATED ${updated}
SYNOPSIS
USAGE ${name} \\
-g Queri3D/RESULTS/ALPHAFOLD Queri3D/RESULTS/RAPTORX
OPTIONS
-g (--gesamt) Directory(s) containing GESAMT results
-f (--foldseek) Directory(s) containing FoldSeek results
-q (--qscore) Q-score cut-off value [Default = 0.3]
-t (--tm) TM cut-off value [Default = 0.3]
-b (--best) Keep best 'X' results [Default = 25]
-a (--all) Keep all results
-o (--outdir) Output directory [Default = "./3D_homology_results_parsed"]
EXIT
die("\n$usage\n") unless(@ARGV);
my %result_dirs;
my $qscore_cut = 0.3;
my $tm_cut = 0.3;
my $best = 25;
my $all;
my $outdir = "3D_homology_results_parsed";
GetOptions(
'g|gesamt=s{1,}' => \$result_dirs{"GESAMT"},
'f|foldseek=s{1,}' => \$result_dirs{"FoldSeek"},
'q|qscore=s' => \$qscore_cut,
't|tm=s' => \$tm_cut,
'b|best=s' => \$best,
'a|all' => \$all,
'o|outdir=s' => \$outdir,
);
unless(-d $outdir){
make_path($outdir,{mode=>0755}) or die("Unable to create directory $outdir: $!\n");
}
my %results;
foreach my $predictor (keys(%result_dirs)){
if (($predictor eq "GESAMT") && ($result_dirs{$predictor})){
if (-d $result_dirs{$predictor}){
opendir(ODOR,$result_dirs{$predictor}) or die "Unable to access $result_dirs{$predictor}: $!";
foreach my $directory (readdir(ODIR)){
if (-d $result_dirs{$predictor}."/".$directory && $directory !~ /^\./){
opendir(DIR,$result_dirs{$predictor}."/".$directory);
my @source = split(/\//,$directory);
my $pred_struct_source = $source[-1];
while(my $file = readdir(DIR)){
unless(-d $result_dirs{$predictor}."/".$directory."/".$file){
my $query_struct;
my $gzip="";
if ($file =~ /(\w+).normal.gesamt.gz$/){
$query_struct = $1;
$gzip = ":gzip";
}
elsif ($file =~ /(\w+).normal.gesamt$/){
$query_struct = $1;
}
open IN, "<", $result_dirs{$predictor}."/".$directory."/".$file;
while(my $line = <IN>){
chomp($line);
unless(($line =~ /^\#/)||($line eq '')){
# print($line."\n");
my @data = split('\s+',$line);
my ($qscore,$rmsd,$seq_id,$n_align,$nRes,$predicted_file) = @data[($#data-5)..$#data];
my ($predicted_structure,$model_number) = $predicted_file =~ /^(\w+)(?:-(m\d+))*(?:-\w+)*\.pdb(?:\.gz)*$/;
if($qscore >= $qscore_cut){
push(@{$results{$predictor}{$query_struct}{$predicted_structure}},($model_number,$pred_struct_source,$qscore,$rmsd,$seq_id,$n_align,$nRes));
}
}
}
}
}
}
}
close ODIR;
}
}
elsif(($predictor eq "FoldSeek") && ($result_dirs{$predictor})){
if (-d $result_dirs{$predictor}){
opendir(ODIR,$result_dirs{$predictor}) or die "Unable to access directory $result_dirs{$predictor}: $!\n";
foreach my $directory (readdir(ODIR)){
if ((-d $result_dirs{$predictor}."/".$directory) && ($directory !~ /^\./)){
opendir(DIR,$result_dirs{$predictor}."/".$directory);
my @source = split(/\//,$directory);
my $pred_struct_source = $source[-1];
while(my $file = readdir(DIR)){
unless(-d $result_dirs{$predictor}."/".$directory."/".$file || ($file eq "error.log")){
my $query_struct;
my $gzip="";
if ($file =~ /(\w+)_w_tmscore.fseek.gz$/){
$query_struct = $1;
$gzip = ":gzip";
}
elsif ($file =~ /(\w+)_w_tmscore.fseek$/){
$query_struct = $1;
}
open IN, "<$gzip", $result_dirs{$predictor}."/".$directory."/".$file or die "Unable to open $result_dirs{$predictor}/$directory/$file: $!";
while(my $line = <IN>){
chomp($line);
unless(($line =~ /^\#/)||($line eq '')){
my @data = split('\s+',$line);
my ($predicted_structure,$model_number) = $data[1] =~ /^(\w+)(?:-(m\d+))*(?:-\w+)*\.pdb(?:\.gz)*$/;
if ($data[$#data] >= $tm_cut){
push(@{$results{$predictor}{$query_struct}{$predicted_structure}},($model_number,$pred_struct_source,@data[2..12]));
}
}
}
}
}
}
}
close ODOR;
}
}
}
foreach my $predictor (keys(%results)){
print($predictor."\n");
open ALL, ">", "$outdir/${predictor}_parsed_results.matches" or die "Unable to write to $outdir/${predictor}_parsed_results.matches: $!";
print "$outdir/${predictor}_parsed_results.matches\n";
if ($predictor eq "GESAMT"){
print ALL ("### Locus\tModel #\tSource\tQ-Score\tr.m.s.d\tSeq. Id.\tNalign\tnRes\n\n");
foreach my $query_struct (sort(keys(%{$results{$predictor}}))){
print ALL ("## $query_struct\n");
my $query_count = 0;
foreach my $predicted_structure (sort{$results{$predictor}{$query_struct}{$b}[1] <=> $results{$predictor}{$query_struct}{$a}[1]}(keys(%{$results{$predictor}{$query_struct}}))){
my ($model_number,$pred_struct_source,$qscore,$rmsd,$seq_id,$n_align,$nRes) = @{$results{$predictor}{$query_struct}{$predicted_structure}};
unless($model_number){
$model_number = '-';
}
if($all){
print ALL ("$predicted_structure\t$model_number\t$pred_struct_source\t$qscore\t$rmsd\t$seq_id\t$n_align\t$nRes\n");
}
elsif($query_count < $best){
print ALL ("$predicted_structure\t$model_number\t$pred_struct_source\t$qscore\t$rmsd\t$seq_id\t$n_align\t$nRes\n");
}
$query_count++;
}
print ALL ("\n");
}
}
elsif($predictor eq "FoldSeek"){
print ALL ("### Locus\tModel #\tSource\tfident\talnlen\tmismatch.\tgapopen\tqstart\tqend\ttstart\ttend\teval\tbits\ttmscore\n\n");
foreach my $query_struct (sort(keys(%{$results{$predictor}}))){
print ALL ("## $query_struct\n");
my $query_count = 0;
foreach my $predicted_structure (sort{$results{$predictor}{$query_struct}{$b}[11] <=> $results{$predictor}{$query_struct}{$a}[11]}(keys(%{$results{$predictor}{$query_struct}}))){
my ($model_number,$pred_struct_source,$fident,$alnlen,$mismatch,$gapopen,$qstart,$qend,$tstart,$tend,$eval,$bits,$tmscore) = @{$results{$predictor}{$query_struct}{$predicted_structure}};
unless($model_number){
$model_number = '-';
}
if($all){
print ALL ("$predicted_structure\t$model_number\t$pred_struct_source\t$fident\t$alnlen\t$mismatch\t$gapopen\t$qstart\t$qend\t$tstart\t$tend\t$eval\t$bits\t$tmscore\n");
}
elsif($query_count < $best){
print ALL ("$predicted_structure\t$model_number\t$pred_struct_source\t$fident\t$alnlen\t$mismatch\t$gapopen\t$qstart\t$qend\t$tstart\t$tend\t$eval\t$bits\t$tmscore\n");
}
$query_count++;
}
print ALL ("\n");
}
}
close ALL;
}