File: Window.java

package info (click to toggle)
beagle 241217-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,712 kB
  • sloc: java: 17,684; sh: 55; makefile: 11
file content (244 lines) | stat: -rw-r--r-- 9,149 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
/*
 * 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 ints.IntArray;
import ints.IntList;
import ints.WrappedIntArray;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.IntStream;

/**
 * Class {@code Window} represents a sliding window of target VCF records
 * or a sliding window of reference and target VCF records.
 *
 * <p>Instances of class {@code Window} are immutable.</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public class Window {

    /**
     * An array that replaces an array of allele counts for a monomorphic
     * marker.
     */
    public static final IntArray ZERO_FREQ_ARRAY = new WrappedIntArray(new int[0]);

    /**
     * An array that replaces an array of allele counts for a marker
     * with high minor allele frequency.
     */
    public static final IntArray HIGH_FREQ_ARRAY = new WrappedIntArray(new int[0]);

    private final GeneticMap genMap;
    private final int windowIndex;
    private final boolean lastWindow;
    private final MarkerIndices indices;

    private final BasicGT targGT;
    private final RefGT refGT;          // null if no reference haplotypes
    private final RefGT restrictRefGT;  // null if no reference haplotypes

    /**
     * Constructs a new {@code Window} instance from the specified data.
     * @param genMap the genetic map
     * @param windowIndex the window index
     * @param lastWindow {@code true} if this window is the last window
     * in the analysis
     * @param markerIndices marker indices of overlap regions and splice points
     * @param targGT the target genotype data
     * @param refGT the reference genotype data or {@code null} if there is
     * no reference genotype data
     * @throws IllegalArgumentException if
     * {@code markerIndices.nTargMarkers() != targGT.nMarkers()}
     * @throws IllegalArgumentException if
     * {@code refGT != null && markerIndices.nMarkers() != refGT.nMarkers()}
     * @throws NullPointerException if
     * {@code genMap == null || markerIndices == null || targGT == null}
     */
    public Window(GeneticMap genMap, int windowIndex, boolean lastWindow,
            MarkerIndices markerIndices, RefGT refGT, BasicGT targGT) {
        if (genMap==null) {
            throw new NullPointerException(GeneticMap.class.toString());
        }
        if (targGT.nMarkers()!=markerIndices.nTargMarkers()) {
            throw new IllegalArgumentException("inconsistent data");
        }
        if (refGT!=null && refGT.nMarkers()!=markerIndices.nMarkers()) {
            throw new IllegalArgumentException("inconsistent data");
        }
        this.genMap = genMap;
        this.windowIndex = windowIndex;
        this.lastWindow = lastWindow;
        this.indices = markerIndices;
        this.targGT = targGT;
        this.refGT = refGT;
        this.restrictRefGT = refGT==null ? null :
                refGT.restrict(targGT.markers(), markerIndices.targMarkerToMarker());
    }

    /**
     * Returns the genetic map.
     * @return the genetic map
     */
    public GeneticMap genMap() {
        return genMap;
    }

    /**
     * Returns the chromosome index for the markers in this window.
     * @return the chromosome index for the markers in this window
     */
    public int chromIndex() {
        return targGT.marker(0).chromIndex();
    }

    /**
     * Returns {@code true} if this window is the last window in the analysis,
     * and returns {@code false} otherwise.
     * @return {@code true} if this window is the last window in the analysis
     */
    public boolean lastWindow() {
        return lastWindow;
    }

    /**
     * Returns the index of this window. The first window has index 1.
     * @return the index of this window.
     */
    public int windowIndex() {
        return windowIndex;
    }

    /**
     * Returns the input genotypes for the target samples
     * in this window.
     * @return the input genotypes for the target samples
     * in this window
     */
    public BasicGT targGT() {
        return targGT;
    }

    /**
     * Returns the optional phased, nonmissing input genotypes for the reference
     * samples in this window.
     * @return the optional phased, nonmissing input genotypes for the reference
     * samples in this window
     */
    public Optional<RefGT> refGT() {
        return refGT==null ? Optional.empty() : Optional.of(refGT);
    }

    /**
     * Returns the optional phased, nonmissing input genotypes for the reference
     * samples restricted to the target data markers in this window.
     * @return the optional phased, nonmissing input genotypes for the reference
     * samples restricted to the target data markers in this window
     */
    public Optional<RefGT> restrictRefGT() {
        return restrictRefGT==null ? Optional.empty()
                : Optional.of(restrictRefGT);
    }

    /**
     * Return a {@code MarkerIndices} instance which stores the overlap
     * and splice points between this window and the preceding
     * and next windows, and the map between reference and target marker
     * indices.
     * @return the {@code MarkerIndices} for this window
     */
    public MarkerIndices indices() {
        return indices;
    }

    /**
     * <p>Returns the indices of the reference and target carriers for each
     * low-frequency allele at the target data markers.  The reference sample
     * indices will be shifted by the number of target samples so that the
     * first reference sample will have an index equal to the number of target
     * samples. An element of the returned array will be empty and equal to
     * {@code Window.ZERO_FREQ_ARRAY} if the allele has no carriers, and the
     * the element will be empty and equal to {@code Window.HIGH_FREQ_ARRAY}
     * if the number of carriers of the allele exceeds the specified
     * maximum number of carriers.</p>
     *
     * <p>The list of carriers for the {@code k}-th allele of the {@code j}-th
     * target marker are stored in entry {@code (j, k)} of the returned array.
     * if the number of carriers is less than or equal to the specified
     * maximum number of carriers.</p>
     * @param maxCarriers the maximum number of carriers in any list
     * of the returned array.
     * @return the indices of the reference and target carriers for each
     * low-frequency allele
     */
    public IntArray[][] carriers(int maxCarriers) {
        return IntStream.range(0, targGT.nMarkers())
                .parallel()
                .mapToObj(j -> carriers(j, maxCarriers))
                .toArray(IntArray[][]::new);
    }

    private IntArray[] carriers(int m, int maxCarriers) {
        int nAlleles = targGT.marker(m).nAlleles();
        IntList[] carriers = IntStream.range(0, nAlleles)
                .mapToObj(i -> new IntList(16))
                .toArray(IntList[]::new);
        int nTargSamples = targGT.nSamples();
        int nRefSamples = (restrictRefGT!=null) ? restrictRefGT.nSamples() : 0;
        for (int s=0; s<nTargSamples; ++s) {
            int hap1 = s << 1;
            int a1 = targGT.allele(m, hap1);
            int a2 = targGT.allele(m, hap1 | 0b1);
            if (a1>=0 && carriers[a1].size()<=maxCarriers) {
                carriers[a1].add(s);
            }
            if (a2>=0 && a2!=a1 && carriers[a2].size()<=maxCarriers) {
                carriers[a2].add(s);
            }
        }
        if (restrictRefGT!=null) {
            for (int s=0; s<nRefSamples; ++s) {
                int hap1 = s << 1;
                int a1 = restrictRefGT.allele(m, hap1);
                int a2 = restrictRefGT.allele(m, hap1 | 0b1);
                if (a1>=0 && carriers[a1].size()<=maxCarriers) {
                    carriers[a1].add(nTargSamples + s);
                }
                if (a2>=0 && a2!=a1 && carriers[a2].size()<=maxCarriers) {
                    carriers[a2].add(nTargSamples + s);
                }
            }
        }
        return Arrays.stream(carriers)
                .map(list -> {
                    if (list.isEmpty()) {
                        return ZERO_FREQ_ARRAY;
                    }
                    else if (list.size() <= maxCarriers) {
                        return new WrappedIntArray(list);
                    }
                    else {
                        return HIGH_FREQ_ARRAY;
                    }
                })
                .toArray(IntArray[]::new);
    }
}