use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class GridMassTask method run.
/**
* @see Runnable#run()
*/
public void run() {
Format mzFormat = MZmineCore.getConfiguration().getMZFormat();
Format timeFormat = MZmineCore.getConfiguration().getRTFormat();
setStatus(TaskStatus.PROCESSING);
logger.info("Started GRIDMASS v1.0 [Apr-09-2014] on " + dataFile);
scans = scanSelection.getMatchingScans(dataFile);
scanNumbers = scanSelection.getMatchingScanNumbers(dataFile);
totalScans = scans.length;
// Check if we have any scans
if (totalScans == 0) {
setStatus(TaskStatus.ERROR);
setErrorMessage("No scans match the selected criteria");
return;
}
// Check if the scans are properly ordered by RT
double prevRT = Double.NEGATIVE_INFINITY;
for (Scan s : scans) {
if (s.getRetentionTime() < prevRT) {
setStatus(TaskStatus.ERROR);
final String msg = "Retention time of scan #" + s.getScanNumber() + " is smaller then the retention time of the previous scan." + " Please make sure you only use scans with increasing retention times." + " You can restrict the scan numbers in the parameters, or you can use the Crop filter module";
setErrorMessage(msg);
return;
}
prevRT = s.getRetentionTime();
}
// Create new feature list
newPeakList = new SimplePeakList(dataFile + " " + suffix, dataFile);
int j;
// minimumTimeSpan
Scan scan = scans[0];
double minRT = scan.getRetentionTime();
double maxRT = scan.getRetentionTime();
retentiontime = new double[totalScans];
int i;
for (i = 0; i < totalScans; i++) {
scan = scans[i];
double irt = scan.getRetentionTime();
if (irt < minRT)
minRT = irt;
if (irt > maxRT)
maxRT = irt;
retentiontime[i] = irt;
}
rtPerScan = (maxRT - minRT) / i;
// "tolerable" units in scans
tolScans = Math.max(2, (int) ((minimumTimeSpan / rtPerScan)));
maxTolScans = Math.max(2, (int) ((maximumTimeSpan / rtPerScan)));
// Algorithm to find masses:
// (1) copy masses:intensity > threshold
// (2) sort intensities descend
// (3) Find "spot" for each intensity
// (3.1) if they have not spot ID assigned
// (3.1.1) Extend mass in mass and time while > 70% pixels > threshold
// (3.1.2) If extension > mintime ==> mark all pixels with the spot ID
// (3.1.3) if extension < mintime ==> mark all pixels with spot ID = -1
// (4) Group spots within a time-tolerance and mass-tolerance
logger.info("Getting data points on " + dataFile);
roi = new Datum[totalScans][];
ArrayList<Datum> roiAL = new ArrayList<Datum>();
long passed = 0, nopassed = 0;
minMasa = Double.MAX_VALUE;
maxMasa = 0;
int maxJ = 0;
boolean[] scanOk = new boolean[totalScans];
Arrays.fill(scanOk, true);
logger.info("Smoothing data points on " + dataFile + " (Time min=" + smoothTimeSpan + "; Time m/z=" + smoothTimeMZ + ")");
IndexedDataPoint[][] data = smoothDataPoints(dataFile, smoothTimeSpan, smoothTimeMZ, 0, smoothMZ, 0, minimumHeight);
logger.info("Determining intensities (mass sum) per scan on " + dataFile);
for (i = 0; i < totalScans; i++) {
if (isCanceled())
return;
scan = scans[i];
// scan.getDataPoints();
IndexedDataPoint[] mzv = data[i];
double prev = (mzv.length > 0 ? mzv[0].datapoint.getMZ() : 0);
double massSum = 0;
for (j = 0; j < mzv.length; j++) {
if (mzv[j].datapoint.getIntensity() >= minimumHeight)
massSum += mzv[j].datapoint.getMZ() - prev;
prev = mzv[j].datapoint.getMZ();
if (mzv[j].datapoint.getMZ() < minMasa)
minMasa = mzv[j].datapoint.getMZ();
if (mzv[j].datapoint.getMZ() > maxMasa)
maxMasa = mzv[j].datapoint.getMZ();
}
double dm = 100.0 / (maxMasa - minMasa);
if (i % 30 == 0 && debug > 0) {
System.out.println("");
System.out.print("t=" + Math.round(retentiontime[i] * 100) / 100.0 + ": (in %) ");
}
if (scanOk[i]) {
if (!scanOk[i]) {
// Disable neighbouring scans, how many ?
for (j = i; j > 0 && retentiontime[j] + additionTimeMaxPeaksPerScan > retentiontime[i]; j--) {
scanOk[j] = false;
}
for (j = i; j < totalScans && retentiontime[j] - additionTimeMaxPeaksPerScan < retentiontime[i]; j++) {
scanOk[j] = false;
}
}
if (debug > 0)
System.out.print(((int) (massSum * dm)) + (scanOk[i] ? " " : "*** "));
} else {
if (debug > 0)
System.out.print(((int) (massSum * dm)) + (scanOk[i] ? " " : "* "));
}
setProcedure(i, totalScans, 1);
}
if (debug > 0)
System.out.println("");
String[] it = ignoreTimes.trim().split(", ?");
for (j = 0; j < it.length; j++) {
String[] itj = it[j].split("-");
if (itj.length == 2) {
Double a = Double.parseDouble(itj[0].trim());
Double b = Double.parseDouble(itj[1].trim());
for (i = Math.abs(Arrays.binarySearch(retentiontime, a)); i < totalScans && retentiontime[i] <= b; i++) {
if (retentiontime[i] >= a) {
scanOk[i] = false;
}
}
}
}
passed = 0;
nopassed = 0;
for (i = 0; i < totalScans; i++) {
if (i % 100 == 0 && isCanceled())
return;
if (scanOk[i]) {
scan = scans[i];
IndexedDataPoint[] mzv = data[i];
DataPoint[] mzvOriginal = scan.getDataPoints();
ArrayList<Datum> dal = new ArrayList<Datum>();
for (j = 0; j < mzv.length; j++) {
if (mzv[j].datapoint.getIntensity() >= minimumHeight) {
dal.add(new Datum(mzv[j].datapoint, i, mzvOriginal[mzv[j].index]));
passed++;
} else {
nopassed++;
}
}
if (j > maxJ)
maxJ = j;
roi[i] = dal.toArray(new Datum[0]);
roiAL.addAll(dal);
}
setProcedure(i, totalScans, 2);
}
logger.info(passed + " intensities >= " + minimumHeight + " of " + (passed + nopassed) + " (" + Math.round(passed * 10000.0 / (double) (passed + nopassed)) / 100.0 + "%) on " + dataFile);
// New "probing" algorithm
// (1) Generate probes all over chromatograms
// (2) Move each probe to their closest maximum until it cannot find a
// new maximum
// (3) assign spot id to each "center" using all points within region
// (1) Generate probes all over
double byMZ = Math.max(mzTol * 2, 1e-6);
int byScan = Math.max(1, tolScans / 4);
logger.info("Creating Grid of probes on " + dataFile + " every " + mzFormat.format(byMZ) + " m/z and " + byScan + " scans");
double m;
int ndata = (int) Math.round((((double) totalScans / (double) byScan) + 1) * ((maxMasa - minMasa + byMZ) / byMZ));
Probe[] probes = new Probe[ndata];
int idata = 0;
for (i = 0; i < totalScans; i += byScan) {
if (i % 100 == 0 && isCanceled())
return;
for (m = minMasa - (i % 2) * byMZ / 2; m <= maxMasa; m += byMZ) {
probes[idata++] = new Probe(m, i);
}
setProcedure(i, totalScans, 3);
}
// (2) Move each probe to their closest center
double mzR = byMZ / 2;
int scanR = Math.max(byScan - 1, 2);
logger.info("Finding local maxima for each probe on " + dataFile + " radius: scans=" + scanR + ", m/z=" + mzR);
int okProbes = 0;
for (i = 0; i < idata; i++) {
if (i % 100 == 0 && isCanceled())
return;
moveProbeToCenter(probes[i], scanR, mzR);
if (probes[i].intensityCenter < minimumHeight) {
probes[i] = null;
} else {
okProbes++;
}
setProcedure(i, idata, 4);
}
if (okProbes > 0) {
Probe[] pArr = new Probe[okProbes];
for (okProbes = i = 0; i < idata; i++) {
if (probes[i] != null) {
pArr[okProbes++] = probes[i];
}
}
probes = pArr;
pArr = null;
}
// (3) Assign spot id to each "center"
logger.info("Sorting probes " + dataFile);
Arrays.sort(probes);
logger.info("Assigning spot id to local maxima on " + dataFile);
SpotByProbes sbp = new SpotByProbes();
ArrayList<SpotByProbes> spots = new ArrayList<SpotByProbes>();
double mzA = -1;
int scanA = -1;
for (i = 0; i < probes.length; i++) {
if (probes[i] != null && probes[i].intensityCenter >= minimumHeight) {
if (probes[i].mzCenter != mzA || probes[i].scanCenter != scanA) {
if (i % 10 == 0 && isCanceled())
return;
if (sbp.size() > 0) {
spots.add(sbp);
sbp.assignSpotId();
// System.out.println(sbp.toString());
}
sbp = new SpotByProbes();
mzA = probes[i].mzCenter;
scanA = probes[i].scanCenter;
}
sbp.addProbe(probes[i]);
}
setProcedure(i, probes.length, 5);
}
if (sbp.size() > 0) {
spots.add(sbp);
sbp.assignSpotId();
// System.out.println(sbp.toString());
}
logger.info("Spots:" + spots.size());
// Assign specific datums to spots to avoid using datums to several
// spots
logger.info("Assigning intensities to local maxima on " + dataFile);
i = 0;
for (SpotByProbes sx : spots) {
if (sx.size() > 0) {
if (i % 100 == 0 && isCanceled())
return;
assignSpotIdToDatumsFromScans(sx, scanR, mzR);
}
setProcedure(i++, spots.size(), 6);
}
// (4) Join Tolerable Centers
logger.info("Joining tolerable maxima on " + dataFile);
int criticScans = Math.max(1, tolScans / 2);
int joins = 0;
for (i = 0; i < spots.size() - 1; i++) {
SpotByProbes s1 = spots.get(i);
if (s1.center != null && s1.size() > 0) {
if (i % 100 == 0 && isCanceled())
return;
for (j = i; j > 0 && j < spots.size() && spots.get(j - 1).center != null && spots.get(j - 1).center.mzCenter + mzTol > s1.center.mzCenter; j--) ;
for (; j < spots.size(); j++) {
SpotByProbes s2 = spots.get(j);
if (i != j && s2.center != null) {
if (s2.center.mzCenter - s1.center.mzCenter > mzTol)
break;
int l = Math.min(Math.abs(s1.minScan - s2.minScan), Math.abs(s1.minScan - s2.maxScan));
int r = Math.min(Math.abs(s1.maxScan - s2.minScan), Math.abs(s1.maxScan - s2.maxScan));
int d = Math.min(l, r);
boolean overlap = !(s2.maxScan < s1.minScan || s2.minScan > s1.maxScan);
if ((d <= criticScans || overlap) && (intensityRatio(s1.center.intensityCenter, s2.center.intensityCenter) > intensitySimilarity)) {
if (debug > 2)
System.out.println("Joining s1 id " + s1.spotId + "=" + mzFormat.format(s1.center.mzCenter) + " mz [" + mzFormat.format(s1.minMZ) + " ~ " + mzFormat.format(s1.maxMZ) + "] time=" + timeFormat.format(retentiontime[s1.center.scanCenter]) + " int=" + s1.center.intensityCenter + " with s2 id " + s2.spotId + "=" + mzFormat.format(s2.center.mzCenter) + " mz [" + mzFormat.format(s2.minMZ) + " ~ " + mzFormat.format(s2.maxMZ) + "] time=" + timeFormat.format(retentiontime[s2.center.scanCenter]) + " int=" + s2.center.intensityCenter);
assignSpotIdToDatumsFromSpotId(s1, s2, scanR, mzR);
s1.addProbesFromSpot(s2, true);
// restart
j = i;
joins++;
}
// }
}
}
}
setProcedure(i, spots.size(), 7);
}
logger.info("Joins:" + joins);
// (5) Remove "Large" spanned masses
logger.info("Removing long and comparable 'masses' on " + dataFile);
for (i = 0; i < spots.size() - 1; i++) {
SpotByProbes s1 = spots.get(i);
if (s1.center != null && s1.size() > 0) {
if (i % 100 == 0 && isCanceled())
return;
int totalScans = s1.maxScan - s1.minScan + 1;
int lScan = s1.minScan;
int rScan = s1.maxScan;
ArrayList<Integer> toRemove = new ArrayList<Integer>();
toRemove.add(i);
for (j = i; j > 0 && j < spots.size() && spots.get(j - 1).center != null && spots.get(j - 1).center.mzCenter + mzTol > s1.center.mzCenter; j--) ;
for (; j < spots.size(); j++) {
SpotByProbes s2 = spots.get(j);
if (i != j && s2.center != null) {
if (s2.center.mzCenter - s1.center.mzCenter > mzTol)
break;
if (intensityRatio(s1.center.intensityCenter, s2.center.intensityCenter) > intensitySimilarity) {
int dl = Math.min(Math.abs(lScan - s2.minScan), Math.abs(lScan - s2.maxScan));
int dr = Math.min(Math.abs(rScan - s2.minScan), Math.abs(rScan - s2.maxScan));
int md = Math.min(dl, dr);
if (md <= maxTolScans || !(s2.maxScan < lScan || s2.minScan > rScan)) {
// distancia tolerable o intersectan
totalScans += s2.maxScan - s2.minScan + 1;
toRemove.add(j);
lScan = Math.min(lScan, s2.minScan);
rScan = Math.max(rScan, s2.maxScan);
}
}
}
}
if (totalScans * rtPerScan > maximumTimeSpan) {
if (debug > 2)
System.out.println("Removing " + toRemove.size() + " masses around " + mzFormat.format(s1.center.mzCenter) + " m/z (" + s1.spotId + "), time " + timeFormat.format(retentiontime[s1.center.scanCenter]) + ", intensity " + s1.center.intensityCenter + ", Total Scans=" + totalScans + " (" + Math.round(totalScans * rtPerScan * 1000.0) / 1000.0 + " min).");
for (Integer J : toRemove) {
// System.out.println("Removing: "+spots.get(J).spotId);
spots.get(J).clear();
}
}
}
setProcedure(i, spots.size(), 8);
}
// Build peaks from assigned datums
logger.info("Building peak rows on " + dataFile + " (tolereance scans=" + tolScans + ")");
i = 0;
for (SpotByProbes sx : spots) {
if (sx.size() > 0 && sx.maxScan - sx.minScan + 1 >= tolScans) {
if (i % 100 == 0 && isCanceled())
return;
sx.buildMaxDatumFromScans(roi, minimumHeight);
if (sx.getMaxDatumScans() >= tolScans && (sx.getContigousMaxDatumScans() >= tolScans || sx.getContigousToMaxDatumScansRatio() > 0.5)) {
Chromatogram peak = new Chromatogram(dataFile, scanNumbers);
if (addMaxDatumFromScans(sx, peak) > 0) {
peak.finishChromatogram();
if (peak.getArea() > 1e-6) {
newPeakID++;
SimplePeakListRow newRow = new SimplePeakListRow(newPeakID);
newRow.addPeak(dataFile, peak);
newRow.setComment(sx.toString(retentiontime));
newPeakList.addRow(newRow);
if (debug > 0)
System.out.println("Peak added id=" + sx.spotId + " " + mzFormat.format(sx.center.mzCenter) + " mz, time=" + timeFormat.format(retentiontime[sx.center.scanCenter]) + ", intensity=" + sx.center.intensityCenter + ", probes=" + sx.size() + ", data scans=" + sx.getMaxDatumScans() + ", cont scans=" + sx.getContigousMaxDatumScans() + ", cont ratio=" + sx.getContigousToMaxDatumScansRatio() + " area = " + peak.getArea());
if (debug > 1) {
// Peak info:
System.out.println(sx.toString());
sx.printDebugInfo();
}
} else {
if (debug > 0)
System.out.println("Ignored by area ~ 0 id=" + sx.spotId + " " + mzFormat.format(sx.center.mzCenter) + " mz, time=" + timeFormat.format(retentiontime[sx.center.scanCenter]) + ", intensity=" + sx.center.intensityCenter + ", probes=" + sx.size() + ", data scans=" + sx.getMaxDatumScans() + ", cont scans=" + sx.getContigousMaxDatumScans() + ", cont ratio=" + sx.getContigousToMaxDatumScansRatio() + " area = " + peak.getArea());
}
}
} else {
if (debug > 0)
System.out.println("Ignored by continous criteria: id=" + sx.spotId + " " + mzFormat.format(sx.center.mzCenter) + " mz, time=" + timeFormat.format(retentiontime[sx.center.scanCenter]) + ", intensity=" + sx.center.intensityCenter + ", probes=" + sx.size() + ", data scans=" + sx.getMaxDatumScans() + ", cont scans=" + sx.getContigousMaxDatumScans() + ", cont ratio=" + sx.getContigousToMaxDatumScansRatio());
}
} else {
if (sx.size() > 0) {
if (debug > 0)
System.out.println("Ignored by time range criteria: id=" + sx.spotId + " " + mzFormat.format(sx.center.mzCenter) + " mz, time=" + timeFormat.format(retentiontime[sx.center.scanCenter]) + ", intensity=" + sx.center.intensityCenter + ", probes=" + sx.size() + ", data scans=" + sx.getMaxDatumScans() + ", cont scans=" + sx.getContigousMaxDatumScans() + ", cont ratio=" + sx.getContigousToMaxDatumScansRatio());
}
}
setProcedure(i++, spots.size(), 9);
}
logger.info("Peaks on " + dataFile + " = " + newPeakList.getNumberOfRows());
// Add new peaklist to the project
project.addPeakList(newPeakList);
// Add quality parameters to peaks
QualityParameters.calculateQualityParameters(newPeakList);
setStatus(TaskStatus.FINISHED);
logger.info("Finished chromatogram builder (RT) on " + dataFile);
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class GridMassTask method smoothDataPoints.
public IndexedDataPoint[][] smoothDataPoints(RawDataFile dataFile, double timeSpan, double timeMZSpan, int scanSpan, double mzTol, int mzPoints, double minimumHeight) {
int[] scanNumbers = dataFile.getScanNumbers(1);
int totalScans = scanNumbers.length;
// [relative scan][j value]
DataPoint[][] mzValues = null;
DataPoint[] mzValuesJ = null;
int[] mzValuesScan = null;
int[] mzValuesMZidx = null;
IndexedDataPoint[][] newMZValues = null;
IndexedDataPoint[] tmpDP = new IndexedDataPoint[0];
newMZValues = new IndexedDataPoint[totalScans][];
int i, j, si, sj, ii, k, ssi, ssj, m;
double timeSmoothingMZtol = Math.max(timeMZSpan, 1e-6);
int modts = Math.max(1, totalScans / 10);
for (i = 0; i < totalScans; i++) {
if (i % 100 == 0 && isCanceled())
return null;
// Smoothing in TIME space
Scan scan = dataFile.getScan(scanNumbers[i]);
double rt = retentiontime[i];
DataPoint[] xDP = null;
IndexedDataPoint[] iDP = null;
sj = si = i;
ssi = ssj = i;
int t = 0;
if (timeSpan > 0 || scanSpan > 0) {
if (scan != null) {
for (si = i; si > 1; si--) {
if (retentiontime[si - 1] < rt - timeSpan / 2) {
break;
}
}
for (sj = i; sj < totalScans - 1; sj++) {
if (retentiontime[sj + 1] >= rt + timeSpan / 2) {
break;
}
}
ssi = i - (scanSpan - 1) / 2;
ssj = i + (scanSpan - 1) / 2;
if (ssi < 0) {
ssj += -ssi;
ssi = 0;
}
if (ssj >= totalScans) {
ssi -= (ssj - totalScans + 1);
ssj = totalScans - 1;
}
if (sj - si + 1 < scanSpan) {
si = ssi;
sj = ssj;
}
}
if (scan != null && sj > si) {
// Allocate
if (mzValues == null || mzValues.length < sj - si + 1) {
mzValues = new DataPoint[sj - si + 1][];
mzValuesScan = new int[sj - si + 1];
mzValuesMZidx = new int[sj - si + 1];
}
// Load Data Points
for (j = si; j <= sj; j++) {
int jsi = j - si;
if (mzValues[jsi] == null || jsi >= mzValuesScan.length - 1 || mzValuesScan[jsi + 1] != scanNumbers[j]) {
Scan xscan = dataFile.getScan(scanNumbers[j]);
mzValues[jsi] = xscan.getDataPoints();
mzValuesScan[jsi] = scanNumbers[j];
} else {
mzValues[jsi] = mzValues[jsi + 1];
mzValuesScan[jsi] = mzValuesScan[jsi + 1];
}
mzValuesMZidx[jsi] = 0;
}
// Estimate Averages
ii = i - si;
if (tmpDP.length < mzValues[ii].length)
tmpDP = new IndexedDataPoint[mzValues[ii].length * 3 / 2];
for (k = 0; k < mzValues[ii].length; k++) {
DataPoint dp = mzValues[ii][k];
double mz = dp.getMZ();
double intensidad = 0;
if (dp.getIntensity() > 0) {
// only process those > 0
double a = 0;
short c = 0;
int f = 0;
for (j = 0; j <= sj - si; j++) {
for (mzValuesJ = mzValues[j]; mzValuesMZidx[j] < mzValuesJ.length - 1 && mzValuesJ[mzValuesMZidx[j] + 1].getMZ() < mz - timeSmoothingMZtol; mzValuesMZidx[j]++) ;
f = mzValuesMZidx[j];
for (m = mzValuesMZidx[j] + 1; m < mzValuesJ.length && mzValuesJ[m].getMZ() < mz + timeSmoothingMZtol; m++) {
if (Math.abs(mzValuesJ[m].getMZ() - mz) < Math.abs(mzValuesJ[f].getMZ() - mz)) {
f = m;
} else {
// parar la búsqueda
break;
}
}
if (f > 0 && f < mzValuesJ.length && Math.abs(mzValuesJ[f].getMZ() - mz) <= timeSmoothingMZtol && mzValuesJ[f].getIntensity() > 0) {
// >=
// minimumHeight
// ?
// System.out.println("mz="+mz+"; Closer="+mzValuesJ[f].getMZ()+", f="+f+",
// Intensity="+mzValuesJ[f].getIntensity());
a += mzValuesJ[f].getIntensity();
c++;
}
}
intensidad = c > 0 ? a / c : 0;
if (intensidad >= minimumHeight) {
tmpDP[t++] = new IndexedDataPoint(k, new SimpleDataPoint(mz, intensidad));
}
}
}
}
} else if (scan != null) {
xDP = scan.getDataPoints();
if (tmpDP.length < xDP.length)
tmpDP = new IndexedDataPoint[xDP.length];
for (k = 0; k < xDP.length; k++) {
if (xDP[k].getIntensity() >= minimumHeight) {
tmpDP[t++] = new IndexedDataPoint(k, xDP[k]);
}
}
}
iDP = new IndexedDataPoint[t];
for (k = 0; k < t; k++) {
iDP[k] = tmpDP[k];
}
newMZValues[i] = iDP;
setProcedure(i, totalScans, 0);
if (i % modts == 0) {
logger.info("Smoothing/Caching " + dataFile + "..." + (i / modts) * 10 + "%");
}
}
return newMZValues;
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class GridMassTask method massCenter.
double[] massCenter(int l, int r, double min, double max) {
double x = 0;
double y = 0;
double sum = 0;
double maxValue = 0;
for (int i = l; i <= r; i++) {
DataPoint[] mzs = getCachedDataPoints(scans[i].getScanNumber());
if (mzs != null) {
for (int j = findFirstMass(min, mzs); j < mzs.length; j++) {
double mass = mzs[j].getMZ();
if (mass >= min) {
if (mass <= max) {
double intensity = mzs[j].getIntensity();
if (intensity >= minimumHeight) {
x += i * intensity;
y += mass * intensity;
sum += intensity;
if (intensity > maxValue) {
maxValue = intensity;
}
}
} else {
break;
}
}
}
}
}
if (sum > 0.0) {
x /= sum;
y /= sum;
}
return new double[] { x, y, maxValue };
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class GridMassTask method findFirstMass.
static int findFirstMass(double mass, DataPoint[] mzValues) {
int l = 0;
int r = mzValues.length - 1;
int mid = 0;
while (l < r) {
mid = (r + l) / 2;
if (mzValues[mid].getMZ() > mass) {
r = mid - 1;
} else if (mzValues[mid].getMZ() < mass) {
l = mid + 1;
} else {
return mid;
}
}
while (l > 0 && mzValues[l].getMZ() > mass) l--;
return l;
}
use of net.sf.mzmine.datamodel.DataPoint in project mzmine2 by mzmine.
the class CsvReadTask method run.
@Override
public void run() {
setStatus(TaskStatus.PROCESSING);
Scanner scanner;
logger.setLevel(Level.ALL);
try {
scanner = new Scanner(file);
dataSource = getFileName(scanner);
if (dataSource == null) {
setErrorMessage("Could not open data file " + file.getAbsolutePath());
setStatus(TaskStatus.ERROR);
return;
}
logger.info("opening raw file " + dataSource);
String acquisitionDate = getAcqusitionDate(scanner);
if (acquisitionDate == null) {
setErrorMessage("Could not find acquisition date in file " + file.getAbsolutePath());
setStatus(TaskStatus.ERROR);
return;
}
logger.info("Date of acquisition " + acquisitionDate);
// scanner.useDelimiter(",");
List<String> mzsList = new ArrayList<String>();
String mstype = "";
String ions = "";
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
logger.fine("checking line: " + line + " for 'Time'...");
if (line.startsWith("Time")) {
String[] axes = line.split(",");
logger.fine("Found axes" + Arrays.toString(axes));
for (int i = 1; i < axes.length; i++) {
String axis = axes[i];
ions += axis + ", ";
if (axis.contains("->")) {
mstype = "MS/MS";
logger.fine("axis " + axis + " is an ms^2 scan");
String mz = axis.substring(axis.indexOf("-> ") + 3);
mz.trim();
logger.fine("Axis " + axis + " was scanned at m/z = '" + mz + "'");
mzsList.add(mz);
} else {
String mz = axis.replaceAll("[^0-9]", "");
logger.fine("axis " + axis + " was scanned at " + mz);
mzsList.add(mz);
}
}
break;
}
}
int[] mzs = new int[mzsList.size()];
for (int i = 0; i < mzsList.size(); i++) mzs[i] = Integer.valueOf(mzsList.get(i));
Range<Double> mzRange = Range.closed((double) mzs[0] - 10, (double) mzs[1] + 10);
int scanNumber = 1;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line == null || line.trim().equals(""))
continue;
String[] columns = line.split(",");
if (columns == null || columns.length != mzs.length + 1)
continue;
double rt = Double.valueOf(columns[0]) / 60;
DataPoint[] dataPoints = new SimpleDataPoint[mzs.length];
for (int i = 0; i < dataPoints.length; i++) {
String intensity = columns[i + 1];
dataPoints[i] = new SimpleDataPoint(mzs[i], Double.valueOf(intensity));
}
Scan scan = new SimpleScan(null, scanNumber, 1, rt, 0.0, 1, null, dataPoints, MassSpectrumType.CENTROIDED, PolarityType.POSITIVE, "ICP-" + mstype + " " + ions.substring(0, ions.length() - 2), mzRange);
newMZmineFile.addScan(scan);
scanNumber++;
}
finalRawDataFile = newMZmineFile.finishWriting();
project.addFile(finalRawDataFile);
} catch (Exception e) {
setErrorMessage(e.getMessage());
setStatus(TaskStatus.ERROR);
return;
}
this.setStatus(TaskStatus.FINISHED);
}
Aggregations