Turn a line of VCF output into a VcfRecord @param field the fields of the record after splitting on tab @return the corresponding record
(String... field)
| 124 | * @return the corresponding record |
| 125 | */ |
| 126 | public VcfRecord parseFields(String... field) { |
| 127 | if (field.length < 8) { |
| 128 | throw new VcfFormatException("Expected at least 8 fields"); |
| 129 | } |
| 130 | for (int i = 0; i < field.length; i++) { |
| 131 | if (field[i].trim().length() == 0) { |
| 132 | throw new VcfFormatException("Field in column " + (i + 1) + " is empty"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | final int pos; |
| 137 | try { |
| 138 | pos = Integer.parseInt(field[POS_FIELD]) - 1; |
| 139 | } catch (NumberFormatException e) { |
| 140 | throw new VcfFormatException(e.getMessage()); |
| 141 | } |
| 142 | final String ref = field[REF_FIELD]; |
| 143 | if (ref.length() == 0) { /// VCF spec implies (but is not specific) that we could also reject if VcfRecord.MISSING.equals(ref) |
| 144 | throw new VcfFormatException("REF field cannot be missing"); |
| 145 | } |
| 146 | final VcfRecord rec = new VcfRecord(field[CHROM_FIELD], pos, ref); |
| 147 | rec.setId(field[ID_FIELD]); |
| 148 | if (!VcfRecord.MISSING.equals(field[ALT_FIELD])) { |
| 149 | final String[] altSplit = StringUtils.split(field[ALT_FIELD], ','); |
| 150 | for (final String anAltSplit : altSplit) { |
| 151 | if (anAltSplit.length() == 0) { |
| 152 | throw new VcfFormatException("An empty ALT allele is not permitted"); |
| 153 | } |
| 154 | rec.addAltCall(anAltSplit); |
| 155 | } |
| 156 | } |
| 157 | rec.setQuality(field[QUAL_FIELD]); // "." or float. |
| 158 | final String[] filterSplit = StringUtils.split(field[FILTER_FIELD], ';'); |
| 159 | for (final String aFilterSplit : filterSplit) { |
| 160 | if (!VcfRecord.MISSING.equals(aFilterSplit)) { |
| 161 | rec.addFilter(aFilterSplit); |
| 162 | } |
| 163 | } |
| 164 | if (!VcfRecord.MISSING.equals(field[INFO_FIELD])) { |
| 165 | final String[] infoSplit = StringUtils.split(field[INFO_FIELD], ';'); |
| 166 | for (final String anInfoSplit : infoSplit) { |
| 167 | final String[] singleInfoSplit = StringUtils.split(anInfoSplit, '=', 2); |
| 168 | final String key = singleInfoSplit[0]; |
| 169 | if (rec.hasInfo(key)) { |
| 170 | throw new VcfFormatException("Duplicate INFO field: " + key); |
| 171 | } |
| 172 | if (singleInfoSplit.length == 1) { |
| 173 | rec.setInfo(key); |
| 174 | } else { |
| 175 | rec.setInfo(key, singleInfoSplit[1]); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | rec.setNumberOfSamples(0); |
| 180 | // now parse each sample field. |
| 181 | if (field.length > 8) { |
| 182 | if (field.length == 9) { |
| 183 | throw new VcfFormatException("Format field exists without sample fields"); |
no test coverage detected