<commit_before>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */


package org.biojava.bio.alignment;

import java.util.*;

import org.biojava.bio.seq.*;

/**
 * Annotates a database with hits to a weight-matrix.
 *
 * @author <NAME>
 */
public class WeightMatrixAnnotator implements Annotator {
  private WeightMatrix matrix;
  private double prior;
  private double threshold;

  public SequenceDB annotate(SequenceDB sdb)
        throws IllegalResidueException, java.io.IOException {
    HashSequenceDB hitDB = new HashSequenceDB(null);

    for(Iterator i = sdb.ids().iterator(); i.hasNext(); ) {
      try {
        String id = (String) i.next();
        Sequence seq = sdb.getSequence(id);
        if(annotate(seq)) {
          hitDB.addSequence(id, seq);
        }
      } catch (SeqException se){
      }
    }

    return hitDB;
  }

  public boolean annotate(Sequence seq)
         throws IllegalResidueException {
    if(!(seq instanceof MutableFeatureHolder)) {
      return false;
    }
    
    int cols = matrix.columns();
    for(int offset = 1;
        offset <= seq.length() - cols + 1;
        offset++) {
      double score = DP.score(matrix, seq.subList(offset, offset+cols-1));
      double q = Math.exp(score) * prior;
      double pmd = q/(1.0+q);
      if(pmd >= threshold) {
        Feature hit =
         seq.createFeature((MutableFeatureHolder) seq,
                           new RangeLocation(offset, offset+cols-1),
                           "WeightMatrix", "WeightMatrix",
                           null);
        hit.getAnnotation().setProperty("score", new Double(score));
        hit.getAnnotation().setProperty("weightMatrix", matrix);
        hit.getAnnotation().setProperty("p-value", new Double(pmd));
        return true;
      }
    }
    return false;
  }

  public WeightMatrixAnnotator(WeightMatrix wm, double prior, double threshold) {
    this.matrix = wm;
    this.prior = prior;
    this.threshold = threshold;
  }
}
<commit_msg>Should work with the new features I hope.<commit_after>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */


package org.biojava.bio.alignment;

import java.util.*;

import org.biojava.bio.seq.*;

/**
 * Annotates a database with hits to a weight-matrix.
 *
 * @author <NAME>
 */
public class WeightMatrixAnnotator extends AbstractAnnotator {
  private WeightMatrix matrix;
  private double prior;
  private double threshold;

  public boolean annotate(Sequence seq)
         throws IllegalResidueException {
    if(!(seq instanceof MutableFeatureHolder)) {
      return false;
    }
    
    int cols = matrix.columns();
    for(int offset = 1;
        offset <= seq.length() - cols + 1;
        offset++) {
      double score = DP.score(matrix, seq.subList(offset, offset+cols-1));
      double q = Math.exp(score) * prior;
      double pmd = q/(1.0+q);
      if(pmd >= threshold) {
        Feature hit =
         seq.createFeature((MutableFeatureHolder) seq,
                           new RangeLocation(offset, offset+cols-1),
                           "WeightMatrix", "WeightMatrix",
                           null);
        hit.getAnnotation().setProperty("score", new Double(score));
        hit.getAnnotation().setProperty("weightMatrix", matrix);
        hit.getAnnotation().setProperty("p-value", new Double(pmd));
        return true;
      }
    }
    return false;
  }

  public WeightMatrixAnnotator(WeightMatrix wm, double prior, double threshold) {
    this.matrix = wm;
    this.prior = prior;
    this.threshold = threshold;
  }
}

{'fraction_non_alphanumeric': 0.10678772157371379, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 2.4607329842931938, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3952557', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1302, 'n_words': 400}