-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparalog_pairs.pl
executable file
·183 lines (151 loc) · 4.22 KB
/
paralog_pairs.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
#!/usr/bin/perl -w
# Copyright 2023 Matthieu Barba
# This program is free software under AGPLv3 license
# License terms are in the LICENSE file, or at <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use Getopt::Std;
use Bio::SeqIO;
our %opt; # Getopt options
my $default_id = 40;
#################################################
# Message about this program and how to use it
sub usage {
print STDERR "[ $_[0] ]\n" if $_[0];
print STDERR << "EOF";
This script creates a list of paralogs for each gene in a blast.
-h : this (help) message
Input
-i <path> : blasthits input file
-g <path> : genes data file
Output
-o <path> : paralogs output file
-s <num> : identity threshold for paralogs (default: $default_id)
-v : verbose
EOF
exit 1;
}
##################################
# Command line options processing
sub init {
getopts('hi:g:o:s:v', \%opt) or usage();
usage() if $opt{h};
usage("Blasthits file needed (-i)") unless $opt{i};
usage("Genes data file needed (-g)") unless $opt{g};
usage("Pairing file needed (-o)") unless $opt{o};
$default_id = $opt{s} if defined( $opt{s} );
}
sub get_genes_data {
my ($path, $protdata) = @_;
my %protdata = ();
my %protorder = ();
open(DATA, $path) or die("$path: $!");
# Get fields names
my $head = <DATA>;
chomp($head);
$head =~ s/^--//;
my @prothead = split(/\t/, $head);
while(my $line = <DATA>) {
next if $line =~ /^--/;
chomp($line);
my @fields = split(/\t/, $line);
my %data = ();
for (my $i = 0; $i < @prothead; $i++) {
$data{ $prothead[$i] } = $fields[$i];
}
$protdata{ $data{'pid'} } = \%data;
}
close(DATA);
return \%protdata;
}
sub para_search {
my ($inpath, $protdata) = @_;
my %hits = ();
my $nhits = 0;
my $nhits_all = 0;
my $condemned = 0;
# Read the blasthits
open(BH, $inpath) or die("$inpath: $!");
while(my $line = <BH>) {
next if $line =~ /^#/;
chomp($line);
$nhits_all++;
# query subject %id alignmentlength mismatches gapopenings querystart queryend subjectstart subjectend Evalue bitscore
my ($query, $subj, $id, $allen, $mis, $gaps, $qstart, $qend, $sstart, $send, $eval, $bits) = split(/\t/, $line);
# Filter
if (not defined($protdata->{ $query })) {
warn("No protein data for '$query'") if $condemned < 10;
warn("...\n") if $condemned == 10;
$condemned++;
next;
}
if (not defined($protdata->{ $subj })) {
warn("No protein data for '$subj'") if $condemned < 10;
warn("...\n") if $condemned == 10;
$condemned++;
next;
}
# Don't care about different genome comparisons
if ( $protdata->{ $query }->{ 'sp' } ne $protdata->{ $subj }->{ 'sp' } or
$query eq $subj ) {
next;
}
my $sp = $protdata->{ $query }->{ 'sp' };
my $qlen = $protdata->{ $query }->{'length'}; # /3: nucleic length!
$qlen = $qlen/3;
my $slen = $protdata->{ $subj }->{'length'};
$slen = $slen/3;
my $smaller = $qlen < $slen ? $qlen : $slen;
# Filter by length, identity and evalue
if ($allen < $smaller * 0.5) {
next;
}
elsif ($id < $default_id) {
next;
}
elsif ($eval > 1E-20) {
next;
# KEEP THIS HIT!
} else {
if (not $hits{ $sp }{ $query }{ $subj } or $hits{ $sp }{ $query }{ $subj } < $id) {
$hits{ $sp }{ $query }{ $subj } = $id;
$nhits++;
}
}
}
close(BH);
warn("$nhits / $nhits_all filtered hits\n") if $opt{v};
if ($condemned) {
die("Some sequences have no data in the proteins table: can't continue. ($condemned errors)");
}
return \%hits;
}
sub print_para
{
my ($outpath, $paras) = @_;
open(OUT, ">$outpath") or die("$outpath: $!");
my $n = 0;
foreach my $sp (keys %$paras) {
foreach my $query (sort { $a cmp $b } keys %{ $paras->{ $sp } }) {
my @line = ();
push @line, $query;
my @para_list = ();
foreach my $subj (sort { $a cmp $b } keys %{ $paras->{ $sp }->{ $query } }) {
my $id = int($paras->{ $sp }->{ $query }->{ $subj });
my $text = "$subj ($id\%)";
push @para_list, $text;
}
push @line, $#para_list+1;
push @line, join(", ", @para_list);
print OUT join("\t", @line) . "\n";
}
}
close(OUT);
}
##################################
# MAIN
init();
my $protdata = get_genes_data( $opt{g} );
my $para = para_search( $opt{i}, $protdata );
print_para( $opt{o}, $para );
__END__