|
| 1 | +package programminglife.model; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +/** |
| 9 | + * An Annotation for a genome. |
| 10 | + */ |
| 11 | +public class Annotation { |
| 12 | + private final String id; |
| 13 | + private final String file; |
| 14 | + private int genomeIndex; |
| 15 | + private final String originalGenomeID; |
| 16 | + |
| 17 | + private final int startCoordinate; |
| 18 | + private final int endCoordinate; |
| 19 | + |
| 20 | + private final Map<String, Set<String>> textFields; |
| 21 | + |
| 22 | + /** |
| 23 | + * A constructor for an Annotation. |
| 24 | + * @param id The ID of this Annotation. |
| 25 | + * @param file The file for this Annotation. |
| 26 | + * @param originalGenomeID The original String for the genome. These should be mapped to the |
| 27 | + * genome indices in the {@link GenomeGraph}. |
| 28 | + * @param startCoordinate The starting coordinate within the genome. |
| 29 | + * @param endCoordinate The end coordinate within the genome. |
| 30 | + */ |
| 31 | + public Annotation(String id, String file, String originalGenomeID, |
| 32 | + int startCoordinate, int endCoordinate) { |
| 33 | + this.id = id; |
| 34 | + this.file = file; |
| 35 | + this.genomeIndex = -1; |
| 36 | + this.originalGenomeID = originalGenomeID; |
| 37 | + this.startCoordinate = startCoordinate; |
| 38 | + this.endCoordinate = endCoordinate; |
| 39 | + this.textFields = new LinkedHashMap<>(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Add a text attribute. |
| 44 | + * If the attribute already exists, this value is added to it. |
| 45 | + * @param name The name of the attribute. |
| 46 | + * @param value The value of the attribute. |
| 47 | + */ |
| 48 | + public void addAttribute(String name, String value) { |
| 49 | + getOrCreateAttribute(name).add(value); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Add multiple values to a single attribute. |
| 54 | + * If the attribute did not exist yet, it is created. |
| 55 | + * @param name The name of the attribute. |
| 56 | + * @param values The values to add. |
| 57 | + */ |
| 58 | + public void addMultiAttribute(String name, Set<String> values) { |
| 59 | + getOrCreateAttribute(name).addAll(values); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add multiple values to a single attribute. |
| 64 | + * If the attribute did not exist yet, it is created. |
| 65 | + * @param name The name of the attribute. |
| 66 | + * @param values The values to add. |
| 67 | + */ |
| 68 | + public void addMultiAttribute(String name, String[] values) { |
| 69 | + Collections.addAll(getOrCreateAttribute(name), values); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get or create an attribute. This returns the value set for an attribute. |
| 74 | + * If the attribute didn't exist yet, it is created. |
| 75 | + * @param name The name of the attribute. |
| 76 | + * @return The set of values for this attribute. |
| 77 | + */ |
| 78 | + @NotNull |
| 79 | + private Set<String> getOrCreateAttribute(String name) { |
| 80 | + return textFields.computeIfAbsent(name, k -> new HashSet<>()); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean equals(Object o) { |
| 85 | + if (this == o) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + if (o == null || getClass() != o.getClass()) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + Annotation that = (Annotation) o; |
| 93 | + |
| 94 | + return id.equals(that.id) |
| 95 | + && file.equals(that.file) |
| 96 | + && startCoordinate == that.startCoordinate |
| 97 | + && endCoordinate == that.endCoordinate |
| 98 | + && ( |
| 99 | + genomeIndex == that.genomeIndex && genomeIndex != -1 // they have the same genomeIndex |
| 100 | + || originalGenomeID.equals(that.originalGenomeID) // or the original names were equal |
| 101 | + ) |
| 102 | + && textFields.equals(that.textFields); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int hashCode() { |
| 107 | + return id.hashCode() + 31 * file.hashCode(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + return String.format("Annotations {ID: %s; file: %s; genomeIndex: %d; " |
| 113 | + + "originalGenomeID: %s; start: %d; end: %d; textFields: %s}", |
| 114 | + id, |
| 115 | + file, |
| 116 | + genomeIndex, |
| 117 | + originalGenomeID, |
| 118 | + startCoordinate, |
| 119 | + endCoordinate, |
| 120 | + textFields.entrySet().stream().map(entry -> |
| 121 | + String.format("{%s: {%s}}", entry.getKey(), |
| 122 | + entry.getValue().stream().collect(Collectors.joining(", "))) |
| 123 | + ).collect(Collectors.joining(", ")) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public String getId() { |
| 128 | + return id; |
| 129 | + } |
| 130 | + |
| 131 | + public String getFileName() { |
| 132 | + return file; |
| 133 | + } |
| 134 | + |
| 135 | + public int getGenomeIndex() { |
| 136 | + return genomeIndex; |
| 137 | + } |
| 138 | + |
| 139 | + public void setGenomeIndex(int genomeIndex) { |
| 140 | + this.genomeIndex = genomeIndex; |
| 141 | + } |
| 142 | + |
| 143 | + public String getOriginalGenomeID() { |
| 144 | + return originalGenomeID; |
| 145 | + } |
| 146 | + |
| 147 | + public int getStartCoordinate() { |
| 148 | + return startCoordinate; |
| 149 | + } |
| 150 | + |
| 151 | + public int getEndCoordinate() { |
| 152 | + return endCoordinate; |
| 153 | + } |
| 154 | + |
| 155 | + public Map<String, Set<String>> getTextFields() { |
| 156 | + return textFields; |
| 157 | + } |
| 158 | +} |
0 commit comments