forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplicatesMatch.pl
executable file
·203 lines (181 loc) · 4.73 KB
/
replicatesMatch.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
#!/usr/bin/perl
my $usage="
replicatesMatch.pl : (version 0.7, September 27 2015)
Selects polymorphic variants that have identical genotypes among replicates.
Genotypes of two replicates should be identical or missing; the maximal fraction of
genotype pairs with missing genotypes is controlled by missing= argument.
Arguments:
vcf=[file name] input vcf file
replicates=[file name] - a two column tab-delimited table listing
pairs of samples that are replicates (at least 3 pairs)
matching=[float] required fraction of matching genotypes
(missing counts as match if there are other non-missing matching pairs),
default 1 (all must match)
missing=[float] allowed fraction of missing genotypes, default 0.25
altPairs=2 minimal number of matching genotypes involving non-reference alleles.
hetPairs=0 minimal number of matching heterozygotes.
max.het=0.75 maximum fraction of heterozygotes among non-missing genotypes
(guards against lumped paralogous loci).
polyonly=[1|0] extract only polymorphic sites. Default 0.
Example:
replicatesMatch.pl vcf=round2.vcf replicates=clonepairs.tab > vqsr.vcf
";
my $vcf;
my $reps;
my $missing=0.25;
my $fmatch=1;
#my $allalts=0;
my $maxhet=0.75;
my $hetPairs=0;
my $altPairs=2;
my $polyonly=0;
if ("@ARGV"=~/vcf=(\S+)/) { $vcf=$1;}
else { die $usage; }
if ("@ARGV"=~/replicates=(\S+)/) { $reps=$1;}
else { die $usage; }
if ("@ARGV"=~/missing=(\S+)/) { $missing=$1;}
if ("@ARGV"=~/matching=(\S+)/) { $fmatch=$1;}
#if ("@ARGV"=~/allAlts=1/ ) { $allalts=1;}
if ("@ARGV"=~/max.het=(\S+)/) { $maxhet=$1;}
if ("@ARGV"=~/hetPairs=(\d+)/) { $hetPairs=$1;}
if ("@ARGV"=~/altPairs=(\d+)/) { $altPairs=$1;}
if ("@ARGV"=~/polyonly=1/) { $polyonly=1;}
open VCF, $vcf or die "cannot open vcf file $vcf\n";
my @samples;
my @pairs=();
my %indr={};
my @npairs=();
my $r1;
my $r2;
my $nreps=0;
my $pass=0;
my $hetpass=0;
my $altpass=0;
my $total=0;
my $poly=0;
my $numalt=0;
my $numout=0;
my $numhets=0;
while (<VCF>) {
if ($_=~/^#/) {
if ($_=~/contig/) { next;}
elsif ($_=~/^#CHROM/){
print $_;
chop;
@samples=split("\t",$_);
my @lead=splice (@samples,0,9);
open REP, $reps or die "cannot open replicates file $reps\n";
while (<REP>){
next if ($_!~/\S+/);
$nreps++;
chomp;
($r1,$r2)=split(/\s+/,$_);
my $collect=0;
for(my $i=0;my $s=$samples[$i];$i++){
if ($s eq $r1) {
$indr{$r1}=$i;
$collect++;
}
if ($s eq $r2) {
$indr{$r2}=$i;
$collect++;
}
}
push @pairs,"$indr{$r1}:$indr{$r2}";
push @npairs,"$r1:$r2";
if ($collect<2) { die "cannot locate samples $r1 and/or $r2\n";}
}
close REP;
$minfalt=1/$nreps unless $nreps==0;
#warn "$nreps replicate pairs\n";
#warn join("\t",@npairs),"\n",join("\t",@pairs),"\n";
}
else { print $_;}
next;
}
chop;
$total++;
my @lin=split("\t",$_);
my @start=splice(@lin,0,9);
my $Missing = () = "@lin" =~ /\.\/\./gi;
my $Heteros = () = "@lin" =~ /0[\/\|]1/gi;
# my $Althomos = () = "@lin" =~ /1[\/\|]1/gi;
# my $Refhomos = () = "@lin" =~ /0[\/\|]0/gi;
my $Nsam= scalar @lin;
if ($Heteros/($Nsam-$Missing+0.01) > $maxhet) {
#warn "badHet: Het:$Heteros;Miss:$Missing;Tot:$Nsam\n";
next;
}
#warn "--------------\n$start[0]_$start[1]\n\n";
my @rest;
my $match=0;
my $miss=0;
my $anum=0;
my $g1;
my $g2;
my $a1;
my $a2;
my $nalt=0;
my $nref=0;
my $het=0;
my $nalleles=0;
my $altpairs=0;
my %seen={};
for(my $p=0;$pp=$pairs[$p];$p++) {
($r1,$r2)=split(":",$npairs[$p]);
($i1,$i2)=split(":",$pp);
($g1,@rest)=split(":",$lin[$i1]);
($g2,@rest)=split(":",$lin[$i2]);
if ($g1=~/\./ || $g2=~/\./) {
$miss++;
$match++;
}
elsif ($g1 eq $g2) {
$match++;
($a1,$a2)=split(/\D/,$g1);
if (!$seen{$a1}) {
$seen{$a1}=1;
$nalleles++;
}
if (!$seen{$a2}) {
$seen{$a2}=1;
$nalleles++;
}
if ($a1=~/[123456789]/) { $nalt++; }
else { $nref++;}
if ($a2=~/[123456789]/) {
$nalt++;
$altpairs++;
}
else { $nref++;}
if ($a1 ne $a2) {$het++;}
}
}
next if ($match < ($nreps*$fmatch) );
next if ( ($miss/$nreps) > $missing);
$pass++;
if ($nalt) { $numalt++;} else { next;}
next if ($altpairs<$altPairs );
$altpass++;
next if ( $het<$hetPairs );
$hetpass++;
if ($nalleles>1) {
$poly++;
$numout++;
print join("\t",@start)."\t".join("\t",@lin)."\n";
}
elsif (!$polyonly){
if ($nalt) {
$numout++;
print join("\t",@start)."\t".join("\t",@lin)."\n";
}
}
}
warn "
$total total SNPs
$pass pass hets and match filters
$numalt show non-reference alleles
$altpass have alterantive alleles in at least $altPairs replicate pair(s)
$hetpass have matching heterozygotes in at least $hetPairs replicate pair(s)
$poly polymorphic\n$numout written
";