Beispiel #1
0
  static boolean allGenotypesAreUnfilteredAndCalled(VariantContext vc) {
    for (final Genotype gt : vc.getGenotypes()) {
      if (gt.isNoCall() || gt.isFiltered()) return false;
    }

    return true;
  }
Beispiel #2
0
  protected final void printCallInfo(
      final VariantContext vc,
      final double[] log10AlleleFrequencyPriors,
      final long runtimeNano,
      final AFCalcResult result) {
    printCallElement(vc, "type", "ignore", vc.getType());

    int allelei = 0;
    for (final Allele a : vc.getAlleles())
      printCallElement(vc, "allele", allelei++, a.getDisplayString());

    for (final Genotype g : vc.getGenotypes())
      printCallElement(vc, "PL", g.getSampleName(), g.getLikelihoodsString());

    for (int priorI = 0; priorI < log10AlleleFrequencyPriors.length; priorI++)
      printCallElement(vc, "priorI", priorI, log10AlleleFrequencyPriors[priorI]);

    printCallElement(vc, "runtime.nano", "ignore", runtimeNano);
    printCallElement(vc, "log10PosteriorOfAFEq0", "ignore", result.getLog10PosteriorOfAFEq0());
    printCallElement(vc, "log10PosteriorOfAFGt0", "ignore", result.getLog10PosteriorOfAFGT0());

    for (final Allele allele : result.getAllelesUsedInGenotyping()) {
      if (allele.isNonReference()) {
        printCallElement(vc, "MLE", allele, result.getAlleleCountAtMLE(allele));
        printCallElement(
            vc, "pNonRefByAllele", allele, result.getLog10PosteriorOfAFGt0ForAllele(allele));
      }
    }

    callReport.flush();
  }
Beispiel #3
0
 public PhaseAndQuality(Genotype gt) {
   this.isPhased = gt.isPhased();
   if (this.isPhased) {
     this.PQ = gt.getAttributeAsDouble(ReadBackedPhasingWalker.PQ_KEY, -1);
     if (this.PQ == -1) this.PQ = null;
   }
 }
  /**
   * Returns a context identical to this with the REF and ALT alleles reverse complemented.
   *
   * @param vc variant context
   * @return new vc
   */
  public static VariantContext reverseComplement(VariantContext vc) {
    // create a mapping from original allele to reverse complemented allele
    HashMap<Allele, Allele> alleleMap = new HashMap<Allele, Allele>(vc.getAlleles().size());
    for (Allele originalAllele : vc.getAlleles()) {
      Allele newAllele;
      if (originalAllele.isNoCall() || originalAllele.isNull()) newAllele = originalAllele;
      else
        newAllele =
            Allele.create(
                BaseUtils.simpleReverseComplement(originalAllele.getBases()),
                originalAllele.isReference());
      alleleMap.put(originalAllele, newAllele);
    }

    // create new Genotype objects
    GenotypesContext newGenotypes = GenotypesContext.create(vc.getNSamples());
    for (final Genotype genotype : vc.getGenotypes()) {
      List<Allele> newAlleles = new ArrayList<Allele>();
      for (Allele allele : genotype.getAlleles()) {
        Allele newAllele = alleleMap.get(allele);
        if (newAllele == null) newAllele = Allele.NO_CALL;
        newAlleles.add(newAllele);
      }
      newGenotypes.add(Genotype.modifyAlleles(genotype, newAlleles));
    }

    return new VariantContextBuilder(vc).alleles(alleleMap.values()).genotypes(newGenotypes).make();
  }
Beispiel #5
0
  public void validateAlternateAlleles() {
    if (!hasGenotypes()) return;

    List<Allele> reportedAlleles = getAlleles();
    Set<Allele> observedAlleles = new HashSet<Allele>();
    observedAlleles.add(getReference());
    for (final Genotype g : getGenotypes()) {
      if (g.isCalled()) observedAlleles.addAll(g.getAlleles());
    }

    if (reportedAlleles.size() != observedAlleles.size())
      throw new TribbleException.InternalCodecException(
          String.format(
              "the ALT allele(s) for the record at position %s:%d do not match what is observed in the per-sample genotypes",
              getChr(), getStart()));

    int originalSize = reportedAlleles.size();
    // take the intersection and see if things change
    observedAlleles.retainAll(reportedAlleles);
    if (observedAlleles.size() != originalSize)
      throw new TribbleException.InternalCodecException(
          String.format(
              "the ALT allele(s) for the record at position %s:%d do not match what is observed in the per-sample genotypes",
              getChr(), getStart()));
  }
 @Test
 public void testAsGenotypeLocations() {
   Genome genome = new Genome("profileId", "ACGT__");
   Genotype genotype = genome.asGenotype("rs41362547", "rs28358280", "rs3915952");
   assertEquals(genome.getProfileId(), genotype.getProfileId());
   assertEquals("AC", genotype.getValues().get("rs41362547"));
   assertEquals("GT", genotype.getValues().get("rs28358280"));
   assertEquals("__", genotype.getValues().get("rs3915952"));
 }
Beispiel #7
0
  /**
   * Returns the number of chromosomes carrying any allele in the genotypes (i.e., excluding
   * NO_CALLS)
   *
   * @return chromosome count
   */
  public int getCalledChrCount() {
    int n = 0;

    for (final Genotype g : getGenotypes()) {
      for (final Allele a : g.getAlleles()) n += a.isNoCall() ? 0 : 1;
    }

    return n;
  }
  public static GenotypesContext stripPLs(GenotypesContext genotypes) {
    GenotypesContext newGs = GenotypesContext.create(genotypes.size());

    for (final Genotype g : genotypes) {
      newGs.add(g.hasLikelihoods() ? removePLs(g) : g);
    }

    return newGs;
  }
Beispiel #9
0
  /**
   * Returns the number of chromosomes carrying allele A in the genotypes
   *
   * @param a allele
   * @return chromosome count
   */
  public int getCalledChrCount(Allele a) {
    int n = 0;

    for (final Genotype g : getGenotypes()) {
      n += g.getAlleles(a).size();
    }

    return n;
  }
Beispiel #10
0
  private void calculateGenotypeCounts() {
    if (genotypeCounts == null) {
      genotypeCounts = new int[Genotype.Type.values().length];

      for (final Genotype g : getGenotypes()) {
        genotypeCounts[g.getType().ordinal()]++;
      }
    }
  }
Beispiel #11
0
  static boolean alleleSegregationIsKnown(Genotype gt1, Genotype gt2) {
    if (gt1.getPloidy() != gt2.getPloidy()) return false;

    /* If gt2 is phased or hom, then could even be MERGED with gt1 [This is standard].

      HOWEVER, EVEN if this is not the case, but gt1.isHom(),
      it is trivially known that each of gt2's alleles segregate with the single allele type present in gt1.
    */
    return (gt2.isPhased() || gt2.isHom() || gt1.isHom());
  }
Beispiel #12
0
  static boolean allSamplesAreMergeable(VariantContext vc1, VariantContext vc2) {
    // Check that each sample's genotype in vc2 is uniquely appendable onto its genotype in vc1:
    for (final Genotype gt1 : vc1.getGenotypes()) {
      Genotype gt2 = vc2.getGenotype(gt1.getSampleName());

      if (!alleleSegregationIsKnown(gt1, gt2)) // can merge if: phased, or if either is a hom
      return false;
    }

    return true;
  }
Beispiel #13
0
  /**
   * Update the agent's sentiment. The new sentiment is based on an average of the values of the
   * technical indicators, weighted by the genetically determined degrees to which the agents trusts
   * them. A kind of mental inertia is accounted for by taking the previous mental state as a term
   * of the average.
   */
  private void updateSentiment() {
    double sum = sentiment;
    double totalWeight = 1.0;

    for (int i = 1; i <= 22; i++) {
      double signal = (double) Market.signal(i);
      sum += signal * genotype.gene(i);
      totalWeight += genotype.gene(i);
    }
    sentiment = sum / totalWeight;
    if (sentiment < -1.0 || sentiment > 1.0) throw new RuntimeException("sentiment = " + sentiment);
  }
Beispiel #14
0
 public Color getGenotypeColor(Genotype genotype, boolean isFiltered) {
   if (genotype.isNoCall()) {
     return isFiltered ? colorNoCallAlpha : colorNoCall;
   } else if (genotype.isHomRef()) {
     return isFiltered ? colorHomRefAlpha : colorHomRef;
   } else if (genotype.isHomVar()) {
     return isFiltered ? colorHomVarAlpha : colorHomVar;
   } else if (genotype.isHet()) {
     return isFiltered ? colorHetAlpha : colorHet;
   }
   return Color.white;
 }
Beispiel #15
0
  private void validateGenotypes() {
    if (this.genotypes == null) throw new IllegalStateException("Genotypes is null");

    for (final Genotype g : this.genotypes) {
      if (g.isAvailable()) {
        for (Allele gAllele : g.getAlleles()) {
          if (!hasAllele(gAllele) && gAllele.isCalled())
            throw new IllegalStateException(
                "Allele in genotype " + gAllele + " not in the variant context " + alleles);
        }
      }
    }
  }
 @Test
 public void testAsGenotype() {
   StringBuilder sb = new StringBuilder();
   for (String location : Locations.locations()) {
     sb.append("AC");
   }
   Genome genome = new Genome("profileId", sb.toString());
   Genotype genotype = genome.asGenotype();
   assertEquals(genome.getProfileId(), genotype.getProfileId());
   for (String location : Locations.locations()) {
     assertEquals("AC", genotype.getValues().get(location));
   }
 }
Beispiel #17
0
  /**
   * helper routine for subcontext
   *
   * @param genotypes genotypes
   * @return allele set
   */
  private final Set<Allele> allelesOfGenotypes(Collection<Genotype> genotypes) {
    final Set<Allele> alleles = new HashSet<Allele>();

    boolean addedref = false;
    for (final Genotype g : genotypes) {
      for (final Allele a : g.getAlleles()) {
        addedref = addedref || a.isReference();
        if (a.isCalled()) alleles.add(a);
      }
    }
    if (!addedref) alleles.add(getReference());

    return alleles;
  }
  private static void mergeGenotypes(
      GenotypesContext mergedGenotypes,
      VariantContext oneVC,
      AlleleMapper alleleMapping,
      boolean uniqifySamples) {
    for (Genotype g : oneVC.getGenotypes()) {
      String name = mergedSampleName(oneVC.getSource(), g.getSampleName(), uniqifySamples);
      if (!mergedGenotypes.containsSample(name)) {
        // only add if the name is new
        Genotype newG = g;

        if (uniqifySamples || alleleMapping.needsRemapping()) {
          final List<Allele> alleles =
              alleleMapping.needsRemapping() ? alleleMapping.remap(g.getAlleles()) : g.getAlleles();
          newG =
              new Genotype(
                  name,
                  alleles,
                  g.getLog10PError(),
                  g.getFilters(),
                  g.getAttributes(),
                  g.isPhased());
        }

        mergedGenotypes.add(newG);
      }
    }
  }
  public static VariantContext purgeUnallowedGenotypeAttributes(
      VariantContext vc, Set<String> allowedAttributes) {
    if (allowedAttributes == null) return vc;

    GenotypesContext newGenotypes = GenotypesContext.create(vc.getNSamples());
    for (final Genotype genotype : vc.getGenotypes()) {
      Map<String, Object> attrs = new HashMap<String, Object>();
      for (Map.Entry<String, Object> attr : genotype.getAttributes().entrySet()) {
        if (allowedAttributes.contains(attr.getKey())) attrs.put(attr.getKey(), attr.getValue());
      }
      newGenotypes.add(Genotype.modifyAttributes(genotype, attrs));
    }

    return new VariantContextBuilder(vc).genotypes(newGenotypes).make();
  }
Beispiel #20
0
 /** Initializes map of genotypes to probabilities */
 private void initGeneMap() {
   int size = genotype.getGeneList().size();
   for (int i = 0; i < size; i++) {
     geneProbMap.put(i, 1);
   }
   geneProbSum = size;
 }
 /**
  * @throws Exception
  * @author Klaus Meffert
  */
 public void testGetter_0() throws Exception {
   Configuration conf = new Configuration();
   Genotype.setStaticConfiguration(conf);
   assertEquals(false, conf.isLocked());
   FitnessFunction fitFunc = new StaticFitnessFunction(2);
   conf.setFitnessFunction(fitFunc);
   Gene gene = new BooleanGene(conf);
   Chromosome sample = new Chromosome(conf, gene, 55);
   conf.setSampleChromosome(sample);
   NaturalSelector natSel = new WeightedRouletteSelector();
   conf.addNaturalSelector(natSel, false);
   RandomGenerator randGen = new StockRandomGenerator();
   conf.setRandomGenerator(randGen);
   IEventManager evMan = new EventManager();
   conf.setEventManager(evMan);
   GeneticOperator mutOp = new MutationOperator(conf);
   conf.addGeneticOperator(mutOp);
   GeneticOperator croOp = new CrossoverOperator(conf);
   conf.addGeneticOperator(croOp);
   conf.setPopulationSize(7);
   assertEquals(fitFunc, conf.getFitnessFunction());
   assertEquals(natSel, conf.getNaturalSelectors(false).get(0));
   assertEquals(randGen, conf.getRandomGenerator());
   assertEquals(sample, conf.getSampleChromosome());
   assertEquals(evMan, conf.getEventManager());
   assertEquals(7, conf.getPopulationSize());
   assertEquals(2, conf.getGeneticOperators().size());
   assertEquals(mutOp, conf.getGeneticOperators().get(0));
   assertEquals(croOp, conf.getGeneticOperators().get(1));
 }
 /**
  * Write report on eveluation to the given stream.
  *
  * @param a_fitnessFunction p_SupergeneChangeFitnessFunction
  * @param a_population Genotype
  * @return Chromosome
  */
 public IChromosome report(
     SupergeneChangeFitnessFunction a_fitnessFunction, Genotype a_population) {
   IChromosome bestSolutionSoFar = a_population.getFittestChromosome();
   if (!REPORT_ENABLED) {
     return bestSolutionSoFar;
   }
   System.out.println(
       "\nThe best solution has a fitness value of " + bestSolutionSoFar.getFitnessValue());
   System.out.println("It contained the following: ");
   System.out.println(
       "\t"
           + a_fitnessFunction.getNumberOfCoinsAtGene(bestSolutionSoFar, QUARTERS)
           + " quarters.");
   System.out.println(
       "\t" + a_fitnessFunction.getNumberOfCoinsAtGene(bestSolutionSoFar, DIMES) + " dimes.");
   System.out.println(
       "\t" + a_fitnessFunction.getNumberOfCoinsAtGene(bestSolutionSoFar, NICKELS) + " nickels.");
   System.out.println(
       "\t" + a_fitnessFunction.getNumberOfCoinsAtGene(bestSolutionSoFar, PENNIES) + " pennies.");
   System.out.println(
       "For a total of "
           + a_fitnessFunction.amountOfChange(bestSolutionSoFar)
           + " cents in "
           + a_fitnessFunction.getTotalNumberOfCoins(bestSolutionSoFar)
           + " coins.");
   return bestSolutionSoFar;
 }
 /**
  * @throws Exception
  * @author Klaus Meffert
  */
 public void testAddNaturalSelector_1() throws Exception {
   Configuration conf = new Configuration();
   Genotype.setStaticConfiguration(conf);
   NaturalSelector selector = new WeightedRouletteSelector();
   conf.addNaturalSelector(selector, false);
   assertEquals(selector, conf.getNaturalSelectors(false).get(0));
 }
Beispiel #24
0
  /** Create a population of DoubleGenes */
  public static Population<DoubleGene, Double> newDoubleGenePopulation(
      final int ngenes, final int nchromosomes, final int npopulation) {
    final MSeq<DoubleChromosome> chromosomes = MSeq.ofLength(nchromosomes);

    for (int i = 0; i < nchromosomes; ++i) {
      chromosomes.set(i, DoubleChromosome.of(0, 10, ngenes));
    }

    final Genotype<DoubleGene> genotype = new Genotype<>(chromosomes.toISeq());
    final Population<DoubleGene, Double> population = new Population<>(npopulation);

    for (int i = 0; i < npopulation; ++i) {
      population.add(Phenotype.of(genotype.newInstance(), 0, FF).evaluate());
    }

    return population;
  }
Beispiel #25
0
 /** Recombination constructor. */
 public EvoTrader(EvoTrader mom, EvoTrader dad) {
   super(uniqueId(), 0, 0);
   Market.log.println(name + " = " + mom.name + " * " + dad.name);
   genotype = new Genotype(mom.genotype, dad.genotype);
   genotype.mutate(0.0038);
   long mc = mom.cash / 4;
   long ma = mom.asset / 4;
   long dc = dad.cash / 4;
   long da = dad.asset / 4;
   mom.cash(-mc);
   mom.asset(-ma);
   dad.cash(-dc);
   dad.asset(-da);
   cash = mc + dc;
   asset = ma + da;
   sentiment = 2.0 * genotype.gene(0) - 1.0;
 }
    public String toString() {
      StringBuilder b = new StringBuilder();
      b.append("VariantContextTestData: [");
      final VariantContext vc = vcs.get(0);
      final VariantContextBuilder builder = new VariantContextBuilder(vc);
      builder.noGenotypes();
      b.append(builder.make().toString());
      if (vc.getNSamples() < 5) {
        for (final Genotype g : vc.getGenotypes()) b.append(g.toString());
      } else {
        b.append(" nGenotypes = ").append(vc.getNSamples());
      }

      if (vcs.size() > 1)
        b.append(" ----- with another ").append(vcs.size() - 1).append(" VariantContext records");
      b.append("]");
      return b.toString();
    }
Beispiel #27
0
  static boolean someSampleHasDoubleNonReferenceAllele(VariantContext vc1, VariantContext vc2) {
    for (final Genotype gt1 : vc1.getGenotypes()) {
      Genotype gt2 = vc2.getGenotype(gt1.getSampleName());

      List<Allele> site1Alleles = gt1.getAlleles();
      List<Allele> site2Alleles = gt2.getAlleles();

      Iterator<Allele> all2It = site2Alleles.iterator();
      for (Allele all1 : site1Alleles) {
        Allele all2 = all2It.next(); // this is OK, since allSamplesAreMergeable()

        if (all1.isNonReference() && all2.isNonReference()) // corresponding alleles are alternate
        return true;
      }
    }

    return false;
  }
  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());
          }
        }
      }
    }
  }
 /**
  * @throws Exception
  * @author Klaus Meffert
  */
 public void testAddNaturalSelector_4() throws Exception {
   Configuration conf = new Configuration();
   Genotype.setStaticConfiguration(conf);
   NaturalSelector selector = new WeightedRouletteSelector();
   conf.addNaturalSelector(selector, true);
   conf.getNaturalSelectors(true).clear();
   conf.addNaturalSelector(selector, true);
   int i = conf.getNaturalSelectors(true).size();
   assertEquals(1, i);
 }
Beispiel #30
0
  /** Count the number of different genes. */
  public static int diff(
      final Population<DoubleGene, Double> p1, final Population<DoubleGene, Double> p2) {
    int count = 0;
    for (int i = 0; i < p1.size(); ++i) {
      final Genotype<?> gt1 = p1.get(i).getGenotype();
      final Genotype<?> gt2 = p2.get(i).getGenotype();

      for (int j = 0; j < gt1.length(); ++j) {
        final Chromosome<?> c1 = gt1.getChromosome(j);
        final Chromosome<?> c2 = gt2.getChromosome(j);

        for (int k = 0; k < c1.length(); ++k) {
          if (!c1.getGene(k).equals(c2.getGene(k))) {
            ++count;
          }
        }
      }
    }
    return count;
  }