File: Steps.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 (108 lines) | stat: -rw-r--r-- 3,335 bytes parent folder | download | duplicates (3)
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
/*
 * 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 blbutil.DoubleArray;
import ints.IntList;

/**
 * <p>Class {@code Steps} represents a partition of a list of markers into
 * a sequence of sets of consecutive markers (the steps).</p>
 *
 * <p>Instances of class {@code Steps} are immutable.</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public class Steps {

    private final MarkerMap map;
    private final int[] stepEnds;

    /**
     * Constructs a new {@code Steps} instance from the specified data.
     * @param map the marker map
     * @param minStep the minimum distance between the first markers in
     * consecutive steps
     *
     * @throws IllegalArgumentException if
     * {@code minStep <= 0f || Float.isFinite(minStep) == false}
     * @throws NullPointerException if {@code map == null}
     */
    public Steps(MarkerMap map, float minStep) {
        if (minStep <= 0f || Float.isFinite(minStep)==false) {
            throw new IllegalArgumentException(String.valueOf(minStep));
        }
        this.map = map;
        this.stepEnds = stepEnds(map, minStep);
    }

     private static int[] stepEnds(MarkerMap map, double minStep) {
        DoubleArray genPos = map.genPos();
        int nMarkers = genPos.size();
        IntList indices = new IntList(nMarkers>>1);
        int end = 0;
        while (end<nMarkers) {
            double minGenPos = genPos.get(end) + minStep;
            ++end;
            while (end<genPos.size() && genPos.get(end)<minGenPos) {
                ++end;
            }
            indices.add(end);
        }
        return indices.toArray();
    }

    /**
     * Returns the number of steps.
     * @return the number of steps
     */
    public int size() {
        return stepEnds.length;
    }

    /**
     * Returns the index of the first marker in the specified step.
     * @param step a step index
     * @return the index of the first marker in the specified step
     * @throws IllegalArgumentException if
     * {@code step < 0 || step >= this.nSteps()}
     */
    public int start(int step) {
        return step==0 ? 0 : stepEnds[step-1];
    }

    /**
     * Returns the index of the last marker (exclusive) in the specified step.
     * @param step a step index
     * @return the index of the last marker (exclusive) in the specified step
     * @throws IllegalArgumentException if
     * {@code step < 0 || step >= this.nSteps()}
     */
    public int end(int step) {
        return stepEnds[step];
    }

    /**
     * Return the marker map.
     * @return the marker map
     */
    public MarkerMap map() {
        return map;
    }
}