Utility method for splitting a VCF genotype subfield into an array of numeric allele identifiers. @param gt a VCF genotype subfield (the GT value). @return a new int[] containing one int per allele. Any missing values ('.') will be assigned index -1. @throws NumberFormatEx
(String gt)
| 219 | * @throws NumberFormatException if the subfield is malformed. |
| 220 | */ |
| 221 | public static int[] splitGt(String gt) { |
| 222 | try { |
| 223 | final int gtlen = gt.length(); |
| 224 | if (gtlen == 1) { // Typical haploid call |
| 225 | return new int[]{alleleId(gt.charAt(0))}; |
| 226 | } else { |
| 227 | int[] result = new int[2]; // Initialize assuming the most common case, diploid, and resize if needed |
| 228 | int ploid = 0; |
| 229 | int allelestart = 0; |
| 230 | for (int i = 0; i < gtlen; ++i) { |
| 231 | final char c = gt.charAt(i); |
| 232 | if (c == PHASED_SEPARATOR || c == UNPHASED_SEPARATOR) { |
| 233 | if (ploid == result.length) { // More than diploid call! |
| 234 | result = Arrays.copyOf(result, result.length + 1); |
| 235 | } |
| 236 | result[ploid++] = alleleId(gt, allelestart, i - allelestart); |
| 237 | allelestart = i + 1; |
| 238 | } |
| 239 | } |
| 240 | if (allelestart < gtlen) { |
| 241 | if (ploid == result.length) { // More than diploid call! |
| 242 | result = Arrays.copyOf(result, result.length + 1); |
| 243 | } |
| 244 | result[ploid++] = alleleId(gt, allelestart, gtlen - allelestart); |
| 245 | } |
| 246 | if (ploid < result.length) { // Can only happen if a haploid genotype with allele id > 9 |
| 247 | result = Arrays.copyOf(result, ploid); |
| 248 | } |
| 249 | if (ploid == 0) { |
| 250 | throw new NumberFormatException(); |
| 251 | } |
| 252 | return result; |
| 253 | } |
| 254 | } catch (NumberFormatException e) { |
| 255 | throw new VcfFormatException("Malformed VCF GT value \"" + gt + "\""); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Utility method for creating a VCF genotype subfield from an array of |