void ApplyClonalFilter (Mask& mask, std::vector<RegionalizedData *>& sliced_chip, const deque<float>& ppf, const deque<float>& ssq, const PolyclonalFilterOpts & opts) { clonal_filter filter; filter_counts counts; make_filter (filter, counts, ppf, ssq, opts); unsigned int numRegions = sliced_chip.size(); for (unsigned int rgn=0; rgn<numRegions; ++rgn) { RegionalizedData& local_patch = *sliced_chip[rgn]; int numWells = local_patch.GetNumLiveBeads(); int rowOffset = local_patch.region->row; int colOffset = local_patch.region->col; for (int well=0; well<numWells; ++well) { BeadParams& bead = local_patch.GetParams (well); bead_state& state = *bead.my_state; int row = rowOffset + bead.y; int col = colOffset + bead.x; if(mask.Match(col, row, MaskLib)) state.clonal_read = filter.is_clonal (state.ppf, state.ssq, opts.mixed_stringency); else if(mask.Match(col, row, MaskTF)) state.clonal_read = true; } } }
void ApplySuperMixedFilter (Mask& mask, std::vector<RegionalizedData *>& sliced_chip) { unsigned int numRegions = sliced_chip.size(); for (unsigned int rgn=0; rgn<numRegions; ++rgn) { RegionalizedData& local_patch = *sliced_chip[rgn]; int numWells = local_patch.GetNumLiveBeads(); int rowOffset = local_patch.region->row; int colOffset = local_patch.region->col; for (int well=0; well<numWells; ++well) { BeadParams& bead = local_patch.GetParams (well); bead_state& state = *bead.my_state; int row = rowOffset + bead.y; int col = colOffset + bead.x; if(mask.Match(col, row, MaskLib)) if (state.ppf < mixed_ppf_cutoff()) state.clonal_read = true; else state.clonal_read = false; else if(mask.Match(col, row, MaskTF)) state.clonal_read = true; } } }
void UpdateMask(Mask& mask, std::vector<RegionalizedData *>& sliced_chip) { unsigned int numRegions = sliced_chip.size(); for (unsigned int rgn=0; rgn<numRegions; ++rgn) { RegionalizedData& local_patch = *sliced_chip[rgn]; int numWells = local_patch.GetNumLiveBeads(); int rowOffset = local_patch.region->row; int colOffset = local_patch.region->col; for (int well=0; well<numWells; ++well) { BeadParams& bead = local_patch.GetParams (well); bead_state& state = *bead.my_state; // Record clonal reads in mask: int row = rowOffset + bead.y; int col = colOffset + bead.x; if(mask.Match(col, row, MaskLib)){ if(state.bad_read) mask.Set(col, row, MaskFilteredBadKey); else if(state.ppf >= mixed_ppf_cutoff()) mask.Set(col, row, MaskFilteredBadResidual); else if(not state.clonal_read) mask.Set(col, row, MaskFilteredBadPPF); } } } }
void BaseCallerLite::BasecallerWorker() { while (true) { deque<int> wellX; deque<int> wellY; deque<vector<float> > wellMeasurements; pthread_mutex_lock(&wellsAccessMutex); if (nextRegionY >= numRegionsY) { pthread_mutex_unlock(&wellsAccessMutex); return; } int currentRegionX = nextRegionX; int currentRegionY = nextRegionY; int currentRegion = currentRegionX + numRegionsX * currentRegionY; int beginY = currentRegionY * regionYSize; int beginX = currentRegionX * regionXSize; int endY = min((currentRegionY+1) * regionYSize,rows); int endX = min((currentRegionX+1) * regionXSize,cols); wellsPtr->SetChunk(beginY, endY-beginY, beginX, endX-beginX, 0, numFlows); wellsPtr->ReadWells(); for (int y = beginY; y < endY; y++) { for (int x = beginX; x < endX; x++) { if (!maskPtr->Match(x, y, MaskLib)) continue; wellX.push_back(x); wellY.push_back(y); wellMeasurements.push_back(vector<float>()); wellMeasurements.back().resize(numFlows); const WellData *w = wellsPtr->ReadXY(x, y); copy(w->flowValues, w->flowValues + numFlows, wellMeasurements.back().begin()); } } if (currentRegionX == 0) printf("% 5d/% 5d: ", currentRegionY*regionYSize, rows); if (wellX.size() == 0) printf(" "); else if (wellX.size() < 750) printf(". "); else if (wellX.size() < 1500) printf("o "); else if (wellX.size() < 2250) printf("# "); else printf("$ "); nextRegionX++; if (nextRegionX == numRegionsX) { nextRegionX = 0; nextRegionY++; printf("\n"); } fflush(NULL); pthread_mutex_unlock(&wellsAccessMutex); BasecallerRead currentRead; DPTreephaser dpTreephaser(flowOrder); dpTreephaser.SetModelParameters(CF, IE, 0); // Process the data deque<SFFEntry> libReads; deque<int>::iterator x = wellX.begin(); deque<int>::iterator y = wellY.begin(); deque<std::vector<float> >::iterator measurements = wellMeasurements.begin(); for (; x != wellX.end() ; x++, y++, measurements++) { if (!maskPtr->Match(*x, *y, (MaskType)(MaskLib|MaskKeypass), MATCH_ALL)) continue; libReads.push_back(SFFEntry()); SFFEntry& readResults = libReads.back(); stringstream wellNameStream; wellNameStream << runId << ":" << (*y) << ":" << (*x); readResults.name = wellNameStream.str(); readResults.clip_qual_left = 4; // TODO readResults.clip_qual_right = 0; readResults.clip_adapter_left = 0; readResults.clip_adapter_right = 0; readResults.flowgram.resize(numFlows); int minReadLength = 8; // TODO currentRead.SetDataAndKeyNormalize(&(measurements->at(0)), numFlows, &libKeyFlows[0], libNumKeyFlows - 1); dpTreephaser.NormalizeAndSolve5(currentRead, numFlows); // sliding window adaptive normalization readResults.n_bases = 0; for (int iFlow = 0; iFlow < numFlows; iFlow++) { readResults.flowgram[iFlow] = 100 * currentRead.solution[iFlow]; readResults.n_bases += currentRead.solution[iFlow]; } if(readResults.n_bases < minReadLength) { libReads.pop_back(); continue; } bool isFailKeypass = false; for (int iFlow = 0; iFlow < (libNumKeyFlows-1); iFlow++) if (libKeyFlows[iFlow] != currentRead.solution[iFlow]) isFailKeypass = true; if(isFailKeypass) { libReads.pop_back(); continue; } readResults.flow_index.reserve(readResults.n_bases); readResults.bases.reserve(readResults.n_bases); readResults.quality.reserve(readResults.n_bases); unsigned int prev_used_flow = 0; for (int iFlow = 0; iFlow < numFlows; iFlow++) { for (hpLen_t hp = 0; hp < currentRead.solution[iFlow]; hp++) { readResults.flow_index.push_back(1 + iFlow - prev_used_flow); readResults.bases.push_back(flowOrder[iFlow]); readResults.quality.push_back(20); // BaseCallerLite is stripped of QV generator prev_used_flow = iFlow + 1; } } } libSFF.WriteRegion(currentRegion,libReads); } }
void BFReference::FilterRegionOutliers(Image &bfImg, Mask &mask, float iqrThreshold, int rowStart, int rowEnd, int colStart, int colEnd) { const RawImage *raw = bfImg.GetImage(); /* Figure out how many wells are not pinned/excluded right out of the gate. */ int okCount = 0; for (int r = rowStart; r < rowEnd; r++) { for (int c = colStart; c < colEnd; c++) { int idx = r * raw->cols + c; if (!mask.Match(c, r, MaskPinned) && !mask.Match(c,r,MaskExclude) && mWells[idx] != Exclude && mWells[idx] != Filtered) { okCount++; } } } /* If not enough, just mark them all as bad. */ if (okCount <= MIN_OK_WELLS || (mNumEmptiesPerRegion > 0 && okCount < mNumEmptiesPerRegion)) { for (int r = rowStart; r < rowEnd; r++) { for (int c = colStart; c < colEnd; c++) { mWells[r * raw->cols + c] = Exclude; } } return; } // Make a mtrix for our region mTraces.set_size(okCount, raw->frames ); // wells in row major order by frames int count = 0; vector<int> mapping(mTraces.n_rows, -1); for (int r = rowStart; r < rowEnd; r++) { for (int c = colStart; c < colEnd; c++) { int idx = r * raw->cols + c; if (!mask.Match(c, r, MaskPinned) && !mask.Match(c,r,MaskExclude) && mWells[idx] != Exclude && mWells[idx] != Filtered) { for (int f = 0; f < raw->frames; f++) { mTraces.at(count,f) = bfImg.At(r,c,f) - bfImg.At(r,c,0); } mapping[count++] = r * raw->cols + c; } } } for (size_t r = 0; r < mTraces.n_rows; r++) { for (size_t c = 0; c < mTraces.n_cols; c++) { assert(isfinite(mTraces.at(r,c))); } } assert(mapping.size() == (size_t)count); /* Subtract off the median. */ fmat colMed = median(mTraces); frowvec colMedV = colMed.row(0); for(size_t i = 0; i < mTraces.n_rows; i++) { mTraces.row(i) = mTraces.row(i) - colMedV; } /* Get the quantiles of the mean difference for well and exclude outliers */ fmat mad = mean(mTraces, 1); fvec madV = mad.col(0); mMadSample.Clear(); mMadSample.Init(1000); mMadSample.AddValues(madV.memptr(), madV.n_elem); float minVal = mMadSample.GetQuantile(.25) - iqrThreshold * mMadSample.GetIQR(); float maxVal = mMadSample.GetQuantile(.75) + iqrThreshold * mMadSample.GetIQR(); for (size_t i = 0; i < madV.n_rows; i++) { if (madV[i] <= minVal || madV[i] >= maxVal) { mWells[mapping[i]] = Filtered; } } }