GoPoTsvParser { ImmutableMap<String, OntologyTerm> parse() throws IOException { String startsWithGoPoRegex = "^(" + Joiner.on("|").join(VALID_PREFIXES) + ").+"; ImmutableMultimap.Builder<String, OntologyTerm> multimapBuilder = ImmutableMultimap.builder(); String[] line; while ((line = csvReader.readNext()) != null) { if (line.length > TERM_ACCESSION_COLUMN_INDEX) { String accession = line[TERM_ACCESSION_COLUMN_INDEX]; if (accession.matches(startsWithGoPoRegex)) { String name = line.length > TERM_NAME_COLUMN_INDEX ? line[TERM_NAME_COLUMN_INDEX] : ""; int depth = parseDepth(line); multimapBuilder.put(accession, OntologyTerm.create(accession, name, "", depth)); } } } ImmutableMultimap<String, OntologyTerm> accessionToGoPoTerms = multimapBuilder.build(); ImmutableMap.Builder<String, OntologyTerm> mapBuilder = ImmutableMap.builder(); for (String accession : accessionToGoPoTerms.keySet()) { int minDepth = minDepthOfTerms(accessionToGoPoTerms.get(accession)); String name = accessionToGoPoTerms.get(accession).iterator().next().name(); mapBuilder.put(accession, OntologyTerm.create(accession, name, "", minDepth)); } return mapBuilder.build(); } GoPoTsvParser(CSVReader csvReader);  static final int DEFAULT_DEPTH; }
@Test public void parseLineWithDepthField() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] { GO_0000001, MITOCHONDRION_INHERITANCE, Integer.toString(DEPTH_6)}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.get(GO_0000001).accession(), is(GO_0000001)); assertThat(map.get(GO_0000001).name(), is(MITOCHONDRION_INHERITANCE)); assertThat(map.get(GO_0000001).depth(), is(DEPTH_6)); }
@Test public void duplicatesGetMinimumDepth() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] {GO_0000001, MITOCHONDRION_INHERITANCE, Integer.toString(DEPTH_6 + 1)}) .willReturn(new String[] {GO_0000001, MITOCHONDRION_INHERITANCE, Integer.toString(DEPTH_6)}) .willReturn(new String[] {GO_0000001, MITOCHONDRION_INHERITANCE, Integer.toString(DEPTH_6 + 2)}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.get(GO_0000001).accession(), is(GO_0000001)); assertThat(map.get(GO_0000001).name(), is(MITOCHONDRION_INHERITANCE)); assertThat(map.get(GO_0000001).depth(), is(DEPTH_6)); }
@Test public void lineWithoutDepthGetsDefaultDepth() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] {PO_0000001, EMBRYO_PROPER}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.get(PO_0000001).accession(), is(PO_0000001)); assertThat(map.get(PO_0000001).name(), is(EMBRYO_PROPER)); assertThat(map.get(PO_0000001).depth(), is(GoPoTsvParser.DEFAULT_DEPTH)); }
@Test public void ignoreLineWithoutValidAccessionPrefix() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] {"foo:0000001", "bar"}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.isEmpty(), is(true)); }
@Test public void emptyFieldsAreIgnored() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] {GO_0000001, "", "", "", "", ""}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.size(), is(1)); assertThat(map.get(GO_0000001).name(), is("")); assertThat(map.get(GO_0000001).depth(), is(GoPoTsvParser.DEFAULT_DEPTH)); }
@Test public void emptyLinesAreIgnored() throws Exception { given(tsvReaderMock.readNext()) .willReturn(new String[] {}) .willReturn(new String[] {"", "", "", "", "", ""}) .willReturn(null); Map<String, OntologyTerm> map = subject.parse(); assertThat(map.isEmpty(), is(true)); }