]> git.donarmstrong.com Git - samtools.git/commitdiff
improve the LD statistics
authorHeng Li <lh3@live.co.uk>
Thu, 14 Oct 2010 03:32:12 +0000 (03:32 +0000)
committerHeng Li <lh3@live.co.uk>
Thu, 14 Oct 2010 03:32:12 +0000 (03:32 +0000)
bcftools/call1.c
bcftools/ld.c
bcftools/vcfutils.pl

index 68b24f0ef606b9574cb4d27ee5648a8dad31a88f..2b284527185c0e12747ca1dfb5387aa0bd615c36 100644 (file)
@@ -348,7 +348,7 @@ int bcfview(int argc, char *argv[])
                                kstring_t s;
                                s.m = s.l = 0; s.s = 0;
                                if (*b->info) kputc(';', &s);
-                               ksprintf(&s, "NEIR=%.3lf", r2);
+                               ksprintf(&s, "NEIR=%.3lf;NEIF=%.3lf,%.3lf", r2, f[0]+f[2], f[0]+f[1]);
                                bcf_append_info(b, s.s, s.l);
                                free(s.s);
                        }
index 01145b84ef81538e9310a2cc61feb295dd9481d8..aa7ec072b2c7fc2e60ea3d1fe8145711a8841d28 100644 (file)
@@ -90,7 +90,7 @@ double bcf_ld_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4])
        { // calculate r^2
                double p[2], q[2], D;
                p[0] = f[0] + f[1]; q[0] = 1 - p[0];
-               p[1] = f[2] + f[3]; q[1] = 1 - p[1];
+               p[1] = f[0] + f[2]; q[1] = 1 - p[1];
                D = f[0] * f[3] - f[1] * f[2];
                r = sqrt(D * D / (p[0] * p[1] * q[0] * q[1]));
                // fprintf(stderr, "R(%lf,%lf,%lf,%lf)=%lf\n", f[0], f[1], f[2], f[3], r2);
index 4caa846cc5119b2bc30d581806f6e1987c114baa..d0b797143cf56fe0a67383ec4f610096eaecf2c0 100755 (executable)
@@ -104,38 +104,25 @@ sub fillac {
 }
 
 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;
+  my %opts = (t=>0.9);
+  getopts('t:', \%opts);
+  die("Usage: vcfutils.pl ldstats [-t $opts{t}] <in.vcf>\n") if (@ARGV == 0 && -t STDIN);
+  my $cutoff = $opts{t};
+  my ($last, $lastchr) = (0x7fffffff, '');
+  my ($x, $y, $n) = (0, 0, 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};
+       if (/^([^#\s]+)\s(\d+)/) {
+         my ($chr, $pos) = ($1, $2);
+         if (/NEIR=([\d\.]+)/) {
+               ++$n;
+               ++$y, $x += $pos - $last if ($lastchr eq $chr && $pos > $last && $1 > $cutoff);
+         }
+         $last = $pos; $lastchr = $chr;
        }
-       ++$c[0]; ++$c[1]; $c[2] += $p->[0]; $c[3] += $p->[1];
-       $last = $p->[0];
   }
+  print "Number of SNP intervals in strong LD (r > $opts{t}): $y\n";
+  print "Fraction: ", $y/$n, "\n";
+  print "Length: $x\n";
 }
 
 sub qstats {