-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.pm
More file actions
391 lines (349 loc) · 9.83 KB
/
common.pm
File metadata and controls
391 lines (349 loc) · 9.83 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
## return: -1:timeout; 1:finished
sub Run_monitor_timeout0{
my ($time, $cmd, $sh)=@_;
print "###", $cmd,"\n";
if(defined $sh){
print $sh $cmd,"\n";
}
my $pid;
eval {
##正在调用的子程序pid为$pid+1, 好像是中间多一步调用: sh -c; 偶尔可能需要+2
local $SIG{ALRM} = sub {my $pidn=$pid+1; `kill -9 $pidn`; die "alarm\n"; };
alarm $time;
$pid=fork;
if(not $pid){
exec($cmd);
}
# print "pid: ", $pid,"\n";
waitpid($pid,0);
# $? = `$cmd`; ##这种形式和system,子程序不会中断
alarm 0;
};
#print "\$\$+28: ", $$+28,"\n"; ## current pid 应该是主程序的pid,通常子程序的pid为$$+28
if ($@) { # timed out
die unless $@ eq "alarm\n";
print "Run time out! $cmd\n";
return -1;
}else { # didn't timeout
return 1;
}
}
## 超时中断,但无法判断是否超时中断
sub Run_monitor_timeout{
my ($time, $cmd, $sh)=@_;
print "###", $cmd,"\n";
if(defined $sh){
print $sh $cmd,"\n";
}
## 可以实现中断退出,但中断与否返回值retval都是0,无法判断是否超时中断。
my $retval = system("ulimit -t $time; $cmd");
}
sub Run{
my ($cmd, $sh, $nodie)=@_;
print "###", $cmd,"\n";
if(defined $sh){
print $sh $cmd,"\n";
}
my $ret = system($cmd);
if ($ret) {
print STDERR "Run $cmd failed!\n";
if(!defined $nodie){
die;
}
}
}
sub convert_StartEndRefAlt{ ## copy from annovar
my ($start, $ref, $alt)=@_;
my ($head, $newstart, $newend, $newref, $newalt);
if (length ($ref) == 1 and length ($alt) == 1) { #SNV
($newstart, $newend) = ($start, $start+length($ref)-1);
($newref, $newalt) = ($ref, $alt);
} elsif (length ($ref) > length ($alt)) { #deletion or block substitution
$head = substr ($ref, 0, length ($alt));
if ($head eq $alt) {
($newstart, $newend) = ($start+length ($head), $start + length ($ref)-1);
($newref, $newalt) = (substr($ref, length($alt)), '-');
} else {
($newstart, $newend) = ($start, $start+length($ref)-1); #changed to length(ref) on 20130820
($newref, $newalt) = ($ref, $alt);
}
($newstart, $newend, $newref, $newalt) = adjustStartEndRefAlt ($newstart, $newend, $newref, $newalt);
} elsif (length ($ref) < length ($alt)) { #insertion or block substitution
$head = substr ($alt, 0, length ($ref));
if ($head eq $ref) {
($newstart, $newend) = ($start+length($ref)-1, $start+length($ref)-1);
($newref, $newalt) = ('-', substr($alt, length($ref)));
} else {
($newstart, $newend) = ($start, $start+length($ref)-1);
($newref, $newalt) = ($ref, $alt);
}
($newstart, $newend, $newref, $newalt) = adjustStartEndRefAlt ($newstart, $newend, $newref, $newalt);
}
return ($newstart, $newend, $newref, $newalt);
}
sub adjustStartEndRefAlt {
my ($newstart, $newend, $newref, $newalt) = @_;
until (substr($newref,-1) ne substr($newalt,-1)) {
chop $newref;
chop $newalt;
$newend--;
if (not $newref) {
$newref = '-';
last;
}
if (not $newalt) {
$newalt = '-';
last;
}
}
until (substr($newref,0,1) ne substr ($newalt,0,1)) {
substr ($newref,0,1) = '';
substr ($newalt,0,1) = '';
$newstart++;
if (not $newref) {
$newref = '-';
last;
}
if (not $newalt) {
$newalt = '-';
last;
}
}
return ($newstart, $newend, $newref, $newalt);
}
sub md_split{
my($md)=@_;
my @unit = split //, $md;
my @mds;
my $temp;
for(my $i=0; $i<@unit; $i++){
if($unit[$i]=~/[ATCGN]/){
push @mds, $temp;
push @mds, $unit[$i];
$temp = "";
}elsif($unit[$i] eq "^"){
push @mds, $temp;
$temp = $unit[$i];
my $j;
for($j=$i+1;$j<@unit;$j++){
last if($unit[$j]!~/[ATCGN]/);
$temp .= $unit[$j];
}
$i=$j-1;
push @mds, $temp;
$temp="";
}elsif($unit[$i]=~/[0-9]/){
$temp.=$unit[$i];
}
}
push @mds, $temp;
return(\@mds);
}
sub cigar_split{
my($cigar, $keepH)=@_;
my @ucigar = split //, $cigar;
my (@match_n,@match_str);
my $nstr="";
foreach my $i(0..$#ucigar){
if($ucigar[$i] eq "M" || $ucigar[$i] eq "I" || $ucigar[$i] eq "D" || $ucigar[$i] eq "S"){
push @match_str, $ucigar[$i];
push @match_n, $nstr;
$nstr = "";
}elsif($ucigar[$i] eq "H"){
if(defined $keepH){
push @match_str, $ucigar[$i];
push @match_n, $nstr;
}
$nstr = "";
}else{
$nstr .= $ucigar[$i];
}
}
return(\@match_n,\@match_str);
}
sub explain_bam_flag{
my ($flag)=@_;
my $flag_bin=sprintf("%b", $flag);
my @flag_bin = split //, $flag_bin;
my $is_read1 = $flag_bin[-7];
my $is_read2 = @flag_bin>=8? $flag_bin[-8]: 0;
my $is_supplementary = @flag_bin>=12? $flag_bin[-12]: 0;
my $is_proper_pair = $flag_bin[-2];
my $is_reverse = $flag_bin[-5];
my $is_unmap = $flag_bin[-3];
my $is_munmap = $flag_bin[-4];
my $dup = @flag_bin>=11? $flag_bin[-11]: 0;
my $rnum = $is_read1==1? 1: 2;
return($rnum, $is_proper_pair, $is_reverse, $is_unmap, $is_munmap, $is_supplementary);
}
sub GC_stat{
my ($p)=@_;
my @u = split //, $p;
my %stat;
$stat{"total"}{'G'}=0;
$stat{"total"}{'C'}=0;
$stat{5}{'G'}=0;
$stat{5}{'C'}=0;
$stat{3}{'G'}=0;
$stat{3}{'C'}=0;
my $total = scalar @u;
for(my $i=0; $i<$total; $i++){
if ($i<$GC5_num){
$stat{5}{$u[$i]}++;
}
if ($i>$total-$GC3_num-1){
$stat{3}{$u[$i]}++;
}
$stat{"total"}{$u[$i]}++;
}
my $GC = ($stat{"total"}{'G'}+$stat{"total"}{'C'})/$total;
my $GC5= ($stat{5}{'G'}+$stat{5}{'C'})/$GC5_num;
my $GC3 = ($stat{3}{'G'}+$stat{3}{'C'})/$GC3_num;
return ($GC, $GC5, $GC3);
}
sub GC_array{
my @u=@_;
my $total = 0;
my $gc = 0;
foreach $b (@u){
$total++;
if($b eq 'G' || $b eq 'C' || $b eq "g" || $b eq "c"){
$gc++;
}
}
return($gc/$total);
}
sub GC{
my ($s)=@_;
my @u=split //, $s;
my $total = 0;
my $gc = 0;
foreach $b (@u){
$total++;
if($b eq 'G' || $b eq 'C' || $b eq "g" || $b eq "c"){
$gc++;
}
}
return($gc/$total);
}
sub GC_info_stat{
my ($p, $Wind_GC)=@_;
my @u = split //, $p;
my $high_GC=0.8;
my $low_GC=0.2;
my ($high_len, $low_len)=(0,0);
my $GC = &GC($p);
my $min_GC=1;
my $max_GC=0;
for(my $i=0; $i<@u-$Wind_GC+1; $i++){
my @sub = @u[$i..$i+$Wind_GC-1];
my $sub_gc = &GC_array(@sub);
if($sub_gc>=$high_GC){
$high_len++;
}
if($sub_gc<=$low_GC){
$low_len++;
}
if($sub_gc>$max_GC){
$max_GC = $sub_gc;
}
if($sub_gc<$min_GC){
$min_GC = $sub_gc;
}
}
$GC = sprintf "%0.2f", $GC;
return ($GC, $high_len, $low_len, $max_GC, $min_GC);
}
sub revcom{#&revcom($ref_seq);
#获取字符串序列的反向互补序列,以字符串形式返回。ATTCCC->GGGAAT
my $seq=shift;
$seq=~tr/ATCGatcg/TAGCtagc/;
$seq=~tr/RYMKSWHDBVN/YRKMSWDHVBN/;
$seq=reverse $seq;
return $seq;
}
###############################################################################################################
sub SortByNum { ###### 对那种以数字结尾的list按数字的大小排序, 排序类型用"+"表示升序,"-"表示降序。
###### 例如:LG1,LG6,LG3,LG5 按升序排序后得到:LG1,LG3,LG5,LG6
my ($sort_type,$ref_list,$ref_sorted)=@_;
my %hash;
my $sum=0;
foreach my $name (@{$ref_list}) {
$sum++;
my ($word,$num)=$name=~/(\S*\D+)(\d+)/;
$hash{$word}{$sum}=$num;
}
if ($sort_type eq "+") {
foreach my $word (sort {$a cmp $b} keys %hash) {
foreach my $sum (sort {$hash{$word}{$a} <=> $hash{$word}{$b}} keys %{$hash{$word}}) {
push @{$ref_sorted},$word.$hash{$word}{$sum};
}
}
}elsif($sort_type eq "-"){
foreach my $word (sort {$a cmp $b} keys %hash) {
foreach my $sum (sort {$hash{$word}{$b} <=> $hash{$word}{$a}} keys %{$hash{$word}}) {
push @{$ref_sorted},$word.$hash{$word}{$sum};
}
}
}
}
################################################################################################################
sub SSR {#返回序列的低复杂度
my @arr=split "",uc shift;
my %hash;
$hash{$_}++ foreach (@arr);
my ($first,$second)=sort {$b <=> $a} values %hash;
return ($first+$second)/scalar @arr;
}
sub AbsolutePath
{ #获取指定目录或文件的决定路径
my ($type,$input) = @_;
my $return;
$/="\n";
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 SHOW_TIME {
#显示当时时间函数,参数内容是时间前得提示信息,为字符串
my ($str)=@_;
my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst)=localtime(time());
my $temp=sprintf("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $day, $hour, $min, $sec);
print "$str:\t[".$temp."]\n";
}
sub sub_format_datetime #datetime subprogram
{
my($sec , $min , $hour , $day , $mon , $year , $wday , $yday , $isdst) = @_;
sprintf("%4d-%02d-%02d %02d:%02d:%02d" , ($year + 1900) , $mon , $day , $hour , $min , $sec);
}
sub TimeCount() {
my ($start,$stop)=@_;
my $min=int(($stop-$start)/60);
my $second=sprintf("%02d",($stop-$start) % 60);
my $return=$min.":".$second;
return ($return);
}
1;