forked from lh3/fastARG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phaseARG.pl
executable file
·217 lines (202 loc) · 5.21 KB
/
phaseARG.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
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Std;
use Cwd qw/getcwd abs_path/;
&main;
exit;
sub main {
my %opts = (r=>20, n=>40);
getopts('r:n:', \%opts);
die("Usage: phaseARG.pl <in.fad> <out.prefix>\n") if (@ARGV != 2);
my ($n_rounds, $n_splits, %data);
my ($fn, $prefix) = @ARGV;
my $fastARG = &gwhich('fastARG');
system("rm -f $prefix.*.fad");
&arg_data_read($fn, \%data);
$n_rounds = $opts{r};
$opts{n} = $data{n} if ($opts{n} > $data{n});
$n_splits = int($data{n} / $opts{n} + .499);
&init_tmp_file(">$prefix.pileup", $data{m}, $data{n}, $n_rounds);
my $fh = &gopen("+<$prefix.pileup");
my @ida;
$ida[$_] = 0 for (0 .. $data{n});
my %globals = (PRE=>$prefix, DATA=>\%data, PROG=>$fastARG, M=>$data{m}, N=>$data{n}, IDA=>\@ida, NR=>$n_rounds,
FH=>$fh);
for (my $r = 0; $r < $n_rounds; ++$r) {
my @list = &gen_sub($data{n}, $n_splits);
for my $x (@list) {
&arg_phase1(\%globals, $x, $r%2==1);
}
}
close($fh);
unlink("$prefix.tmp.fad");
&pileup2fads($prefix, \%data, $n_rounds);
}
sub init_tmp_file {
my ($fn, $m, $n, $n_rounds) = @_;
my $fh = &gopen($fn);
for (my $i = 0; $i < $n; ++$i) {
for (my $j = 0; $j < $n_rounds; ++$j) {
print $fh "?"x$m, "\n";
}
print $fh "-"x$m, "\n";
}
close($fh) unless (ref($fn) eq 'GLOB');
}
sub pileup2fads {
my ($prefix, $data, $n_rounds) = @_;
my ($m, $n) = ($data->{m}, $data->{n});
my @r;
my @conv = ('00', '01', '10', '11');
my $fh = &gopen("$prefix.pileup");
for (my $j = 0; $j < $n_rounds; ++$j) {
my $x = \%{$r[$j]};
$x->{m} = $data->{m}; $x->{n} = $data->{n}; @{$x->{p}} = @{$data->{p}};
}
for (my $i = 0; $i < $n; ++$i) {
for (my $j = 0; $j < $n_rounds; ++$j) {
$_ = <$fh>; chomp;
s/(\d)/$conv[$1]/eg;
$r[$j]{s}[$i] = $_;
}
<$fh>;
}
close($fh);
for (my $j = 0; $j < $n_rounds; ++$j) {
&arg_data_print($r[$j], sprintf(">$prefix.%.4d.fad", $j));
}
}
sub arg_phase1 {
my ($g, $list, $is_b) = @_;
my @inds = split(',', $list);
my (%sub, %rst);
my $fn = "$g->{PRE}.tmp.fad";
my $ida = $g->{IDA};
&arg_data_sub($g->{DATA}, \@inds, \%sub);
&arg_data_print(\%sub, ">$g->{PRE}.tmp.fad");
my $b = $is_b? '-b' : '';
&arg_data_read("$g->{PROG} build -Ls0 $b $fn 2>/dev/null | $g->{PROG} leafseq - |", \%rst);
for (my $i = 0; $i < @inds; ++$i) {
my $x = $inds[$i];
my $offset = ($x * ($g->{NR} + 1) + $ida->[$x]) * ($g->{M} + 1);
seek($g->{FH}, $offset, 0);
my $y = $rst{s}[$i];
$y =~ s/(\d)(\d)/$1<<1|$2/eg;
print {$g->{FH}} $y;
}
++$ida->[$_] for (@inds);
}
sub arg_data_read {
my ($fn, $data) = @_;
my $fh = &gopen($fn);
%$data = ();
my ($d, $pos) = (\@{$data->{s}}, \@{$data->{p}});
while (<$fh>) {
my @t = split;
die("[arg_read_data] number of haplotypes is not an even number\n") if (length($t[1])%2 != 0);
push(@$pos, $t[0]);
my $l = length($t[1]);
for (my $i = 0; $i < $l; $i += 2) {
my $id = int($i/2);
if (defined $d->[$id]) {
$d->[$id] .= substr($t[1], $i, 2);
} else {
$d->[$id] = substr($t[1], $i, 2);
}
}
}
close($fh) unless (ref($fn) eq 'GLOB');
$data->{m} = length($d->[0])/2;
$data->{n} = @$d;
}
sub arg_data_print {
my ($data, $fn) = @_;
my $fh = &gopen($fn);
my ($m, $n) = ($data->{m}, $data->{n});
my ($d, $pos) = (\@{$data->{s}}, \@{$data->{p}});
for my $x (0 .. $m-1) {
my $str = '';
$str .= substr($_, $x*2, 2) for (@$d);
print $fh "$pos->[$x]\t", $str, "\n";
}
close($fh) unless (ref($fn) eq 'GLOB');
}
sub arg_data_sub {
my ($data, $inds, $sub) = @_;
%$sub = ();
my ($d, $pos) = (\@{$data->{s}}, \@{$data->{p}});
my $sd = \@{$sub->{s}};
@{$sub->{p}} = @{$data->{p}};
$sub->{m} = $data->{m};
$sub->{n} = @$inds;
$sd->[$_] = $d->[$inds->[$_]] for (0 .. @$inds-1);
}
sub gen_sub {
my ($m, $n) = @_;
die if ($m < $n);
my $x = int($m/$n); # floor
my $y = $m - $n * $x;
my @a = &shuffle($m);
my @b;
if ($y) {
push(@b, join(',', @a[$_*($x+1) .. ($_+1)*($x+1)-1])) for (0 .. $y-1);
}
my $z = $y * ($x + 1);
push(@b, join(',', @a[$z+$_*$x .. $z+($_+1)*$x-1])) for (0 .. $n-$y-1);
return @b;
}
sub shuffle {
my $n = shift;
my @a = (0 .. $n-1);
for (my $i = $n - 1; $i > 0; --$i) {
my $x = int(rand() * $i);
$a[$i] ^= $a[$x]; $a[$x] ^= $a[$i]; $a[$i] ^= $a[$x];
}
return @a;
}
sub dirname {
my $prog = shift;
my $cwd = getcwd;
return $cwd if ($prog !~ /\//);
$prog =~ s/\/[^\s\/]+$//g;
return $prog;
}
sub which {
my $file = shift;
my $path = (@_)? shift : $ENV{PATH};
return if (!defined($path));
foreach my $x (split(":", $path)) {
$x =~ s/\/$//;
return "$x/$file" if (-x "$x/$file" && -f "$x/$file");
}
return;
}
sub gwhich {
my $progname = shift;
my $addtional_path = shift if (@_);
my $dirname = &dirname($0);
my $tmp;
chomp($dirname);
if (-x $progname && -f $progname) {
return abs_path($progname);
} elsif (defined($dirname) && (-x "$dirname/$progname" && -f "$dirname/$progname")) {
return abs_path("$dirname/$progname");
} elsif (($tmp = &which($progname))) { # on the $PATH
return $tmp;
} else {
warn("[gwhich] fail to find executable $progname anywhere.");
return;
}
}
sub gopen {
my $f = shift;
return $f if (ref($f) eq 'GLOB');
if (!ref($f)) {
my $fh;
unless (open($fh, $f)) {
die("[gopen] fail to open file $f.\n");
}
return $fh;
}
}