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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* 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 vcf;
import java.util.Arrays;
import java.util.stream.IntStream;
/**
* <p>Class {@code RefGT} stores a list of samples and a
* haplotype pair for each sample.
* </p>
* <p>Instances of class {@code BasicRefGT} are immutable.<p>
*
* @author Brian L. Browning {@code <browning@uw.edu>}
*/
public class RefGT implements GT {
private final Markers markers;
private final Samples samples;
private final RefGTRec[] recs;
/**
* Constructs a new {@code RefGT} instance.
* @param markers the sequence of markers
* @param samples the sequence of samples
* @param refVcfRecs the sequence of per-marker genotype data
*
* @throws IllegalArgumentException if
* {@code markers.nMarkers() != refVcfRecs.length}
* @throws IllegalArgumentException if
* {@code refVcfRecs[k].samples().equals(samples) == false} for any
* {@code k} satisfying {@code 0 <= k && k < refVcfRecs.length}
* @throws IllegalArgumentException if
* {@code refVcfRecs[k].marker().equals(markers.marker(k)) == false}
* for any {@code k} satisfying {@code 0 <= k && k < refVcfRecs.length}
* @throws IllegalArgumentException if
* {@code refVcfRecs[k].isPhased() == false} for any {@code k}
* satisfying {@code 0 <= k && k < refVcfRecs.length}
* @throws NullPointerException if
* {@code markers == null || samples == null || refVcfRecs == null
* || refVcfRecs[k] == null} for any {@code k} satisfying
* {@code 0 <= k && k <= refVcfRecs.length}
*/
public RefGT(Markers markers, Samples samples, RefGTRec[] refVcfRecs) {
checkData(markers, samples, refVcfRecs);
this.markers = markers;
this.samples = samples;
this.recs = refVcfRecs.clone();
}
/**
* Constructs a new {@code RefHapPairs} instance.
* @param refVcfRecs the sequence of per-marker genotype data
*
* @throws IllegalArgumentException if {@code refVcfRecs.length == 0}
* @throws IllegalArgumentException if
* {@code refVcfRecs[k].samples().equals(samples) == false} for any
* {@code k} satisfying {@code 0 <= k && k < refVcfRecs.length}
* @throws IllegalArgumentException if
* {@code refVcfRecs[k].isPhased() == false} for any {@code k}
* satisfying {@code 0 <= k && k < refVcfRecs.length}
* @throws NullPointerException if
* {@code samples == null || refVcfRecs == null}
* @throws NullPointerException if
* {@code (refVcfRecs[k] == null)} for any {@code k} satisfying
* {@code (0 <= k && k <= refVcfRecs.length)}
*/
public RefGT(RefGTRec[] refVcfRecs) {
this.samples = checkData(refVcfRecs);
Marker[] ma = Arrays.stream(refVcfRecs)
.parallel()
.map(rec -> rec.marker())
.toArray(Marker[]::new);
this.markers = Markers.create(ma);
this.recs = refVcfRecs.clone();
}
private static Samples checkData(GTRec[] refVcfRecs) {
if (refVcfRecs.length==0) {
String s = "Missing data in VCF file";
throw new IllegalArgumentException(s);
}
Samples samples = refVcfRecs[0].samples();
for (int j=0; j<refVcfRecs.length; ++j) {
if (refVcfRecs[j].samples().equals(samples)==false) {
String s = "sample inconsistency at index " + j;
throw new IllegalArgumentException(s);
}
if (refVcfRecs[j].isPhased()==false) {
String s = "non-reference data at marker index " + j;
throw new IllegalArgumentException(s);
}
}
return samples;
}
private static void checkData(Markers markers, Samples samples,
GTRec[] refVcfRecs) {
if (markers.size()!=refVcfRecs.length) {
String s = "markers.nMarkers()=" + markers.size()
+ " refVcfRecs.length=" + refVcfRecs.length;
throw new IllegalArgumentException(s);
}
for (int j=0; j<refVcfRecs.length; ++j) {
if (refVcfRecs[j].samples().equals(samples)==false) {
String s = "sample inconsistency at index " + j;
throw new IllegalArgumentException(s);
}
if (refVcfRecs[j].marker().equals(markers.marker(j))==false) {
String s = "marker inconsistency at index " + j;
throw new IllegalArgumentException(s);
}
if (refVcfRecs[j].isPhased()==false) {
String s = "non-reference data at marker index " + j;
throw new IllegalArgumentException(s);
}
}
}
@Override
public boolean isReversed() {
return false;
}
@Override
public int nMarkers() {
return markers.size();
}
@Override
public Marker marker(int marker) {
return markers.marker(marker);
}
@Override
public Markers markers() {
return markers;
}
@Override
public int nHaps() {
return 2*samples.size();
}
@Override
public int nSamples() {
return samples.size();
}
@Override
public Samples samples() {
return samples;
}
@Override
public boolean isPhased() {
return true;
}
@Override
public int allele1(int marker, int hapPair) {
return recs[marker].allele1(hapPair);
}
@Override
public int allele2(int marker, int hapPair) {
return recs[marker].allele2(hapPair);
}
@Override
public int allele(int marker, int haplotype) {
return recs[marker].get(haplotype);
}
/**
* Returns a {@code RefGT} instance restricted to genotype data for
* the specified markers.
* @param refGT the {@code RefGT} instance to be restricted
* @param indices a list of distinct marker indices (from
* {@code this.markers())} in increasing order
* @return a {@code RefGT} instance restricted to genotype data for
* the specified markers
*
* @throws IndexOutOfBoundsException if there exists {@code j} such that
* {@code (0 <= j && j < indices.length)} such that
* {@code (indices[j] < 0 || indices[j] >= gt.nMarkers())}
* @throws IllegalArgumentException if there exists {@code j} such that
* {@code (1 <= j && j < indices.length)} such that
* {@code (indices[j] <= indice[j - 1])}
* @throws NullPointerException if
* {@code gt == null || indices == null}
*/
public static RefGT restrict(RefGT refGT, int[] indices) {
RefGTRec[] rra = new RefGTRec[indices.length];
for (int j=0; j<rra.length; ++j) {
if (j>0 && indices[j] <= indices[j-1]) {
throw new IllegalArgumentException(String.valueOf(indices[j]));
}
rra[j] = refGT.recs[indices[j]];
}
return new RefGT(refGT.markers, refGT.samples, rra);
}
@Override
public RefGT restrict(Markers markers, int[] indices) {
RefGTRec[] rra = new RefGTRec[indices.length];
for (int j=0; j<rra.length; ++j) {
if (j>0 && indices[j] <= indices[j-1]) {
throw new IllegalArgumentException(String.valueOf(indices[j]));
}
rra[j] = this.recs[indices[j]];
}
return new RefGT(markers, samples, rra);
}
@Override
public RefGT restrict(int start, int end) {
Markers restrictMarkers = markers.restrict(start, end);
RefGTRec[] restrictRecs = IntStream.range(start, end)
.mapToObj(j -> recs[j])
.toArray(RefGTRec[]::new);
return new RefGT(restrictMarkers, samples, restrictRecs);
}
/**
* Returns the {@code RefGTRec} for the specified marker.
* @param marker the marker index
* @return the {@code RefGTRec} for the specified marker
* @throws IndexOutOfBoundsException if
* {@code marker < 0 || marker >= this.nMarkers()}
*/
public RefGTRec get(int marker) {
return recs[marker];
}
}
|