File: RestrictedGT.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 (131 lines) | stat: -rw-r--r-- 4,093 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
/*
 * 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.stream.IntStream;

/**
 * <p>Class {@code RestrictedGT} is a wrapper for a {@code GT}
 * instance that restricts the data to a subset of the VCF records.
 * </p>
 * <p>Instances of class {@code RestrictGT} are immutable.
 * </p>
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public class RestrictedGT implements GT {

    private final GT gt;
    private final Markers restrictedMarkers;
    private final int[] inclusionMap;

    /**
     * Constructs a new {@code RestrictedGT} instance from the specified
     * data
     * @param gt the genotypes to be wrapped
     * @param markers the list of markers in the returned instance
     * @param indices the mapping of marker indices from {@code markers}
     * to {@code gt.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 IllegalArgumentException if there exists {@code j} such that
     * {@code (0 <= j && j < indices.length)} such that
     * {@code (gt.marker(indices[j]).equals(markers.marker(j)) == false)}
     * @throws NullPointerException if
     * {@code (gt == null || markers == null || include == null)}
     */
    public RestrictedGT(GT gt, Markers markers, int[] indices) {
        for (int j=0; j<indices.length; ++j) {
            if (j>0 && indices[j] <= indices[j-1]) {
                throw new IllegalArgumentException(String.valueOf(indices[j]));
            }
            if (gt.marker(indices[j]).equals(markers.marker(j))==false) {
                throw new IllegalArgumentException(markers.marker(j).toString());
            }
        }
        this.gt = gt;
        this.restrictedMarkers = markers;
        this.inclusionMap = indices.clone();
    }

    @Override
    public boolean isReversed() {
        return false;
    }

    @Override
    public int nMarkers() {
        return restrictedMarkers.size();
    }

    @Override
    public Marker marker(int marker) {
        return restrictedMarkers.marker(marker);
    }

    @Override
    public Markers markers() {
        return restrictedMarkers;
    }

    @Override
    public int nHaps() {
        return gt.nHaps();
    }

    @Override
    public int nSamples() {
        return gt.nSamples();
    }

    @Override
    public Samples samples() {
        return gt.samples();
    }

    @Override
    public boolean isPhased() {
        return gt.isPhased();
    }

    @Override
    public int allele(int marker, int hap) {
        return gt.allele(inclusionMap[marker], hap);
    }

    @Override
    public RestrictedGT restrict(Markers markers, int[] indices) {
        return new RestrictedGT(this, markers, indices);
    }

    @Override
    public RestrictedGT restrict(int start, int end) {
        Markers restrictMarkers = restrictedMarkers.restrict(start, end);
        int[] indices = IntStream.range(start, end).toArray();
        return new RestrictedGT(this, restrictMarkers, indices);
    }

    @Override
    public String toString() {
        return RestrictedGT.class.toString() + " : " + gt.toString();
    }
}