HmfTranscriptRegion implements TranscriptRegion { @Value.Derived @Nullable public List<GenomeRegion> codonRangeByIndex(int startCodon, int endCodon) { if (startCodon < 1 || endCodon < 1) { return null; } if (codingStart() == 0 || codingEnd() == 0) { return null; } List<GenomeRegion> codonRegions = Lists.newArrayList(); int effectiveStartBase = 1 + (startCodon - 1) * 3; int effectiveEndBase = 3 + (endCodon - 1) * 3; int basesCovered = 0; Long startPosition = null; Long endPosition = null; for (HmfExonRegion exon : strandSortedExome()) { long exonCodingStart = Math.max(exon.start(), codingStart()); long exonCodingEnd = Math.min(exon.end(), codingEnd()); long exonBaseLength = exonCodingEnd - exonCodingStart + 1; if (exonBaseLength <= 0) { continue; } if (basesCovered + exonBaseLength >= effectiveStartBase && startPosition == null) { startPosition = strand() == Strand.FORWARD ? exonCodingStart + effectiveStartBase - basesCovered - 1 : exonCodingEnd - effectiveStartBase + basesCovered + 1; } if (basesCovered + exonBaseLength >= effectiveEndBase && endPosition == null) { endPosition = strand() == Strand.FORWARD ? exonCodingStart + effectiveEndBase - basesCovered - 1 : exonCodingEnd - effectiveEndBase + basesCovered + 1; } basesCovered += exonBaseLength; GenomeRegion region = decideOnRangeToIncludeForExon(startPosition, endPosition, exonCodingStart, exonCodingEnd, codonRegions.size() > 0); if (region != null) { codonRegions.add(region); } if (startPosition != null && endPosition != null) { Collections.sort(codonRegions); return codonRegions; } } return null; }  @NotNull abstract String geneID(); abstract long geneStart(); abstract long geneEnd(); abstract long codingStart(); abstract long codingEnd(); @NotNull abstract Strand strand(); @NotNull abstract List<Integer> entrezId(); @NotNull abstract List<HmfExonRegion> exome(); @Value.Derived @Nullable HmfExonRegion exonByIndex(int index); @Value.Derived @Nullable // TODO: Convert to using long. List<GenomeRegion> codingRangeByGenomicCoordinates(int genomicStartPosition, int genomicEndPosition); @Value.Derived @Nullable List<GenomeRegion> codonByIndex(int index); @NotNull List<GenomeRegion> codonRangeAtGenomicPosition(long position); @Value.Derived @Nullable List<GenomeRegion> codonRangeByIndex(int startCodon, int endCodon);  }
@Test public void canRetrieveCodonRangeByIndexOnForwardGene() { HmfTranscriptRegion transcript = create(Strand.FORWARD); assertNull(transcript.codonRangeByIndex(-1, 1)); assertNull(transcript.codonRangeByIndex(0, 0)); List<GenomeRegion> codonsOneAndTwo = transcript.codonRangeByIndex(1, 2); assertNotNull(codonsOneAndTwo); GenomeRegion codonsOneAndTwoPart1 = codonsOneAndTwo.get(0); assertEquals(2, codonsOneAndTwoPart1.start()); assertEquals(5, codonsOneAndTwoPart1.end()); GenomeRegion codonsOneAndTwoPart2 = codonsOneAndTwo.get(1); assertEquals(7, codonsOneAndTwoPart2.start()); assertEquals(8, codonsOneAndTwoPart2.end()); List<GenomeRegion> allCodons = transcript.codonRangeByIndex(1, 5); assertNotNull(allCodons); GenomeRegion allCodonsPart1 = allCodons.get(0); assertEquals(2, allCodonsPart1.start()); assertEquals(5, allCodonsPart1.end()); GenomeRegion allCodonsPart2 = allCodons.get(1); assertEquals(7, allCodonsPart2.start()); assertEquals(16, allCodonsPart2.end()); GenomeRegion allCodonsPart3 = allCodons.get(2); assertEquals(18, allCodonsPart3.start()); assertEquals(18, allCodonsPart3.end()); assertNull(transcript.codonRangeByIndex(0, 6)); }
@Test public void canRetrieveCodonRangeByIndexOnReverseGene() { HmfTranscriptRegion transcript = create(Strand.REVERSE); assertNull(transcript.codonRangeByIndex(-1, 1)); assertNull(transcript.codonRangeByIndex(0, 0)); List<GenomeRegion> codonsOneAndTwo = transcript.codonRangeByIndex(1, 2); assertNotNull(codonsOneAndTwo); assertEquals(2, codonsOneAndTwo.size()); GenomeRegion codonsOneAndTwoPart1 = codonsOneAndTwo.get(0); assertEquals(12, codonsOneAndTwoPart1.start()); assertEquals(16, codonsOneAndTwoPart1.end()); GenomeRegion codonsOneAndTwoPart2 = codonsOneAndTwo.get(1); assertEquals(18, codonsOneAndTwoPart2.start()); assertEquals(18, codonsOneAndTwoPart2.end()); List<GenomeRegion> allCodons = transcript.codonRangeByIndex(1, 5); assertNotNull(allCodons); GenomeRegion allCodonsPart1 = allCodons.get(0); assertEquals(2, allCodonsPart1.start()); assertEquals(5, allCodonsPart1.end()); GenomeRegion allCodonsPart2 = allCodons.get(1); assertEquals(7, allCodonsPart2.start()); assertEquals(16, allCodonsPart2.end()); GenomeRegion allCodonsPart3 = allCodons.get(2); assertEquals(18, allCodonsPart3.start()); assertEquals(18, allCodonsPart3.end()); assertNull(transcript.codonRangeByIndex(0, 6)); }