TxtImportAlgorithm { public static @Nonnull Collection<SimpleMsSpectrum> parseMsSpectra(@Nonnull Reader reader) { Collection<SimpleMsSpectrum> result = new ArrayList<>(); Scanner scanner = new Scanner(reader); while (scanner.hasNextLine()) { SimpleMsSpectrum spectrum = parseMsSpectrum(scanner); if (spectrum != null) { result.add(spectrum); } } scanner.close(); 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 testTwoSpetra() { Collection<SimpleMsSpectrum> spectra = TxtImportAlgorithm.parseMsSpectra(new StringReader(spectraText)); assertThat(spectra.size(), equalTo(2)); Iterator<SimpleMsSpectrum> iterator = spectra.iterator(); MsSpectrum spectrum = iterator.next(); assertThat(spectrum.getMzValues()[0], equalTo(10.0)); assertThat(spectrum.getMzValues()[3], equalTo(40.0)); assertThat(spectrum.getIntensityValues()[1], equalTo(200.0f)); assertThat(spectrum.getIntensityValues()[2], equalTo(300.0f)); spectrum = iterator.next(); assertThat(spectrum.getMzValues()[0], equalTo(50.0)); assertThat(spectrum.getMzValues()[1], equalTo(60.0)); assertThat(spectrum.getIntensityValues()[0], equalTo(500.0f)); assertThat(spectrum.getIntensityValues()[1], equalTo(600.0f)); }
@Test public void testSpectrumWithHeader() { StringBuilder builder = new StringBuilder(); builder.append("# header line 1\n"); builder.append("header line 2\n"); builder.append(spectrumText); Collection<SimpleMsSpectrum> spectra = TxtImportAlgorithm.parseMsSpectra(new StringReader(builder.toString())); assertThat(spectra.size(), equalTo(1)); Iterator<SimpleMsSpectrum> iterator = spectra.iterator(); MsSpectrum spectrum = iterator.next(); assertThat(spectrum.getMzValues()[0], equalTo(10.0)); assertThat(spectrum.getMzValues()[3], equalTo(40.0)); assertThat(spectrum.getIntensityValues()[1], equalTo(200.0f)); assertThat(spectrum.getIntensityValues()[2], equalTo(300.0f)); }
@Test public void testSpectrumWithFooter() { StringBuilder builder = new StringBuilder(); builder.append(spectrumText); builder.append("# footer line 1\n"); builder.append("footer line 2\n"); Collection<SimpleMsSpectrum> spectra = TxtImportAlgorithm.parseMsSpectra(new StringReader(builder.toString())); assertThat(spectra.size(), equalTo(1)); Iterator<SimpleMsSpectrum> iterator = spectra.iterator(); MsSpectrum spectrum = iterator.next(); assertThat(spectrum.getMzValues()[0], equalTo(10.0)); assertThat(spectrum.getMzValues()[3], equalTo(40.0)); assertThat(spectrum.getIntensityValues()[1], equalTo(200.0f)); assertThat(spectrum.getIntensityValues()[2], equalTo(300.0f)); }