public static double BLAST_SpougeStoE(int y_, Blast_KarlinBlk kbp, Blast_GumbelBlk gbp, int m_, int n_) { // the score and lambda may have been rescaled. We will compute the scaling factor // and use it to scale a, alpha, and Sigma similarly. double scale_factor = kbp.Lambda / gbp.Lambda; // the pair-wise e-value must be scaled back to db-wise e-value double db_scale_factor = (gbp.db_length.HasValue) ? (double)gbp.db_length.Value / (double)n_ : 1.0; double lambda_ = kbp.Lambda; double k_ = kbp.K; double ai_hat_ = gbp.a * scale_factor; double bi_hat_ = gbp.b; double alphai_hat_ = gbp.Alpha * scale_factor; double betai_hat_ = gbp.Beta; double sigma_hat_ = gbp.Sigma * scale_factor; double tau_hat_ = gbp.Tau; /* here we consider symmetric matrix only */ double aj_hat_ = ai_hat_; double bj_hat_ = bi_hat_; double alphaj_hat_ = alphai_hat_; double betaj_hat_ = betai_hat_; /* this is 1/sqrt(2.0*PI) */ double const_val = 0.39894228040143267793994605993438; double m_li_y, vi_y, sqrt_vi_y, m_F, P_m_F; double n_lj_y, vj_y, sqrt_vj_y, n_F, P_n_F; double c_y, p1, p2, area; double e_value; m_li_y = m_ - (ai_hat_ * y_ + bi_hat_); vi_y = Math.Max(2.0 * alphai_hat_ / lambda_, alphai_hat_ * y_ + betai_hat_); sqrt_vi_y = Math.Sqrt(vi_y); m_F = m_li_y / sqrt_vi_y; P_m_F = AdvancedMath.Erfc(-m_F / Math.Sqrt(2.0)) / 2.0; p1 = m_li_y * P_m_F + sqrt_vi_y * const_val * Math.Exp(-0.5 * m_F * m_F); n_lj_y = n_ - (aj_hat_ * y_ + bj_hat_); vj_y = Math.Max(2.0 * alphaj_hat_ / lambda_, alphaj_hat_ * y_ + betaj_hat_); sqrt_vj_y = Math.Sqrt(vj_y); n_F = n_lj_y / sqrt_vj_y; P_n_F = AdvancedMath.Erfc(-n_F / Math.Sqrt(2.0)) / 2.0; p2 = n_lj_y * P_n_F + sqrt_vj_y * const_val * Math.Exp(-0.5 * n_F * n_F); c_y = Math.Max(2.0 * sigma_hat_ / lambda_, sigma_hat_ * y_ + tau_hat_); area = p1 * p2 + c_y * P_m_F * P_n_F; e_value = area * k_ * Math.Exp(-lambda_ * y_) * db_scale_factor; //ASSERT(e_value >= 0.0); return(e_value); }
public static double BLAST_KarlinStoE_simple(int score, Blast_KarlinBlk kbp, int searchsp) { var lambda = kbp.Lambda; var k = kbp.K; var h = kbp.H; if (lambda < 0.0 || k < 0.0 || h < 0.0) { return(-1); } return(searchsp * Math.Exp(-lambda * score + kbp.logK)); }