protected static String[] getOri(StudyEntry studyEntry) { List<FileEntry> files = studyEntry.getFiles(); if (!files.isEmpty()) { String call = files.get(0).getCall(); if (call != null && !call.isEmpty()) { return call.split(":"); } } return null; }
private static FileEntry getFileEntry(Variant variant, String studyId, String fileId) { StudyEntry studyEntry = getStudyEntry(variant, studyId); if (studyEntry == null) { return null; } if (fileId == null) { if (studyEntry.getFiles().size() != 1) { throw new IllegalArgumentException("Required one File per variant. Found " + studyEntry.getFiles().size() + " files instead"); } return studyEntry.getFiles().get(0); } else { return studyEntry.getFile(fileId); } } }
protected Map<String, String> parseInfoAttributes(Variant variant, int customFileNumber) { Map<String, String> infoMap = variant.getStudies().get(0).getFiles().get(0).getAttributes(); Map<String, String> parsedInfo = new HashMap<>(); for (String attribute : infoMap.keySet()) { if (customFileFields.get(customFileNumber).contains(attribute)) { parsedInfo.put(attribute, infoMap.get(attribute)); // parsedInfo.put(attribute, getValueFromString(infoMap.get(attribute))); } } return parsedInfo; }
public static String extractFileAttribute(Variant a, String key) { List<StudyEntry> studies = a.getStudies(); if (studies.isEmpty()) { return null; } StudyEntry studyEntry = studies.get(0); List<FileEntry> files = studyEntry.getFiles(); if (files == null || files.isEmpty()) { return null; } return files.get(0).getAttributes().get(key); }
@Override public List<Variant> apply(List<Variant> batch) throws Exception { batch.forEach(variant -> variant.getStudies() .forEach((StudyEntry studyEntry) -> { studyEntry.setStudyId(studyIdStr); studyEntry.getFiles().forEach((FileEntry fileEntry) -> fileEntry.setFileId(fileIdStr)); })); return batch; } }
@Deprecated private static String[] getSvInsSeq(Variant variant) { String leftSvInsSeq = null; String rightSvInsSeq = null; if (variant.getStudies()!= null && !variant.getStudies().isEmpty() && !variant.getStudies().get(0).getFiles().isEmpty()) { if (variant.getStudies().get(0).getFiles().get(0).getAttributes().containsKey(LEFT_SVINSSEQ_INFO)) { leftSvInsSeq = variant.getStudies().get(0).getFiles().get(0).getAttributes().get(LEFT_SVINSSEQ_INFO); } if (variant.getStudies().get(0).getFiles().get(0).getAttributes().containsKey(RIGHT_SVINSSEQ_INFO)) { rightSvInsSeq = variant.getStudies().get(0).getFiles().get(0).getAttributes().get(RIGHT_SVINSSEQ_INFO); } } return new String[]{leftSvInsSeq, rightSvInsSeq}; }
public Document convertToStorageType(Variant variant, StudyEntry studyEntry) { return convertToStorageType(variant, studyEntry, studyEntry.getFiles(), new LinkedHashSet<>(studyEntry.getOrderedSamplesName())); }
@Deprecated public static int[] getImpreciseStart(Variant variant) { if (variant.getStudies()!= null && !variant.getStudies().isEmpty() && !variant.getStudies().get(0).getFiles().isEmpty() && variant.getStudies().get(0).getFiles().get(0).getAttributes().containsKey(CIPOS_INFO)) { String[] parts = variant.getStudies().get(0).getFiles().get(0).getAttributes().get(CIPOS_INFO).split(",", 2); return new int[]{variant.getStart() + Integer.parseInt(parts[0]), variant.getStart() + Integer.parseInt(parts[1])}; } else { return new int[]{variant.getStart(), variant.getStart()}; } }
@Deprecated public static int[] getImpreciseEnd(Variant variant) { if (variant.getStudies()!= null && !variant.getStudies().isEmpty() && !variant.getStudies().get(0).getFiles().isEmpty() && variant.getStudies().get(0).getFiles().get(0).getAttributes().containsKey(CIEND_INFO)) { String[] parts = variant.getStudies().get(0).getFiles().get(0).getAttributes().get(CIEND_INFO).split(",", 2); return new int[]{variant.getEnd() + Integer.parseInt(parts[0]), variant.getEnd() + Integer.parseInt(parts[1])}; } else { return new int[]{variant.getEnd(), variant.getEnd()}; } }
@Override protected List<String> featureValueOf(StudyEntry actual) { return actual.getFiles() .stream() .map(FileEntry::getFileId).collect(Collectors.toList()); } };
private boolean samePhase(Variant variant1, Variant variant2) { if (variant1.getStudies() != null && !variant1.getStudies().isEmpty()) { if (variant2.getStudies() != null && !variant2.getStudies().isEmpty()) { int psIdx1 = variant1.getStudies().get(0).getFormat().indexOf("PS"); if (psIdx1 != -1) { int psIdx2 = variant2.getStudies().get(0).getFormat().indexOf("PS"); if (psIdx2 != -1 && // variant2 does have PS set // same phase set value in both variants variant2.getStudies().get(0).getSamplesData().get(0).get(psIdx2) .equals(variant1.getStudies().get(0).getSamplesData().get(0).get(psIdx1)) // Same genotype call in both variants (e.g. 1|0=1|0). // WARNING: assuming variant1 and variant2 do have Files. && variant1.getStudies().get(0).getFiles().get(0).getCall() .equals(variant2.getStudies().get(0).getFiles().get(0).getCall())) { return true; } } } } return false; }
private void addVariant(Variant variant) { String chromosome = variant.getChromosome(); // Remap studyId and fileId StudyEntry studyEntry = variant.getStudies().get(0); studyEntry.setStudyId(studyId); studyEntry.getFiles().get(0).setFileId(fileId); long[] coveredSlicePositions = getCoveredSlicePositions(variant); for (long slicePos : coveredSlicePositions) { addVariant(variant, chromosome, slicePos); } }
@Test public void testIncludeAll() { for (Variant variant : allVariants.getResult()) { assertThat(variant.getStudies(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getStats(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getFiles(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getSamplesData(), not(is(Collections.emptyList()))); assertNotNull(variant.getAnnotation()); } }
@Test public void testExcludeFiles() { for (String exclude : Arrays.asList("studies.files", "files")) { queryResult = query(new Query(), new QueryOptions(QueryOptions.EXCLUDE, exclude)); assertEquals(allVariants.getResult().size(), queryResult.getResult().size()); for (Variant variant : queryResult.getResult()) { assertThat(variant.getStudies().get(0).getFiles(), is(Collections.emptyList())); assertThat(new HashSet<>(variant.getStudies().get(0).getFormat()), is(FORMAT)); } } }
@Test public void testReturnNoneFiles() { queryResult = query(new Query(INCLUDE_FILE.key(), VariantQueryUtils.NONE), new QueryOptions()); assertEquals(allVariants.getResult().size(), queryResult.getResult().size()); for (Variant variant : queryResult.getResult()) { assertThat(variant.getStudies().get(0).getFiles(), is(Collections.emptyList())); assertThat(new HashSet<>(variant.getStudies().get(0).getFormat()), is(FORMAT)); } }
private VariantProto.StudyEntry.Builder toProto(StudyEntry study) { VariantProto.StudyEntry.Builder studyBuilder = VariantProto.StudyEntry.newBuilder(); studyBuilder.setStudyId(study.getStudyId()); set(study::getStudyId, studyBuilder::setStudyId); set(study::getFormat, studyBuilder::addAllFormat); for (List<String> sampleData : study.getSamplesData()) { studyBuilder.addSamplesData(VariantProto.StudyEntry.SamplesDataInfoEntry.newBuilder().addAllInfo(sampleData)); } for (Map.Entry<String, VariantStats> entry : study.getStats().entrySet()) { VariantStats stats = entry.getValue(); VariantProto.VariantStats.Builder variantStats = toProto(stats); studyBuilder.putStats(entry.getKey(), variantStats.build()); } for (FileEntry fileEntry : study.getFiles()) { VariantProto.FileEntry.Builder fileBuilder = toProto(fileEntry); studyBuilder.addFiles(fileBuilder); } return studyBuilder; }
@Test public void testSummary() { queryResult = query(new Query(), new QueryOptions(VariantField.SUMMARY, true).append(QueryOptions.LIMIT, 1000)); System.out.println("queryResult = " + ((VariantQueryResult) queryResult).getSource()); assertEquals(allVariants.getResult().size(), queryResult.getResult().size()); for (Variant variant : queryResult.getResult()) { assertThat(variant.getStudies().get(0).getSamplesData(), is(Collections.emptyList())); assertThat(variant.getStudies().get(0).getFiles(), is(Collections.emptyList())); } }
@Test public void resolve_INS_SNP_SEC_ALT() throws Exception { Variant a = getVariant("1:100:-:GGTTG", "PASS", "390", "1/2"); Variant b = getVariant("1:100:G:-", "PASS", "390", "0/0"); Variant c = getVariant("1:102:-:AGGA", "PASS", "390", "0/1"); b.setType(NO_VARIATION); a.getStudies().get(0).getSecondaryAlternates().add( new AlternateCoordinate(c.getChromosome(), c.getStart(), c.getEnd(), c.getReference(), c.getAlternate(), INDEL)); a.getStudies().get(0).getFiles().get(0).setCall("100:TT:GGTTGTT,TTAGGA:0"); b.getStudies().get(0).getFiles().get(0).setCall("100:TT:GGTTGTT,TTAGGA:0"); c.getStudies().get(0).getFiles().get(0).setCall("100:TT:GGTTGTT,TTAGGA:1"); Collection<Variant> resolved = new VariantLocalConflictResolver().resolveConflicts(Arrays.asList(a, b, c)); assertEquals(2, resolved.size()); }
@Test public void resolveSameVariantWithSecAlt() throws Exception { Variant a = getVariant("2:10048155:TCTTTTTTTT:AC", "PASS", "220", "1/2"); Variant b = getVariant("2:10048155:TCTTTTTTTT:-", "PASS", "220", "2/1"); a.getStudies().get(0).getSecondaryAlternates().add(new AlternateCoordinate("2", b.getStart(), b.getEnd(), b.getReference(), b.getAlternate(), INDEL)); a.getStudies().get(0).getFiles().get(0).setCall("10048155:TTCTTTTTTTT:TAC,T:0"); b.getStudies().get(0).getSecondaryAlternates().add(new AlternateCoordinate("2", a.getStart(), a.getEnd(), a.getReference(), a.getAlternate(), INDEL)); b.getStudies().get(0).getFiles().get(0).setCall("10048155:TTCTTTTTTTT:TAC,T:1"); Collection<Variant> resolved = new VariantLocalConflictResolver().resolveConflicts(Arrays.asList(a, b)); assertEquals(1, resolved.size()); }
@Test public void testInclude() { queryResult = query(new Query(), new QueryOptions(QueryOptions.INCLUDE, "studies")); assertEquals(allVariants.getResult().size(), queryResult.getResult().size()); for (Variant variant : queryResult.getResult()) { assertThat(variant.getStudies(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getStats(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getFiles(), not(is(Collections.emptyList()))); assertThat(variant.getStudies().get(0).getSamplesData(), not(is(Collections.emptyList()))); assertNull(variant.getAnnotation()); } queryResult = query(new Query(), new QueryOptions(QueryOptions.INCLUDE, "annotation")); assertEquals(allVariants.getResult().size(), queryResult.getResult().size()); for (Variant variant : queryResult.getResult()) { assertThat(variant.getStudies(), is(Collections.emptyList())); assertNotNull(variant.getAnnotation()); } }