TxtImportAlgorithm { private static @Nonnull SimpleMsSpectrum parseMsSpectrum(@Nonnull Scanner scanner) { double mzValues[] = new double[16]; float intensityValues[] = new float[16]; int size = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); Matcher m = linePattern.matcher(line); if (!m.find()) break; String mzString = m.group(1); String intensityString = m.group(3); double mz = Double.parseDouble(mzString); float intensity = Float.parseFloat(intensityString); mzValues = ArrayUtil.addToArray(mzValues, mz, size); intensityValues = ArrayUtil.addToArray(intensityValues, intensity, size); size++; } if (size == 0) { return null; } DataPointSorter.sortDataPoints(mzValues, intensityValues, size, SortingProperty.MZ, SortingDirection.ASCENDING); MsSpectrumType specType = SpectrumTypeDetectionAlgorithm.detectSpectrumType(mzValues, intensityValues, size); SimpleMsSpectrum result = new SimpleMsSpectrum(mzValues, intensityValues, size, specType); return result; }  static SimpleMsSpectrum parseMsSpectrum(@Nonnull String spectrumText); static @Nonnull Collection<SimpleMsSpectrum> parseMsSpectra(@Nonnull Reader reader); static SimpleMsSpectrum parseMsSpectrum(@Nonnull Reader reader);  }
@Test public void test4Peaks() throws MSDKException { MsSpectrum spectrum = TxtImportAlgorithm.parseMsSpectrum(spectrumText); assertThat(spectrum.getNumberOfDataPoints(), equalTo(4)); assertThat(spectrum.getMzValues()[0], equalTo(10.0)); assertThat(spectrum.getMzValues()[3], equalTo(40.0)); assertThat(spectrum.getIntensityValues()[0], equalTo(100.0f)); assertThat(spectrum.getIntensityValues()[2], equalTo(300.0f)); }
@Test public void test4PeaksFromFile() throws IOException { File file = folder.newFile(); FileWriter writer = new FileWriter(file); writer.write(spectrumText); writer.close(); MsSpectrum spectrum = TxtImportAlgorithm.parseMsSpectrum(new FileReader(file)); assertThat(spectrum.getNumberOfDataPoints(), equalTo(4)); assertThat(spectrum.getMzValues()[0], equalTo(10.0)); assertThat(spectrum.getMzValues()[3], equalTo(40.0)); assertThat(spectrum.getIntensityValues()[0], equalTo(100.0f)); assertThat(spectrum.getIntensityValues()[2], equalTo(300.0f)); }
@Test public void testNoSpectrumAsString() { MsSpectrum spectrum = TxtImportAlgorithm.parseMsSpectrum("not a spectrum"); assertThat(spectrum, nullValue()); }
@Test public void testNoSpectrumAsReader() { MsSpectrum spectrum = TxtImportAlgorithm.parseMsSpectrum(new StringReader("not a spectrum")); assertThat(spectrum, nullValue()); }