|
| 1 | +package org.htsjdk.core.api; |
| 2 | + |
| 3 | +/** |
| 4 | + * Represents the location of a sequence with respect to another sequence (reference). |
| 5 | + * |
| 6 | + * <p> |
| 7 | + * This interface represents a location or "mapping" with respect to a reference sequence. Examples |
| 8 | + * of this abstraction are features located on the genome or alignment between two sequences (query |
| 9 | + * with respect to a reference). |
| 10 | + * </p> |
| 11 | + * |
| 12 | + * @implSpec note that 0-length intervals are allowed for intervals with undefined end. |
| 13 | + */ |
| 14 | +public interface Locatable { |
| 15 | + |
| 16 | + /** |
| 17 | + * Represents the reference name for unplaced locations. |
| 18 | + * |
| 19 | + * @see #getRefName() |
| 20 | + */ |
| 21 | + public static final String UNPLACED_REF_NAME = "*"; |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets the reference name where this location is placed. |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * {@link Locatable} should either be placed on a reference sequence or being unplaced |
| 28 | + * ({@link #UNPLACED_REF_NAME}). |
| 29 | + * </p> |
| 30 | + * |
| 31 | + * @return name of the reference for the location; {@link #UNPLACED_REF_NAME} if unplaced. |
| 32 | + * Never {@code null} |
| 33 | + */ |
| 34 | + String getRefName(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets the start position on the reference (1-based). |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * {@link Locatable} should have a non-negative location on the reference sequence; if unplaced |
| 41 | + * ({@code getReferenceName() == UNPLACED_REF_NAME}), position is undefined. |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * @return 1-based start position on the reference; undefined if {@link #getRefName()} |
| 45 | + * returns {@link #UNPLACED_REF_NAME}. |
| 46 | + */ |
| 47 | + long getStart(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets the end position on the reference (1-based fully-closed). |
| 51 | + * |
| 52 | + * @return 1-based fully-closed if present; {@code getStart() - 1} otherwise. |
| 53 | + * |
| 54 | + * @implSpec if present, end position should be downstream te start position ({@code getStart() |
| 55 | + * > getEnd()}); otherwise, {@code getEnd() == getStart() - 1 }. |
| 56 | + * @see #getStart() |
| 57 | + */ |
| 58 | + long getEnd(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets the number of bases of reference covered by this location. |
| 62 | + * |
| 63 | + * <p> |
| 64 | + * Returns the number of bases on te reference from the start to the end (independently of the |
| 65 | + * alignment, if any). 0-lenght intervals represents the special case of undetermined eng. |
| 66 | + * </p> |
| 67 | + * |
| 68 | + * @return number of bases of reference; 0 for undefined end. |
| 69 | + * |
| 70 | + * @implNote default implementation uses {@code getEnd() - getStart() + 1} to compute |
| 71 | + * the length on the reference. |
| 72 | + * @implSpec return value should be the same as the default implementation, but subclasses could |
| 73 | + * override to more efficient implementations. |
| 74 | + * @see #getStart() |
| 75 | + * @see #getEnd() |
| 76 | + */ |
| 77 | + default long getLengthOnReference() { |
| 78 | + return getEnd() - getStart() + 1; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Determines whether this location overlaps with other. |
| 83 | + * |
| 84 | + * @param other location to test. |
| 85 | + * |
| 86 | + * @return {@code true} if this location overlaps other; {@code false} otherwise. |
| 87 | + * |
| 88 | + * @implNote default implementation returns {@code withinDistanceOf(other, 0)}. |
| 89 | + * @implSpec should return {@code false} if {@code referenceMatch(other) == false}. |
| 90 | + * @see #withinDistanceOf(Locatable, int) |
| 91 | + */ |
| 92 | + // TODO: default implementation? |
| 93 | + default boolean overlaps(Locatable other) { |
| 94 | + return withinDistanceOf(other, 0); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Determines whether this location is within a {@code distance} of other (overlap with margin). |
| 99 | + * |
| 100 | + * @param other location to test. |
| 101 | + * @param distance number of bases between the intervals to use as padding for test the |
| 102 | + * overlap. |
| 103 | + * |
| 104 | + * @return {@code true} if this location overlaps other with the provided margin; {@code false} |
| 105 | + * otherwise. |
| 106 | + * |
| 107 | + * @implSpec should return {@code false} if {@code referenceMatch(other) == false}. |
| 108 | + */ |
| 109 | + // TODO: default implementation? |
| 110 | + boolean withinDistanceOf(Locatable other, int distance); |
| 111 | + |
| 112 | + /** |
| 113 | + * Determines whether this location contains entirely other. |
| 114 | + * |
| 115 | + * <p> |
| 116 | + * A location contains the entire location represented by other when it contains all positions |
| 117 | + * spanned by the other (or more). |
| 118 | + * </p> |
| 119 | + * |
| 120 | + * @param other location to test. |
| 121 | + * |
| 122 | + * @return {@code true} if this location contains the other; {@code false} otherwise. |
| 123 | + * |
| 124 | + * @implSpec should return {@code false} if {@code referenceMatch(other) == false}. |
| 125 | + */ |
| 126 | + // TODO: default implementation? |
| 127 | + boolean contains(Locatable other); |
| 128 | + |
| 129 | + /** |
| 130 | + * Determines if this location is on the same reference as other. |
| 131 | + * |
| 132 | + * @return {@code true} if both locations are in the same reference; {@code false} otherwise. |
| 133 | + * |
| 134 | + * @implNote default implementation compares {@link #getRefName()} values. |
| 135 | + * @implSpec should return the same value as {@code this.getRefName().equals(other.getRefName())}. |
| 136 | + * @see #getRefName() |
| 137 | + */ |
| 138 | + // TODO: default implementation? |
| 139 | + boolean referenceMatch(Locatable other); |
| 140 | +} |
0 commit comments