File: PbwtPhaseIbs.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 (271 lines) | stat: -rw-r--r-- 10,906 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
/*
 * 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 vcf.Steps;
import beagleutil.PbwtDivUpdater;
import ints.IndexArray;
import ints.WrappedIntArray;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;
import vcf.XRefGT;

/**
 * <p>Class {@code PbwtPhaseIBS} uses the Positional Burrows-Wheeler
 * Transform (PBWT) to find long IBS haplotypes for each sample that
 * contain a specified small genomic interval.</p>
 *
 * <p>Instances of class {@code PbwtPhaseIbs} are thread-safe.</p>
 *
 * <p>Reference: Durbin, R. 2014. Bioinformatics 30(9):1266–1272.
 * doi:10.1093/bioinformatics/btu014</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class PbwtPhaseIbs {

    private final PhaseData phaseData;
    private final XRefGT allHaps;
    private final WrappedIntArray[] ibsHaps;  //[step][targ hap]

    /**
     * Constructs a new {@code PbwtPhaseIBS} instance from the
     * specified data.
     * @param phaseData the current genotype phase estimates and parameter
     * values
     * @param codedSteps the coded steps
     * @param useBwd {@code true} if last-to-first PBWT should be used
     * @throws IllegalArgumentException if
     * {@code phaseData.fpd().stage1Steps() != codedSteps.steps()}
     * @throws IllegalArgumentException if
     * {@code phaseData.fpd().stage1XRefGT()!=codedSteps.refHaps()}
     * @throws IllegalArgumentException if
     * {@code phaseData.fpd().targGT().samples()!=codedSteps.targSamples()}
     * @throws NullPointerException if
     * {@code phaseData == null || codedSteps == null}
     */
    public PbwtPhaseIbs(PhaseData phaseData, CodedSteps codedSteps,
            boolean useBwd) {
        checkConsistency(phaseData, codedSteps);
        this.phaseData = phaseData;
        this.allHaps = codedSteps.allHaps();
        PbwtIbsData data = new PbwtIbsData(phaseData, codedSteps);
        if (useBwd) {
            this.ibsHaps = IntStream.range(0, data.nBatches())
                    .parallel()
                    .mapToObj(j -> bwdIbsHaps(data, j))
                    .flatMap(a -> Arrays.stream(a))
                    .toArray(WrappedIntArray[]::new);
        }
        else {
            this.ibsHaps = IntStream.range(0, data.nBatches())
                    .parallel()
                    .mapToObj(j -> fwdIbsHaps(data, j))
                    .flatMap(a -> Arrays.stream(a))
                    .toArray(WrappedIntArray[]::new);
        }
    }

    private static void checkConsistency(PhaseData phaseData,
            CodedSteps codedSteps) {
        FixedPhaseData fpd = phaseData.fpd();
        if (fpd.stage1Steps()!=codedSteps.steps()
                || fpd.stage1XRefGT()!=codedSteps.refHaps()
                || fpd.targGT().samples()!=codedSteps.targSamples()) {
            throw new IllegalArgumentException("inconsistent data");
        }
    }

    private WrappedIntArray[] bwdIbsHaps(PbwtIbsData data, int batch) {
        int startStep = data.startStep(batch);
        int endStep = data.endStep(batch);
        int bufferEndStep = data.bufferEndStep(endStep);

        WrappedIntArray[] ibsHaps0 = new WrappedIntArray[endStep - startStep];
        int nHaps = data.nHaps();
        PbwtDivUpdater pbwt = new PbwtDivUpdater(nHaps);
        int[] a = IntStream.range(0, nHaps).toArray();
        int[] d = IntStream.range(0, nHaps+1).map(j -> (bufferEndStep-1)).toArray(); // last entry is sentinal

        for (int j=(bufferEndStep-1); j>=endStep; --j) {
            IndexArray ia = data.codedSteps().get(j);
            pbwt.bwdUpdate(ia, ia.valueSize(), j, a, d);
        }
        for (int j=(endStep-1); j>=startStep; --j) {
            IndexArray ia = data.codedSteps().get(j);
            pbwt.bwdUpdate(ia, ia.valueSize(), j, a, d);
            ibsHaps0[j-startStep] = getBwdIbsHaps(j, a, d, data);
        }
        return ibsHaps0;
    }

    private WrappedIntArray[] fwdIbsHaps(PbwtIbsData data, int batch) {
        int startStep = data.startStep(batch);
        int endStep = data.endStep(batch);
        int bufferStartStep = data.bufferStartStep(startStep);

        WrappedIntArray[] ibsHaps0 = new WrappedIntArray[endStep - startStep];
        int nHaps = data.nHaps();
        PbwtDivUpdater pbwt = new PbwtDivUpdater(nHaps);
        int[] a = IntStream.range(0, nHaps).toArray();
        int[] d = IntStream.range(0, nHaps+1).map(j -> bufferStartStep).toArray(); // last entry is sentinal

        for (int j=bufferStartStep; j<startStep; ++j) {
            IndexArray ia = data.codedSteps().get(j);
            pbwt.fwdUpdate(ia, ia.valueSize(), j, a, d);
        }
        for (int j=startStep; j<endStep; ++j) {
            IndexArray ia = data.codedSteps().get(j);
            pbwt.fwdUpdate(ia, ia.valueSize(), j, a, d);
            ibsHaps0[j-startStep] = getfwdIbsHaps(j, a, d, data);
        }
        return ibsHaps0;
    }

    private WrappedIntArray getBwdIbsHaps(int step, int[] a, int[] d, PbwtIbsData data) {
        Random rand = new Random(phaseData.seed() + step);
        int mStart = data.codedSteps().steps().start(step);
        int mInclEnd = data.codedSteps().steps().end(step) - 1;
        int[] selectedHaps = new int[data.nTargHaps()];
        Ibs2 ibs2 = phaseData.fpd().stage1Ibs2();
        d[0] = d[a.length] = step - 2;  // set sentinals
        // no need to save and restore old d[0], d[a.length] values
        for (int i=0; i<a.length; ++i) {
            if (a[i]<data.nTargHaps()) {
                int hap = a[i];
                int s1 = hap>>1;
                int u = i;          // inclusive start
                int v = i + 1;      // exclusive end
                int uNextMatchEnd = d[u];
                int vNextMatchEnd = d[v];
                while ((v - u)<data.nCandidates()
                        && (step<=uNextMatchEnd || step<=vNextMatchEnd)) {
                    if (uNextMatchEnd<=vNextMatchEnd) {
                        vNextMatchEnd = Math.min(d[++v], vNextMatchEnd);
                    }
                    else {
                        uNextMatchEnd = Math.min(d[--u], uNextMatchEnd);
                    }
                }
                int n = v-u;
                selectedHaps[hap] = -1;
                if (n>1) {
                    int index = u + rand.nextInt(n);
                    for (int j=0; j<n; ++j, ++index) {
                        if (index==v) {
                            index = u;
                        }
                        if (index!=i) {
                            if (ibs2.areIbs2(s1, a[index]>>1, mStart, mInclEnd)==false) {
                                selectedHaps[hap] = a[index];
                                break;
                            }
                        }
                    }
                }
            }
        }
        return new WrappedIntArray(selectedHaps);
    }

    private WrappedIntArray getfwdIbsHaps(int step, int[] a, int[] d,
            PbwtIbsData data) {
        Steps steps = phaseData.fpd().stage1Steps();
        Random rand = new Random(phaseData.seed() + step);
        int nTargHaps = phaseData.fpd().targGT().nHaps();
        int mStart = steps.start(step);
        int mInclEnd = steps.end(step) - 1;
        int[] selectedHaps = new int[nTargHaps];
        Ibs2 ibs2 = phaseData.fpd().stage1Ibs2();
        d[0] = d[a.length] = step + 2;  // set sentinals
        // no need to save and restore old d[0], d[a.length] values
        for (int i=0; i<a.length; ++i) {
            if (a[i]<nTargHaps) {
                int hap = a[i];
                int s1 = hap>>1;
                int u = i;          // inclusive start
                int v = i + 1;      // exclusive end
                int uNextMatchStart = d[u];
                int vNextMatchStart = d[v];
                while ((v - u)<data.nCandidates()
                        && (uNextMatchStart<=step || vNextMatchStart<=step)) {
                    if (vNextMatchStart<=uNextMatchStart) {
                        vNextMatchStart = Math.max(d[++v], vNextMatchStart);
                    }
                    else {
                        uNextMatchStart = Math.max(d[--u], uNextMatchStart);
                    }
                }
                int n = v-u;
                selectedHaps[hap] = -1;
                if (n>1) {
                    int index = u + rand.nextInt(n);
                    for (int j=0; j<n; ++j, ++index) {
                        if (index==v) {
                            index = u;
                        }
                        if (index!=i) {
                            if (ibs2.areIbs2(s1, a[index]>>1, mStart, mInclEnd)==false) {
                                selectedHaps[hap] = a[index];
                                break;
                            }
                        }
                    }
                }
            }
        }
        return new WrappedIntArray(selectedHaps);
    }

    /**
     * Returns the current genotype phase estimates and parameter values.
     * @return the current genotype phase estimates and parameter values
     */
    public PhaseData phaseData() {
        return phaseData;
    }

    /**
     * Returns the estimated phased genotypes for the target and reference
     * samples.
     * @return the estimated phased genotypes for the target and reference
     * samples
     */
    public XRefGT allHaps() {
        return allHaps;
    }

    /**
     * Returns the index of a haplotype that is identical by state
     * with the specified target haplotype in the specified genomic interval,
     * or {@code -1} if there is no identical-by-state haplotype.
     * @param hap a target haplotype index
     * @param step an index of a genomic interval
     * @return the index of a haplotype that is identical by state
     * with the specified haplotype int the specified genomic interval
     * @throws IndexOutOfBoundsException if
     * {@code hap < 0 || hap >= this.phaseData().targGT().nHaps()}
     * @throws IndexOutOfBoundsException if
     * {@code step < 0 || step >= this.phaseData().fpd().stage1Steps().size()}
     */
    public int ibsHap(int hap, int step) {
        return ibsHaps[step].get(hap);
    }
}