// g++ -O3 -Wall compute_qvalues.cpp -I/home/pl88/boost_1_58_0/install/include -o compute_qvalues #include #include #include #include #include #include #include using namespace std; int main(void) { boost::math::chi_squared chisq_dist(1); string line; int lineCtr = 0; vector < vector > lineTokens; vector chisq(1, NAN), pVal(1, NAN), pValMax(1, NAN); // NAN for header; N+1 entries vector < pair > pValMaxInd[2]; // no header; N=Nneg+Npos entries (p, 1-based index) while (getline(cin, line)) { istringstream iss(line); vector tokens; string token; while (iss >> token) tokens.push_back(token); lineTokens.push_back(tokens); if (lineCtr > 0) { double betaMarginal; sscanf(tokens[3].c_str(), "%lf", &betaMarginal); double pMarginal; sscanf(tokens[7].c_str(), "%lf", &pMarginal); double chi2; sscanf(tokens[10].c_str(), "%lf", &chi2); chisq.push_back(chi2); double p = boost::math::cdf(complement(chisq_dist, chi2)); pVal.push_back(p); double pMax = max(pMarginal, p); pValMax.push_back(pMax); pValMaxInd[betaMarginal>0].push_back(make_pair(pMax, lineCtr)); } lineCtr++; } vector qVal(lineCtr, NAN); // NAN for header; N+1 entries for (int dir = 0; dir < 2; dir++) { sort(pValMaxInd[dir].begin(), pValMaxInd[dir].end()); int Ndir = pValMaxInd[dir].size(); vector qValIndSort(Ndir); for (int i = 0; i < Ndir; i++) { qValIndSort[i] = pValMaxInd[dir][i].first * Ndir / (i+1); for (int i0 = 0; i0 < i; i0++) if (qValIndSort[i0] > qValIndSort[i]) qValIndSort[i0] = qValIndSort[i]; } for (int i = 0; i < Ndir; i++) qVal[pValMaxInd[dir][i].second] = qValIndSort[i]; } for (int i = 0; i < lineCtr; i++) { for (int j = 0; j < 10; j++) cout << lineTokens[i][j] << " "; if (i == 0) cout << "CHISQ_F PVAL_F PVAL_MAX P_M_BONF QVAL_MAX"; else printf("%.2f %.1E %.1E %.2g %.2g", chisq[i], pVal[i], pValMax[i], pValMax[i]*(lineCtr-1), qVal[i]); for (int j = 11; j < (int) lineTokens[i].size(); j++) cout << " " << lineTokens[i][j]; cout << endl; } return 0; }