+double mann_whitney_1947(int n, int m, int U)
+{
+ if (U<0) return 0;
+ if (n==0||m==0) return U==0 ? 1 : 0;
+ return (double)n/(n+m)*mann_whitney_1947(n-1,m,U-m) + (double)m/(n+m)*mann_whitney_1947(n,m-1,U);
+}
+
+void calc_ReadPosBias(bcf_callaux_t *bca, bcf_call_t *call)
+{
+ int i, nref = 0, nalt = 0;
+ unsigned long int U = 0;
+ for (i=0; i<bca->npos; i++)
+ {
+ nref += bca->ref_pos[i];
+ nalt += bca->alt_pos[i];
+ U += nref*bca->alt_pos[i];
+ bca->ref_pos[i] = 0;
+ bca->alt_pos[i] = 0;
+ }
+#if 0
+//todo
+ double var = 0, avg = (double)(nref+nalt)/bca->npos;
+ for (i=0; i<bca->npos; i++)
+ {
+ double ediff = bca->ref_pos[i] + bca->alt_pos[i] - avg;
+ var += ediff*ediff;
+ bca->ref_pos[i] = 0;
+ bca->alt_pos[i] = 0;
+ }
+ call->read_pos.avg = avg;
+ call->read_pos.var = sqrt(var/bca->npos);
+ call->read_pos.dp = nref+nalt;
+#endif
+ if ( !nref || !nalt )
+ {
+ call->read_pos_bias = -1;
+ return;
+ }
+
+ if ( nref>=8 || nalt>=8 )
+ {
+ // normal approximation
+ double mean = ((double)nref*nalt+1.0)/2.0;
+ double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
+ double z = (U-mean)/sqrt(var2);
+ call->read_pos_bias = z;
+ //fprintf(stderr,"nref=%d nalt=%d U=%ld mean=%e var=%e zval=%e\n", nref,nalt,U,mean,sqrt(var2),call->read_pos_bias);
+ }
+ else
+ {
+ double p = mann_whitney_1947(nalt,nref,U);
+ // biased form claimed by GATK to behave better empirically
+ // double var2 = (1.0+1.0/(nref+nalt+1.0))*(double)nref*nalt*(nref+nalt+1.0)/12.0;
+ double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
+ double z;
+ if ( p >= 1./sqrt(var2*2*M_PI) ) z = 0; // equal to mean
+ else
+ {
+ if ( U >= nref*nalt/2. ) z = sqrt(-2*log(sqrt(var2*2*M_PI)*p));
+ else z = -sqrt(-2*log(sqrt(var2*2*M_PI)*p));
+ }
+ call->read_pos_bias = z;
+ //fprintf(stderr,"nref=%d nalt=%d U=%ld p=%e var2=%e zval=%e\n", nref,nalt,U, p,var2,call->read_pos_bias);
+ }
+}
+
+float mean_diff_to_prob(float mdiff, int dp, int readlen)
+{
+ if ( dp==2 )
+ {
+ if ( mdiff==0 )
+ return (2.0*readlen + 4.0*(readlen-1.0))/((float)readlen*readlen);
+ else
+ return 8.0*(readlen - 4.0*mdiff)/((float)readlen*readlen);
+ }
+
+ // This is crude empirical approximation and is not very accurate for
+ // shorter read lengths (<100bp). There certainly is a room for
+ // improvement.
+ const float mv[24][2] = { {0,0}, {0,0}, {0,0},
+ { 9.108, 4.934}, { 9.999, 3.991}, {10.273, 3.485}, {10.579, 3.160},
+ {10.828, 2.889}, {11.014, 2.703}, {11.028, 2.546}, {11.244, 2.391},
+ {11.231, 2.320}, {11.323, 2.138}, {11.403, 2.123}, {11.394, 1.994},
+ {11.451, 1.928}, {11.445, 1.862}, {11.516, 1.815}, {11.560, 1.761},
+ {11.544, 1.728}, {11.605, 1.674}, {11.592, 1.652}, {11.674, 1.613},
+ {11.641, 1.570} };
+
+ float m, v;
+ if ( dp>=24 )
+ {
+ m = readlen/8.;
+ if (dp>100) dp = 100;
+ v = 1.476/(0.182*pow(dp,0.514));
+ v = v*(readlen/100.);
+ }
+ else
+ {
+ m = mv[dp][0];
+ v = mv[dp][1];
+ m = m*readlen/100.;
+ v = v*readlen/100.;
+ v *= 1.2; // allow more variability
+ }
+ return 1.0/(v*sqrt(2*M_PI)) * exp(-0.5*((mdiff-m)/v)*((mdiff-m)/v));
+}
+
+void calc_vdb(bcf_callaux_t *bca, bcf_call_t *call)
+{
+ int i, dp = 0;
+ float mean_pos = 0, mean_diff = 0;
+ for (i=0; i<bca->npos; i++)
+ {
+ if ( !bca->alt_pos[i] ) continue;
+ dp += bca->alt_pos[i];
+ int j = i<bca->npos/2 ? i : bca->npos - i;
+ mean_pos += bca->alt_pos[i]*j;
+ }
+ if ( dp<2 )
+ {
+ call->vdb = -1;
+ return;
+ }
+ mean_pos /= dp;
+ for (i=0; i<bca->npos; i++)
+ {
+ if ( !bca->alt_pos[i] ) continue;
+ int j = i<bca->npos/2 ? i : bca->npos - i;
+ mean_diff += bca->alt_pos[i] * fabs(j - mean_pos);
+ }
+ mean_diff /= dp;
+ call->vdb = mean_diff_to_prob(mean_diff, dp, bca->npos);
+}
+
+/**
+ * bcf_call_combine() - sets the PL array and VDB, RPB annotations, finds the top two alleles
+ * @n: number of samples
+ * @calls: each sample's calls
+ * @bca: auxiliary data structure for holding temporary values
+ * @ref_base: the reference base
+ * @call: filled with the annotations
+ */
+int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call)