File: IntervalVcfIt.java

package info (click to toggle)
beagle 220722-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,644 kB
  • sloc: java: 17,045; sh: 55; makefile: 11
file content (138 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download
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
/*
 * 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 beagleutil.ChromInterval;
import blbutil.Const;
import blbutil.SampleFileIt;
import java.io.File;
import java.util.NoSuchElementException;

/**
 * <p>Class {@code IntervalVcfIterator} is a sample file iterator whose
 * {@code next()} method returns a marker container.</p>
 *
 * @param <E> the type parameter
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class IntervalVcfIt<E extends MarkerContainer>
        implements SampleFileIt<E> {

    private final SampleFileIt<E> it;
    private final ChromInterval interval;
    private E next;

    /**
     * Constructs a new {@code IntervalVcfIterator} instance.
     * @param it an iterator whose {@code next()} method returns a marker
     * container
     * @param chromInt a chromosome interval
     * @throws NullPointerException if {@code it == null || interval == null}
     */
    public IntervalVcfIt(SampleFileIt<E> it, ChromInterval chromInt) {
        E firstRecord = readFirstRecord(it, chromInt);
        if (firstRecord==null) {
            String s = "No VCF records found in the specified interval."
                    + Const.nl + "Check chromosome identifier and interval: "
                    + chromInt.toString();
            throw new IllegalArgumentException(s);
        }
        this.it = it;
        this.interval = chromInt;
        this.next = firstRecord;
    }

    @Override
    public File file() {
        return it.file();
    }

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

    /**
     * Returns {@code true} if the iteration has more elements.
     * @return {@code true} if the iteration has more elements.
     */
    @Override
    public boolean hasNext() {
        return (next != null);
    }

    /**
     * Returns the next element in the iteration.
     * @return the next element in the iteration.
     * @throws NoSuchElementException if the iteration has no more elements.
     */
    @Override
    public E next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        E current = next;
        this.next = readNextRecord(it, interval);
        return current;
    }

    private E readFirstRecord(SampleFileIt<E> it, ChromInterval interval) {
        E nextRecord = null;
        while (nextRecord==null && it.hasNext()) {
            E candidate = it.next();
            if (inInterval(interval, candidate.marker())) {
                nextRecord = candidate;
            }
        }
        return nextRecord;
    }

    private E readNextRecord(SampleFileIt<E> it, ChromInterval interval) {
        E nextRecord = null;
        if (it.hasNext()) {
            E candidate = it.next();
            if (inInterval(interval, candidate.marker())) {
                nextRecord = candidate;
            }
        }
        return nextRecord;
    }

    private static boolean inInterval(ChromInterval interval, Marker marker) {
        return (marker.chromIndex() == interval.chromIndex()
                && interval.start() <= marker.pos()
                && marker.pos() <= interval.inclEnd());
    }

    /**
     * The {@code remove} method is not supported by this iterator.
     * @throws UnsupportedOperationException if this method is invoked
     */
    @Override
    public void remove() {
        throw new UnsupportedOperationException(this.getClass().toString());
    }

    @Override
    public void close() {
        it.close();
        next = null;
    }
}