use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.
the class TestSupport method createRandomPedigreeFromFile.
public Path createRandomPedigreeFromFile(final String vcfFile) throws IOException {
class TmpSample {
String fam = "F1";
String name;
String father = "0";
String mother = "0";
int sex = 0;
int status = 0;
}
int status = 0;
final Path vcfIn = Paths.get(vcfFile);
final VCFReader r = VCFReaderFactory.makeDefault().open(vcfIn, false);
final VCFHeader header = r.getHeader();
r.close();
final List<String> samples = new ArrayList<>(header.getSampleNamesInOrder());
final List<TmpSample> ped = new ArrayList<>();
if (samples.isEmpty())
return null;
while (!samples.isEmpty()) {
final TmpSample indi = new TmpSample();
indi.name = samples.remove(0);
indi.fam = ped.stream().filter(P -> P.father.equals(indi.name) || P.mother.equals(indi.name)).map(P -> P.fam).findFirst().orElse("F" + this.random.nextInt());
if (ped.stream().anyMatch(P -> P.father.equals(indi.name))) {
indi.sex = 1;
} else if (ped.stream().anyMatch(P -> P.mother.equals(indi.name))) {
indi.sex = 2;
} else {
indi.sex = this.random.nextBoolean() ? 1 : 2;
}
// TODO
indi.sex = 0;
if (random.nextBoolean()) {
final List<String> remain = new ArrayList<>(samples);
if (!remain.isEmpty()) {
indi.father = remain.remove(0);
} else {
indi.father = "0";
}
if (!remain.isEmpty()) {
indi.mother = remain.remove(0);
} else {
indi.mother = "0";
}
} else {
indi.father = "0";
indi.mother = "0";
}
indi.status = (status++) % 2;
ped.add(indi);
}
final Path pedFile = createTmpPath(".ped");
final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(pedFile));
for (TmpSample ts : ped) {
pw.print(ts.fam);
pw.print('\t');
pw.print(ts.name);
pw.print('\t');
pw.print(ts.father);
pw.print('\t');
pw.print(ts.mother);
pw.print('\t');
pw.print(ts.sex);
pw.print('\t');
pw.print(ts.status);
pw.println();
}
pw.flush();
pw.close();
return pedFile;
}
use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.
the class TestSupport method wc.
public long wc(final Path f) throws IOException {
if (f.getFileName().toString().endsWith(".bam") || f.getFileName().toString().endsWith(".sam")) {
final SamReader sr = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(f);
final SAMRecordIterator iter = sr.iterator();
final long n = iter.stream().count();
iter.close();
sr.close();
return n;
} else if (f.getFileName().toString().endsWith(".vcf") || f.getFileName().toString().endsWith(".vcf.gz")) {
final VCFReader sr = VCFReaderFactory.makeDefault().open(f, false);
final CloseableIterator<VariantContext> iter = sr.iterator();
final long n = iter.stream().count();
iter.close();
sr.close();
return n;
}
final BufferedReader br = IOUtils.openPathForBufferedReading(f);
final long n = br.lines().count();
br.close();
return n;
}
use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.
the class VcfPeekAfTest method testACAN01.
public void testACAN01() throws IOException {
try {
String vcfin = support.resource("test_vcf01.vcf");
String vcfdb = vcfin;
Path out = support.createTmpPath(".vcf");
Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "ACAN", "--treshold", "0.5", "--database", vcfdb, vcfin }), 0);
support.assertIsVcf(out);
try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
Assert.assertEquals(r1.iterator().stream().filter(V -> V.getNAlleles() == 2 && V.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY)).filter(V -> V.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0) > 0.5).count(), 0L);
}
} finally {
support.removeTmpFiles();
}
}
use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.
the class VcfPeekAfTest method testGT01.
public void testGT01() throws IOException {
try {
String vcfin = support.resource("test_vcf01.vcf");
String vcfdb = vcfin;
Path out = support.createTmpPath(".vcf");
Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "GT", "--treshold", "0.5", "--database", vcfdb, vcfin }), 0);
support.assertIsVcf(out);
try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
Assert.assertEquals(r1.iterator().stream().filter(V -> V.getNAlleles() == 2 && V.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY)).filter(V -> V.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0) > 0.5).count(), 0L);
}
} finally {
support.removeTmpFiles();
}
}
use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.
the class VcfPeekAfTest method testAF02.
public void testAF02() throws IOException {
try {
final String fname = "THEFILTER";
String vcfin = support.resource("test_vcf01.vcf");
String vcfdb = vcfin;
Path out = support.createTmpPath(".vcf");
Assert.assertEquals(new VcfPeekAf().instanceMain(new String[] { "-o", out.toString(), "--peeker", "AF", "--treshold", "0.5", "--database", vcfdb, "--filter", fname, vcfin }), 0);
support.assertIsVcf(out);
try (VCFReader r1 = VCFReaderFactory.makeDefault().open(out, false)) {
CloseableIterator<VariantContext> iter = r1.iterator();
while (iter.hasNext()) {
VariantContext ctx = iter.next();
if (ctx.getNAlleles() != 2)
continue;
if (!ctx.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY))
continue;
double af = ctx.getAttributeAsDouble(VCFConstants.ALLELE_FREQUENCY_KEY, 1.0);
if (af <= 0.5) {
Assert.assertFalse(ctx.getFilters().contains(fname));
} else {
Assert.assertTrue(ctx.getFilters().contains(fname));
}
}
iter.close();
}
} finally {
support.removeTmpFiles();
}
}
Aggregations