SdrfParser { public Map<String, Set<String>> parseHeader(String experimentAccession) { try (TsvStreamer sdrfStreamer = dataFileHub.getExperimentFiles(experimentAccession).sdrf.get()) { Pattern pattern = Pattern.compile("(.*?)(\\[)(.*?)(].*)"); return Arrays.stream( sdrfStreamer .get() .findFirst() .orElse(new String[0])) .filter(x -> x.toLowerCase().contains("characteristic") || x.toLowerCase().contains("factor")) .filter(x -> x.matches(pattern.toString())) .map(header -> { Matcher matcher = pattern.matcher(header); String headerType = ""; String headerValue = ""; if (matcher.matches()) { headerType = StringUtils.trimAllWhitespace(matcher.group(1)).toLowerCase(); headerValue = matcher.group(3).trim().toLowerCase(); } return new ImmutablePair<>(headerType, headerValue); }) .collect( Collectors.groupingBy( ImmutablePair::getLeft, Collectors.mapping( ImmutablePair::getRight, Collectors.toCollection(LinkedHashSet::new)))); } } @Inject  SdrfParser(DataFileHub dataFileHub); Map<String, Set<String>> parseHeader(String experimentAccession);  }
@Test void parseHeaderWithCharacteristicsAndFactors() { String experimentAccession = RandomDataTestUtils.generateRandomExperimentAccession(); dataFileHub.addSdrfFile(experimentAccession, Arrays.asList(SDRF_TXT_MIXED_SPACING)); Map<String, Set<String>> result = subject.parseHeader(experimentAccession); assertThat(result) .hasSize(2) .containsOnlyKeys(CHARACTERISTICS, FACTORS); assertThat(result.get(CHARACTERISTICS)) .containsExactly("organism", "developmental stage", "organism part"); assertThat(result.get(FACTORS)) .containsExactly("organism part", "organism"); }
@Test void parseHeaderWithCharacteristicsOnly() { String experimentAccession = RandomDataTestUtils.generateRandomExperimentAccession(); dataFileHub.addSdrfFile(experimentAccession, Arrays.asList(SDRF_TXT_NO_FACTORS)); Map<String, Set<String>> result = subject.parseHeader(experimentAccession); assertThat(result) .hasSize(1) .containsOnlyKeys(CHARACTERISTICS) .doesNotContainKeys(FACTORS); assertThat(result.get(CHARACTERISTICS)) .containsExactly("organism", "developmental stage", "organism part"); }