-
Notifications
You must be signed in to change notification settings - Fork 4
/
homology_check.pl
188 lines (167 loc) · 6.2 KB
/
homology_check.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
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use FindBin qw($Bin $Script);
use File::Basename qw(basename dirname);
require "$Bin/path.pm";
my $BEGIN_TIME=time();
my $version="1.0.0";
#######################################################################################
# ------------------------------------------------------------------
# GetOptions
# ------------------------------------------------------------------
our $REF_GRCh37;
our $SAMTOOLS;
my ($fIn,$fkey,$outdir);
my $ftemplate;
my $fref = $REF_GRCh37;
my $min_ratio = 0.8;
my $max_gap_num = 3;
my $max_gap_len = 20;
GetOptions(
"help|?" =>\&USAGE,
"it:s"=>\$ftemplate,
"ir:s"=>\$fref,
"k:s"=>\$fkey,
"mr:s"=>\$min_ratio,
"mgn:s"=>\$max_gap_num,
"mgl:s"=>\$max_gap_len,
"od:s"=>\$outdir,
) or &USAGE;
&USAGE unless ($ftemplate and $fkey);
$outdir||="./";
`mkdir $outdir` unless (-d $outdir);
$outdir=AbsolutePath("dir",$outdir);
our $BLAT;
my @frefs=split /,/, $fref;
foreach my $fref(@frefs){
$fref=AbsolutePath("file", $fref);
my $name = basename($fref);
`ln -s $fref $outdir/$name`;
my $sh = "$BLAT -noHead $fref $ftemplate $outdir/$name.psl";
print $sh,"\n";
`$sh`;
}
open(I, $ftemplate) or die $!;
my %pseq;
my %idout;
$/=">";
while(<I>){
chomp;
next if(/^$/);
my ($head, @seq)=split /\n/,$_;
my $seq=join("", @seq);
my ($id)=split /\s+/, $head;
$seq=uc($seq);
$pseq{$id}=$seq;
$idout{$id}=$id;
if(/XP:Z:/){
my ($ori, $chr, $s, $e)=$head=~/XP:Z:([+-])(\w+):(\d+)-(\d+)/;
my $idn = $id."(".$ori.$chr.":".$s."-".$e.")";
$idout{$id}=$idn;
}
}
close(I);
open(O, ">$outdir/$fkey.homology.seq") or die $!;
print O "#templateID\ttemplateStart\ttemplateEnd\tDatabaseChrAndOrient\tDatabaseStart\tDatabaseEnd\tMatch\tMismatch\tGapNumT\tGapLengthT\tGapNumD\tGapLengthD\n";
my @fpsl=glob("$outdir/*psl");
foreach my $fpsl(@fpsl){
my $fref=$fpsl;
$fref=~s/\.psl//;
open(I, $fpsl) or die $!;
$/="\n";
#psl v1:
#149 14 0 0 1 28 1 29 + PTS-0639-U 201 10 201 chrX 155270560 38777907 38778099 2 99,64, 10,137, 38777907,38778035,
#176 13 0 0 0 0 1 1 + PTS-0639-U 201 12 201 chr8 146364022 49220714 49220904 2 124,65, 12,136, 49220714,49220839,
#172 19 0 0 0 0 0 0 + PTS-0639-U 201 10 201 chr8 146364022 144837574 144837765 1 191, 10, 144837574,
#173 18 0 0 0 0 0 0 + PTS-0639-U 201 10 201 chr7 159138663 98139617 98139808 1 191, 10, 98139617,
#psl v2:
#501 0 0 0 0 0 0 0 + chr11 135006516 1269880 1270381 chr11:1270382-U 501 0 501 1 501, 1269880,
#499 2 0 0 0 0 0 0 + chr11 135006516 1266109 1266610 chr11:1270382-U 501 0 501 1 501, 1266109,
#490 11 0 0 0 0 0 0 + chr11 135006516 1268209 1268710 chr11:1270382-U 501 0 501 1 501, 1268209,
#487 14 0 0 0 0 0 0 + chr11 135006516 1264438 1264939 chr11:1270382-U 501 0 501 1 501, 1264438,
while(<I>){
chomp;
next if($_!~/^\d/);
my ($match, $mismatch, $gap1,$gaplen1, $gap2, $gaplen2, $ori, $pid, $len, $ps, $pe, $chr, $s, $e)=(split /\s+/, $_)[0,1,4,5,6,7,8,9,10, 11,12,13,15,16]; #psl v1
# my ($match, $mismatch, $gap1, $gap2, $ori, $pid, $len, $ps, $pe, $chr, $s, $e)=(split /\s+/, $_)[0,1,4,6,8,13,14,15,16,9,11,12]; #v2
# next if($match == $len && $mismatch==0 && $gap1==0 && $gap2==0);
next if($match<$len*$min_ratio || $gap1>$max_gap_num || $gaplen1>$max_gap_len || $gap2>$max_gap_num || $gaplen2>$max_gap_len);
# next if($ori eq $ori0 && $chr eq $chr0 && $s==$s0-1 && $e==$e0);
$s++;
my $fa=`$SAMTOOLS faidx $fref $chr:$s-$e`;
my ($head, @line) = split /\n/, $fa;
my $seq = join("", @line);
$seq = uc($seq);
if($ori eq "-"){
$seq=~tr/ATCG/TAGC/;
$seq=reverse $seq;
}
my $minfo = `perl $Bin/sw.pl $pseq{$pid} $seq -print_overall`;
#print "perl /home/zenghp/bin/sw.pl -print_overall $pseq{$pid} $seq\n";
chomp $minfo;
my @minfo = split /\n/, $minfo;
print O ">",join("\t",$idout{$pid},$ps,$pe, $chr.$ori, $s, $e, $match, $mismatch, $gap1, $gaplen1, $gap2, $gaplen2),"\n";
print O join("\n", @minfo[0..2]),"\n";
}
close(I);
}
close(O);
#######################################################################################
print STDOUT "\nDone. Total elapsed time : ",time()-$BEGIN_TIME,"s\n";
#######################################################################################
# ------------------------------------------------------------------
# sub function
# ------------------------------------------------------------------
sub AbsolutePath
{ #获取指定目录或文件的决定路径
my ($type,$input) = @_;
my $return;
if ($type eq 'dir')
{
my $pwd = `pwd`;
chomp $pwd;
chdir($input);
$return = `pwd`;
chomp $return;
chdir($pwd);
}
elsif($type eq 'file')
{
my $pwd = `pwd`;
chomp $pwd;
my $dir=dirname($input);
my $file=basename($input);
chdir($dir);
$return = `pwd`;
chomp $return;
$return .="\/".$file;
chdir($pwd);
}
return $return;
}
sub GetTime {
my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst)=localtime(time());
return sprintf("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $day, $hour, $min, $sec);
}
sub USAGE {#
my $usage=<<"USAGE";
Program:
Version: $version
Contact:zeng huaping<huaping.zeng\@genetalks.com>
Usage:
Options:
-it <file> Input template fa file, forced
-ir <files> reference fasta files separated by ",", [$fref]
-k <str> Key of output file, forced
-mr <float> min match ratio, [$min_ratio]
-mgn <int> max gap num, [$max_gap_num]
-mgl <int> max gap length, [$max_gap_len]
-od <dir> Dir of output file, default ./
-h Help
USAGE
print $usage;
exit;
}