/** * Collapsing @param in (people by marker) to @param out (people by marker), * if @param freqIn is empty, then frequncy is calculated from @param in * or according to @param freqIn to rearrange columns of @param in. * Reordered frequency are stored in @param freqOut, in ascending order */ void rearrangeGenotypeByFrequency(Matrix& in, const std::vector<double>& freqIn, Matrix* out, std::vector<double>* freqOut) { std::map<double, std::vector<int> > freqGroup; std::map<double, std::vector<int> >::const_iterator freqGroupIter; if (freqIn.empty()) { getMarkerFrequency(in, freqOut); groupFrequency(*freqOut, &freqGroup); } else { groupFrequency(freqIn, &freqGroup); } Matrix& sortedGenotype = *out; sortedGenotype.Dimension(in.rows, freqGroup.size()); sortedGenotype.Zero(); freqOut->clear(); int idx = 0; for (freqGroupIter = freqGroup.begin(); freqGroupIter != freqGroup.end(); freqGroupIter++) { freqOut->push_back(freqGroupIter->first); const std::vector<int>& cols = freqGroupIter->second; for (size_t j = 0; j != cols.size(); ++j) { for (int i = 0; i < in.rows; ++i) { sortedGenotype[i][cols[j]] += in[i][cols[j]]; } } ++idx; } }
void makeVariableThreshodlGenotype( Matrix& in, Matrix* out, std::vector<double>* freqOut, void (*collapseFunc)(Matrix&, const std::vector<int>&, Matrix*, int)) { std::vector<double> freqIn; getMarkerFrequency(in, &freqIn); makeVariableThreshodlGenotype(in, freqIn, out, freqOut, collapseFunc); }
void fpCollapse(Matrix& in, Matrix* out) { assert(out); int& numPeople = in.rows; int numMarker = in.cols; out->Dimension(numPeople, 1); out->Zero(); for (int m = 0; m < numMarker; m++) { // calculate weight double freq = getMarkerFrequency(in, m); if (freq <= 0.0 || freq >= 1.0) continue; // avoid freq == 1.0 double weight = 1.0 / sqrt(freq * (1.0 - freq)); // fprintf(stderr, "freq = %f\n", freq); for (int p = 0; p < numPeople; p++) { (*out)[p][0] += in[p][m] * weight; } } }
void getMarkerFrequency(Matrix& in, std::vector<double>* freq) { freq->resize(in.cols); for (int i = 0; i < in.cols; ++i) { (*freq)[i] = getMarkerFrequency(in, i); } }