File: MarkerUtils.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 (291 lines) | stat: -rw-r--r-- 10,607 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * 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.ChromIds;
import blbutil.Const;
import blbutil.StringUtil;
import blbutil.Utilities;
import ints.IntList;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Class {@code MarkerUtils} contains static helper methods for the
 * {@code Marker} class.</p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class MarkerUtils  {

    private static final String[] EMPTY_ID_ARRAY = new String[0];

    private MarkerUtils() {
        // private constructor to prevent instantiation
    }

    /**
     * Returns the number of distinct genotypes, which equals
     * {@code nAlleles*(1 + nAlleles())/2}.
     * @param nAlleles the number of distinct alleles
     * @return the number of distinct genotypes
     */
    public static int nGenotypes(int nAlleles) {
        return (nAlleles*(nAlleles + 1)) >> 1;
    }

    /**
     * Returns {@code (marker.chrom() + ':' + marker.pos())}.
     * @param marker a marker
     * @return {@code (marker.chrom() + ':' + marker.pos())}
     * @throws NullPointerException if {@code marker == null}
     */
    public static String coordinate(Marker marker) {
        StringBuilder sb = new StringBuilder(marker.chrom());
        sb.append(Const.colon);
        sb.append(marker.pos());
        return sb.toString();
    }

    /**
     * Returns
     * {@code (marker.chrom() + ':' + marker.pos() + ':'
     * + marker.alleles().replace(Const.tab, Const.colon))}.
     * @param marker a marker
     * @return {{@code (marker.chrom() + ':' + marker.pos() + ':'
     * + marker.alleles().replace(Const.tab, Const.colon))}
     * @throws NullPointerException if {@code marker == null}
     */
    public static String coordinateAndAlleles(Marker marker) {
        StringBuilder sb = new StringBuilder(marker.chrom());
        sb.append(Const.colon);
        sb.append(marker.pos());
        sb.append(Const.colon);
        sb.append(marker.alleles().replace(Const.tab, Const.colon));
        return sb.toString();
    }

    /**
     * Returns the list of identifiers in the VCF ID field. An array of
     * length 0 is returned if {@code (marker.hasIdData() == false)}.
     * @param marker a marker
     * @return the list of identifiers in the VCF ID field
     * @throws NullPointerException if {@code marker == null}
     */
    public static String[] ids(Marker marker) {
        if (marker.hasIdData()) {
            return StringUtil.getFields(marker.id(), Const.semicolon);
        }
        else {
            return EMPTY_ID_ARRAY;
        }
    }

    /**
     * Returns the list of marker alleles in the VCF REF and ALT fields in
     * order of their appearance in the VCF record.
     * @param marker a marker
     * @return the list of marker alleles
     * @throws NullPointerException if {@code marker == null}
     */
    public static String[] alleles(Marker marker) {
        String refAlt = marker.alleles();
        int nAlleles = marker.nAlleles();
        String[] alleles = new String[nAlleles];
        int start = 0;
        int endIndex = refAlt.indexOf(Const.tab);
        alleles[0] = refAlt.substring(start, endIndex);
        for (int j=1; j<alleles.length; ++j) {
            int startIndex = endIndex+1;
            endIndex = refAlt.indexOf(Const.comma, startIndex);
            alleles[j] = (endIndex<0) ? refAlt.substring(startIndex)
                    : refAlt.substring(startIndex, endIndex);
        }
        return alleles;
    }

    /**
     * Prints the first seven tab-delimited VCF fields for the specified
     * marker to the specified {@code PrintWriter}. The delimiter preceding
     * the 8-th VCF field (the INFO field) is not appended.
     * @param marker a marker
     * @param out the {@code PrintWriter} that will receive output
     * @throws NullPointerException if {@code (marker == null) || (out == null)}
     */
    public static void printFirst7Fields(Marker marker, PrintWriter out) {
        out.print(marker.chrom());
        out.print(Const.tab);
        out.print(marker.pos());
        out.print(Const.tab);
        out.print(marker.id());
        out.print(Const.tab);
        out.print(marker.alleles());
        out.print(Const.tab);
        out.print(marker.qual());
        out.print(Const.tab);
        out.print(marker.filter());
    }

    /**
     * Appends the first seven tab-delimited VCF fields for the specified
     * marker to the specified {@code StringBuilder}. The delimiter preceding
     * the 8-th field (the INFO field) is not appended.
     * @param marker a marker
     * @param sb the {@code StringBuilder} that will be appended
     * @throws NullPointerException if {@code (marker == null) || (sb == null)}
     */
    public static void appendFirst7Fields(Marker marker, StringBuilder sb) {
        sb.append(marker.chrom());
        sb.append(Const.tab);
        sb.append(marker.pos());
        sb.append(Const.tab);
        sb.append(marker.id());
        sb.append(Const.tab);
        sb.append(marker.alleles());
        sb.append(Const.tab);
        sb.append(marker.qual());
        sb.append(Const.tab);
        sb.append(marker.filter());
    }

    /**
     * Returns {@code s.substring(0, maxLength)} if
     * {@code s.length() > maxLength} and returns {@code s} otherwise.
     * @param s a string
     * @param maxLength a maximum length
     * @return {@code s.substring(0, maxLength)} if
     * {@code s.length() > maxLength} and returns {@code s} otherwise
     */
    static String truncate(String s, int maxLength) {
        return s.substring(0, Math.min(s.length(), maxLength));
    }

    /**
     * Returns a list of indices of the first 8 tabs (in increasing order) in
     * the specified VCF record
     * @param vcfRec a VCF record
     * @return a list of indices of the first 8 tabs in the specified VCF
     * record
     * @throws IllegalArgumentException if the specified VCF record does
     * not contain at least 8 tab characters
     * @throws NullPointerException if {@code vcfRec == null}
     */
    static IntList first8TabIndices(String vcfRec) {
        int nTabs = 8;
        IntList indices = new IntList(nTabs);
        int index = vcfRec.indexOf(Const.tab);
        for (int j=0; j<nTabs && index>=0; ++j) {
            indices.add(index);
            index = vcfRec.indexOf(Const.tab, index+1);
        }
        if (indices.size() < nTabs) {
            String s = "VCF record does not contain " + nTabs + " tabs:"
                    + truncate(vcfRec, 800);
            throw new IllegalArgumentException(s);
        }
        return indices;
    }

    /**
     * Returns the index of the specified chromosome
     * @param vcfRec a VCF record
     * @param chrom a chromosome identifier from the VCF record
     * @return the index of the specified chromosome
     * @throws IllegalArgumentException if
     * {@code chrom.isEmpty()}
     * @throws IllegalArgumentException if
     * {@code (chrom.length()==1 && chrom.charAt(0)=='.')}
     * @throws IllegalArgumentException if {@code chrom} contains
     * a whitespace character.
     * @throws IndexOutOfBoundsException if
     * {@code ChromIds.instance().getIndex(chrom) >= Short.MAX_VALUE}.
     */
    static short chromIndex(String vcfRec, String chrom) {
        if (chrom.isEmpty()
                || (chrom.length()==1 && chrom.charAt(0)==Const.MISSING_DATA_CHAR)) {
            String s = "ERROR: missing chromosome: " + truncate(vcfRec, 80);
            throw new IllegalArgumentException(s);
        }
        for (int j=0, n=chrom.length(); j<n; ++j) {
            char c = chrom.charAt(j);
            if (Character.isWhitespace(c)) {
                String s = "ERROR: CHROM field contains whitespace: "
                        + truncate(vcfRec, 80);
                Utilities.exit(s);
            }
        }
        int chrIndex = ChromIds.instance().getIndex(chrom);
        if (chrIndex>=Short.MAX_VALUE) {
            throw new IndexOutOfBoundsException(String.valueOf(chrIndex));
        }
        return (short) chrIndex;
    }

    /**
     * Returns a sorted {@code String[]} array whose elements are
     * REF and ALT fields for single nucleotide and monomorphic variants.
     * @return an sorted {@code String[]} array whose elements are
     * REF and ALT fields for single nucleotide and monomorphic variants
     */
    static String[] snvPerms() {
        char[] bases = new char[] {'*', 'A', 'C', 'G', 'T'};
        Arrays.sort(bases);
        List<String> perms = new ArrayList<>(120);
        permute(new char[0], bases, perms);
        // Add markers without ALT allele
        perms.add("A\t.");
        perms.add("C\t.");
        perms.add("G\t.");
        perms.add("T\t.");
        String[] array = perms.toArray(new String[0]);
        Arrays.sort(array);
        return array;
    }

    private static void permute(char[] start, char[] end,
            List<String> perms) {
        if (end.length==0 && start[0]!='*') {
            StringBuilder sb = new StringBuilder();
            sb.append(start[0]);
            for (int j=1; j<start.length; ++j) {
                sb.append(j==1 ? Const.tab : Const.comma);
                sb.append(start[j]);
            }
            perms.add(sb.toString());
        }
        else {
            for (int j=0; j<end.length; ++j) {
                char[] newStart = Arrays.copyOf(start, start.length + 1);
                newStart[start.length] = end[j];

                char[] newEnd = new char[end.length - 1];
                if (j>0) {
                    System.arraycopy(end, 0, newEnd, 0, j);
                }
                if (j<newEnd.length) {
                    System.arraycopy(end, j+1, newEnd, j, (newEnd.length - j));
                }
                permute(newStart, newEnd, perms);
            }
        }
    }

}