コード例 #1
0
ファイル: BFReference.cpp プロジェクト: golharam/TS
void BFReference::CalcSignalReference2(const std::string &datFile, const std::string &bgFile,
				      Mask &mask, int traceFrame) {
  Image bfImg;
  Image bfBkgImg;
  bfImg.SetImgLoadImmediate (false);
  bfBkgImg.SetImgLoadImmediate (false);
  bool loaded = bfImg.LoadRaw(datFile.c_str());
  bool bgLoaded = bfBkgImg.LoadRaw(bgFile.c_str());
  if (!loaded) {
    ION_ABORT("*Error* - No beadfind file found, did beadfind run? are files transferred?  (" + datFile + ")");
  }
  if (!bgLoaded) {
    ION_ABORT("*Error* - No beadfind background file found, did beadfind run? are files transferred?  (" + bgFile + ")");
  }
  const RawImage *raw = bfImg.GetImage();
  
  assert(raw->cols == GetNumCol());
  assert(raw->rows == GetNumRow());
  assert(raw->cols == mask.W());
  assert(raw->rows == mask.H());
  int StartFrame = bfImg.GetFrame(-663); //5
  int EndFrame = bfImg.GetFrame(350); //20
  int NNinnerx = 1, NNinnery = 1, NNouterx = 12, NNoutery = 8;
  cout << "DC start frame: " << StartFrame << " end frame: " << EndFrame << endl;
  bfImg.FilterForPinned(&mask, MaskEmpty, false);
  bfImg.XTChannelCorrect();
  // bfImg.XTChannelCorrect(&mask);
  Traces trace;  
  trace.Init(&bfImg, &mask, FRAMEZERO, FRAMELAST, FIRSTDCFRAME,LASTDCFRAME);
  bfImg.Normalize(StartFrame, EndFrame);
  if (mDoRegionalBgSub) {
     trace.SetMeshDist(0);
  }
  trace.CalcT0(true);
  if (mDoRegionalBgSub) {
    GridMesh<float> grid;
    grid.Init(raw->rows, raw->cols, mRegionYSize, mRegionXSize);
    int numBin = grid.GetNumBin();
    int rowStart = -1, rowEnd = -1, colStart = -1, colEnd = -1;
    for (int binIx = 0; binIx < numBin; binIx++) {
      cout << "BG Subtract Region: " << binIx << endl;
      grid.GetBinCoords(binIx, rowStart, rowEnd, colStart, colEnd);
      Region reg;
      reg.row = rowStart;
      reg.h = rowEnd - rowStart;
      reg.col = colStart;
      reg.w = colEnd - colStart;
      bfImg.BackgroundCorrectRegion(&mask, reg, MaskAll, MaskEmpty, NNinnerx, NNinnery, NNouterx, NNoutery, NULL);
    }
  }
  else {
    bfImg.BackgroundCorrect(&mask, MaskEmpty, MaskEmpty, NNinnerx, NNinnery, NNouterx, NNoutery, NULL);
  }
  int length = GetNumRow() * GetNumCol();
  mBfMetric.resize(length, std::numeric_limits<double>::signaling_NaN());
  for (int wIx = 0; wIx < length; wIx++) {
    if (mask[wIx] & MaskExclude || mask[wIx] & MaskPinned) 
      continue;
    int t0 = (int)trace.GetT0(wIx);
    mBfMetric[wIx] = 0;
    float zSum  = 0;
    int count = 0;
    for (int fIx = min(t0-20, 0); fIx < t0-10; fIx++) {
      zSum += bfImg.At(wIx,fIx);
      count ++;
    }
    for (int fIx = t0+3; fIx < t0+15; fIx++) {
      mBfMetric[wIx] += (bfImg.At(wIx,fIx) - (zSum / count));
    }
  }
  bfImg.Close();
  for (int i = 0; i < length; i++) {
    if (mask[i] & MaskExclude || mWells[i] == Exclude) {
      mWells[i] = Exclude;
    }
    else {
      mask[i] = MaskIgnore;
    }
  }
  cout << "Filling reference. " << endl;
  FillInReference(mWells, mBfMetric, mGrid, mMinQuantile, mMaxQuantile, mNumEmptiesPerRegion);
  for (int i = 0; i < length; i++) {
    if (mWells[i] == Reference) {
      mask[i] = MaskEmpty;
    }
  }
}
コード例 #2
0
ファイル: GetTraces.cpp プロジェクト: Jorges1000/TS
int main(int argc, char *argv[])
{
	int numCycles = 2;//int numCycles = 25;
	int numFlows = numCycles*4;
	int numFrames = 100;
	int hasWashFlow = 1;

    int n; // number of elements read

	// inputs:
	//    beadmask
	//    ignore mask
	//    target mask
	//    weka vector

	// for each flow:
	// X.  memmap acq file
	// 1.  save raw traces for each target bead
	// 2.  calc and save nsub trace for each target bead

	// for each target bead:
	// X.  write out all raw traces for all flows
	// 1.  write out (raw - nsub) for all flows
	// 2.  write out (raw - nsub) - (0-mer[nuc] - nsub) for all flows
	// 3.  write out weka*(raw - nsub) for all flows

	// char *beadMaskName = "Beads300x300";
	// char *targetMaskName = "Golden";
	// char *wekaVector = NULL;
	char *coordFile = "/home/ion/JD1114.blastn.coords";

	// set a few defaults
	char *dirExt = ".";

	// process cmd line args
	int argcc = 1;
	while (argcc < argc) {
		switch (argv[argcc][1]) {
			case 'd': // directory to pull raw data from
				argcc++;
				dirExt = argv[argcc];
			break;

			case 'f': // coord file
				argcc++;
				coordFile = argv[argcc];
			break;
		}
		argcc++;
	}

        // crazy, but only way to get rows/cols right now is from mask.
        Mask mask(1,1);
	char maskPath[MAX_PATH_LENGTH];
	sprintf(maskPath, "%s/bfmask.bin", dirExt);
        // mask.SetMask(maskPath);
        mask.SetMask("/results/analysis/output/IonEast/Flux_v.029_VZ_069/bfmask.bin");

	// for each flow, make an avg trace from all row/col's requested

	int flow;
	char acqFileName[MAX_PATH_LENGTH];
	char *acqPrefix = "acq_";
	Image   img;
	img.SetMaxFrames(numFrames);
	int frame;
	double beadTrace[numFlows][numFrames];
	memset(beadTrace, 0, sizeof(beadTrace));
	for(flow=0;flow<numFlows;flow++) {
		Mask localMask(&mask);
		sprintf(acqFileName, "%s/%s%04d.dat", dirExt, acqPrefix, (flow + (flow/4)*hasWashFlow));
		img.LoadRaw(acqFileName);
		img.FilterForPinned(&localMask, MaskEmpty);
		img.Normalize(0, 5);
		img.BackgroundCorrect(&localMask, MaskBead, MaskEmpty, 2, 5, NULL);
		//img.BackgroundCorrectMulti(&localMask, MaskBead, MaskEmpty, 2, 5, NULL);

		const RawImage *raw = img.GetImage();
		int frameStride = raw->rows*raw->cols;

		// FILE *fp = fopen("/home/ion/aligReadsPlusCords", "r");
		FILE *fp = fopen(coordFile, "r");
		char line[512];
		int row, col;
		int count = 0;
		while(fgets(line, sizeof(line), fp) != NULL) {
			n = sscanf(line, "%d %d", &row, &col);
            assert(n==2);
			count++;
			for(frame=0;frame<numFrames;frame++) {
				beadTrace[flow][frame] += raw->image[col+row*raw->cols+frame*frameStride];
			}
		}
		fclose(fp);
		for(frame=0;frame<numFrames;frame++) {
			beadTrace[flow][frame] /= count;
		}
	}

	// now dump the avg traces
	for(flow=0;flow<numFlows;flow++) {
		for(frame=0;frame<numFrames;frame++) {
			printf("%.2lf\t", beadTrace[flow][frame]);
		}
		printf("\n");
	}
	printf("Done.\n");

#ifdef NOT_USED_RIGHT_NOW

	// char *ignoreMaskName = "Exclude300x300";
	char *ignoreMaskName = "all2430";

	// read in masks
	Mask targetMask(1348, 1152);
	int numBeads = 5;
	// LoadMask(targetMask, targetMaskName, 1, MaskBead);
	LoadMask(targetMask, ignoreMaskName, 1, MaskIgnore);
/*
	sprintf (fileName, "%s/%s", dirExt, targetMaskName);
	int numBeads = LoadMask(targetMask, fileName, 1, MaskBead);
	if (numBeads <= 0)
	{
		fprintf (stderr, "No beads loaded!\n\n");
		return (1);
	}
	sprintf (fileName, "%s/%s", dirExt, ignoreMaskName);
	LoadMask(targetMask, fileName, 1, MaskIgnore);
*/
	int ox = 1348/2-50;
	int oy = 1150/2 - 450;

	targetMask[(5+oy)*1348 + 19+ox] |= MaskBead;
	targetMask[(10+oy)*1348 + 17+ox] |= MaskBead;
	targetMask[(20+oy)*1348 + 66+ox] |= MaskBead;
	targetMask[(27+oy)*1348 + 18+ox] |= MaskBead;
	targetMask[(30+oy)*1348 + 69+ox] |= MaskBead;

	printf("Analyzing %d beads.\n", numBeads);

	// make array of bead XY positions
	int *beadx = new int[numBeads];
	int *beady = new int[numBeads];
	int i;
	int k = 0;
	int x, y;
	for(i=0;i<targetMask.W()*targetMask.H();i++) {
		x = i%targetMask.W();
		y = i/targetMask.W();
		if (targetMask[i] &  MaskBead) {
			beadx[k] = i%targetMask.W();
			beady[k] = i/targetMask.W();
printf("Bead %d as (%d,%d)\n", k, beadx[k], beady[k]);
			k++;
		}
	}

printf("Allocating...\n");
	// allocate trace storage
	short **beadTrace = new short *[numBeads*numFlows];
	for(i=0;i<numBeads*numFlows;i++)
		beadTrace[i] = new short[numFrames];

	// loop through all flows
	// load up acq file and extract nsub data for the target beads
	int flow;
	char acqFileName[MAX_PATH_LENGTH];
	char *acqPrefix = "acq_";
	Image   img;
	img.SetMaxFrames(numFrames);
	int bead;
	int frame;
	for(flow=0;flow<numFlows;flow++) {
		Mask localMask(&targetMask);
		sprintf(acqFileName, "%s/%s%04d.dat", dirExt, acqPrefix, (flow + (flow/4)*hasWashFlow));
		img.LoadRaw(acqFileName);
		img.FilterForPinned(&localMask, MaskEmpty);
		img.Normalize(0, 5);
		img.BackgroundCorrect(&localMask, MaskBead, MaskEmpty, 2, 5, NULL);

		// save off our nsub traces
printf("Saving bead nsub traces...\n");
		const RawImage *raw = img.GetImage();
		int frameStride = raw->rows*raw->cols;
		for(bead=0;bead<numBeads;bead++) {
			for(frame=0;frame<numFrames;frame++) {
				beadTrace[bead+flow*numBeads][frame] = raw->image[beadx[bead]+beady[bead]*raw->cols+frame*frameStride];
			}
		}
	}

	// int zeromer[4] = {0, 5, 2, 3}; // TF key: ATCG
	 int zeromer[4] = {4, 1, 6, 3}; // TF key: TCAG
	short *beadzero = new short[numFrames];

	// write out bead raw 0-mer subtracted data
	for(bead=0;bead<numBeads;bead++) {
		printf("Bead: %d at (%d,%d)\n", bead, beadx[bead], beady[bead]);
		for(flow=0;flow<numFlows;flow++) {
			// first generate a smoothed 0-mer to subtract from... (grossly inefficient since I really only need to calc this 4 times)
			img.SGFilterApply(beadTrace[bead+zeromer[flow%4]*numBeads], beadzero);
			for(frame=0;frame<numFrames;frame++) {
				printf("%d", beadTrace[bead+flow*numBeads][frame] - beadzero[frame]);
				if (frame < (numFrames-1))
					printf(",");
			}
			printf("\n");
		}
	}

	// write out bead smoothed 0-mer subtracted data
	short *smoothedTraceIn = new short[numFrames];
	short *smoothedTraceOut = new short[numFrames];
	for(bead=0;bead<numBeads;bead++) {
		printf("Smoothed Bead: %d at (%d,%d)\n", bead, beadx[bead], beady[bead]);
		for(flow=0;flow<numFlows;flow++) {
			// first generate a smoothed 0-mer to subtract from... (grossly inefficient since I really only need to calc this 4 times)
			img.SGFilterSet(2, 1); // mild smoothing
			img.SGFilterApply(beadTrace[bead+zeromer[flow%4]*numBeads], beadzero);

			// now smooth the 0-mer subtracted trace
			for(frame=0;frame<numFrames;frame++) {
				smoothedTraceIn[frame] = beadTrace[bead+flow*numBeads][frame] - beadzero[frame];
			}
			img.SGFilterSet(4, 2); // moderate smoothing
			img.SGFilterApply(smoothedTraceIn, smoothedTraceOut);

			// now plot the trace
			for(frame=0;frame<numFrames;frame++) {
				printf("%d", smoothedTraceOut[frame]);
				if (frame < (numFrames-1))
					printf(",");
			}
			printf("\n");
		}
	}

	// load up our weka filter
	float *filterVec = new float[numFrames];
	FILE *fp = fopen_s(&fp, "Weights.csv", "r");
	if (fp) {
		char line[4096];
		// skip the first line
		fgets(line, sizeof(line), fp);
		// skip the first 3 values
		float dummy;
		n = fscanf(fp, "%f,", &dummy);
        assert(n==1);
		n = fscanf(fp, "%f,", &dummy);
        assert(n==1);
		n = fscanf(fp, "%f,", &dummy);
        assert(n==1);
		// read in the vector
		for(frame=0;frame<numFrames;frame++) {
			n = fscanf(fp, "%f,", &filterVec[frame]);
            assert(n==1);
		}
		fclose(fp);
		printf("Filter vector:\n");
		for(frame=0;frame<numFrames;frame++) {
			printf("%.3f ", filterVec[frame]);
		}
		printf("\n");
	}

	// write out weka-vector applied & smoothed traces
	float *filteredTraceIn = new float[numFrames];
	float *filteredTraceOut = new float[numFrames];
	for(bead=0;bead<numBeads;bead++) {
		printf("Filtered Bead: %d at (%d,%d)\n", bead, beadx[bead], beady[bead]);
		for(flow=0;flow<numFlows;flow++) {
			// first generate a smoothed 0-mer to subtract from... (grossly inefficient since I really only need to calc this 4 times)
			img.SGFilterSet(2, 1); // mild smoothing
			img.SGFilterApply(beadTrace[bead+zeromer[flow%4]*numBeads], beadzero);

			// now filter the 0-mer subtracted trace
			for(frame=0;frame<numFrames;frame++) {
				smoothedTraceIn[frame] = beadTrace[bead+flow*numBeads][frame] - beadzero[frame];
				filteredTraceIn[frame] = smoothedTraceIn[frame] * filterVec[frame];
			}

			// now smooth the filtered 0-mer subtracted trace
			img.SGFilterSet(4, 2); // moderate smoothing
			img.SGFilterApply(filteredTraceIn, filteredTraceOut);

			// now plot the trace
			for(frame=0;frame<numFrames;frame++) {
				printf("%.3f", filteredTraceOut[frame]);
				if (frame < (numFrames-1))
					printf(",");
			}
			printf("\n");
		}
	}

	// cleanups
	delete [] beadzero;
	delete [] smoothedTraceIn;
	delete [] smoothedTraceOut;
	delete [] filterVec;
	delete [] filteredTraceIn;
	delete [] filteredTraceOut;
    delete [] beadx;
    delete [] beady;
#endif /* NOT_USED_RIGHT_NOW */
}
コード例 #3
0
ファイル: BFReference.cpp プロジェクト: golharam/TS
void BFReference::CalcReference(const std::string &datFile, Mask &mask, std::vector<float> &metric) {
  Image bfImg;
  bfImg.SetImgLoadImmediate (false);
  bool loaded = bfImg.LoadRaw(datFile.c_str());
  if (!loaded) {
    ION_ABORT("*Error* - No beadfind file found, did beadfind run? are files transferred?  (" + datFile + ")");
  }

  const RawImage *raw = bfImg.GetImage();
  
  assert(raw->cols == GetNumCol());
  assert(raw->rows == GetNumRow());
  assert(raw->cols == mask.W());
  assert(raw->rows == mask.H());
  if (!mDebugFile.empty()) {
    DebugTraces(mDebugFile, mask, bfImg);
  }
  bfImg.FilterForPinned(&mask, MaskEmpty, false);
  // int StartFrame= bfImg.GetFrame((GetDcStart()*1000/15)-1000);
  // int EndFrame = bfImg.GetFrame((GetDcEnd()*1000/15)-1000);
  int StartFrame = bfImg.GetFrame(-663); //5
  int EndFrame = bfImg.GetFrame(350); //20
  cout << "DC start frame: " << StartFrame << " end frame: " << EndFrame << endl;
  bfImg.XTChannelCorrect();
  FilterForOutliers(bfImg, mask, mIqrOutlierMult, mRegionYSize, mRegionXSize);
  bfImg.Normalize(StartFrame, EndFrame);
  // bfImg.XTChannelCorrect(&mask);

  int NNinnerx = 1, NNinnery = 1, NNouterx = 12, NNoutery = 8;
  if (mDoRegionalBgSub) {
    GridMesh<float> grid;
    grid.Init(raw->rows, raw->cols, mRegionYSize, mRegionXSize);
    int numBin = grid.GetNumBin();
    int rowStart = -1, rowEnd = -1, colStart = -1, colEnd = -1;
    for (int binIx = 0; binIx < numBin; binIx++) {
      grid.GetBinCoords(binIx, rowStart, rowEnd, colStart, colEnd);
      Region reg;
      reg.row = rowStart;
      reg.h = rowEnd - rowStart;
      reg.col = colStart;
      reg.w = colEnd - colStart;
      bfImg.BackgroundCorrectRegion(&mask, reg, MaskAll, MaskEmpty, NNinnerx, NNinnery, NNouterx, NNoutery, NULL);
    }
  }
  else {
    bfImg.BackgroundCorrect(&mask, MaskEmpty, MaskEmpty, NNinnerx, NNinnery, NNouterx, NNoutery, NULL);
  }
  Region region;
  region.col = 0;
  region.row = 0;
  region.w = GetNumCol(); //mGrid.GetColStep();
  region.h = GetNumRow(); // mGrid.GetRowStep();

  int startFrame = bfImg.GetFrame(12); // frame 15 on uncompressed 314
  //  int endFrame = bfImg.GetFrame(raw->timestamps[bfImg.Ge]5300); // frame 77 or so
  int endFrame = bfImg.GetFrame(5000); // frame 77 or so
  bfImg.CalcBeadfindMetric_1(&mask, region, "pre", startFrame, endFrame);
  const double *results = bfImg.GetResults();

  int length = GetNumRow() * GetNumCol();
  metric.resize(length);
  copy(&results[0], &results[0] + (length), metric.begin());
  bfImg.Close();
}