File: Ibs2.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 (262 lines) | stat: -rw-r--r-- 10,048 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
/*
 * 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.DoubleArray;
import blbutil.FloatArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import vcf.GT;
import vcf.MarkerMap;

/**
 * <p>Class {@code Ibs2} stores IBS2 segments that any target sample shares
 * with another target sample.</p>
 *
 * <p>Instances of {@code Ibs2} are immutable.</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class Ibs2 {

    private static final float MIN_IBS2_CM = 2.0f;
    private static final float MAX_IBD_GAP_CM = 4.0f;
    private static final Comparator<SampleSeg> sampComparator = SampleSeg.sampleComp();

    private final int nMarkers;
    private final SampleSeg[][] sampleSegs; // [targ sample][segment]

    /**
     * Constructs a new {@code Ibs2} instance from the specified data.
     * @param targGT the target genotype data
     * @param map a list whose {@code j}-th element is the genetic map position
     * of the {@code j}-th the marker
     * @param maf a list whose {@code j}-th element is the estimated
     * minor allele frequency of the {@code j}-th the marker
     * @throws IllegalArgumentException if
     * {@code targGT.nMarkers() != map.genPos().size()}
     * @throws IllegalArgumentException if
     * {@code targGT.nMarkers() != maf.size()}
     * @throws NullPointerException if
     * {@code (targGT == null || map == null || maf == null)}
     */
    public Ibs2(GT targGT, MarkerMap map, FloatArray maf) {
        Ibs2Markers ibs2Markers = new Ibs2Markers(targGT, map, maf);
        Ibs2Sets ibs2Sets = new Ibs2Sets(targGT, ibs2Markers);
        DoubleArray genPos = map.genPos();
        this.nMarkers = targGT.nMarkers();
        this.sampleSegs = IntStream.range(0, targGT.nSamples())
                .parallel()
                .mapToObj(s -> ibs2Segments(targGT, genPos, ibs2Sets, s))
                .toArray(SampleSeg[][]::new);
    }

    private static SampleSeg[] ibs2Segments(GT targGT,  DoubleArray genPos,
            Ibs2Sets ibs2Sets, int sample) {
        SampleSeg[] segList = ibs2Sets.segList(sample);
        Arrays.sort(segList, sampComparator);
        segList = mergeSegments(segList, genPos);
        segList = extendSegments(targGT, sample, segList);
        segList = mergeSegments(segList, genPos);
        return applyLengthFilter(segList, genPos);
    }

    private static SampleSeg[] mergeSegments(SampleSeg[] list, DoubleArray genPos)  {
        if (list.length<2) {
            return list;
        }
        List<SampleSeg> merged = new ArrayList<>();
        SampleSeg prev = list[0];
        for (int j=1; j<list.length; ++j) {
            SampleSeg next = list[j];
            if (prev.sample()==next.sample()
                    && gapCM(prev, next, genPos) <= MAX_IBD_GAP_CM) {
                // Two segments can have identical end markers after extension
                // because homozygous regions are excluded in initial IBS2
                // discovery but are included in extendSegments() method.
                assert prev.inclEnd()<=next.inclEnd();
                prev = new SampleSeg(prev.sample(), prev.start(), next.inclEnd());
            }
            else {
                merged.add(prev);
                prev = next;
            }
        }
        merged.add(prev);
        return merged.toArray(new SampleSeg[0]);
    }

    private static double gapCM(SampleSeg prev, SampleSeg next,
            DoubleArray genPos) {
        return genPos.get(next.start()) - genPos.get(prev.inclEnd());
    }

    private static SampleSeg[] extendSegments(GT targGT, int sample,
            SampleSeg[] list) {
        return Arrays.stream(list)
                .map(ss -> extend(targGT, sample, ss))
                .toArray(SampleSeg[]::new);
        }

    private static SampleSeg extend(GT targGT, int sample, SampleSeg ss) {
        int nMarkers = targGT.nMarkers();
        int sample2 = ss.sample();
        int inclStart = ss.start();
        int exclEnd = ss.inclEnd() + 1;
        while (inclStart>0 && ibs2(targGT, inclStart-1, sample, sample2)) {
            --inclStart;
        }
        while (exclEnd<nMarkers && ibs2(targGT, exclEnd, sample, sample2)) {
            ++exclEnd;
        }
        return new SampleSeg(sample2, inclStart, exclEnd - 1);
    }

    private static boolean ibs2(GT targGT, int m, int s1, int s2) {
        int hap1 = s1 << 1;
        int hap2 = s2 << 1;
        int a1 = targGT.allele(m, hap1);
        int a2 = targGT.allele(m, hap1 | 0b1);
        int b1 = targGT.allele(m, hap2);
        int b2 = targGT.allele(m, hap2 | 0b1);
        return arePhaseConsistent(a1, a2, b1, b2)
                || arePhaseConsistent(a1, a2, b2, b1);
    }

    private static boolean arePhaseConsistent(int a1, int a2, int b1, int b2) {
        return (a1<0 || b1<0 || a1==b1) && (a2<0 || b2<0 || a2==b2);
    }

    private static SampleSeg[] applyLengthFilter(SampleSeg[] list, DoubleArray genPos) {
        Predicate<SampleSeg> predicate =
                ss -> (genPos.get(ss.inclEnd()) - genPos.get(ss.start())) >= MIN_IBS2_CM;
        return Arrays.stream(list)
                .filter(predicate)
                .toArray(SampleSeg[]::new);
    }

    /**
     * Returns the number of markers.
     * @return the number of markers
     */
    public int nMarkers() {
        return nMarkers;
    }

    /**
     * Returns the number of target samples.
     * @return the number of target samples
     */
    public int nTargSamples() {
        return sampleSegs.length;
    }

    /**
     * Returns the number of IBS2 segments for the specified target sample
     * @param targSample a target sample index
     * @return the number of IBS2 segments for the specified target sample
     * @throws IndexOutOfBoundsException if
     * {@code targSample < 0 || targSample >= this.nTargSamples()}
     */
    public int nIbs2Segments(int targSample) {
        return sampleSegs[targSample].length;
    }

    /**
     * Returns {@code true} if {@code (otherSample < this.nTargSamples()}
     * and the specified samples are estimated to be IBS2 at the specified
     * marker, and returns {@code false} otherwise.
     * @param targSample a target sample index
     * @param otherSample a sample index
     * @param marker a marker index
     * @return {@code true} if the specified samples are estimated
     * to be IBD2 at the specified marker
     * @throws IndexOutOfBoundsException if
     * {@code (targSample < 0 || targSample >= this.nTargSamples())}
     * @throws IndexOutOfBoundsException if {@code otherSample < 0}
     * @throws IndexOutOfBoundsException if
     * {@code (marker < 0 || marker >= this.nMarkers())}
     */
    public boolean areIbs2(int targSample, int otherSample, int marker) {
        if (targSample>=sampleSegs.length) {
            throw new IndexOutOfBoundsException(String.valueOf(targSample));
        }
        if (otherSample<0) {
            throw new IndexOutOfBoundsException(String.valueOf(otherSample));
        }
        if (marker<0 || marker >= nMarkers) {
            throw new IndexOutOfBoundsException(String.valueOf(marker));
        }
        if (targSample==otherSample) {
            return true;
        }
        if (sampleSegs[targSample].length>0 && otherSample<sampleSegs.length) {
            for (SampleSeg ss : sampleSegs[targSample]) {
                if (ss.sample()==otherSample) {
                    if (ss.start()<=marker && marker<=ss.inclEnd()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Returns {@code true} if
     * {@code (0 <= otherSample && otherSample < this.nTargSamples())}
     * and the specified samples are estimated to be IBS2 within the
     * specified interval and returns {@code false} otherwise.
     * @param targSample a target sample index
     * @param otherSample a sample index
     * @param start the starting marker index
     * @param inclEnd the ending marker index (inclusive)
     * @return {@code true} if
     * {@code (0 <= otherSample && otherSample < this.nTargSamples())}
     * and the specified samples are estimated to be IBS2 within the
     * specified interval
     * @throws IndexOutOfBoundsException if
     * {@code (targSample < 0 || targSample >= this.nTargSamples())}
     * @throws IndexOutOfBoundsException if
     * {@code start > inclEnd}
     */
    public boolean areIbs2(int targSample, int otherSample, int start, int inclEnd) {
        if (start>inclEnd) {
            throw new IndexOutOfBoundsException(String.valueOf(start));
        }
        final boolean sameSample = targSample==otherSample;
        // "sampleSegs[targSample].length==0" comparison must come first in following
        // conditional statement so that targSample is checked for out-of-bounds error
        if (sampleSegs[targSample].length==0 || sameSample) {
            return sameSample;
        }
        for (SampleSeg ss : sampleSegs[targSample]) {
            if (ss.sample()==otherSample) {
                if (start<=ss.inclEnd() && ss.start()<=inclEnd) {
                    return true;
                }
            }
        }
        return false;
    }
}