1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
* Copyright (C) 2014-2021 Brian L. Browning
*
* This file is part of Beagle
*
* Beagle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Beagle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package phase;
import beagleutil.IntInterval;
import blbutil.Const;
import java.util.Comparator;
/**
* <p>Class {@code SampleSeg} represents a segment of genotype data in
* a sample.</p>
*
* <p>Instances of class {@code SampleSeg} are immutable</p>
*
* @author Brian L. Browning {@code <browning@uw.edu>}
*/
public class SampleSeg implements Comparable<SampleSeg>, IntInterval {
private final int sample;
private final int start;
private final int inclEnd;
/**
* Constructs a new {@code SampleSeg} instance from the specified data.
* @param sample the sample index
* @param start the start marker index (inclusive)
* @param inclEnd the end marker index (inclusive)
* @throws IllegalArgumentException if {@code start > inclEnd}
*/
public SampleSeg(int sample, int start, int inclEnd) {
if (start > inclEnd) {
throw new IllegalArgumentException(String.valueOf(start));
}
this.sample = sample;
this.start = start;
this.inclEnd = inclEnd;
}
/**
* Returns the sample index.
* @return the sample index
*/
public int sample() {
return sample;
}
/**
* Returns the start marker index (inclusive).
* @return the start marker index (inclusive)
*/
@Override
public int start() {
return start;
}
/**
* Returns the end marker index (inclusive).
* @return the end marker index (inclusive)
*/
@Override
public int inclEnd() {
return inclEnd;
}
/**
* Returns a string representation of {@code this}. The exact
* details of the representation are unspecified and subject to change.
* @return a string representation of {@code this}
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder(70);
sb.append("[");
sb.append(sample);
sb.append(": ");
sb.append(start);
sb.append(Const.hyphen);
sb.append(inclEnd);
sb.append("]");
return sb.toString();
}
/**
* <p>Returns the hash code value for this object. The hash code is defined
* by the following calculation:
* </p>
* <pre>
* int hash = 5;
* hash = 89 * hash + this.hap();
* hash = 89 * hash + this.start();
* hash = 89 * hash + this.inclEnd();
</pre>
* @return the hash code value for this object
*/
@Override
public int hashCode() {
int hash = 5;
hash = 89*hash + this.sample;
hash = 89*hash + this.start;
hash = 89*hash + this.inclEnd;
return hash;
}
/**
* Compares the specified object with this {@code SampleSeg} for
* equality. Returns {@code true} if the specified object is a
* {@code SampleSeg} instance and if this {@code SampleSeg} is
* equal to the specified {@code SampleSeg}, and returns
* {@code false} otherwise. Two {@code SampleSeg} instances
* are equal if they have equal sample indices,
* equal start marker indices, and equal end marker indices.
* @param o the object to be compared with this {@code SampleSeg}
* for equality
* @return {@code true} if the specified object is equal to {@code this}
*/
@Override
public boolean equals(Object o) {
if (o==null) {
return false;
}
if (getClass()!=o.getClass()) {
return false;
}
final SampleSeg other=(SampleSeg) o;
return (this.sample==other.sample && this.start==other.start
&& this.inclEnd==other.inclEnd);
}
/**
* Compares this {@code SampleSeg} with the specified {@code SampleSeg}
* for order. Returns a negative integer, zero, or a positive integer
* as this object is less than, equal to, or greater than the specified
* object. {@code SampleSeg} instances are ordered first by
* {@code this.start()}, then by {@code this.end()}, and finally by
* t{@code this.sample()}.
* @param ss the {@code SampleSeg} to be compared with this {@code SampleSeg}
* @return a negative integer, zero, or a positive integer as this
* {@code SampleSeg} is less than, equal to, or greater than the
* specified {@code SampleSeg}
* @throws NullPointerException if {@code ss == null}
*/
@Override
public int compareTo(SampleSeg ss) {
if (this.start != ss.start) {
return (this.start < ss.start) ? -1 : 1;
}
else if (this.inclEnd != ss.inclEnd) {
return (this.inclEnd < ss.inclEnd) ? -1 : 1;
}
if (this.sample != ss.sample) {
return (this.sample < ss.sample) ? -1 : 1;
}
return 0;
}
/**
* Returns a comparator that orders first by {@code this.sample()}, then
* by {@code this.start()}, and finally by {@code this.end()}.
* @return a comparator that orders first by {@code this.sample()}, then
* by {@code this.start()}, and finally by {@code this.end()}
*/
public static Comparator<SampleSeg> sampleComp() {
return (SampleSeg t1, SampleSeg t2) -> {
if (t1.sample != t2.sample) {
return (t1.sample < t2.sample) ? -1 : 1;
}
if (t1.start != t2.start) {
return (t1.start < t2.start) ? -1 : 1;
}
else if (t1.inclEnd != t2.inclEnd) {
return (t1.inclEnd < t2.inclEnd) ? -1 : 1;
}
return 0;
} ;
}
}
|