/**
   * Read in a list of ExactCall objects from reader, keeping only those with starts in startsToKeep
   * or all sites (if this is empty)
   *
   * @param reader a just-opened reader sitting at the start of the file
   * @param startsToKeep a list of start position of the calls to keep, or empty if all calls should
   *     be kept
   * @param parser a genome loc parser to create genome locs
   * @return a list of ExactCall objects in reader
   * @throws IOException
   */
  public static List<ExactCall> readExactLog(
      final BufferedReader reader, final List<Integer> startsToKeep, GenomeLocParser parser)
      throws IOException {
    if (reader == null) throw new IllegalArgumentException("reader cannot be null");
    if (startsToKeep == null) throw new IllegalArgumentException("startsToKeep cannot be null");
    if (parser == null) throw new IllegalArgumentException("GenomeLocParser cannot be null");

    List<ExactCall> calls = new LinkedList<ExactCall>();

    // skip the header line
    reader.readLine();

    // skip the first "type" line
    reader.readLine();

    while (true) {
      final VariantContextBuilder builder = new VariantContextBuilder();
      final List<Allele> alleles = new ArrayList<Allele>();
      final List<Genotype> genotypes = new ArrayList<Genotype>();
      final double[] posteriors = new double[2];
      final double[] priors = MathUtils.normalizeFromLog10(new double[] {0.5, 0.5}, true);
      final List<Integer> mle = new ArrayList<Integer>();
      final Map<Allele, Double> log10pNonRefByAllele = new HashMap<Allele, Double>();
      long runtimeNano = -1;

      GenomeLoc currentLoc = null;
      while (true) {
        final String line = reader.readLine();
        if (line == null) return calls;

        final String[] parts = line.split("\t");
        final GenomeLoc lineLoc = parser.parseGenomeLoc(parts[0]);
        final String variable = parts[1];
        final String key = parts[2];
        final String value = parts[3];

        if (currentLoc == null) currentLoc = lineLoc;

        if (variable.equals("type")) {
          if (startsToKeep.isEmpty() || startsToKeep.contains(currentLoc.getStart())) {
            builder.alleles(alleles);
            final int stop = currentLoc.getStart() + alleles.get(0).length() - 1;
            builder.chr(currentLoc.getContig()).start(currentLoc.getStart()).stop(stop);
            builder.genotypes(genotypes);
            final int[] mleInts = ArrayUtils.toPrimitive(mle.toArray(new Integer[] {}));
            final AFCalcResult result =
                new AFCalcResult(mleInts, 1, alleles, posteriors, priors, log10pNonRefByAllele);
            calls.add(new ExactCall(builder.make(), runtimeNano, result));
          }
          break;
        } else if (variable.equals("allele")) {
          final boolean isRef = key.equals("0");
          alleles.add(Allele.create(value, isRef));
        } else if (variable.equals("PL")) {
          final GenotypeBuilder gb = new GenotypeBuilder(key);
          gb.PL(GenotypeLikelihoods.fromPLField(value).getAsPLs());
          genotypes.add(gb.make());
        } else if (variable.equals("log10PosteriorOfAFEq0")) {
          posteriors[0] = Double.valueOf(value);
        } else if (variable.equals("log10PosteriorOfAFGt0")) {
          posteriors[1] = Double.valueOf(value);
        } else if (variable.equals("MLE")) {
          mle.add(Integer.valueOf(value));
        } else if (variable.equals("pNonRefByAllele")) {
          final Allele a = Allele.create(key);
          log10pNonRefByAllele.put(a, Double.valueOf(value));
        } else if (variable.equals("runtime.nano")) {
          runtimeNano = Long.valueOf(value);
        } else {
          // nothing to do
        }
      }
    }
  }
 private final double getRefBinomialProb(final int depth) {
   if (depth < binomialProbabilityDepthCache.length) return binomialProbabilityDepthCache[depth];
   else return MathUtils.binomialProbability(0, depth, 0.5);
 }
  /**
   * query='dbConn.((0 as i close by (i + 100 where i <= max(Trener.getPremia())) as i join
   * count((Trener where getPremia() >= i and getPremia() < i + 100)) as c).(c + " Trenerow uzyskuje
   * premie pomiedzy " + i + " i " + i + 100) as message)' '
   */
  public java.util.List<java.lang.String> executeQuery(
      final ObjectContainerBase ocb, final Transaction t) {
    final LocalTransaction transLocal = (LocalTransaction) t;
    java.lang.Integer _asResult_i = 0;
    java.util.List<java.lang.Integer> _closeByResult = new ArrayList<java.lang.Integer>();
    _closeByResult.add(_asResult_i);

    int _i3 = 0;

    while (_i3 < _closeByResult.size()) {
      java.lang.Integer _closeByEl = _closeByResult.get(_i3);

      if (_closeByEl != null) {
        ocb.activate(_closeByEl, 1);
      }

      java.lang.Integer _ident_i = _closeByEl;
      java.lang.Integer _plusResult = _ident_i + 100;

      if (_plusResult != null) {
        ocb.activate(_plusResult, 2);
      }

      java.lang.Integer _whereEl = _plusResult;
      java.lang.Integer _ident_i1 = _closeByEl;
      final java.util.Collection<pl.wcislo.sbql4j.javac.test.football.model.Trener> _ident_Trener =
          new java.util.ArrayList<pl.wcislo.sbql4j.javac.test.football.model.Trener>();
      ClassMetadata _classMeta31 =
          ocb.classCollection()
              .getClassMetadata("pl.wcislo.sbql4j.javac.test.football.model.Trener");
      long[] _ids31 = _classMeta31.getIDs(transLocal);

      for (long _id31 : _ids31) {
        LazyObjectReference _ref31 = transLocal.lazyReferenceFor((int) _id31);
        _ident_Trener.add((pl.wcislo.sbql4j.javac.test.football.model.Trener) _ref31.getObject());
      }

      java.util.Collection<java.lang.Integer> _dotResult =
          new java.util.ArrayList<java.lang.Integer>();
      int _dotIndex = 0;

      if (_ident_Trener != null) {
        for (pl.wcislo.sbql4j.javac.test.football.model.Trener _dotEl : _ident_Trener) {
          if (_dotEl == null) {
            continue;
          }

          if (_dotEl != null) {
            ocb.activate(_dotEl, 1);
          }

          java.lang.Integer _mth_getPremiaResult = _dotEl.getPremia();

          if (_mth_getPremiaResult != null) {
            ocb.activate(_mth_getPremiaResult, 1);
          }

          if (_mth_getPremiaResult != null) {
            ocb.activate(_mth_getPremiaResult, 1);
          }

          _dotResult.add(_mth_getPremiaResult);
          _dotIndex++;
        }
      }

      Number _max7 = null;

      for (Number _maxEl7 : _dotResult) {
        _max7 = MathUtils.max(_max7, _maxEl7);
      }

      java.lang.Integer _maxResult = (java.lang.Integer) _max7;

      Boolean _less_or_equalResult = _ident_i1 <= _maxResult;
      java.lang.Integer _whereResult = null;

      if (_less_or_equalResult) {
        _whereResult = _plusResult;
      }

      java.lang.Integer _asResult_i1 = _whereResult;

      if (_asResult_i1 != null) {
        _closeByResult.add(_asResult_i1);
      }

      _i3++;
    }

    java.util.List<pl.wcislo.sbql4j.java.model.runtime.Struct> _joinResult =
        new java.util.ArrayList<pl.wcislo.sbql4j.java.model.runtime.Struct>();
    int _joinIndex = 0;

    for (java.lang.Integer _joinEl : _closeByResult) {
      if (_joinEl != null) {
        ocb.activate(_joinEl, 1);
      }

      final java.util.Collection<pl.wcislo.sbql4j.javac.test.football.model.Trener> _ident_Trener1 =
          new java.util.ArrayList<pl.wcislo.sbql4j.javac.test.football.model.Trener>();
      ClassMetadata _classMeta32 =
          ocb.classCollection()
              .getClassMetadata("pl.wcislo.sbql4j.javac.test.football.model.Trener");
      long[] _ids32 = _classMeta32.getIDs(transLocal);

      for (long _id32 : _ids32) {
        LazyObjectReference _ref32 = transLocal.lazyReferenceFor((int) _id32);
        _ident_Trener1.add((pl.wcislo.sbql4j.javac.test.football.model.Trener) _ref32.getObject());
      }

      java.util.Collection<pl.wcislo.sbql4j.javac.test.football.model.Trener> _whereResult1 =
          new java.util.ArrayList<pl.wcislo.sbql4j.javac.test.football.model.Trener>();
      int _whereLoopIndex1 = 0;

      for (pl.wcislo.sbql4j.javac.test.football.model.Trener _whereEl1 : _ident_Trener1) {
        if (_whereEl1 == null) {
          continue;
        }

        if (_whereEl1 != null) {
          ocb.activate(_whereEl1, 1);
        }

        java.lang.Integer _mth_getPremiaResult1 = _whereEl1.getPremia();

        if (_mth_getPremiaResult1 != null) {
          ocb.activate(_mth_getPremiaResult1, 1);
        }

        java.lang.Integer _ident_i2 = _joinEl;

        if (_ident_i2 != null) {
          ocb.activate(_ident_i2, 1);
        }

        Boolean _more_or_equalResult = _mth_getPremiaResult1 >= _ident_i2;
        java.lang.Boolean _andResult;

        if (!_more_or_equalResult) {
          _andResult = false;
        } else {
          java.lang.Integer _mth_getPremiaResult2 = _whereEl1.getPremia();

          if (_mth_getPremiaResult2 != null) {
            ocb.activate(_mth_getPremiaResult2, 1);
          }

          java.lang.Integer _ident_i3 = _joinEl;

          if (_ident_i3 != null) {
            ocb.activate(_ident_i3, 1);
          }

          java.lang.Integer _plusResult1 = _ident_i3 + 100;

          Boolean _lessResult = _mth_getPremiaResult2 < _plusResult1;
          _andResult = _lessResult;
        }

        if (_andResult) {
          _whereResult1.add(_whereEl1);
        }

        _whereLoopIndex1++;
      }

      java.lang.Integer _countResult = _whereResult1.size();
      java.lang.Integer _asResult_c = _countResult;
      _joinResult.add(OperatorUtils.cartesianProductSS(_joinEl, _asResult_c, "i", "c"));
      _joinIndex++;
    }

    java.util.List<java.lang.String> _dotResult1 = new java.util.ArrayList<java.lang.String>();
    int _dotIndex1 = 0;

    if (_joinResult != null) {
      for (pl.wcislo.sbql4j.java.model.runtime.Struct _dotEl1 : _joinResult) {
        if (_dotEl1 == null) {
          continue;
        }

        if (_dotEl1 != null) {
          ocb.activate(_dotEl1, 1);
        }

        java.lang.Integer _ident_c = (java.lang.Integer) _dotEl1.get("c");

        if (_ident_c != null) {
          ocb.activate(_ident_c, 1);
        }

        java.lang.String _plusResult2 = _ident_c + " Trenerow uzyskuje premie pomiedzy  ";
        java.lang.Integer _ident_i4 = (java.lang.Integer) _dotEl1.get("i");

        if (_ident_i4 != null) {
          ocb.activate(_ident_i4, 1);
        }

        java.lang.String _plusResult3 = _plusResult2 + _ident_i4;
        java.lang.String _plusResult4 = _plusResult3 + " i ";
        java.lang.Integer _ident_i5 = (java.lang.Integer) _dotEl1.get("i");

        if (_ident_i5 != null) {
          ocb.activate(_ident_i5, 1);
        }

        java.lang.Integer _plusResult5 = _ident_i5 + 100;
        java.lang.String _plusResult6 = _plusResult4 + _plusResult5;

        if (_plusResult6 != null) {
          ocb.activate(_plusResult6, 1);
        }

        _dotResult1.add(_plusResult6);
        _dotIndex1++;
      }
    }

    java.util.List<java.lang.String> _asResult_message = _dotResult1;
    pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_asResult_message, ocb);

    return _asResult_message;
  }
 static {
   for (int i = 1; i < binomialProbabilityDepthCache.length; i++) {
     binomialProbabilityDepthCache[i] = MathUtils.binomialProbability(0, i, 0.5);
   }
 }