use of com.github.lindenb.jvarkit.pedigree.Trio in project jvarkit by lindenb.
the class VCFTrios method doVcfToVcf.
@Override
public int doVcfToVcf(final String inputName, VCFIterator r, final VariantContextWriter w) {
long count_incompats = 0L;
final Set<String> sampleNotFoundInVcf = new HashSet<>();
Pedigree pedigree = null;
final List<TrioTriple> trios = new ArrayList<>();
try {
final DeNovoDetector detector = new DeNovoDetector();
detector.setConvertingNoCallToHomRef(this.nocall_to_homref);
final VCFHeader header = r.getHeader();
final PedigreeParser pedParser = new PedigreeParser();
pedigree = pedParser.parse(this.pedigreeFile);
final VCFHeader h2 = new VCFHeader(header);
final Set<VCFHeaderLine> meta = new HashSet<>();
meta.add(new VCFInfoHeaderLine(this.attributeName, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "Samples with mendelian incompatibilities." + (this.pedigreeFile == null ? "" : " Pedigree File was : " + this.pedigreeFile)));
meta.add(VCFStandardHeaderLines.getFormatLine(VCFConstants.GENOTYPE_FILTER_KEY, true));
if (!StringUtil.isBlank(this.filterAnyIncompat)) {
meta.add(new VCFFilterHeaderLine(this.filterAnyIncompat, "Variant contains at least one mendelian incompatibilities"));
}
if (!StringUtil.isBlank(this.filterNoIncompat)) {
meta.add(new VCFFilterHeaderLine(this.filterNoIncompat, "Variant does not contain any mendelian incompatibilities"));
}
meta.stream().forEach(H -> h2.addMetaDataLine(H));
JVarkitVersion.getInstance().addMetaData(this, h2);
for (final Trio pedTrio : pedigree.getTrios()) {
final TrioTriple trio = new TrioTriple();
final Sample child = pedTrio.getChild();
trio.child_id = header.getSampleNameToOffset().getOrDefault(child.getId(), -1);
if (trio.child_id < 0)
continue;
if (pedTrio.hasFather()) {
final Sample parent = pedTrio.getFather();
trio.father_id = header.getSampleNameToOffset().getOrDefault(parent.getId(), -1);
}
if (pedTrio.hasMother()) {
final Sample parent = pedTrio.getMother();
trio.mother_id = header.getSampleNameToOffset().getOrDefault(parent.getId(), -1);
}
if (trio.father_id == -1 && trio.mother_id == -1) {
continue;
}
trios.add(trio);
}
LOG.info("trios(s) in pedigree: " + trios.size());
final ProgressFactory.Watcher<VariantContext> progress = ProgressFactory.newInstance().dictionary(header).logger(LOG).build();
w.writeHeader(h2);
while (r.hasNext()) {
final VariantContext ctx = progress.apply(r.next());
final Set<String> incompatibilities = new HashSet<String>();
for (final TrioTriple trio : trios) {
final Genotype gChild = ctx.getGenotype(trio.child_id);
if (gChild == null)
throw new IllegalStateException();
final Genotype gFather = trio.father_id < 0 ? null : ctx.getGenotype(trio.father_id);
final Genotype gMother = trio.mother_id < 0 ? null : ctx.getGenotype(trio.mother_id);
final DeNovoDetector.DeNovoMutation mut = detector.test(ctx, gFather, gMother, gChild);
if (mut != null) {
incompatibilities.add(gChild.getSampleName());
}
}
final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
if (!incompatibilities.isEmpty()) {
// set filter for samples that are not a mendelian violation
if (!StringUtil.isBlank(this.genotypeFilterNameNoIncompat)) {
vcb.genotypes(ctx.getGenotypes().stream().map(G -> incompatibilities.contains(G.getSampleName()) ? G : new GenotypeBuilder(G).filters(this.genotypeFilterNameNoIncompat).make()).collect(Collectors.toList()));
}
++count_incompats;
// set INFO attribute
vcb.attribute(attributeName, incompatibilities.toArray());
// set FILTER
if (!StringUtil.isBlank(this.filterAnyIncompat)) {
vcb.filter(this.filterAnyIncompat);
} else if (!ctx.isFiltered()) {
vcb.passFilters();
}
} else // No denovo
{
// dicard variant
if (this.discard_variants_without_mendelian_incompat) {
continue;
}
// set filters
if (!StringUtil.isBlank(this.filterNoIncompat)) {
vcb.filter(this.filterNoIncompat);
} else if (!ctx.isFiltered()) {
vcb.passFilters();
}
}
w.add(vcb.make());
}
progress.close();
LOG.info("incompatibilitie(s) N=" + count_incompats);
if (!sampleNotFoundInVcf.isEmpty()) {
LOG.info("SAMPLE(S) not found: " + String.join(" / ", sampleNotFoundInVcf));
}
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
}
use of com.github.lindenb.jvarkit.pedigree.Trio in project jvarkit by lindenb.
the class SwingTrioTableModel method setVariant.
public void setVariant(final VariantContext ctx) {
final List<DeNovoDetector.DeNovoMutation> L;
if (ctx == null || !ctx.hasGenotypes() || this.trios.isEmpty()) {
L = Collections.emptyList();
} else {
L = new ArrayList<>();
for (final Trio trio : this.trios) {
final DeNovoMutation mut = this.detector.test(ctx, trio);
if (mut != null)
L.add(mut);
}
}
setRows(L);
}
Aggregations