+sub ldstats {
+ my %opts = (s=>0.01);
+ getopts('ps:', \%opts);
+ die("Usage: vcfutils.pl ldstats [-s $opts{s}] <in.vcf>\n") if (@ARGV == 0 && -t STDIN);
+ my ($lastchr, $lastpos) = ('', 0);
+ my @a;
+ my $is_print = defined($opts{p})? 1 : 0;
+ while (<>) {
+ next if (/^#/);
+ my @t = split;
+ if ($t[0] ne $lastchr) {
+ $lastchr = $t[0];
+ } elsif (/NEIR=([\d\.]+)/) {
+ push(@a, [$t[1] - $lastpos, $1, $t[1]]);
+ }
+ $lastpos = $t[1];
+ }
+ my $max = 1000000000;
+ push(@a, [$max, 0, 0]); # end marker
+ @a = sort {$a->[0]<=>$b->[0]} @a;
+ my $next = $opts{s};
+ my $last = $a[0];
+ my @c = (0, 0, 0, 0);
+ for my $p (@a) {
+ print STDERR "$p->[0]\t$p->[1]\t$p->[2]\n" if ($is_print);
+ if ($p->[0] == $max || ($p->[0] != $last && $c[0]/@a > $next)) {
+ printf("%d\t%.2f\t%.4f\n", $c[1], $c[2]/$c[1], $c[3]/$c[1]);
+ $c[1] = $c[2] = $c[3] = 0;
+ $next = $c[0]/@a + $opts{s};
+ }
+ ++$c[0]; ++$c[1]; $c[2] += $p->[0]; $c[3] += $p->[1];
+ $last = $p->[0];
+ }
+}
+