GeneticInterval implements ChromosomalRegion { public static GeneticInterval parseString(String interval) { String intervalPattern = "chr(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|X|Y|M):[0-9]+-[0-9]+"; if (!Pattern.matches(intervalPattern, interval)) { throw new IllegalArgumentException(String.format("Genetic interval %s does not match expected pattern %s", interval, intervalPattern)); } String[] intervalSections = interval.split(":"); int localChr = Contig.parseId(intervalSections[0]); String positions = intervalSections[1]; String[] startEnd = positions.split("-"); int localStart = Integer.parseInt(startEnd[0]); int localEnd = Integer.parseInt(startEnd[1]); return new GeneticInterval(localChr, localStart, localEnd); } GeneticInterval(int chromosome, int start, int end); static GeneticInterval parseString(String interval); @Override int getChromosome(); @Override int getStart(); @Override int getEnd(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString();  }
@Test public void testParseStringNumericChromosomeSingleDigit() { String interval = "chr3:123-456"; GeneticInterval expResult = new GeneticInterval(3, 123, 456); GeneticInterval result = GeneticInterval.parseString(interval); assertThat(result, equalTo(expResult)); }
@Test public void testParseStringNumericChromosomeDoubleDigit() { String interval = "chr14:123-456"; GeneticInterval expResult = new GeneticInterval(14, 123, 456); GeneticInterval result = GeneticInterval.parseString(interval); assertThat(result, equalTo(expResult)); }
@Test public void testParseStringChromosomeX() { String interval = "chrX:123-456"; GeneticInterval expResult = new GeneticInterval(23, 123, 456); GeneticInterval result = GeneticInterval.parseString(interval); assertThat(result, equalTo(expResult)); }
@Test public void testParseStringChromosomeY() { String interval = "chrY:123-456"; GeneticInterval expResult = new GeneticInterval(24, 123, 456); GeneticInterval result = GeneticInterval.parseString(interval); assertThat(result, equalTo(expResult)); }
@Test public void testParseStringChromosomeM() { String interval = "chrM:123-456"; GeneticInterval expResult = new GeneticInterval(25, 123, 456); GeneticInterval result = GeneticInterval.parseString(interval); assertThat(result, equalTo(expResult)); }
@Test public void testParseStringThrowsErrorOnMissingChromosomePrefix() { String interval = "3:123-456"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorOnIncorrectChromosomeNumber() { String interval = "chr33:123-456"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorOnAnotherIncorrectChromosomeNumber() { String interval = "chr666:123-456"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorOnUndefinedStart() { String interval = "chr6:-456"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorOnUndefinedEnd() { String interval = "chr6:123-"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorWithIncorrectPositionDelimiter() { String interval = "chr6:123:456"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }
@Test public void testParseStringThrowsErrorWithSwitchedPositions() { String interval = "chr6:456:123"; assertThrows(IllegalArgumentException.class, () -> GeneticInterval.parseString(interval)); }