/** * Get genotype string by index * * <p>WARNING: If the genotype is missing, it returns an empty string. E.g.: './.' -> * getGenotype(0) = "" * * @return */ public String getGenotype(int idx) { parseFields(); // Lazy parse if (genotype == null) return ""; // Missing genotype int num = genotype[idx]; if (num < 0) return ""; // Missing genotype return num == 0 ? vcfEntry.getRef() : vcfEntry.getAlts()[num - 1]; }
/** * Parse GT field * * @param value */ void parseGt(String value) { String gtStr[] = null; if (value.indexOf('|') >= 0) { gtStr = value.split("\\|"); phased = true; // Phased } else { gtStr = value.split("/"); phased = false; // Unphased } // Create fields genotype = new int[gtStr.length]; for (int i = 0; i < genotype.length; i++) if (gtStr[i].isEmpty() || gtStr[i].equals(".")) genotype[i] = -1; // Genotype '-1' means missing values else { genotype[i] = Gpr.parseIntSafe(gtStr[i]); // Sanity check if ((genotype[i] - 1) >= vcfEntry.getAlts().length) { boolean plural = vcfEntry.getAlts().length > 1; throw new RuntimeException( "Error: Bad genotype field '" + value + "'. Genotype says '" + genotype[i] + "' but there " + (plural ? "are" : "is") + " only '" + vcfEntry.getAlts().length + "' allele" + (plural ? "s" : "") + " ('" + vcfEntry.getAltsStr() + "')."); } } }
/** * Return genotypes as string (e.g. "A/C") * * @return */ public String getGenotypeStr() { parseFields(); // Lazy parse StringBuilder sb = new StringBuilder(); if (genotype != null) { for (int i = 0; i < genotype.length; i++) { int num = genotype[i]; String gen = "."; if (num == 0) gen = vcfEntry.getRef(); else if (num > 0) gen = vcfEntry.getAlts()[num - 1]; sb.append(gen); if (i < (genotype.length - 1)) { if (isPhased()) sb.append("|"); else sb.append("/"); } } } return sb.toString(); }