/**
   * Assert that two variant contexts are actually equal
   *
   * @param actual
   * @param expected
   */
  public static void assertEquals(final VariantContext actual, final VariantContext expected) {
    Assert.assertNotNull(actual, "VariantContext expected not null");
    Assert.assertEquals(actual.getChr(), expected.getChr(), "chr");
    Assert.assertEquals(actual.getStart(), expected.getStart(), "start");
    Assert.assertEquals(actual.getEnd(), expected.getEnd(), "end");
    Assert.assertEquals(actual.getID(), expected.getID(), "id");
    Assert.assertEquals(
        actual.getAlleles(), expected.getAlleles(), "alleles for " + expected + " vs " + actual);

    assertAttributesEquals(actual.getAttributes(), expected.getAttributes());
    Assert.assertEquals(
        actual.filtersWereApplied(), expected.filtersWereApplied(), "filtersWereApplied");
    Assert.assertEquals(actual.isFiltered(), expected.isFiltered(), "isFiltered");
    VariantBaseTest.assertEqualsSet(actual.getFilters(), expected.getFilters(), "filters");
    VariantBaseTest.assertEqualsDoubleSmart(
        actual.getPhredScaledQual(), expected.getPhredScaledQual());

    Assert.assertEquals(actual.hasGenotypes(), expected.hasGenotypes(), "hasGenotypes");
    if (expected.hasGenotypes()) {
      VariantBaseTest.assertEqualsSet(
          actual.getSampleNames(), expected.getSampleNames(), "sample names set");
      Assert.assertEquals(
          actual.getSampleNamesOrderedByName(),
          expected.getSampleNamesOrderedByName(),
          "sample names");
      final Set<String> samples = expected.getSampleNames();
      for (final String sample : samples) {
        assertEquals(actual.getGenotype(sample), expected.getGenotype(sample));
      }
    }
  }
  public static void testReaderWriterWithMissingGenotypes(
      final VariantContextIOTest tester, final VariantContextTestData data) throws IOException {
    final int nSamples = data.header.getNGenotypeSamples();
    if (nSamples > 2) {
      for (final VariantContext vc : data.vcs)
        if (vc.isSymbolic())
          // cannot handle symbolic alleles because they may be weird non-call VCFs
          return;

      final File tmpFile = File.createTempFile("testReaderWriter", tester.getExtension());
      tmpFile.deleteOnExit();

      // write expected to disk
      final EnumSet<Options> options = EnumSet.of(Options.INDEX_ON_THE_FLY);
      final VariantContextWriter writer = tester.makeWriter(tmpFile, options);

      final Set<String> samplesInVCF = new HashSet<String>(data.header.getGenotypeSamples());
      final List<String> missingSamples = Arrays.asList("MISSING1", "MISSING2");
      final List<String> allSamples = new ArrayList<String>(missingSamples);
      allSamples.addAll(samplesInVCF);

      final VCFHeader header = new VCFHeader(data.header.getMetaDataInInputOrder(), allSamples);
      writeVCsToFile(writer, header, data.vcs);

      // ensure writing of expected == actual
      final VariantContextContainer p = tester.readAllVCs(tmpFile);
      final Iterable<VariantContext> actual = p.getVCs();

      int i = 0;
      for (final VariantContext readVC : actual) {
        if (readVC == null) continue; // sometimes we read null records...
        final VariantContext expected = data.vcs.get(i++);
        for (final Genotype g : readVC.getGenotypes()) {
          Assert.assertTrue(allSamples.contains(g.getSampleName()));
          if (samplesInVCF.contains(g.getSampleName())) {
            assertEquals(g, expected.getGenotype(g.getSampleName()));
          } else {
            // missing
            Assert.assertTrue(g.isNoCall());
          }
        }
      }
    }
  }
示例#3
0
  public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) {
    if (tracker == null || !BaseUtils.isRegularBase(ref.getBase())) return 0;

    Collection<VariantContext> contexts = getVariantContexts(tracker, ref);

    for (VariantContext vc : contexts) {
      VariantContextBuilder builder = new VariantContextBuilder(vc);

      // set the appropriate sample name if necessary
      if (sampleName != null && vc.hasGenotypes() && vc.hasGenotype(variants.getName())) {
        Genotype g =
            new GenotypeBuilder(vc.getGenotype(variants.getName())).name(sampleName).make();
        builder.genotypes(g);
      }

      final VariantContext withID = variantOverlapAnnotator.annotateRsID(tracker, builder.make());
      writeRecord(withID, tracker, ref.getLocus());
    }

    return 1;
  }