use of htsjdk.variant.variantcontext.Allele in project jvarkit by lindenb.
the class SamFindClippedRegions method doWork.
/*private static boolean closeTo(int pos1,int pos2, int max)
{
return Math.abs(pos2-pos1)<=max;
}*/
/*
private static boolean same(char c1,char c2)
{
if(c1=='N' || c2=='N') return false;
return Character.toUpperCase(c1)==Character.toUpperCase(c2);
}*/
@Override
public int doWork(List<String> args) {
int readLength = 150;
if (args.isEmpty()) {
LOG.error("illegal.number.of.arguments");
return -1;
}
List<Input> inputs = new ArrayList<Input>();
VariantContextWriter w = null;
// SAMFileWriter w=null;
try {
SAMSequenceDictionary dict = null;
/* create input, collect sample names */
Map<String, Input> sample2input = new HashMap<String, Input>();
for (final String filename : args) {
Input input = new Input(new File(filename));
// input.index=inputs.size();
inputs.add(input);
if (sample2input.containsKey(input.sampleName)) {
LOG.error("Duplicate sample " + input.sampleName + " in " + input.bamFile + " and " + sample2input.get(input.sampleName).bamFile);
return -1;
}
sample2input.put(input.sampleName, input);
if (dict == null) {
dict = input.header.getSequenceDictionary();
} else if (!SequenceUtil.areSequenceDictionariesEqual(dict, input.header.getSequenceDictionary())) {
LOG.error("Found more than one dictint sequence dictionary");
return -1;
}
}
LOG.info("Sample N= " + sample2input.size());
/* create merged iterator */
List<SAMFileHeader> headers = new ArrayList<SAMFileHeader>(sample2input.size());
for (Input input : inputs) headers.add(input.header);
SamFileHeaderMerger headerMerger = new SamFileHeaderMerger(SortOrder.coordinate, headers, true);
List<SamReader> readers = new ArrayList<SamReader>(sample2input.size());
for (Input input : inputs) readers.add(input.samFileReaderScan);
MergingSamRecordIterator merginIter = new MergingSamRecordIterator(headerMerger, readers, true);
Allele reference_allele = Allele.create("N", true);
Allele[] alternate_alleles = new Allele[] { Allele.create("<CLIP5>", false), Allele.create("<CLIP3>", false) };
Set<VCFHeaderLine> vcfHeaderLines = new HashSet<VCFHeaderLine>();
for (Allele alt : alternate_alleles) {
vcfHeaderLines.add(new VCFSimpleHeaderLine("<ID=" + alt.getDisplayString() + ",Description=\"StructVar\">", VCFHeaderVersion.VCF4_1, VCFConstants.ALT_HEADER_START.substring(2), Arrays.asList("ID", "Description")));
}
vcfHeaderLines.add(new VCFInfoHeaderLine("COUNT_SAMPLES", 1, VCFHeaderLineType.Integer, "Number of samples with depth>=" + this.min_depth));
vcfHeaderLines.add(new VCFInfoHeaderLine(VCFConstants.DEPTH_KEY, 1, VCFHeaderLineType.Integer, "Approximate read depth."));
vcfHeaderLines.add(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_KEY, 1, VCFHeaderLineType.String, "Genotype"));
vcfHeaderLines.add(new VCFFormatHeaderLine(VCFConstants.DEPTH_KEY, 1, VCFHeaderLineType.Integer, "Approximate read depth"));
vcfHeaderLines.add(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
vcfHeaderLines.add(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
for (int side = 0; side < 2; ++side) {
vcfHeaderLines.add(new VCFFormatHeaderLine("CN" + (side == 0 ? 5 : 3), 1, VCFHeaderLineType.Integer, "count clipped in " + (side == 0 ? 5 : 3) + "'"));
}
if (dict != null) {
vcfHeaderLines.addAll(VCFUtils.samSequenceDictToVCFContigHeaderLine(dict));
}
VCFHeader vcfHeader = new VCFHeader(vcfHeaderLines, sample2input.keySet());
w = VCFUtils.createVariantContextWriterToStdout();
w.writeHeader(vcfHeader);
final IntervalTreeMap<Boolean> intervals = new IntervalTreeMap<>();
// w=swf.make(header, System.out);
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(dict);
if (bedFile != null) {
final BedLineCodec bedLineCodec = new BedLineCodec();
LOG.info("Reading " + bedFile);
BufferedReader r = IOUtils.openFileForBufferedReading(bedFile);
String line;
while ((line = r.readLine()) != null) {
BedLine bedLine = bedLineCodec.decode(line);
if (bedLine == null)
continue;
if (dict != null && dict.getSequence(bedLine.getContig()) == null) {
LOG.warning("undefined chromosome in " + bedFile + " " + line);
continue;
}
intervals.put(bedLine.toInterval(), true);
}
CloserUtil.close(r);
}
LinkedList<SAMRecord> buffer = new LinkedList<SAMRecord>();
final Predicate<SAMRecord> filterSamRecords = new Predicate<SAMRecord>() {
@Override
public boolean test(SAMRecord rec) {
if (rec.getReadUnmappedFlag())
return false;
if (rec.isSecondaryOrSupplementary())
return false;
if (rec.getDuplicateReadFlag())
return false;
if (rec.getReadFailsVendorQualityCheckFlag())
return false;
Cigar cigar = rec.getCigar();
if (cigar == null || cigar.numCigarElements() < 2)
return false;
boolean found_S = false;
for (int side = 0; side < 2; ++side) {
CigarElement ce = cigar.getCigarElement(side == 0 ? 0 : cigar.numCigarElements() - 1);
// read must be clipped on 5' or 3' with a good length
if (!ce.getOperator().equals(CigarOperator.S))
continue;
found_S = true;
break;
}
if (!found_S)
return false;
SAMReadGroupRecord g = rec.getReadGroup();
if (g == null || g.getSample() == null || g.getSample().isEmpty())
return false;
return true;
}
};
final FilteringIterator<SAMRecord> forwardIterator = new FilteringIterator<SAMRecord>(merginIter, filterSamRecords);
for (; ; ) {
SAMRecord rec = null;
if (forwardIterator.hasNext()) {
rec = forwardIterator.next();
progress.watch(rec);
if (intervals != null && !intervals.containsOverlapping(new Interval(rec.getReferenceName(), rec.getAlignmentStart(), rec.getAlignmentEnd())))
continue;
}
// need to flush buffer ?
if (rec == null || (!buffer.isEmpty() && !buffer.getLast().getReferenceIndex().equals(rec.getReferenceIndex())) || (!buffer.isEmpty() && buffer.getLast().getUnclippedEnd() + readLength < rec.getUnclippedStart())) {
if (!buffer.isEmpty()) {
int chromStart = buffer.getFirst().getUnclippedStart();
int chromEnd = buffer.getFirst().getUnclippedEnd();
for (SAMRecord sam : buffer) {
chromStart = Math.min(chromStart, sam.getUnclippedStart());
chromEnd = Math.max(chromEnd, sam.getUnclippedEnd());
}
final int winShift = 5;
for (int pos = chromStart; pos + winShift <= chromEnd; pos += winShift) {
int[] count_big_clip = new int[] { 0, 0 };
// int max_depth[]=new int[]{0,0};
List<Genotype> genotypes = new ArrayList<Genotype>();
Set<Allele> all_alleles = new HashSet<Allele>();
all_alleles.add(reference_allele);
boolean found_one_depth_ok = false;
int sum_depth = 0;
int samples_with_high_depth = 0;
for (String sample : sample2input.keySet()) {
GenotypeBuilder gb = new GenotypeBuilder(sample);
int[] count_clipped = new int[] { 0, 0 };
Set<Allele> sample_alleles = new HashSet<Allele>(3);
for (int side = 0; side < 2; ++side) {
for (SAMRecord sam : buffer) {
if (!sam.getReadGroup().getSample().equals(sample))
continue;
Cigar cigar = sam.getCigar();
CigarElement ce = cigar.getCigarElement(side == 0 ? 0 : cigar.numCigarElements() - 1);
if (!ce.getOperator().equals(CigarOperator.S))
continue;
int clipStart = (side == 0 ? sam.getUnclippedStart() : sam.getAlignmentEnd() + 1);
int clipEnd = (side == 0 ? sam.getAlignmentStart() - 1 : sam.getUnclippedEnd());
if ((pos + winShift < clipStart || pos > clipEnd))
continue;
count_clipped[side]++;
if (ce.getLength() >= this.min_clip_length) {
count_big_clip[side]++;
}
sample_alleles.add(alternate_alleles[side]);
gb.attribute("CN" + (side == 0 ? 5 : 3), count_clipped[side]);
}
}
// if(!(found_one_big_clip[0] || found_one_big_clip[1])) continue;
if (count_clipped[0] + count_clipped[1] == 0)
continue;
if ((count_clipped[0] + count_clipped[1]) > min_depth) {
found_one_depth_ok = true;
++samples_with_high_depth;
}
sum_depth += (count_clipped[0] + count_clipped[1]);
gb.alleles(new ArrayList<Allele>(sample_alleles));
all_alleles.addAll(sample_alleles);
gb.DP(count_clipped[0] + count_clipped[1]);
genotypes.add(gb.make());
}
if (all_alleles.size() == 1) {
// all homozygotes
continue;
}
if (!found_one_depth_ok) {
continue;
}
if (!(count_big_clip[0] >= 1 || count_big_clip[1] >= 1)) {
continue;
}
Map<String, Object> atts = new HashMap<String, Object>();
atts.put("COUNT_SAMPLES", samples_with_high_depth);
atts.put(VCFConstants.DEPTH_KEY, sum_depth);
VariantContextBuilder vcb = new VariantContextBuilder();
vcb.chr(buffer.getFirst().getReferenceName());
vcb.start(pos);
vcb.stop(pos + winShift);
vcb.alleles(all_alleles);
vcb.attributes(atts);
vcb.genotypes(genotypes);
w.add(vcb.make());
}
buffer.clear();
}
if (rec == null) {
break;
}
}
buffer.add(rec);
}
merginIter.close();
progress.finish();
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
for (Input input : inputs) {
CloserUtil.close(input);
}
}
}
use of htsjdk.variant.variantcontext.Allele in project jvarkit by lindenb.
the class VcfSimulator method doWork.
@Override
public int doWork(final List<String> args) {
if (this.indexedFastaSequenceFile == null) {
LOG.error("Reference is undefined");
return -1;
}
if (!args.isEmpty()) {
LOG.error("too many arguments");
return -1;
}
if (this.randomSeed == -1L) {
this.random = new Random(System.currentTimeMillis());
} else {
this.random = new Random(this.randomSeed);
}
if (this.numSamples < 0) {
this.numSamples = 1 + this.random.nextInt(10);
}
while (this.samples.size() < numSamples) this.samples.add("SAMPLE" + (1 + this.samples.size()));
VariantContextWriter writer = null;
PrintStream pw = null;
try {
final Set<VCFHeaderLine> metaData = new HashSet<VCFHeaderLine>();
VCFStandardHeaderLines.addStandardFormatLines(metaData, true, "GT", "DP");
VCFStandardHeaderLines.addStandardInfoLines(metaData, true, "AF", "AN", "AC", "DP");
VariantAttributesRecalculator calc = new VariantAttributesRecalculator();
final VCFHeader header = new VCFHeader(metaData, this.samples);
header.setSequenceDictionary(this.indexedFastaSequenceFile.getSequenceDictionary());
calc.setHeader(header);
pw = super.openFileOrStdoutAsPrintStream(this.outputFile);
writer = VCFUtils.createVariantContextWriterToOutputStream(pw);
writer.writeHeader(header);
long countVariantsSoFar = 0;
for (; ; ) {
if (pw.checkError())
break;
if (this.numberOfVariants >= 0 && countVariantsSoFar >= this.numberOfVariants)
break;
for (final SAMSequenceRecord ssr : this.indexedFastaSequenceFile.getSequenceDictionary().getSequences()) {
if (pw.checkError())
break;
final GenomicSequence genomic = new GenomicSequence(this.indexedFastaSequenceFile, ssr.getSequenceName());
for (int pos = 1; pos <= ssr.getSequenceLength(); ++pos) {
if (pw.checkError())
break;
char REF = Character.toUpperCase(genomic.charAt(pos - 1));
if (REF == 'N')
continue;
char ALT = 'N';
switch(REF) {
case 'A':
ALT = "TGC".charAt(random.nextInt(3));
break;
case 'T':
ALT = "AGC".charAt(random.nextInt(3));
break;
case 'G':
ALT = "TAC".charAt(random.nextInt(3));
break;
case 'C':
ALT = "TGA".charAt(random.nextInt(3));
break;
default:
ALT = 'N';
}
if (ALT == 'N')
continue;
final Allele refAllele = Allele.create((byte) REF, true);
Allele altAllele = Allele.create((byte) ALT, false);
final VariantContextBuilder cb = new VariantContextBuilder();
cb.chr(genomic.getChrom());
cb.start(pos);
cb.stop(pos);
List<Genotype> genotypes = new ArrayList<Genotype>(samples.size());
for (String sample : samples) {
final Allele a1 = (random.nextBoolean() ? refAllele : altAllele);
final Allele a2 = (random.nextBoolean() ? refAllele : altAllele);
GenotypeBuilder gb = new GenotypeBuilder(sample, Arrays.asList(a1, a2));
if (random.nextBoolean()) {
gb = new GenotypeBuilder(sample, Arrays.asList(a1, a2));
gb.DP(1 + random.nextInt(50));
}
genotypes.add(gb.make());
}
cb.genotypes(genotypes);
cb.alleles(Arrays.asList(refAllele, altAllele));
writer.add(calc.apply(cb.make()));
countVariantsSoFar++;
}
}
}
writer.close();
writer = null;
pw.flush();
pw.close();
pw = null;
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(this.indexedFastaSequenceFile);
CloserUtil.close(pw);
CloserUtil.close(writer);
}
}
use of htsjdk.variant.variantcontext.Allele in project jvarkit by lindenb.
the class VcfRemoveGenotypeJs method doVcfToVcf.
@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
try {
this.script = super.compileJavascript(scriptExpr, scriptFile);
final VCFHeader h2 = new VCFHeader(in.getHeader());
if (!this.filterName.isEmpty()) {
h2.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_FILTER_KEY, 1, VCFHeaderLineType.String, "Genotype-level filter"));
}
addMetaData(h2);
out.writeHeader(h2);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader());
final Bindings bindings = this.script.getEngine().createBindings();
bindings.put("header", in.getHeader());
while (in.hasNext()) {
final VariantContext ctx = progress.watch(in.next());
bindings.put("variant", ctx);
final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
List<Genotype> genotypes = new ArrayList<>();
int countCalled = ctx.getNSamples();
for (int i = 0; i < ctx.getNSamples(); ++i) {
Genotype genotype = ctx.getGenotype(i);
bindings.put("genotype", genotype);
if (!genotype.isCalled() || genotype.isNoCall() || !genotype.isAvailable()) {
countCalled--;
} else if (genotype.isCalled() && !super.evalJavaScriptBoolean(this.script, bindings)) {
if (!this.filterName.isEmpty()) {
if (!genotype.isFiltered()) {
genotype = new GenotypeBuilder(genotype).filters(this.filterName).make();
}
} else if (this.replaceByHomRef) {
List<Allele> homRefList = new ArrayList<>(genotype.getPloidy());
for (int p = 0; p < genotype.getPloidy(); ++p) {
homRefList.add(ctx.getReference());
}
genotype = new GenotypeBuilder(genotype).alleles(homRefList).make();
} else {
genotype = GenotypeBuilder.createMissing(genotype.getSampleName(), genotype.getPloidy());
}
countCalled--;
}
genotypes.add(genotype);
}
if (countCalled == 0 && this.removeCtxNoGenotype) {
continue;
}
vcb.genotypes(genotypes);
out.add(vcb.make());
}
progress.finish();
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
this.script = null;
}
}
use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.
the class VariantDataManager method writeOutRecalibrationTable.
public void writeOutRecalibrationTable(final VariantContextWriter recalWriter, final SAMSequenceDictionary seqDictionary) {
// we need to sort in coordinate order in order to produce a valid VCF
Collections.sort(data, VariantDatum.getComparator(seqDictionary));
// create dummy alleles to be used
List<Allele> alleles = Arrays.asList(Allele.create("N", true), Allele.create("<VQSR>", false));
for (final VariantDatum datum : data) {
if (VRAC.useASannotations)
//use the alleles to distinguish between multiallelics in AS mode
alleles = Arrays.asList(datum.referenceAllele, datum.alternateAllele);
VariantContextBuilder builder = new VariantContextBuilder("VQSR", datum.loc.getContig(), datum.loc.getStart(), datum.loc.getEnd(), alleles);
builder.attribute(VCFConstants.END_KEY, datum.loc.getEnd());
builder.attribute(GATKVCFConstants.VQS_LOD_KEY, String.format("%.4f", datum.lod));
builder.attribute(GATKVCFConstants.CULPRIT_KEY, (datum.worstAnnotation != -1 ? annotationKeys.get(datum.worstAnnotation) : "NULL"));
if (datum.atTrainingSite)
builder.attribute(GATKVCFConstants.POSITIVE_LABEL_KEY, true);
if (datum.atAntiTrainingSite)
builder.attribute(GATKVCFConstants.NEGATIVE_LABEL_KEY, true);
recalWriter.add(builder.make());
}
}
use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.
the class GenotypeUtils method computeDiploidGenotypeCounts.
/**
* Returns a triple of ref/het/hom genotype "counts".
*
* The exact meaning of the count is dependent on the rounding behavior.
* if {@code roundContributionFromEachGenotype}: the counts are discrete integer counts of the most probable genotype for each {@link Genotype}
* else: they are the sum of the normalized likelihoods of each genotype and will not be integers
*
* Skips non-diploid genotypes.
*
*
* @param vc the VariantContext that the {@link Genotype}s originated from, non-null
* @param genotypes a GenotypesContext containing genotypes to count, these must be a subset of {@code vc.getGenotypes()}, non-null
* @param roundContributionFromEachGenotype if this is true, the normalized likelihood from each genotype will be rounded before
* adding to the total count
*/
public static GenotypeCounts computeDiploidGenotypeCounts(final VariantContext vc, final GenotypesContext genotypes, final boolean roundContributionFromEachGenotype) {
Utils.nonNull(vc, "vc");
Utils.nonNull(genotypes, "genotypes");
final boolean doMultiallelicMapping = !vc.isBiallelic();
int idxAA = 0, idxAB = 1, idxBB = 2;
double refCount = 0;
double hetCount = 0;
double homCount = 0;
for (final Genotype g : genotypes) {
if (!isDiploidWithLikelihoods(g)) {
continue;
}
// Genotype::getLikelihoods returns a new array, so modification in-place is safe
final double[] normalizedLikelihoods = MathUtils.normalizeFromLog10ToLinearSpace(g.getLikelihoods().getAsVector());
if (doMultiallelicMapping) {
if (g.isHetNonRef()) {
//all likelihoods go to homCount
homCount++;
continue;
}
//get alternate allele for each sample
final Allele a1 = g.getAllele(0);
final Allele a2 = g.getAllele(1);
if (a2.isNonReference()) {
final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a2);
idxAA = idxVector[0];
idxAB = idxVector[1];
idxBB = idxVector[2];
} else //I expect hets to be reference first, but there are no guarantees (e.g. phasing)
if (a1.isNonReference()) {
final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a1);
idxAA = idxVector[0];
idxAB = idxVector[1];
idxBB = idxVector[2];
}
}
if (roundContributionFromEachGenotype) {
refCount += MathUtils.fastRound(normalizedLikelihoods[idxAA]);
hetCount += MathUtils.fastRound(normalizedLikelihoods[idxAB]);
homCount += MathUtils.fastRound(normalizedLikelihoods[idxBB]);
} else {
refCount += normalizedLikelihoods[idxAA];
hetCount += normalizedLikelihoods[idxAB];
homCount += normalizedLikelihoods[idxBB];
}
}
return new GenotypeCounts(refCount, hetCount, homCount);
}
Aggregations