-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gene.overlap.edited.pl
220 lines (167 loc) · 7.22 KB
/
Gene.overlap.edited.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
#!usr/bin/perl
=head1 program
Gene.overlap.pl --find overlap between ref_file and query_files(single || multi) on mRNA&CDS level
=head1 Usage
perl Gene.overlap.pl ref_file [query_file_1,query_file_2,......]
ref_file and each query_file have to be normal gff3(no gff2) type
=head1 option
--num display the overlap by number,default by decimal
--locus regardless of +/- strand,default find overlap on +/- strand separately
--help display the help information
=head1 Explain
if add --num parameter,the meanings of every field as follow:
ref_Gene_id strand scaffold overlap_num query_Gene_id(ref_mRNA_len,query_mRNA_len,mRNA_overlap_len;ref_exon_number,query_exon_number;ref_CDS_len,query_CDS_len,CDS_overlap_len)
if retain default parameter,the meanings of every field as follow:
ref_Gene_id strand scaffold overlap_num query_Gene_id(mRNA_overlap_len/ref_mRNA_len,mRNA_overlap_len/query_mRNA_len;ref_exon_number,query_exon_number;CDS_overlap_len/ref_CDS_len,CDS_overlap_len/query_CDS_len)
CDS in the program means linkage of all exons
=head1 author&version
qiufeng,[email protected]
Version:1.0,Date:2010-11-06
Version:1.1,Date:2010-11-08 add Explain
Version:1.2,Date:2011-03-01 improve some codes
=head1 Example
perl Gene.overlap.pl ~/Dmel.gff ~/cflo.gff > Dmel.cflo.overlap
perl Gene.overlap.pl --num --locus ~/Dmel.gff ~/cflo.gff ~/hsal.gff > Dmel.cflo.hsal.overlap
=cut
use warnings;
use strict;
use Getopt::Long;
my ($locus,$num,$help);
GetOptions(
"locus"=>\$locus,
"num"=>\$num,
"help"=>\$help
);
die `pod2text $0` if (@ARGV==0 || $help);
my $ref_file=shift;my @query_file=@ARGV;
############################################################
###########################Gene#############################
############################################################
my %ref;
open (IN,$ref_file) || die "$!\n";
while(<IN>){
chomp;
if(!($_=~/^#/)){
my @c=split /\t/,$_;
my $id;
$id=$c[0].$c[6] if(!($locus));
$id=$c[0] if($locus);
my $G_id=$1 if($c[8]=~/^\S+?=(\S+?);/);
($c[3],$c[4])=($c[4],$c[3]) if($c[3]>$c[4]);
push @{$ref{$id}{$G_id}},[$c[3],$c[4]];}
}
close IN;
my %query;my $Q_num=0;
foreach my $query_sub_file(@query_file){
open (IN,$query_sub_file) || die "$!\n";
while(<IN>){
chomp;
if(!($_=~/^#/)){
my @c=split /\t/,$_;
my $id;
$id=$c[0].$c[6] if(!($locus));
$id=$c[0] if($locus);
my $G_id=$1 if($c[8]=~/^\S+?=(\S+?);/);
($c[3],$c[4])=($c[4],$c[3]) if($c[3]>$c[4]);
push @{$query{$Q_num}{$id}{$G_id}},[$c[3],$c[4]];}
}close IN;
$Q_num++;}
############################################################
########################find_overlap########################
############################################################
my %RQ;my $overlap=0;my @all_overlap;
foreach my $s(keys %ref){
foreach my $R_G(keys %{$ref{$s}}){
my $R_Gene=\@{$ref{$s}{$R_G}};
push @{$RQ{$R_G}},$R_G;push @{$RQ{$R_G}},$s;
foreach my $num_ID(keys %query){
if(defined $query{$num_ID}{$s}){
foreach my $Q_G(keys %{$query{$num_ID}{$s}}){
my $Q_Gene=\@{$query{$num_ID}{$s}{$Q_G}};
if($R_Gene->[0][0]<=$Q_Gene->[0][1] and $R_Gene->[0][1]>=$Q_Gene->[0][0])
{
#mRNA overlap
if($R_Gene->[0][0]<=$Q_Gene->[0][0] and $R_Gene->[0][1]>=$Q_Gene->[0][1])
{$overlap=$Q_Gene->[0][1]-$Q_Gene->[0][0]+1;}
if($R_Gene->[0][0]>=$Q_Gene->[0][0] and $R_Gene->[0][1]>=$Q_Gene->[0][1])
{$overlap=$Q_Gene->[0][1]-$R_Gene->[0][0]+1;}
if($R_Gene->[0][0]<=$Q_Gene->[0][0] and $R_Gene->[0][1]<=$Q_Gene->[0][1])
{$overlap=$R_Gene->[0][1]-$Q_Gene->[0][0]+1;}
if($R_Gene->[0][0]>=$Q_Gene->[0][0] and $R_Gene->[0][1]<=$Q_Gene->[0][1])
{$overlap=$R_Gene->[0][1]-$R_Gene->[0][0]+1;}
my $overlap_type; #edited for classifying overlap types
if($R_Gene->[0][0]==$Q_Gene->[0][0] and $R_Gene->[0][1]==$Q_Gene->[0][1])
{$overlap_type="===="}
if($R_Gene->[0][0]==$Q_Gene->[0][0] and $R_Gene->[0][1]>$Q_Gene->[0][1])
{$overlap_type="==QR"}
if($R_Gene->[0][0]<$Q_Gene->[0][0] and $R_Gene->[0][1]==$Q_Gene->[0][1])
{$overlap_type="RQ=="}
if($R_Gene->[0][0]<$Q_Gene->[0][0] and $R_Gene->[0][1]>$Q_Gene->[0][1])
{$overlap_type="RQQR"}
if($R_Gene->[0][0]>$Q_Gene->[0][0] and $R_Gene->[0][1]==$Q_Gene->[0][1])
{$overlap_type="QR=="}
if($R_Gene->[0][0]>$Q_Gene->[0][0] and $R_Gene->[0][1]>$Q_Gene->[0][1])
{$overlap_type="QRQR"}
if($R_Gene->[0][0]==$Q_Gene->[0][0] and $R_Gene->[0][1]<$Q_Gene->[0][1])
{$overlap_type="==RQ"}
if($R_Gene->[0][0]<$Q_Gene->[0][0] and $R_Gene->[0][1]<$Q_Gene->[0][1])
{$overlap_type="RQRQ"}
if($R_Gene->[0][0]>$Q_Gene->[0][0] and $R_Gene->[0][1]<$Q_Gene->[0][1])
{$overlap_type="QRRQ"}
my $R_mRNA_len=$R_Gene->[0][1]-$R_Gene->[0][0]+1;my $Q_mRNA_len=$Q_Gene->[0][1]-$Q_Gene->[0][0]+1;
push @all_overlap,$Q_G;
push @all_overlap,$R_mRNA_len;push @all_overlap,$Q_mRNA_len;push @all_overlap,$overlap; #push @all_overlap,$overlap_type;
#CDS overlap
my $R_cds_len=0;my $Q_cds_len=0;
for(my $i=1;$i<@$R_Gene;$i++){$R_cds_len=$R_cds_len+$R_Gene->[$i][1]-$R_Gene->[$i][0]+1;}
for(my $k=1;$k<@$Q_Gene;$k++){$Q_cds_len=$Q_cds_len+$Q_Gene->[$k][1]-$Q_Gene->[$k][0]+1;}
my $cds_overlap=0;my $all_cds_overlap=0;
for(my $i=1;$i<@$R_Gene;$i++){
for(my $k=1;$k<@$Q_Gene;$k++){
if($R_Gene->[$i][0]<=$Q_Gene->[$k][1] and $R_Gene->[$i][1]>=$Q_Gene->[$k][0])
{
if($R_Gene->[$i][0]<=$Q_Gene->[$k][0] and $R_Gene->[$i][1]>=$Q_Gene->[$k][1])
{$cds_overlap=$Q_Gene->[$k][1]-$Q_Gene->[$k][0]+1;}
if($R_Gene->[$i][0]>=$Q_Gene->[$k][0] and $R_Gene->[$i][1]>=$Q_Gene->[$k][1])
{$cds_overlap=$Q_Gene->[$k][1]-$R_Gene->[$i][0]+1;}
if($R_Gene->[$i][0]<=$Q_Gene->[$k][0] and $R_Gene->[$i][1]<=$Q_Gene->[$k][1])
{$cds_overlap=$R_Gene->[$i][1]-$Q_Gene->[$k][0]+1;}
if($R_Gene->[$i][0]>=$Q_Gene->[$k][0] and $R_Gene->[$i][1]<=$Q_Gene->[$k][1])
{$cds_overlap=$R_Gene->[$i][1]-$R_Gene->[$i][0]+1;}
$all_cds_overlap=$all_cds_overlap+$cds_overlap;
}}}
push @all_overlap,@$R_Gene-1;push @all_overlap,@$Q_Gene-1;
push @all_overlap,$R_cds_len;push @all_overlap,$Q_cds_len;push @all_overlap,$all_cds_overlap;
#combine
push @all_overlap,$overlap_type; # edited for classifying overlap types.
push @{$RQ{$R_G}},[@all_overlap];undef @all_overlap;
}}}}}}
undef %ref;undef %query;
############################################################
########################display_overlap#####################
############################################################
foreach my $x(keys %RQ){
if(defined $locus){
printf "%s\t%s\t%d",$RQ{$x}[0],$RQ{$x}[1],@{$RQ{$x}}-2;}
if(!(defined $locus)){
my $str;my $scaf;
if($RQ{$x}[1]=~/(\S+)\+$/){$str="+";$scaf=$1;}
if($RQ{$x}[1]=~/(\S+)\-$/){$str="-";$scaf=$1;}
printf "%s\t%s\t%s\t%d",$RQ{$x}[0],$str,$scaf,@{$RQ{$x}}-2;}
if(defined $num){
for(my $i=2;$i<@{$RQ{$x}};$i++){
my $RQ_x=\@{$RQ{$x}};
printf "\t%s(%d,%d,%d;%d,%d;%d,%d,%d)%s",
$RQ_x->[$i][0],$RQ_x->[$i][1],$RQ_x->[$i][2],$RQ_x->[$i][3],
$RQ_x->[$i][4],$RQ_x->[$i][5],
$RQ_x->[$i][6],$RQ_x->[$i][7],$RQ_x->[$i][8],$RQ_x->[$i][9];}
printf "\n";}
if(!(defined $num)){
for(my $i=2;$i<@{$RQ{$x}};$i++){
my $RQ_x=\@{$RQ{$x}};
my $ref_mRNA_r=$RQ_x->[$i][3]/$RQ_x->[$i][1];my $query_mRNA_r=$RQ_x->[$i][3]/$RQ_x->[$i][2];
my $ref_cds_r=$RQ_x->[$i][8]/$RQ_x->[$i][6];my $query_cds_r=$RQ_x->[$i][8]/$RQ_x->[$i][7];
printf "\t%s(%5.3f,%5.3f;%d,%d;%5.3f,%5.3f)",
$RQ_x->[$i][0],$ref_mRNA_r,$query_mRNA_r,$RQ_x->[$i][4],$RQ_x->[$i][5],$ref_cds_r,$query_cds_r;}
printf "\n";}
}