CondensedSdrfParser { public CondensedSdrfParserOutput parse(String experimentAccession, ExperimentType experimentType) throws CondensedSdrfParserException { ExperimentDesign experimentDesign = new ExperimentDesign(); if (dataFileHub.getExperimentFiles(experimentAccession).sdrf.exists()) { Map<String, Set<String>> orderedHeaders = sdrfParser.parseHeader(experimentAccession); experimentDesign.setOrderedSampleHeaders(orderedHeaders.get("characteristics")); experimentDesign.setOrderedFactorHeaders(orderedHeaders.get("factorvalue")); } ImmutableList.Builder<String[]> factorsBuilder = new ImmutableList.Builder<>(); ImmutableList.Builder<String[]> sampleCharacteristicsBuilder = new ImmutableList.Builder<>(); try (TsvStreamer tsvStreamer = dataFileHub.getExperimentFiles(experimentAccession).condensedSdrf.get()) { for (Iterator<String[]> tsvIterator = tsvStreamer.get().iterator(); tsvIterator.hasNext();) { String[] tsvLine = tsvIterator.next(); switch (tsvLine[FACTOR_OR_CHARACTERISTIC_INDEX]) { case FACTOR: factorsBuilder.add(tsvLine); break; case CHARACTERISTIC: sampleCharacteristicsBuilder.add(tsvLine); break; default: throw new CondensedSdrfParserException(String.format( "Parsing condensed SDRF for %s: Unknown factor/characteristic descriptor “%s”", experimentAccession, tsvLine[FACTOR_OR_CHARACTERISTIC_INDEX])); } } } Multimap<String, String[]> assayRunToTsvLines = mapFactorTsvLinesByAssayRun(factorsBuilder.build()); addFactorValuesToExperimentDesign(experimentDesign, assayRunToTsvLines); addCharacteristicToExperimentDesign(experimentDesign, sampleCharacteristicsBuilder.build()); String species = parseSpeciesFromCharacteristics(sampleCharacteristicsBuilder.build()); addArraysToExperimentDesign(experimentDesign, assayRunToTsvLines); return new CondensedSdrfParserOutput( experimentAccession, experimentType, experimentDesign, species); } @Inject  CondensedSdrfParser(DataFileHub dataFileHub, SdrfParser sdrfParser); CondensedSdrfParserOutput parse(String experimentAccession, ExperimentType experimentType);  }
@Test public void parseWhenSdrfIsPresent() { dataFileHub.addCondensedSdrfFile(E_MTAB_513, Arrays.asList(E_MTAB_513_CONDENSED_SDRF_ARRAY)); dataFileHub.addSdrfFile(E_MTAB_513, Arrays.asList(E_MTAB_513_SDRF_HEADER_ARRAY)); when(mockSdrfParser.parseHeader(E_MTAB_513)).thenReturn(ImmutableMap.of( StringUtils.trimAllWhitespace(CHARACTERISTIC_HEADER_TYPE).toLowerCase(), new LinkedHashSet<>(Arrays.asList(ORGANISM, AGE, SEX, ETHNIC_GROUP, ORGANISM_PART)), StringUtils.trimAllWhitespace(FACTOR_HEADER_TYPE).toLowerCase(), new LinkedHashSet<>(Collections.singletonList(ORGANISM_PART)) )); CondensedSdrfParserOutput output = subject.parse(E_MTAB_513, ExperimentType.RNASEQ_MRNA_BASELINE); assertThat(output.getExperimentAccession()) .isEqualTo(E_MTAB_513); assertThat(output.getExperimentType()) .isEqualByComparingTo(ExperimentType.RNASEQ_MRNA_BASELINE); ExperimentDesign experimentDesign = output.getExperimentDesign(); assertThat(experimentDesign.getAllRunOrAssay()) .containsExactlyInAnyOrder(ASSAYS); assertThat(experimentDesign.getFactorHeaders()) .containsExactly(ORGANISM_PART); assertThat(experimentDesign.getFactors(ASSAYS[0]).size()) .isEqualTo(1); assertThat(experimentDesign.getSampleHeaders()) .containsExactly(ORGANISM, AGE, SEX, ETHNIC_GROUP, ORGANISM_PART); assertThat(experimentDesign.getSampleCharacteristic(ASSAYS[2], ETHNIC_GROUP).value()) .isEqualTo(AFRICAN_AMERICAN); assertThat(experimentDesign.getSampleCharacteristics(ASSAYS[0]).size()) .isEqualTo(4); assertThat(experimentDesign.getSampleCharacteristics(ASSAYS[3]).size()) .isEqualTo(5); assertThat(output.getSpecies()) .isEqualTo(HOMO_SAPIENS); }
@Test public void parseWhenSdrfIsAbsent() { dataFileHub.addCondensedSdrfFile(E_MTAB_513, Arrays.asList(E_MTAB_513_CONDENSED_SDRF_ARRAY)); CondensedSdrfParserOutput output = subject.parse(E_MTAB_513, ExperimentType.RNASEQ_MRNA_BASELINE); assertThat(output.getExperimentAccession()) .isEqualTo(E_MTAB_513); ExperimentDesign experimentDesign = output.getExperimentDesign(); assertThat(experimentDesign.getFactorHeaders()) .containsExactly(ORGANISM_PART); assertThat(experimentDesign.getSampleHeaders()) .containsExactly(AGE, ETHNIC_GROUP, ORGANISM, ORGANISM_PART, SEX); }
@Test public void parseRunWithoutFactorIsDiscarded() { dataFileHub.addCondensedSdrfFile( E_MTAB_513, ImmutableList.copyOf( Arrays.copyOfRange( E_MTAB_513_CONDENSED_SDRF_ARRAY, 0, E_MTAB_513_CONDENSED_SDRF_ARRAY.length - 1))); dataFileHub.addSdrfFile( E_MTAB_513, Arrays.asList(E_MTAB_513_SDRF_HEADER_MISSING_CHARACTERISTIC)); when(mockSdrfParser.parseHeader(E_MTAB_513)).thenReturn(ImmutableMap.of( StringUtils.trimAllWhitespace(CHARACTERISTIC_HEADER_TYPE).toLowerCase(), new LinkedHashSet<>(Arrays.asList(ORGANISM, AGE, ETHNIC_GROUP, ORGANISM_PART)), StringUtils.trimAllWhitespace(FACTOR_HEADER_TYPE).toLowerCase(), new LinkedHashSet<>(Collections.singletonList(ORGANISM_PART)))); CondensedSdrfParserOutput output = subject.parse(E_MTAB_513, ExperimentType.RNASEQ_MRNA_BASELINE); ExperimentDesign experimentDesign = output.getExperimentDesign(); assertThat(experimentDesign.getAllRunOrAssay()) .containsExactlyInAnyOrder(ASSAYS[0], ASSAYS[1], ASSAYS[2]); assertThat(experimentDesign.getSampleHeaders()) .containsExactly(ORGANISM, AGE, ETHNIC_GROUP, ORGANISM_PART); }
@Test public void parseAssayWithCompoundAndDose() { dataFileHub.addCondensedSdrfFile(E_MEXP_1810, Arrays.asList(E_MEXP_1810_CONDENSED_SDRF_ARRAY)); CondensedSdrfParserOutput output = subject.parse(E_MEXP_1810, ExperimentType.RNASEQ_MRNA_BASELINE); ExperimentDesign experimentDesign = output.getExperimentDesign(); assertThat(experimentDesign.getFactor(H_RIL_14_NON_DAUER_1, "compound").getValue()) .isEqualTo(COMPOUND_VALUE + " " + DOSE_VALUE); }
@Test public void parseAssayWithCompoundAndNoDose() { dataFileHub.addCondensedSdrfFile( E_MEXP_1810, ImmutableList.of(E_MEXP_1810_CONDENSED_SDRF_ARRAY[0], E_MEXP_1810_CONDENSED_SDRF_ARRAY[1])); CondensedSdrfParserOutput output = subject.parse(E_MEXP_1810, ExperimentType.RNASEQ_MRNA_BASELINE); ExperimentDesign experimentDesign = output.getExperimentDesign(); assertThat(experimentDesign.getFactor(H_RIL_14_NON_DAUER_1, "compound").getValue()) .isEqualTo(COMPOUND_VALUE); }
@Test public void parseAssayWithDoseAndNoCompoundOrIrradiate() { thrown.expect(CondensedSdrfParser.CondensedSdrfParserException.class); dataFileHub.addCondensedSdrfFile( E_MEXP_1810, ImmutableList.of(E_MEXP_1810_CONDENSED_SDRF_ARRAY[0], E_MEXP_1810_CONDENSED_SDRF_ARRAY[2])); subject.parse(E_MEXP_1810, ExperimentType.MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL); }
@Test public void parseMultipleArrayDesigns() { thrown.expect(CondensedSdrfParser.CondensedSdrfParserException.class); dataFileHub.addCondensedSdrfFile( E_MEXP_1810, ImmutableList.copyOf(MULTIPLE_ARRAY_DESIGNS_CONDENSED_SDRF_ARRAY)); subject.parse(E_MEXP_1810, ExperimentType.MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL); }
@Test public void parseOtherThanFactorOrCharacteristic() { thrown.expect(CondensedSdrfParser.CondensedSdrfParserException.class); dataFileHub.addCondensedSdrfFile( E_MEXP_1810, ImmutableList.copyOf(NO_FACTOR_OR_CHARACTERISTIC_CONDENSED_SDRF_ARRAY)); subject.parse(E_MEXP_1810, ExperimentType.MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL); }