/**
   * Aligns two sequences by Needleman-Wunsch (global)
   *
   * @param s1 sequene #1 ({@link Read})
   * @param s2 sequene #2 ({@link Read})
   * @param matrix scoring matrix ({@link Matrix})
   * @param o open gap penalty
   * @param e extend gap penalty
   * @return alignment object contains the two aligned sequences, the alignment score and alignment
   *     statistics
   * @see Read
   * @see Matrix
   */
  public static Alignment align(Sequence s1, Sequence s2, Matrix matrix, float o, float e) {

    float[][] scores = matrix.getScores();

    Sequence _s1;
    Sequence _s2;

    /*
     * Commented out this stupid sequence switching. Sequence 1 should STAY sequence 1!
     */
    //        if (s1.length() < s2.length()) {
    //            _s1 = s2;
    //            _s2 = s1;
    //        } else {
    _s1 = s1;
    _s2 = s2;
    //        }

    int m = _s1.length() + 1;
    int n = _s2.length() + 1;

    byte[] pointers = new byte[m * n];

    short[] lengths = new short[m * n];

    // Initializes the element (0,0) of the traceback matrix to STOP.
    pointers[0] = Directions.STOP;

    // Initializes the boundaries of the traceback matrix.
    for (int i = 1, k = n; i < m; i++, k += n) {
      pointers[k] = Directions.UP;
      lengths[k] = (short) i;
    }
    for (int j = 1; j < n; j++) {
      pointers[j] = Directions.LEFT;
      lengths[j] = (short) j;
    }

    Cell cell = construct(_s1, _s2, scores, o, e, pointers, lengths);

    Alignment alignment = traceback(_s1, _s2, matrix, pointers, cell, lengths);

    alignment.setMatrix(matrix);
    alignment.setOpen(o);
    alignment.setExtend(e);
    alignment.setName1(_s1.getId());
    alignment.setName2(_s2.getId());
    alignment.setOriginalSequence1(_s1);
    alignment.setOriginalSequence2(_s2);

    return alignment;
  }
Exemplo n.º 2
0
 /**
  * Get the list of ids of sequences in this sequence database
  *
  * @return the list of sequence ids.
  */
 public Set<Integer> getSequenceIDs() {
   Set<Integer> set = new HashSet<Integer>();
   for (Sequence sequence : getSequences()) {
     set.add(sequence.getId());
   }
   return set;
 }
Exemplo n.º 3
0
  /**
   * Actually gets the next id Gets the object of type ID from the database with the specific name.
   * Then increment the id value and returns. If object does not exist, creates t.
   *
   * @param idName
   * @return
   */
  @SuppressWarnings("unchecked")
  private long getNextId(String className) {
    ODB odb = getOdb();

    Objects objects =
        odb.getObjects(new CriteriaQuery(Sequence.class, Where.equal("className", className)));

    if (objects.isEmpty()) {
      Sequence sequence = new Sequence();
      sequence.setClassName(className);
      sequence.setId(1L);

      odb.store(sequence);

      return 1L;
    }

    Sequence sequence = (Sequence) objects.getFirst();

    sequence.increment();

    odb.store(sequence);

    return sequence.getId();
  }
Exemplo n.º 4
0
 /**
  * Get a string representation of this sequence database
  *
  * @return the string representation
  */
 @Override
 public String toString() {
   StringBuilder r = new StringBuilder();
   for (Sequence sequence : sequences) {
     r.append(sequence.getId());
     r.append(":  ");
     r.append(sequence.toString());
     r.append('\n');
   }
   return r.toString();
 }
Exemplo n.º 5
0
  /**
   * It adds a sequence from an array of string that we have to interpret
   *
   * @param integers
   * @param sequenceID
   */
  public void addSequence(String[] integers, int sequenceID) {
    long timestamp = -1;
    Sequence sequence = new Sequence(sequences.size());
    sequence.setID(sequenceID);
    Itemset itemset = new Itemset();
    int inicio = 0;
    Map<Item, Boolean> counted = new HashMap<Item, Boolean>();

    for (int i = inicio; i < integers.length; i++) {
      if (integers[i].codePointAt(0) == '<') { // Timestamp
        String value = integers[i].substring(1, integers[i].length() - 1);
        timestamp = Long.parseLong(value);
        itemset.setTimestamp(timestamp);
      } else if (integers[i].equals("-1")) { // end of an itemset
        long time = itemset.getTimestamp() + 1;
        sequence.addItemset(itemset);
        itemset = new Itemset();
        itemset.setTimestamp(time);
      } else if (integers[i].equals("-2")) { // end of a sequence
        sequences.add(sequence);
      } else {
        // extract the value for an item
        Item item = itemFactory.getItem(Integer.parseInt(integers[i]));
        if (counted.get(item) == null) {
          counted.put(item, Boolean.TRUE);
          BitSet appearances = frequentItems.get(item);
          if (appearances == null) {
            appearances = new BitSet();
            frequentItems.put(item, appearances);
          }
          appearances.set(sequence.getId());
        }
        itemset.addItem(item);
      }
    }
  }