File: SamplePhase.java

package info (click to toggle)
beagle 250227-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,364 kB
  • sloc: java: 17,684; sh: 55; makefile: 11
file content (557 lines) | stat: -rw-r--r-- 20,933 bytes parent folder | download | duplicates (2)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * 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 blbutil.BitArray;
import blbutil.DoubleArray;
import ints.IntArray;
import ints.IntList;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import vcf.Markers;

/**
 * <p>Each instance of class {@code SamplePhase} stores an estimated haplotype
 * pair for a sample.
 * </p>
 * <p>Instances of class {@code SamplePhase} are not thread-safe.
 * </p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class SamplePhase {

    public static enum ClustType {
        MISSING_GT,
        MASKED_HET,
        HOMOZYGOUS_GT,
        PHASED_HET,
        UNPHASED_HET
    }

    private static final ClustType[] clustTypes = ClustType.values();

    private final int sample;
    private final Markers markers;
    private final BitArray hap1;
    private final BitArray hap2;
    private final byte[] clustSize;
    private final byte[] clustType;
    private final int[] clustTypeCnt = new int[clustTypes.length];


    /**
     * Constructs a new {@code SamplePhase} instance from the specified data.
     * @param sample the sample index
     * @param markers the list of markers
     * @param genPos the genetic positions of the specifed markers
     * @param hap1 the list of alleles on the first haplotype
     * @param hap2 the list of alleles on the second haplotype
     * @param unphasedHets the indices of markers whose genotype phase with respect
     * to the preceding heterozygote is unknown
     * @param missingGTs the indices of markers whose genotype is missingGT
     * @throws IllegalArgumentException if {@code sample < 0}
     * @throws IllegalArgumentException if
     * {@code genPos.size() != markers.nMarkers()}
     * @throws IllegalArgumentException if
     * {@code hap1.length != markers.nMarkers() || hap2.length != markers.nMarkers()}
     * @throws IllegalArgumentException if the specified {@code unphasedHet} or
     * {@code missingGT} list is not a strictly increasing list of
     * marker indices between 0 (inclusive) and {@code markers.nMarkers()}
     * (exclusive)
     * @throws NullPointerException if any argument is {@code null}
     */
    public SamplePhase(int sample, Markers markers, DoubleArray genPos,
            int[] hap1, int[] hap2, IntArray unphasedHets, IntArray missingGTs) {
        if (sample < 0) {
            throw new IllegalArgumentException(String.valueOf(sample));
        }
        this.sample = sample;
        int nMarkers = markers.size();
        if (nMarkers!=genPos.size()) {
            throw new IllegalArgumentException(String.valueOf(genPos.size()));
        }
        if (hap1.length!=nMarkers) {
            throw new IllegalArgumentException(String.valueOf(hap1.length));
        }
        if (hap2.length!=nMarkers) {
            throw new IllegalArgumentException(String.valueOf(hap2.length));
        }
        checkIncreasing(unphasedHets, nMarkers);
        checkIncreasing(missingGTs, nMarkers);
        this.markers = markers;
        this.hap1 = new BitArray(markers.sumHapBits());
        this.hap2 = new BitArray(markers.sumHapBits());
        markers.allelesToBits(hap1, this.hap1);
        markers.allelesToBits(hap2, this.hap2);
        float maxClusterCM = 0.005f;
        IntList clustTypeList = new IntList();
        IntList clustSizeList = new IntList();
        setClusters(hap1, hap2, missingGTs, unphasedHets, genPos, maxClusterCM,
                clustTypeList, clustTypeCnt, clustSizeList);
        this.clustType = toByteArray(clustTypeList);
        this.clustSize = toByteArray(clustSizeList);
        assert clustSize.length==clustType.length;
    }

    private static void checkIncreasing(IntArray ia, int nMarkers) {
        int last = -1;
        for (int j=0, n=ia.size(); j<n; ++j) {
            if (ia.get(j)<=last) {
                throw new IllegalArgumentException(ia.toString());
            }
            last = ia.get(j);
        }
        if (last>=nMarkers) {
            throw new IllegalArgumentException(ia.toString());
        }
    }

    private static void setClusters(int[] hap1, int[] hap2, IntArray missingGT,
            IntArray unphHets, DoubleArray genPos, float maxCM,
            IntList clustType, int[] clustTypeCnt, IntList clustSizeList)  {
        int nMarkers = genPos.size();
        double maxClustEnd = genPos.get(0) + maxCM;
        boolean prevIsMissingOrHet = false;
        int lastEnd = 0;
        int missIndex = 0;
        int unphIndex = 0;
        int nextMiss = missIndex<missingGT.size() ? missingGT.get(missIndex++) : -1;
        int nextUnph = unphIndex<unphHets.size() ? unphHets.get(unphIndex++) : -1;
        ClustType prevType = ClustType.HOMOZYGOUS_GT;
        for (int m=0; m<nMarkers; ++m) {
            int size = m - lastEnd;
            ClustType type = clustType(m==nextMiss, m==nextUnph, hap1[m], hap2[m]);
            if (type==ClustType.MISSING_GT) {
                nextMiss = missIndex<missingGT.size() ? missingGT.get(missIndex++) : -1;
            }
            else if (type==ClustType.UNPHASED_HET) {
                nextUnph = unphIndex<unphHets.size() ? unphHets.get(unphIndex++) : -1;
            }
            boolean isMissingOrHet = type==ClustType.MISSING_GT
                    || type==ClustType.UNPHASED_HET
                    || type==ClustType.PHASED_HET;
            if (isMissingOrHet || prevIsMissingOrHet
                    || genPos.get(m)>maxClustEnd || size==255) {
                if (m>0) {
                    clustType.add(prevType.ordinal());
                    ++clustTypeCnt[prevType.ordinal()];
                    clustSizeList.add(size);
                    maxClustEnd = genPos.get(m) + maxCM;
                    lastEnd = m;
                }
                prevType = type;
            }
            prevIsMissingOrHet = isMissingOrHet;
        }
        clustType.add(prevType.ordinal());
        ++clustTypeCnt[prevType.ordinal()];
        clustSizeList.add(nMarkers - lastEnd);
    }

    private static ClustType clustType(boolean isMissing, boolean isUnphased,
            int a1, int a2) {
        if (isMissing) {
            return ClustType.MISSING_GT;
        }
        else if (a1==a2) {
            return ClustType.HOMOZYGOUS_GT;
        }
        else if (isUnphased) {
            return ClustType.UNPHASED_HET;
        }
        else {
           return ClustType.PHASED_HET;
        }
    }

    private static byte[] toByteArray(IntList intList) {
        byte[] ba = new byte[intList.size()];
        for (int j=0; j<ba.length; ++j) {
            ba[j] = (byte) intList.get(j);
        }
        return ba;
    }

    /**
     * Returns the sample index.
     * @return the sample index
     */
    public int sample() {
        return sample;
    }

    /**
     * Masks the trailing unphased heterozygote or heterozygotes in any maximal
     * sequence of consecutive unphased heterozygotes if the maximal sequence
     * has size two or three and spans less than 3000 base pairs.
     */
    public void maskTrailingUnphasedHets() {
        int maxUnphHetClusters = 3;
        int maxMaskedBasePairs = 3000;
        IntList unphHetMarkers = new IntList();
        IntList unphHetClusters = new IntList();
        int startMarker = 0;
        for (int c=0; c<clustType.length; ++c) {
            ClustType ct = clustType(c);
            if (ct==ClustType.PHASED_HET) {
                if (2<=unphHetClusters.size() && unphHetClusters.size()<=maxUnphHetClusters) {
                    maskTrailingUnphasedHets(unphHetClusters, unphHetMarkers, maxMaskedBasePairs);
                }
                unphHetMarkers.clear();
                unphHetClusters.clear();
            }
            else if (ct==ClustType.UNPHASED_HET) {
                unphHetMarkers.add(startMarker);
                unphHetClusters.add(c);
            }
            startMarker += clustSize[c] & 0xff;
        }
        if (2<=unphHetClusters.size() && unphHetClusters.size()<=maxUnphHetClusters) {
            maskTrailingUnphasedHets(unphHetClusters, unphHetMarkers, maxMaskedBasePairs);
        }
        assert startMarker==markers.size();
    }

    private void maskTrailingUnphasedHets(IntList unphHetClusters,
            IntList unphHetMarkers, int maxMaskedBasePairs) {
        int lastMaskedIndex = unphHetClusters.size()-2;
        if (lastMaskedIndex==0) {
            maskHetCluster(unphHetClusters.get(lastMaskedIndex));
        }
        else if (lastMaskedIndex>0) {
            int startPos = markers.marker(unphHetMarkers.get(0)).pos();
            int endPos = markers.marker(unphHetMarkers.get(lastMaskedIndex)).pos();
            if ((endPos - startPos)<=maxMaskedBasePairs) {
                for (int j=0; j<=lastMaskedIndex; ++j) {
                    maskHetCluster(unphHetClusters.get(j));
                }
            }
        }
    }

    /**
     * Masks the unphased heterozygote genotype in the specified cluster.
     * @param cluster a cluster index
     * @throws IllegalArgumentException if
     * {@code this.clustType(cluster) != ClustType.UNPHASED_HET}
     * @throws IndexOutOfBoundsException if
     * {@code (cluster < 0 || cluster >= this.nClusters())}
     */
    public void maskHetCluster(int cluster) {
        if (clustType[cluster]!=ClustType.UNPHASED_HET.ordinal()) {
            throw new IllegalArgumentException(String.valueOf(clustType[cluster]));
        }
        clustType[cluster] = (byte) ClustType.MASKED_HET.ordinal();
        --clustTypeCnt[ClustType.UNPHASED_HET.ordinal()];
        ++clustTypeCnt[ClustType.MASKED_HET.ordinal()];
    }

    /**
     * Marks the specified unphased heterozygote genotype as phased.
     * @param cluster a cluster index
     * @throws IllegalArgumentException if
     * {@code this.clustType(cluster) != ClustType.UNPHASED_HET}
     * @throws IndexOutOfBoundsException if
     * {@code (cluster < 0 || cluster >= this.nClusters())}
     */
    public void markUnphasedHetClusterAsPhased(int cluster) {
        if (clustType[cluster]!=ClustType.UNPHASED_HET.ordinal()) {
            throw new IllegalArgumentException(String.valueOf(clustType[cluster]));
        }
        clustType[cluster] = (byte) ClustType.PHASED_HET.ordinal();
        --clustTypeCnt[ClustType.UNPHASED_HET.ordinal()];
        ++clustTypeCnt[ClustType.PHASED_HET.ordinal()];
    }

    /**
     * Marks the specified masked heterozygote genotype as phased.
     * @param cluster a cluster index
     * @throws IllegalArgumentException if
     * {@code this.clustType(cluster) != ClustType.MASKED_HET}
     * @throws IndexOutOfBoundsException if
     * {@code (cluster < 0 || cluster >= this.nClusters())}
     */
    public void markMaskedHetClusterAsPhased(int cluster) {
        if (clustType[cluster]!=ClustType.MASKED_HET.ordinal()) {
            throw new IllegalArgumentException(String.valueOf(clustType[cluster]));
        }
        clustType[cluster] = (byte) ClustType.PHASED_HET.ordinal();
        --clustTypeCnt[ClustType.MASKED_HET.ordinal()];
        ++clustTypeCnt[ClustType.PHASED_HET.ordinal()];
    }

    /**
     * Returns the (exclusive) end marker indices of each marker cluster.
     * The returned list is sorted in increasing order.
     * @return the (exclusive) end marker indices of each marker cluster
     */
    public int[] clustEnds() {
        int[] clustEnds = new int[clustSize.length];
        int cumSum = 0;
        for (int j=0; j<clustSize.length; ++j) {
            cumSum += (clustSize[j] & 0xff); // convert unsigned byte to integer
            clustEnds[j] = cumSum;
        }
        return clustEnds;
    }

    /**
     * Returns the list of markers.
     * @return the list of markers
     */
    public Markers markers() {
        return markers;
    }

    /**
     * Returns the number of genotype clusters
     * @return the number of genotype clusters
     */
    public int nClusters() {
        return clustSize.length;
    }

    /**
     * Returns the size of the specified cluster
     * @param cluster a cluster index
     * @return the size of the specified cluster
     * @throws IllegalArgumentException if
     * {@code (cluster < 0 || cluster >= this.nClusters())}
     */
    public int clustSize(int cluster) {
        return clustSize[cluster] & 0xff;
    }

    /**
     * Returns the cluster type
     * @param cluster a cluster index
     * @return the cluster type
     * @throws IndexOutOfBoundsException if
     * {@code (cluster < 0 || cluster >= this.clustEnds().length())}
     */
    public ClustType clustType(int cluster) {
        return clustTypes[clustType[cluster]];
    }

    /**
     * Returns the number of unphased, non-masked heterozygotes.
     * @return the number of unphased, non-masked heterozygotes
     */
    public int nUnphased() {
        return clustTypeCnt[ClustType.UNPHASED_HET.ordinal()];
    }

    /**
     * Returns the number of phased, non-masked heterozygotes.
     * @return the number of phased, non-masked heterozygotes
     */
    public int nPhased() {
        return clustTypeCnt[ClustType.PHASED_HET.ordinal()];
    }

    /**
     * Returns the number of masked heterozygotes.
     * @return the number of masked heterozygotes
     */
    public int nMasked() {
        return clustTypeCnt[ClustType.MASKED_HET.ordinal()];
    }

    /**
     * Returns the number of missing genotypes.
     * @return the number of missing genotypes
     */
    public int nMissing() {
        return clustTypeCnt[ClustType.MISSING_GT.ordinal()];
    }

    /**
     * Returns the number of homozygote clusters.
     * @return the number of homozygote clusters
     */
    public int nHomClusters() {
        return clustTypeCnt[ClustType.HOMOZYGOUS_GT.ordinal()];
    }

    /**
     * Copies the stored haplotypes to the specified {@code BitList} objects
     * @param hap1 a {@code BitList} in which the sample's first haplotype's
     * alleles will be  stored
     * @param hap2 a {@code BitList} in which the sample's second haplotype's
     * alleles will be  stored
     * @throws IllegalArgumentException if
     * {@code hap1.size() != this.markers().sumHaplotypeBits()}
     * @throws IllegalArgumentException if
     * {@code hap2.size()!= this.markers().sumHaplotypeBits()}
     * @throws NullPointerException if {@code hap1 == null || hap2 == null}
     */
    public void getHaps(BitArray hap1, BitArray hap2) {
        int nBits = markers.sumHapBits();
        if (hap1.size() != nBits || hap2.size() != nBits) {
            throw new IllegalArgumentException("inconsistent data");
        }
        hap1.copyFrom(this.hap1, 0, this.hap1.size());
        hap2.copyFrom(this.hap2, 0, this.hap2.size());
    }


    /**
     * Returns the allele on the first haplotype for the specified marker.
     * @param marker the marker index
     * @return the allele on the first haplotype for the specified marker
     * @throws IndexOutOfBoundsException if
     * {@code marker < 0 || marker >= this.markers().nMarkers()}
     */
    public int allele1(int marker) {
       return markers.allele(hap1, marker);
    }

    /**
     * Returns the allele on the second haplotype for the specified marker.
     * @param marker the marker index
     * @return the allele on the second haplotype for the specified marker
     * @throws IndexOutOfBoundsException if
     * {@code marker < 0 || marker >= this.markers().nMarkers()}
     */
    public int allele2(int marker) {
        return markers.allele(hap2, marker);
    }

    /**
     * Sets the allele on the first haplotype for the specified marker
     * to the specified allele
     * @param marker the marker index
     * @param allele the allele
     * @throws IndexOutOfBoundsException if
     * {@code marker < 0 || marker >= this.markers().nMarkers()}
     * @throws IndexOutOfBoundsException if
     * {@code allele < 0 || allele >= this.markers().marker(marker).nAlleles()}
     */
    public void setAllele1(int marker, int allele) {
        markers.setAllele(marker, allele, hap1);
    }

    /**
     * Sets the allele on the second haplotype for the specified marker
     * to the specified allele
     * @param marker the marker index
     * @param allele the allele
     * @throws IndexOutOfBoundsException if
     * {@code marker < 0 || marker >= this.markers().nMarkers()}
     * @throws IndexOutOfBoundsException if
     * {@code allele < 0 || allele >= this.markers().marker(marker).nAlleles()}
     */
    public void setAllele2(int marker, int allele) {
        markers.setAllele(marker, allele, hap2);
    }

    /**
     * Swaps the alleles of the two haplotypes in the specified range of
     * markers.
     * @param start the start marker index (inclusive)
     * @param end the end marker index (exclusive)
     * @throws IndexOutOfBoundsException if
     * {@code start < 0 || start > end || start >= this.markers().nMarkers()}
     */
    public void swapHaps(int start, int end) {
        int startBit = markers.sumHapBits(start);
        int endBit = markers.sumHapBits(end);
        BitArray.swapBits(hap1, hap2, startBit, endBit);
    }

    /**
     * Returns the first haplotype.  The haplotype is encoded with the
     * {@code this.markers().allelesToBits()} method.
     * @return the first haplotype
     */
    public BitArray hap1() {
        return new BitArray(this.hap1);
    }

    /**
     * Returns the second haplotype.  The haplotype is encoded with the
     * {@code this.markers().allelesToBits()} method.
     * @return the second haplotype
     */
    public BitArray hap2() {
        return new BitArray(this.hap2);
    }

    /**
     * Returns the current estimated phased genotypes.  This method converts
     * column-major data into row-major data.
     * @param estPhase the current estimated phased genotypes for each target
     * sample
     * @return the current estimated phased genotypes
     * @throws NullPointerException if {@code estPhase == null}
     */
    public static BitArray[] toBitLists(EstPhase estPhase) {
        int nThreads = estPhase.fpd().par().nthreads();
        int nMarkers = estPhase.fpd().stage1TargGT().nMarkers();
        int nRecsPerBatch = (nMarkers + nThreads - 1)/nThreads;
        while (nRecsPerBatch>4096) {
            nRecsPerBatch = (nRecsPerBatch+1) >> 1;
        }
        int stepSize = nRecsPerBatch;
        int nSteps = (nMarkers + (stepSize-1)) / stepSize;
        return IntStream.range(0, nSteps)
                .parallel()
                .boxed()
                .flatMap(step -> bitLists(estPhase, step, stepSize))
                .toArray(BitArray[]::new);
    }

    private static Stream<BitArray> bitLists(EstPhase estPhase, int step, int stepSize) {
        int nSamples = estPhase.fpd().targGT().nSamples();
        int nHaps = nSamples<<1;
        Markers markers = estPhase.fpd().stage1TargGT().markers();
        int mStart = step*stepSize;
        int mEnd = Math.min(mStart + stepSize, markers.size());
        BitArray[] bitLists = IntStream.range(mStart, mEnd)
                .mapToObj(j -> new BitArray(nHaps*markers.marker(j).bitsPerAllele()))
                .toArray(BitArray[]::new);
        int[] bitsPerAllele = IntStream.range(mStart, mEnd)
                .map(m -> markers.marker(m).bitsPerAllele())
                .toArray();
        for (int s=0; s<nSamples; ++s) {
            SamplePhase sampPhase = estPhase.get(s);
            int h1 = s<<1;
            int h2 = h1 | 0b1;
            int inBit1 = markers.sumHapBits(mStart);
            int inBit2 = markers.sumHapBits(mStart);
            for (int m=mStart; m<mEnd; ++m) {
                int mOffset = m - mStart;
                int nBits = bitsPerAllele[mOffset];
                int startOutBit1 = h1*nBits;
                int startOutBit2 = h2*nBits;
                for (int i=0; i<nBits; ++i) {
                    if (sampPhase.hap1.get(inBit1++)) {
                        bitLists[mOffset].set(startOutBit1 + i);
                    }
                    if (sampPhase.hap2.get(inBit2++)) {
                        bitLists[mOffset].set(startOutBit2 + i);
                    }
                }
            }
        }
        return Arrays.stream(bitLists);
    }
}