Exemplo n.º 1
0
  public ArrayList<Double> mapPDScoresToAlignment(
      ArrayList<Double> pdScores, Sequence seq, ArrayList<Integer> indicesOfRowsWithSumZero) {
    ArrayList<Double> result = new ArrayList<Double>();

    int seqLength = seq.length();
    int nonGapPosition = 0;
    Symbol gap = seq.getAlphabet().getGapSymbol();
    for (int i = 0; i < seqLength; i++) {
      double oneScore = Double.MIN_VALUE;
      boolean isSumZero = false;
      isSumZero = (indicesOfRowsWithSumZero.contains(i));
      boolean isGap = false;
      isGap =
          (seq.symbolAt(i + 1)
              == gap); // note: these +1 in indices are because seq  starts from one and not zero!
      if (isSumZero) {
        oneScore = 0.0;
      } else {
        oneScore = pdScores.get(nonGapPosition);
        nonGapPosition++;
      }
      result.add(oneScore);
    }
    return result;
  } /*mapPDScoresToAlignment*/
Exemplo n.º 2
0
  /** @throws BioException */
  private void init() throws BioException {
    similars = 0;
    identicals = 0;
    nGapsQ = 0;
    nGapsS = 0;
    for (int i = 0; i < Math.min(queryEnd - queryStart, subjectEnd - subjectStart); i++) {
      Symbol a = query.symbolAt(i + queryStart);
      Symbol b = subject.symbolAt(i + subjectStart);
      boolean gap = false;
      if (a.equals(b)) {
        identicals++;
      }

      // get score for this pair. if it is positive, they are similar...
      if (a.equals(query.getAlphabet().getGapSymbol())) {
        nGapsQ++;
        gap = true;
      }
      if (b.equals(subject.getAlphabet().getGapSymbol())) {
        nGapsS++;
        gap = true;
      }
      if (!gap && subMatrix != null && subMatrix.getValueAt(a, b) > 0) {
        similars++;
      }
    }
  }
Exemplo n.º 3
0
  public static String toFasta(Sequence... sequences) {
    StringBuilder sb = new StringBuilder();

    for (Sequence sequence : sequences)
      sb.append('>' + sequence.getName() + '\n' + sequence.seqString() + '\n');

    return sb.toString();
  }
 /** {@inheritDoc} */
 @Override
 protected Organism getOrganism(Sequence bioJavaSequence) throws ObjectStoreException {
   Annotation anno = bioJavaSequence.getAnnotation();
   // description_line=sp|Q9V8R9-2|41_DROME Isoform 2 of Protein 4.1 homolog OS=Drosophila
   // melanogaster GN=cora,
   String header = anno.getProperty("description_line").toString();
   final String regexp = "OS\\=\\w+\\s\\w+";
   Pattern p = Pattern.compile(regexp);
   Matcher m = p.matcher(header);
   if (m.find()) {
     header = m.group();
     String[] bits = header.split("=");
     if (bits.length != 2) {
       return null;
     }
     Integer taxonId = getTaxonId(bits[1]);
     if (taxonId == null) {
       return null;
     }
     Organism org = organisms.get(taxonId);
     if (org == null) {
       org = getDirectDataLoader().createObject(Organism.class);
       org.setTaxonId(taxonId);
       getDirectDataLoader().store(org);
       organisms.put(taxonId, org);
     }
     return org;
   }
   return null;
 }
Exemplo n.º 5
0
  public ArrayList<Double> mapViterbiPathToAlignment(
      ArrayList<String> viterbiPath, Sequence seq, ArrayList<Integer> indicesOfRowsWithSumZero) {
    ArrayList<Double> result = new ArrayList<Double>();

    int seqLength = seq.length();
    int nonGapPosition = 0;
    Symbol gap = seq.getAlphabet().getGapSymbol();
    for (int i = 0; i < seqLength; i++) {
      double oneScore = Double.MIN_VALUE;
      boolean isSumZero = false;
      isSumZero = (indicesOfRowsWithSumZero.contains(i));
      boolean isGap = false;
      isGap =
          (seq.symbolAt(i + 1)
              == gap); // note: these +1 in indices are because seq and viterbi path start from one
                       // and not zero!
      if (isSumZero) {
        oneScore = 0.0;
      } else {
        if (viterbiPath.get(nonGapPosition).equals("M")) {
          oneScore = 0.0;
        } else if (viterbiPath.get(nonGapPosition).equals("R")) {
          oneScore = 2.0;
        } else if (viterbiPath.get(nonGapPosition).equals("r")) {
          oneScore = 2.0;
        } else if (viterbiPath.get(nonGapPosition).equals("G")) {
          oneScore = 2.0;
        } else if (viterbiPath.get(nonGapPosition).equals("g")) {
          oneScore = 2.0;
        } else if (viterbiPath.get(nonGapPosition).equals("E")) {
          oneScore = 1.0;
        } else if (viterbiPath.get(nonGapPosition).equals("e")) {
          oneScore = 1.0;
        } else if (viterbiPath.get(nonGapPosition).equals("J")) {
          oneScore = 1.5;
        } else {
          System.err.println("Unknown charecter detected as a state name!");
        }

        nonGapPosition++;
      }
      result.add(oneScore);
    }
    return result;
  } /*mapViterbiPathToAlignment*/
Exemplo n.º 6
0
 /** @return */
 public float getPercentGapsQuery() {
   return nGapsQ / (float) query.length() * 100;
 }
Exemplo n.º 7
0
 /** @return */
 public float getPercentGapsTarget() {
   return nGapsS / (float) subject.length() * 100;
 }
Exemplo n.º 8
0
  /**
   * This method provides a BLAST-like formated alignment from the given <code>String</code>s, in
   * which the sequence coordinates and the information "Query" or "Sbjct", respectively is added to
   * each line. Each line contains <code>width</code> sequence characters including the gap symbols
   * plus the meta information. There is one white line between two pairs of sequences.
   *
   * @param width the number of symbols to be displayed per line.
   * @return formated String.
   * @throws BioException
   */
  public String formatOutput(int width) throws BioException {
    int i, j;
    /*
     * Highlights equal symbols within the alignment, String match/missmatch
     * representation
     */
    StringBuilder path = new StringBuilder();
    for (i = 0; i < Math.min(queryEnd - queryStart, subjectEnd - subjectStart) + 1; i++) {
      Symbol a = query.symbolAt(i + queryStart);
      Symbol b = subject.symbolAt(i + subjectStart);
      if (!a.equals(query.getAlphabet().getGapSymbol())
          && !b.equals(subject.getAlphabet().getGapSymbol())
          && ((subMatrix.getValueAt(a, b) >= 0) || a.equals(b))) {
        path.append('|');
      } else {
        path.append(' ');
      }
    }

    int maxLength = path.length();
    /*
     * Math.max(queryEnd - queryStart, subjectEnd - subjectStart) + 1;
     */
    Formatter output = new Formatter();
    output.format("%n Time (ms):  %s%n", time);
    output.format(" Length:     %d%n", maxLength);
    output.format("  Score:     %d%n", getScore());
    output.format("  Query:     %s, Length: %d%n", query.getName(), query.length() - nGapsQ);
    output.format("  Sbjct:     %s, Length: %d%n", subject.getName(), subject.length() - nGapsS);
    output.format(
        " Identities: %d/%d, i.e., %d %% (query) and %d %% (sbjct)%n",
        identicals,
        maxLength,
        Math.round(getPercentIdentityQuery()),
        Math.round(getPercentIdentitySubject()));
    output.format(
        " Similars:   %d/%d, i.e., %d %% (query) and %d %% (sbjct)%n",
        similars,
        maxLength,
        Math.round(getPercentSimilarityQuery()),
        Math.round(getPercentSimilaritySubject()));
    output.format(
        " No. gaps:   %d (%d %%) in query and %d (%d %%) in sbjct%n",
        nGapsQ, Math.round(getPercentGapsQuery()), nGapsS, Math.round(getPercentGapsTarget()));

    int queryLPos = queryStart, queryRPos, pathLPos = 0, pathRPos;
    int subjectLPos = subjectStart, subjectRPos;
    int ql = queryLPos - 1, qr = queryLPos - 1, qgaps;
    int sl = subjectLPos - 1, sr = subjectLPos - 1, sgaps;

    int widthLeft = String.valueOf(Math.max(queryStart, queryEnd)).length();
    int widthRight = String.valueOf(Math.max(queryEnd, subjectEnd)).length() + 1;

    // Take width of the meta information into account.
    width = Math.max(width - widthLeft - widthRight - 12, 2);

    for (i = 1; i <= Math.ceil((double) maxLength / width); i++) {

      // Query
      queryRPos =
          Math.min(
              queryStart + i * width - 1,
              Math.min(queryEnd, subjectEnd - subjectStart + queryStart));
      qgaps = 0;
      for (j = queryLPos; j <= queryRPos; j++) {
        if (!query.symbolAt(j).equals(query.getAlphabet().getGapSymbol())) {
          qr++;
        } else {
          qgaps++;
        }
      }
      if (qgaps <= queryRPos - queryLPos) {
        ql++;
      }
      output.format("%nQuery:   %" + widthLeft + "d ", ql);
      output.format("%s ", query.subStr(queryLPos, queryRPos));
      output.format("%-" + widthRight + "d%n", qr);
      queryLPos = queryRPos + 1;
      ql = qr;

      // Path
      pathRPos = Math.min(i * width, path.length());
      output.format(
          "%-" + (widthLeft + 10) + "c%s",
          Character.valueOf(' '),
          path.substring(pathLPos, pathRPos));
      pathLPos = pathRPos;

      // Sbjct
      subjectRPos =
          Math.min(
              subjectStart + i * width - 1,
              Math.min(queryEnd - queryStart + subjectStart, subjectEnd));
      sgaps = 0;
      for (j = subjectLPos; j <= subjectRPos; j++) {
        if (!subject.symbolAt(j).equals(subject.getAlphabet().getGapSymbol())) {
          sr++;
        } else {
          sgaps++;
        }
      }
      if (sgaps <= subjectRPos - subjectLPos) {
        sl++;
      }
      output.format("%nSbjct:   %" + widthLeft + "d ", sl);
      output.format("%s ", subject.subStr(subjectLPos, subjectRPos));
      output.format("%-" + widthRight + "d%n", sr);
      subjectLPos = subjectRPos + 1;
      sl = sr;
    }
    return output.toString();
  }
Exemplo n.º 9
0
 /** @return */
 public int getSubjectLength() {
   return subject.length();
 }
Exemplo n.º 10
0
 /** @return */
 public int getQueryLength() {
   return query.length();
 }
Exemplo n.º 11
0
 /** @return */
 public float getPercentIdentitySubject() {
   return identicals / (float) (subject.length() - nGapsS) * 100;
 }
Exemplo n.º 12
0
 /** @return */
 public float getPercentIdentityQuery() {
   return identicals / (float) (query.length() - nGapsQ) * 100;
 }
Exemplo n.º 13
0
 /**
  * @param query
  * @param subject
  * @param subMatrix
  * @throws IllegalArgumentException
  * @throws BioException
  */
 public AlignmentPair(Sequence query, Sequence subject, SubstitutionMatrix subMatrix)
     throws IllegalArgumentException, BioException {
   this(query, subject, 1, query.length(), 1, subject.length(), subMatrix);
 }
Exemplo n.º 14
0
  private void dnaCommand(HttpServletRequest req, DazzleResponse resp, DazzleDataSource dds)
      throws IOException, DataSourceException, ServletException, DazzleException {

    DazzleReferenceSource drs = (DazzleReferenceSource) dds;

    List segments = DazzleTools.getSegments(dds, req, resp);
    if (segments.size() == 0) {
      throw new DazzleException(
          DASStatus.STATUS_BAD_COMMAND_ARGUMENTS, "No segments specified for dna command");
    }

    // Fetch and validate the requests.

    Map segmentResults = new HashMap();
    for (Iterator i = segments.iterator(); i.hasNext(); ) {
      Segment seg = (Segment) i.next();

      try {
        Sequence seq = drs.getSequence(seg.getReference());
        if (seq.getAlphabet() != DNATools.getDNA()) {
          throw new DazzleException(
              DASStatus.STATUS_SERVER_ERROR,
              "Sequence " + seg.toString() + " is not in the DNA alphabet");
        }
        if (seg.isBounded()) {
          if (seg.getMin() < 1 || seg.getMax() > seq.length()) {
            throw new DazzleException(
                DASStatus.STATUS_BAD_COORDS,
                "Segment " + seg.toString() + " doesn't fit sequence of length " + seq.length());
          }
        }
        segmentResults.put(seg, seq);
      } catch (NoSuchElementException ex) {
        throw new DazzleException(DASStatus.STATUS_BAD_REFERENCE, ex);
      } catch (DataSourceException ex) {
        throw new DazzleException(DASStatus.STATUS_SERVER_ERROR, ex);
      }
    }

    //
    // Looks okay -- generate the response document
    //

    XMLWriter xw = resp.startDasXML("DASDNA", "dasdna.dtd");

    try {
      xw.openTag("DASDNA");
      for (Iterator i = segmentResults.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry me = (Map.Entry) i.next();
        Segment seg = (Segment) me.getKey();
        Sequence seq = (Sequence) me.getValue();

        xw.openTag("SEQUENCE");
        xw.attribute("id", seg.getReference());
        xw.attribute("version", drs.getLandmarkVersion(seg.getReference()));
        if (seg.isBounded()) {
          xw.attribute("start", "" + seg.getStart());
          xw.attribute("stop", "" + seg.getStop());
        } else {
          xw.attribute("start", "" + 1);
          xw.attribute("stop", "" + seq.length());
        }

        SymbolList syms = seq;
        if (seg.isBounded()) {
          syms = syms.subList(seg.getMin(), seg.getMax());
        }
        if (seg.isInverted()) {
          syms = DNATools.reverseComplement(syms);
        }

        xw.openTag("DNA");
        xw.attribute("length", "" + syms.length());

        for (int pos = 1; pos <= syms.length(); pos += 60) {
          int maxPos = Math.min(syms.length(), pos + 59);
          xw.println(syms.subStr(pos, maxPos));
        }

        xw.closeTag("DNA");
        xw.closeTag("SEQUENCE");
      }
      xw.closeTag("DASDNA");
      xw.close();
    } catch (Exception ex) {
      throw new DazzleException(ex, "Error writing DNA document");
    }
  }
Exemplo n.º 15
0
  public void main(String[] args) throws BioException, IOException {

    if (count.length != args.length) {
      System.err.println("The number of counts and output file arguments does not match!");
      System.exit(1);
    }

    OutputStream[] outputStreams;
    if ((args != null) && (args.length > 0)) {
      outputStreams = new OutputStream[args.length];
      for (int i = 0; i < args.length; i++) {
        outputStreams[i] = new BufferedOutputStream(new FileOutputStream(args[i]));
      }
    } else {
      outputStreams = new OutputStream[] {System.out};
    }

    RichSequenceIterator seqIterator =
        RichSequence.IOTools.readFastaDNA(new BufferedReader(new FileReader(seqFile)), null);

    List<Sequence> seqs = new ArrayList<Sequence>();
    while (seqIterator.hasNext()) {
      seqs.add(seqIterator.nextSequence());
    }

    List<List<Sequence>> chosenSeqs = new ArrayList<List<Sequence>>();
    if (!sampleWithReplacement) {
      for (int c : count) {
        List<Sequence> cseqs = new ArrayList<Sequence>();
        chosenSeqs.add(cseqs);
        while (c > 0) {
          int randSeqIndex = random.nextInt(seqs.size());
          cseqs.add(seqs.remove(randSeqIndex));
          c--;
        }
      }
    } else if (sampleWithReplacement || (length > 0)) {
      /*
       * if you want to sample from sequences with replacement
       * or if the wanted length is specified
       */

      for (int c : count) {
        int i = 0;
        List<Sequence> cseqs = new ArrayList<Sequence>();
        chosenSeqs.add(cseqs);

        while (c > 0) {
          Sequence randomSeq = seqs.get(random.nextInt(seqs.size()));
          if (length > 0) {
            int startPos = random.nextInt(1 + randomSeq.length() - length);
            cseqs.add(
                new SimpleSequence(
                    randomSeq.subList(startPos, startPos + length),
                    null,
                    randomSeq.getName() + "_" + i++,
                    Annotation.EMPTY_ANNOTATION));
          } else {
            cseqs.add(randomSeq);
          }
          c--;
        }
      }
    }

    int i = 0;
    for (List<Sequence> seqList : chosenSeqs) {
      for (Sequence seq : seqList) {
        Sequence s;
        if (uniqueNames) {
          s =
              new SimpleSequence(
                  seq.subList(1, seq.length()),
                  null,
                  seq.getName() + "_" + i,
                  Annotation.EMPTY_ANNOTATION);
        } else {
          s = seq;
        }
        RichSequence.IOTools.writeFasta(outputStreams[i], s, null);
        outputStreams[i].flush();
      }
      i++;
    }
    seqs = null;
  }
Exemplo n.º 16
0
 /**
  * Creates the Map required by the super class.
  *
  * @param s1
  * @param s2
  * @return
  */
 private static Map<String, SymbolList> createHashMap(Sequence s1, Sequence s2) {
   Map<String, SymbolList> m = new HashMap<String, SymbolList>();
   m.put(s1.getName(), s1);
   m.put(s2.getName(), s2);
   return m;
 }
Exemplo n.º 17
0
    public LinkedHashMap<SimpleAlphabet, SimpleSymbolList> getAlphabetAndSimpleSymbolList(
        Matrix2D m, Sequence sequence) throws IllegalSymbolException {
      LinkedHashMap<SimpleAlphabet, SimpleSymbolList> alphabetAndSymbolList =
          new LinkedHashMap<SimpleAlphabet, SimpleSymbolList>();

      SimpleAlphabet alphabet = new SimpleAlphabet();
      List<AtomicSymbol> listOfSymbols = new ArrayList<AtomicSymbol>();
      alphabet.setName("ObservedSequenceAlphabet");
      int numberofRows = m.rows();
      int seqLength = sequence.length();
      if (numberofRows != seqLength) {
        System.err.print(
            "It was assumed your sequence has a length equal to  the number of rows of the matrix, but found a case that is not true!");
      }

      Symbol gap = sequence.getAlphabet().getGapSymbol();

      for (int i = 0; i < numberofRows; i++) {
        List<Symbol> oneListOfSymbol = new ArrayList<Symbol>(3);

        // red is match, green is flanking and blue is background
        double redValue = m.get(i, 0);
        double greenValue = m.get(i, 1);
        double blueValue = m.get(i, 2);
        double onesum = redValue + greenValue + blueValue;
        boolean isGap = false;
        isGap = (sequence.symbolAt(i + 1) == gap);
        if (onesum
            == 0) { // sum of this value is supposed to be one, but for some rows it sums up to
                    // zero, this is to ignore those up to time we found out why these naouthy rows
                    // sums up to zero!
          // continue;
          // note these three lines is only a dummy solution for positions where red, green and blue
          // are summed up to zero!
          redValue = 0.3333;
          greenValue = 0.3333;
          blueValue = 1 - (redValue + greenValue);
        }

        // make one triplet symbol from three symbols
        Symbol redSymbol = AlphabetManager.createSymbol(Double.toString(redValue));
        Symbol greenSymol = AlphabetManager.createSymbol(Double.toString(greenValue));
        Symbol blueSymbol = AlphabetManager.createSymbol(Double.toString(blueValue));
        oneListOfSymbol.add(redSymbol);
        oneListOfSymbol.add(greenSymol);
        oneListOfSymbol.add(blueSymbol);

        // now create symbol and add it to alphabet
        AtomicSymbol oneSym =
            (AtomicSymbol)
                AlphabetManager.createSymbol(
                    Annotation.EMPTY_ANNOTATION, oneListOfSymbol, alphabet);
        alphabet.addSymbol(oneSym);
        listOfSymbols.add(oneSym);
      }

      SimpleSymbolList ssl = new SimpleSymbolList(alphabet, listOfSymbols);

      alphabetAndSymbolList.put(alphabet, ssl);
      return alphabetAndSymbolList;
    } /*getAlphabetAndSimpleSymbolList*/
Exemplo n.º 18
0
 /** @return */
 public float getPercentSimilarityQuery() {
   return similars / (float) query.length() * 100;
 }
Exemplo n.º 19
0
 /** @return */
 public float getPercentSimilaritySubject() {
   return similars / (float) subject.length() * 100;
 }
  public void doMain(String[] args) throws Exception {
    CmdLineParser parser = new CmdLineParser(this);
    // if you have a wider console, you could increase the value;
    // here 80 is also the default
    parser.setUsageWidth(80);
    try {
      parser.parseArgument(args);

      if ((arguments.size() != 0)) {
        System.err.println(C_USAGE);
        parser.printUsage(System.err);
        System.exit(1);
      }

      if ((outPrefix == null)) {
        System.err.println("Must specify outPrefix and at least one mnasePrefix");
        parser.printUsage(System.err);
        System.err.println(C_USAGE);
        System.exit(1);
      }

    } catch (CmdLineException e) {
      System.err.println(e.getMessage());
      System.err.println(C_USAGE);
      // print the list of available options
      parser.printUsage(System.err);
      System.err.println();
      return;
    }

    Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.SEVERE);

    // Setup output files and print domain finders
    ListUtils.setDelim("-");
    String mnasePrefixStr = mnasePrefix;
    String normString =
        (this.normalizationWindow == 0)
            ? ""
            : String.format(".normalized%dbpWind", this.normalizationWindow);
    String motifString =
        (this.motif == null) ? "" : String.format(".motif%s", this.motif.toUpperCase());
    String withinStr =
        (this.withinFeat == null) ? "" : String.format(".withinFeat-%s", this.withinFeat);

    if (this.filterByMotif != null)
      motifString +=
          String.format(
              ".filteredByMotif%s%s%s",
              this.filterByMotif,
              (this.filterByMotifInvert) ? "-inverted" : "",
              (this.filterByMotifOffset != 0)
                  ? String.format("-offset%d", this.filterByMotifOffset)
                  : "");
    String fwOnlyStr = (this.fwStrandOnly) ? ".fwStrandOnly" : "";

    String name =
        String.format(
            "nucleosomeReads.%s%s%s.minReads%.2f%s.nuc%d.assoc%d%s.%s.%s",
            this.outPrefix,
            motifString,
            withinStr,
            this.minReadsPerBp,
            fwOnlyStr,
            this.footprintSize,
            this.assocSize,
            normString,
            mnasePrefixStr,
            methPrefix);
    String outFn =
        String.format(
            "%s%s%s.minReads%.2f%s.nuc%d.assoc%d%s.%s.%s.csv",
            this.outPrefix,
            motifString,
            withinStr,
            this.minReadsPerBp,
            fwOnlyStr,
            this.footprintSize,
            this.assocSize,
            normString,
            mnasePrefixStr,
            methPrefix);
    String outFnWig = outFn.replace(".csv", ".wig");
    //		PrintWriter pw = new PrintWriter(new FileOutputStream(outFn));
    PrintWriter pwWig = new PrintWriter(new FileOutputStream(outFnWig));
    // pw.printf("%s,%s\n","Meth", "ReadsPerBp");
    pwWig.printf("track type=wiggle_0 name=\"%s\" description=\"%s\"\n", name, name);

    MethylDbQuerier params = new MethylDbQuerier();
    params.setMinCTreads(this.minCTreads);
    params.setUseNonconversionFilter(!this.noNonconvFilter);
    params.setMaxOppstrandAfrac(this.maxOppStrandAfrac);
    params.setMaxNextNonGfrac(this.maxNextNonGfrac);
    if (this.withinFeat != null) params.addFeatFilter(this.withinFeat, this.featFlank);

    // We use the control table simply to limit the valid Cpg.  This is a result of
    // our incorrect loading of Lister 2009 tables, which contains many Cpgs which
    // incorrectly yield a meth level of 0 at CpGs not covered in their sequencing data.
    // This was an artifact of the way they published their data, where they published
    // a list of methy-C positions without positions containing 0 mC reads, so we had
    // to add fake positions for all Cs in the genome, and we didn't know which ones
    // actually had coverage in their data.
    List<String> methTables = Arrays.asList(methPrefix, controlMethPrefix);
    //	List<String> mnaseTables = Arrays.asList(mnasePrefix);

    int nSeries = 1;
    boolean useChipseq[] = new boolean[nSeries];
    for (int i = 0; i < nSeries; i++) {
      // Wow talk about a special case.
      useChipseq[i] = !mnasePrefix.contains("mnase") && !mnasePrefix.contains("Schones");
    }

    double[] methCounts = new double[METHCOUNTER_LEN];
    double[] methTotals = new double[METHCOUNTER_LEN];

    int chromNum = 1;
    for (String chr :
        Arrays.asList(
            "chr22")) // MethylDbUtils.CHROMS_MINUS_TWELVE) //MethylDbUtils.SMALL_CHROMS) //
    // Arrays.asList("chr22")) //,"chr18","chr19","chr20")) //
    {
      System.err.printf("On chrom %d (%s)\n", chromNum++, chr);
      String s = String.format("variableStep\tchrom=%s\n", chr);
      pwWig.append(s);
      int chrInt = (new ChromFeatures()).chrom_from_public_str(chr);

      // Iterator uses DB connection and can use a ton of memory because
      // it loads all rows at once.  This stuff should really be added to iterator
      // class, but until it is , just iterate here over the chromosome
      int onCpg = 0;

      // Get the full array

      try {
        {
          ChromScoresFast counts[] = new ChromScoresFast[2];
          ChromScoresFast meths[] = new ChromScoresFast[2];

          int offs = 0; // 20000000; //0;
          int offe = 30000000; // 0;
          System.err.printf("offs=%d, offe=%d\n", offs, offe);

          // The mnase counts are the same for both cases
          counts =
              MethylDbUtils.chromScoresReadCounts(
                  params, chr, this.mnasePrefix, this.refGenome, offs, offe);

          // We can filter counts by a particular motif
          if (this.filterByMotif != null) {
            ChromScoresFast filterByMotifCounts[] = new ChromScoresFast[2];
            filterByMotifCounts =
                MethylDbUtils.chromScoresMotifCounts(
                    chr, this.refGenome, this.filterByMotif, offs, offe, false);

            System.err.printf(
                "%s, filtering + strand for motif %s. Pre-filter count: %.0f\n",
                chr, this.filterByMotif, counts[0].getScoresTotal(chr));
            counts[0].mask(filterByMotifCounts[0], this.filterByMotifInvert);
            //						counts[0].mask(filterByMotifCounts[1],this.filterByMotifInvert);
            System.err.printf(
                "%s, filtering + strand for motif %s. Post-filter count: %.0f\n",
                chr, this.filterByMotif, counts[0].getScoresTotal(chr));
            System.err.printf(
                "%s, filtering - strand for motif %s. Pre-filter count: %.0f\n",
                chr, this.filterByMotif, counts[1].getScoresTotal(chr));
            counts[1].mask(filterByMotifCounts[1], this.filterByMotifInvert);
            //						counts[1].mask(filterByMotifCounts[0],this.filterByMotifInvert);
            System.err.printf(
                "%s, filtering - strand for motif %s. Post-filter count: %.0f\n",
                chr, this.filterByMotif, counts[1].getScoresTotal(chr));
          }

          // The meth differ if it's a motif
          if (this.autoMnase || this.autoMnaseFw || this.autoMnaseRev) {
            meths = counts;
          } else if (this.motif == null) {
            meths =
                MethylDbUtils.chromScoresMethLevels(
                    params, chr, this.methPrefix, this.refGenome, offs, offe);
          } else {

            char[] seqArr = null;
            {
              Sequence seq = GoldAssembly.chromSeq(this.refGenome, chr);
              String seqStr = seq.seqString();
              if (offe > 0) {
                seqStr = seqStr.substring(offs, offe);
              }
              seqArr = seqStr.toUpperCase().toCharArray();
            }

            System.err.println("Seq length=" + seqArr.length);

            ChromScoresMotifPositions all = new ChromScoresMotifPositions(this.refGenome);
            System.err.printf(
                "About to populate both strands for %s for motif %s\n", chr, this.motif);
            StrandedFeature.Strand motifStrand =
                (this.fwStrandOnly) ? StrandedFeature.POSITIVE : StrandedFeature.UNKNOWN;
            all.populate(chr, this.motif, seqArr, offs, motifStrand);

            meths[0] = all;
            meths[1] = all;

            //						System.err.printf("Fw score at %d = %d\n",24009253,counts[0].getScore(chr,
            // 24009253));
            //						WigOptions wo = new WigOptions();
            //						wo.f_step = 1;
            //						//counts[0] = counts[0].smooth(50, 30);
            //						counts[0].wigOutput("testFw.wig", wo);
            //						counts[1].wigOutput("testRev.wig", wo);

          }

          int minPos = counts[0].chromMinPos(chr);
          System.err.println("Getting min pos: " + minPos);
          int maxPos = counts[0].chromMaxPos(chr);
          System.err.println("Getting max pos: " + maxPos);

          for (int pos = minPos; pos < maxPos; pos += 1) // this.step)
          {

            boolean fwRead = (counts[0].getScore(chr, pos).intValue() >= 1);
            boolean revRead = (counts[1].getScore(chr, pos).intValue() >= 1);
            if (this.fwStrandOnly) revRead = false; // fwRead=false;

            boolean enoughReads = true;
            MnaseOutput mnaseReads = new MnaseOutput();
            mnaseReads.rawCounts = (fwRead) ? 1 : 0;
            if (this.minReadsPerBp > 0.0) {
              if (fwRead) {
                int nucCenter = pos + ((this.PERIODICITY - this.assocSize) / 2);
                mnaseReads =
                    this.countMnaseReads(chr, nucCenter, counts[0], counts[1], useChipseq[0]);
              } else if (revRead) {
                int nucCenter = pos - ((this.PERIODICITY - this.assocSize) / 2);
                mnaseReads =
                    this.countMnaseReads(chr, nucCenter, counts[0], counts[1], useChipseq[0]);
              }
              enoughReads = (mnaseReads.val >= this.minReadsPerBp);
            }

            // if (mnaseReads.rawCounts>2) System.err.printf("Raw counts=%d\n",
            // (int)mnaseReads.rawCounts);

            // enoughReads = true;

            if (enoughReads && fwRead)
              incrementMethCounts(chr, pos, false, methCounts, methTotals, meths, maxPos);
            if (enoughReads && revRead)
              incrementMethCounts(chr, pos, true, methCounts, methTotals, meths, maxPos);

            //
            if ((pos % 1E6) == 0)
              System.err.printf("On pos #%d, meth=%d\n", pos, (int) mnaseReads.rawCounts);

            if (enoughReads) {
              //							pwWig.printf("%d\t%.2f\n",pos, mnaseReads.val);
              ////							pwWig.printf("%d\t%.2f\t%.2f\t%s\n",pos,mnaseReads.rawCounts,
              // mnaseReads.val, fwRead?"+":"-");
              //
              ////							pw.printf("%d,%d", chrInt, pos);
              //////							pw.printf(",%.3f", mnaseReads.val);
              ////							pw.printf(",%d", (int)mnaseReads.rawCounts);
              //////							pw.printf(",%.3f", mnaseReads.normWindCount);
              ////							pw.println();
            }
          }

          // Try to get rid of objects
          counts[0] = null;
          counts[1] = null;
          meths[0] = null;
          meths[1] = null;
          System.gc();
        }

      } catch (Exception e) {
        System.err.printf("%s\nCouldn't do region %s\n", e.toString(), chr);
        e.printStackTrace();
      }
    }

    //		pw.close();
    pwWig.close();

    outFn = outFn.replace(".csv", ".methAlign.csv");
    PrintWriter pw = new PrintWriter(new FileOutputStream(outFn));
    ListUtils.setDelim(",");
    pw.println(ListUtils.excelLine(methCounts));
    pw.println(ListUtils.excelLine(methTotals));
    pw.close();
  } // Main