comparison gfapts/inc/perlmod/ngsutil.pm @ 0:f753b30013e6 draft

Uploaded
author rdaveau
date Fri, 29 Jun 2012 10:20:55 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f753b30013e6
1 package ngsutil;
2 use Exporter;
3 our @ISA = qw[ Exporter ];
4 our @EXPORT = qw[ &explode_varcall &varscan ];
5 use strict;
6 use warnings FATAL => qw[ numeric uninitialized ];
7 use List::Util qw[ sum ];
8
9 sub explode_varcall{
10 my $N=0;
11 $_=shift @_ foreach my($POS, $REF, $ALT);
12 $_=$POS foreach my($START, $END);
13 my(@length, @range, @idx, @VAR, @POS);
14 @{$_}=() foreach (\@length, \@range, \@idx, \@VAR, \@POS);
15 push @length, length($_) foreach ($REF, $ALT);
16 @range=sort{ $a<=>$b } @length;
17 if($range[0]==1){
18 if($range[1]!=1){
19 foreach ($REF, $ALT){
20 $_=substr($_, 1);
21 $_=~s/^$/-/;
22 }
23 if($length[0]!=1){
24 $END+=$length[0]-1;
25 $START++;
26 }
27 }
28 push @POS, $START, $END;
29 push @VAR, $REF, $ALT;
30 }else{
31 my @N=();
32 undef $_ foreach my ($i, $VAR);
33 $_-=2 foreach (@length, @range);
34 $_++ foreach ($START, $END);
35 $_=substr($_, 1) foreach ($REF, $ALT);
36 my $indel='-' x ($range[1]-$range[0]);
37 $VAR.=($_>$range[0])?
38 ('-'):((substr($REF, $_, 1) ne substr($ALT, $_, 1))?
39 0:1) for 0 .. $range[1];
40 $N++ while $VAR =~ /0/g;
41 if($length[0]<$length[1]){
42 @VAR=($VAR);
43 @N=($N);
44 $N=0;
45 undef($VAR);
46 $VAR.=($_>$range[0])?
47 ('-'):((substr($REF, $length[0]-$_, 1) ne substr($ALT, $length[1]-$_, 1))?
48 0:1) for reverse 0 .. $range[1];
49 $N++ while $VAR =~ /0/g;
50 if($N>=$N[0]){ $N=shift(@N); $VAR=shift(@VAR); }
51 else{ $REF=$indel . $REF; }
52 }else{ $ALT.=$indel; }
53 foreach (qw[ 0 \- ]){
54 push @idx, [ $-[0], $+[0]-$-[0] ] while ($VAR =~ /$_+/g);
55 }
56 @{$_}=() foreach (\@VAR, \@POS);
57 foreach my $k (@idx){
58 push @VAR, substr($_, ${$k}[0], ${$k}[1]) || '-' foreach ($REF, $ALT);
59 push @POS, ${$k}[0], sum(@{$k})-1;
60 }
61 $_+=$START foreach @POS;
62 $_=~s/\-+/\-/ foreach @VAR;
63 for($i=0; $i<$#POS; $i+=2){ $POS[$i+1]=$POS[$i] if $VAR[$i] eq '-'; }
64 }
65 return(\@POS, \@VAR);
66 }
67
68 sub varscan{
69 $_=shift @_ foreach my($kname, $fpath, $href);
70 my($k, @buffer);
71 open IN, "<$fpath" or die $!;
72 while(<IN>){
73 next if /^#/;
74 chomp;
75 @buffer=split /\s+/, $_;
76 next if !exists $$href{($k=join(':', @buffer[0..2]))};
77 next if $$href{$k}->{ref} !~ $buffer[3];
78 next if $$href{$k}->{alt} !~ $buffer[4];
79 splice(@buffer, 0, 5);
80 $$href{$k}->{$kname}=join(':', @buffer);
81 }
82 close IN;
83 }
84 1;