use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfStage method makeGenotypePie.
private PieChart makeGenotypePie(final VariantContext ctx) {
final Counter<GenotypeType> countTypes = new Counter<>();
if (ctx != null) {
for (final Genotype g : ctx.getGenotypes()) {
// ignore genotype if not displayed
final SampleDef sampleDef = this.name2sampledef.get(g.getSampleName());
if (sampleDef == null || !sampleDef.isDisplayed())
continue;
countTypes.incr(g.getType());
}
}
final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
for (final GenotypeType t : GenotypeType.values()) {
int c = (int) countTypes.count(t);
if (c == 0)
continue;
pieChartData.add(new PieChart.Data(t.name() + " = " + c, c));
}
final PieChart chart = new PieChart(pieChartData);
chart.setLegendVisible(false);
return chart;
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfStage method paintDrawingArea.
private void paintDrawingArea() {
final double canvaswidth = this.drawingArea.getCanvas().getWidth();
if (canvaswidth < 1)
return;
final double canvasheight = this.drawingArea.getCanvas().getHeight();
if (canvasheight < 1)
return;
final GraphicsContext gc = this.drawingArea.getCanvas().getGraphicsContext2D();
gc.setGlobalAlpha(1.0);
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvaswidth, canvasheight);
if (this.variantTable.getItems().isEmpty())
return;
gc.setFill(Color.BLACK);
final int MAX_VARIANTS = (int) (canvaswidth / 4.0);
if (this.variantTable.getItems().size() > MAX_VARIANTS) {
gc.setFont(Font.font("Verdana", 24));
gc.fillText("Sorry. Too Many Variants (N=" + this.variantTable.getItems().size() + ">" + MAX_VARIANTS + ")", 2, 50);
return;
}
// genome track
int nrows = 1;
for (final SampleDef sampleDef : this.name2sampledef.values()) {
if (!sampleDef.isDisplayed())
continue;
nrows++;
}
double rowheight = canvasheight / nrows;
if (rowheight < 5)
rowheight = 5;
// no margin if no sample
final double marginleft = (nrows <= 1 ? 0.0 : canvaswidth / 10.0);
final Function<Long, Double> convertGenomicIndexToPixel;
final Function<Integer, Double> convertListIndexToPixel;
if (!this.canvasEvenlySpaced.isSelected()) {
Long minGenomicIndex;
Long maxGenomicIndex;
{
final VariantContext ctx = this.variantTable.getItems().get(0);
minGenomicIndex = convertContigPosToGenomicIndex(ctx.getContig(), ctx.getStart());
maxGenomicIndex = minGenomicIndex;
}
{
final VariantContext ctx = this.variantTable.getItems().get(this.variantTable.getItems().size() - 1);
maxGenomicIndex = convertContigPosToGenomicIndex(ctx.getContig(), ctx.getEnd());
}
if (minGenomicIndex == null || maxGenomicIndex == null)
return;
long extend = (long) ((maxGenomicIndex - minGenomicIndex) * 0.1);
minGenomicIndex = minGenomicIndex - extend;
maxGenomicIndex = maxGenomicIndex + extend;
final long genomicIndexStart = minGenomicIndex;
final long genomicIndexLength = (maxGenomicIndex - minGenomicIndex);
convertGenomicIndexToPixel = (GI) -> {
double x = marginleft + (canvaswidth - marginleft) * ((double) (GI - genomicIndexStart)) / ((double) (genomicIndexLength));
return x;
};
convertListIndexToPixel = null;
} else {
final double nItems = 2.0 + this.variantTable.getItems().size();
convertGenomicIndexToPixel = null;
convertListIndexToPixel = (IDX) -> {
return marginleft + ((1 + IDX) / nItems) * (canvaswidth - marginleft);
};
}
// draw vertical lines
String prev_chr = "";
Paint fill_chr1 = Color.BLACK;
Paint fill_chr2 = Color.DARKGRAY;
Paint fill_current = fill_chr1;
gc.setFont(Font.font("Verdana", 7));
for (int idx = 0; idx < this.variantTable.getItems().size(); ++idx) {
final VariantContext ctx = this.variantTable.getItems().get(idx);
if (!ctx.getContig().equals(prev_chr)) {
prev_chr = ctx.getContig();
fill_current = (fill_current == fill_chr1 ? fill_chr2 : fill_chr1);
}
gc.setFill(fill_current);
double x0;
double x1;
if (convertListIndexToPixel == null) {
x0 = convertGenomicIndexToPixel.apply(convertContigPosToGenomicIndex(ctx.getContig(), ctx.getStart()));
x1 = convertGenomicIndexToPixel.apply(convertContigPosToGenomicIndex(ctx.getContig(), ctx.getEnd()));
if (x0 <= x1)
x1 = x0 + 1;
} else {
x0 = convertListIndexToPixel.apply(idx);
x1 = x0 + 1;
}
gc.setGlobalAlpha(0.4);
gc.fillRect(x0, 0, (x1 - x0), canvasheight);
// http://stackoverflow.com/questions/39524792/
gc.setGlobalAlpha(1.0);
gc.setFill(Color.BLACK);
gc.save();
gc.translate(x0, 0);
gc.rotate(90);
gc.fillText(ctx.getContig() + ":" + ctx.getStart(), 5, 0, rowheight);
gc.restore();
}
final double radius = 3.0;
gc.setFont(Font.font("Verdana", 12));
double y = rowheight;
for (final SampleDef sampleDef : this.name2sampledef.values()) {
if (!sampleDef.isDisplayed())
continue;
gc.setLineWidth(1.0);
gc.setGlobalAlpha(1.0);
gc.setFill(Color.BLACK);
gc.setStroke(Color.BLACK);
gc.strokeLine(0, y, canvaswidth, y);
gc.fillText(sampleDef.getName(), 1, y + gc.getFont().getSize(), marginleft);
gc.setLineWidth(0.5);
final double midy = y + rowheight / 2.0;
gc.setGlobalAlpha(0.8);
for (int idx = 0; idx < this.variantTable.getItems().size(); ++idx) {
final VariantContext ctx = this.variantTable.getItems().get(idx);
double x0;
if (convertListIndexToPixel == null) {
x0 = convertGenomicIndexToPixel.apply(convertContigPosToGenomicIndex(ctx.getContig(), ctx.getStart()));
} else {
x0 = convertListIndexToPixel.apply(idx);
}
final Genotype g = ctx.getGenotype(sampleDef.getName());
if (g == null || g.isNoCall())
continue;
if (g.isHomRef()) {
gc.setStroke(Color.DARKGRAY);
gc.strokeOval(x0 - radius, midy - radius, radius * 2, radius * 2);
} else if (g.isHet() && !g.isHetNonRef()) {
gc.setStroke(Color.DARKGRAY);
gc.strokeOval(x0 - radius, midy - radius, radius * 2, radius * 2);
gc.setFill(Color.RED);
gc.fillArc(x0 - radius, midy - radius, radius * 2, radius * 2, 0, 180, ArcType.CHORD);
} else if (g.isHetNonRef()) {
gc.setFill(Color.PINK);
gc.fillOval(x0 - radius, midy - radius, radius * 2, radius * 2);
gc.setFill(Color.RED);
gc.fillArc(x0 - radius, midy - radius, radius * 2, radius * 2, 0, 180, ArcType.CHORD);
} else {
gc.setFill(Color.RED);
gc.fillOval(x0 - radius, midy - radius, radius * 2, radius * 2);
}
}
y += rowheight;
if (y > canvasheight)
break;
}
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfStage method buildTrioTable.
/**
* build Trio table
*/
private TableView<PedFile.TrioGenotype> buildTrioTable() {
final TableView<PedFile.TrioGenotype> table = new TableView<>();
if (getVcfFile().getHeader().getNGenotypeSamples() > 0 && !getPedigree().isEmpty()) {
final Function<Genotype, String> gt2str = new Function<Genotype, String>() {
@Override
public String apply(final Genotype gt) {
if (gt == null || !gt.isCalled())
return null;
return gt.getAlleles().stream().map(S -> allele2stringConverter.apply(S)).collect(Collectors.joining(gt.isPhased() ? "|" : "/"));
}
};
table.getColumns().add(makeColumn("Child", T -> T.getChildren() == null ? null : T.getChildren().getSampleName()));
table.getColumns().add(makeColumn("Child-GT", T -> gt2str.apply(T.getChildren())));
table.getColumns().add(makeColumn("Father", T -> T.getFather() == null ? null : T.getFather().getSampleName()));
table.getColumns().add(makeColumn("Father-GT", T -> gt2str.apply(T.getFather())));
table.getColumns().add(makeColumn("Mother", T -> T.getMother() == null ? null : T.getMother().getSampleName()));
table.getColumns().add(makeColumn("Mother-GT", T -> gt2str.apply(T.getMother())));
table.getColumns().add(makeColumn("Violation", T -> T.isMendelianIncompatibility()));
}
table.setPlaceholder(new Label("No Trio."));
return table;
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class AFBySexChartFactory method visit.
@Override
public void visit(final VariantContext ctx) {
for (final Allele alt : ctx.getAlternateAlleles()) {
final MafCalculator[] mafCalculators = new MafCalculator[PedFile.Sex.values().length];
for (int i = 0; i < mafCalculators.length; ++i) {
mafCalculators[i] = new MafCalculator(alt, ctx.getContig());
}
for (final Genotype gt : ctx.getGenotypes()) {
final PedFile.Sample sample = getPedigree().get(gt.getSampleName());
if (sample == null) {
mafCalculators[PedFile.Sex.Unknown.ordinal()].add(gt, false);
} else {
mafCalculators[sample.getSex().ordinal()].add(gt, sample.isMale());
}
}
for (int i = 0; i < mafCalculators.length; ++i) {
if (mafCalculators[i].isEmpty())
continue;
final Total total = popcount.get(i);
total.num_maf++;
total.sum += mafCalculators[i].getMaf();
}
}
}
use of htsjdk.variant.variantcontext.Genotype in project jvarkit by lindenb.
the class VcfToSql method read.
private void read(File filename) throws IOException {
/* insert ATGC */
this.alleleTable.insert(outputWriter, null, "A");
this.alleleTable.insert(outputWriter, null, "C");
this.alleleTable.insert(outputWriter, null, "G");
this.alleleTable.insert(outputWriter, null, "T");
/* insert this sample */
this.vcfFileTable.insert(outputWriter, null, filename);
final SelectStmt vcffile_id = new SelectStmt(this.vcfFileTable);
final Map<String, SelectStmt> sample2sampleid = new HashMap<String, SelectStmt>();
final Map<String, SelectStmt> filter2filterid = new HashMap<String, SelectStmt>();
final Map<String, SelectStmt> chrom2chromId = new HashMap<String, SelectStmt>();
final VcfIterator r = VCFUtils.createVcfIteratorFromFile(filename);
final VCFHeader header = r.getHeader();
/* parse samples */
for (final String sampleName : header.getSampleNamesInOrder()) {
this.sampleTable.insert(outputWriter, null, sampleName);
SelectStmt sample_id = new SelectStmt(this.sampleTable, "name", sampleName);
sample2sampleid.put(sampleName, sample_id);
this.sample2fileTable.insert(outputWriter, null, vcffile_id, sample_id);
}
/* parse filters */
for (final VCFFilterHeaderLine filter : header.getFilterLines()) {
this.filterTable.insert(outputWriter, null, vcffile_id, filter.getID(), filter.getValue());
filter2filterid.put(filter.getID(), new SelectStmt(this.filterTable, "name", filter.getID()));
}
filter2filterid.put(VCFConstants.PASSES_FILTERS_v4, new SelectStmt(this.filterTable, "name", VCFConstants.PASSES_FILTERS_v4));
final SAMSequenceDictionary dict = header.getSequenceDictionary();
if (dict == null) {
throw new RuntimeException("dictionary missing in VCF");
}
/* parse sequence dict */
for (final SAMSequenceRecord ssr : dict.getSequences()) {
this.chromosomeTable.insert(outputWriter, null, vcffile_id, ssr.getSequenceName(), ssr.getSequenceLength());
chrom2chromId.put(ssr.getSequenceName(), new SelectStmt(this.chromosomeTable, "name", ssr.getSequenceName()));
}
VepPredictionParser vepPredictionParser = new VepPredictionParserFactory(header).get();
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(dict);
int nVariants = 0;
while (r.hasNext()) {
if (this.outputWriter.checkError())
break;
VariantContext var = progress.watch(r.next());
++nVariants;
/* insert ref allele */
this.alleleTable.insert(outputWriter, null, var.getReference().getBaseString());
/* insert variant */
this.variantTable.insert(outputWriter, null, vcffile_id, nVariants, chrom2chromId.get(var.getContig()), var.getStart(), (var.hasID() ? var.getID() : null), new SelectStmt(this.alleleTable, "bases", var.getReference().getBaseString()), (var.hasLog10PError() ? var.getPhredScaledQual() : null));
SelectStmt variant_id = new SelectStmt(variantTable);
/* insert alternate alleles */
for (Allele alt : var.getAlternateAlleles()) {
/* insert alt allele */
this.alleleTable.insert(outputWriter, null, alt.getBaseString());
this.variant2altTable.insert(outputWriter, null, variant_id, new SelectStmt(this.alleleTable, "bases", alt.getBaseString()));
}
/* insert filters */
for (final String filter : var.getFilters()) {
if (filter2filterid.get(filter) == null) {
throw new IOException("VCF Error: filter " + filter + " is not defined in the VCF header.");
}
this.variant2filters.insert(outputWriter, null, variant_id, filter2filterid.get(filter));
}
if (!this.ignore_info) {
for (final VepPrediction pred : vepPredictionParser.getPredictions(var)) {
/*
vepPrediction.insert(
outputWriter,
null,
variant_id,
pred.getEnsemblGene(),
pred.getEnsemblTranscript(),
pred.getEnsemblProtein(),
pred.getSymbol()
);
SelectStmt pred_id = new SelectStmt(vepPrediction);
for(SequenceOntologyTree.Term t: pred.getSOTerms())
{
String term=t.getAcn().replace(':', '_');
soTermTable.insert(
outputWriter,
null,
term,
t.getAcn()
);//for bioportal compatibility
SelectStmt term_id = new SelectStmt(soTermTable,"acn",term);
vepPrediction2so.insert(
outputWriter,
null,
pred_id,
term_id
);
}
*/
}
}
/* insert genotypes */
for (final String sampleName : sample2sampleid.keySet()) {
final Genotype g = var.getGenotype(sampleName);
if (!g.isAvailable() || g.isNoCall())
continue;
genotypeTable.insert(outputWriter, null, variant_id, sample2sampleid.get(sampleName), g.isCalled() ? new SelectStmt(this.alleleTable, "bases", g.getAllele(0).getBaseString()) : null, g.isCalled() ? new SelectStmt(this.alleleTable, "bases", g.getAllele(1).getBaseString()) : null, g.hasDP() ? g.getDP() : null, g.hasGQ() ? g.getGQ() : null);
}
}
r.close();
}
Aggregations