Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.biojava.nbio.core.sequence.location.template.Location;
import org.biojava.nbio.core.sequence.location.template.Point;
import org.biojava.nbio.core.sequence.template.*;
import org.biojava.nbio.core.util.Equals;

import java.io.Serializable;
import java.util.ArrayList;
Expand Down Expand Up @@ -59,7 +60,10 @@ public class SimpleAlignedSequence<S extends Sequence<C>, C extends Compound> im

// cached (lazily initialized)
private int numGaps = -1;
private int[] alignmentFromSequence, sequenceFromAlignment;
private int numGapPositions = -1;

private int[] alignmentFromSequence;
private int[] sequenceFromAlignment;

/**
* Creates an {@link AlignedSequence} for the given {@link Sequence} in a global alignment.
Expand Down Expand Up @@ -131,25 +135,35 @@ public void clearCache() {
sequenceFromAlignment = null;
}

@Override
public int getAlignmentIndexAt(int sequenceIndex) {
if (alignmentFromSequence == null) {
alignmentFromSequence = new int[original.getLength()];
int s = 1, a = 1;
for (int i = 0; i < numBefore; i++, s++) {
alignmentFromSequence[s - 1] = a;
}
for (; s <= alignmentFromSequence.length && a <= length; s++, a++) {
while (a <= length && isGap(a)) {
a++;
}
alignmentFromSequence[s - 1] = a;
}
a--;
for (int i = 0; i < numAfter; i++, s++) {
alignmentFromSequence[s - 1] = a;
private void setAlignmentFromSequence() {
alignmentFromSequence = new int[original.getLength()];
int s = 1, a = 1;
for (int i = 0; i < numBefore; i++, s++) {
alignmentFromSequence[s - 1] = a;
}
for (; s <= alignmentFromSequence.length && a <= length; s++, a++) {
while (a <= length && isGap(a)) {
a++;
}
alignmentFromSequence[s - 1] = a;
}
a--;
for (int i = 0; i < numAfter; i++, s++) {
alignmentFromSequence[s - 1] = a;
}
}

@Override
public int[] getAlignmentFromSequence() {
if (alignmentFromSequence == null)
setAlignmentFromSequence();
return alignmentFromSequence;
}

@Override
public int getAlignmentIndexAt(int sequenceIndex) {
if (alignmentFromSequence == null)
setAlignmentFromSequence();
return alignmentFromSequence[sequenceIndex - 1];
}

Expand All @@ -167,6 +181,7 @@ public Location getLocationInAlignment() {
public int getNumGaps() {
if (numGaps == -1) {
numGaps = 0;
numGapPositions = 0;
C cGap = getCompoundSet().getCompoundForString(gap);
boolean inGap = false;
for (C compound : getAsList()) {
Expand All @@ -175,6 +190,7 @@ public int getNumGaps() {
numGaps++;
inGap = true;
}
numGapPositions++;
} else {
inGap = false;
}
Expand All @@ -194,21 +210,31 @@ public int getOverlapCount() {
return 1;
}

@Override
public int getSequenceIndexAt(int alignmentIndex) {
if (sequenceFromAlignment == null) {
sequenceFromAlignment = new int[length];
int a = 1, s = numBefore + 1;
for (int i = 0; i < getStart().getPosition(); i++, a++) {
sequenceFromAlignment[a - 1] = s;
}
for (; a <= length; a++) {
if (!isGap(a)) {
s++;
}
sequenceFromAlignment[a - 1] = s;
private void setSequenceFromAlignment() {
sequenceFromAlignment = new int[length];
int a = 1, s = numBefore + 1;
for (int i = 0; i < getStart().getPosition(); i++, a++) {
sequenceFromAlignment[a - 1] = s;
}
for (; a <= length; a++) {
if (!isGap(a)) {
s++;
}
sequenceFromAlignment[a - 1] = s;
}
}

@Override
public int[] getSequenceFromAlignment() {
if (sequenceFromAlignment == null)
setSequenceFromAlignment();
return sequenceFromAlignment;
}

@Override
public int getSequenceIndexAt(int alignmentIndex) {
if (sequenceFromAlignment == null)
setSequenceFromAlignment();
return sequenceFromAlignment[alignmentIndex - 1];
}

Expand Down Expand Up @@ -266,6 +292,30 @@ public List<C> getAsList() {
return compounds;
}

@Override
public boolean equals(Object o){

if(! Equals.classEqual(this, o)) {
return false;
}

Sequence<C> other = (Sequence<C>)o;
if ( original.getAsList().size() != other.getAsList().size())
return false;

for ( int i = 0 ; i< original.getAsList().size() ; i++){
if ( ! original.getAsList().get(i).equalsIgnoreCase(other.getAsList().get(i)))
return false;
}
return true;
}

@Override
public int hashCode(){
String s = getSequenceAsString();
return s.hashCode();
}

@Override
public C getCompoundAt(int alignmentIndex) {
return alignmentIndex >= 1 && alignmentIndex <= length && isGap(alignmentIndex) ?
Expand Down Expand Up @@ -382,4 +432,18 @@ private void setLocation(List<Step> steps) {
public SequenceView<C> getInverse() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public int getNumGapPositions() {
if (numGapPositions == -1)
getNumGaps();
return numGapPositions;
}

@Override
public double getCoverage() {

double coverage = getLength() - getNumGapPositions();
return coverage / getOriginalSequence().getLength();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ enum Step { COMPOUND, GAP }
*/
void clearCache();

/** Returns the alignment.
*
* @return the alignment
*/
int[] getAlignmentFromSequence();

/** Returns the sequence positions at each alignment index
*
* @return array of the sequence positions
*/
int[] getSequenceFromAlignment();

/**
* Returns the column index within an alignment corresponding to the given index in the original {@link Sequence}.
* Both indices are 1-indexed and inclusive.
Expand Down Expand Up @@ -130,4 +142,20 @@ enum Step { COMPOUND, GAP }
*/
boolean isGap(int alignmentIndex);

/**
* Returns number of gap positions (gap openings and extensions) in the sequence. This could be determined from the {@link Location}
* information or from gap {@link Compound}s, which may not necessarily result in the same number.
*
* @return number of gap positions in the sequence
*/
int getNumGapPositions();

/**
* Returns the coverage, as a fraction between 0 and 1, of this {@link AlignedSequence} with respect to the original sequence.
* This is equivalent to ({@link #getLength()} - {@link #getNumGapPositions()}) / getOriginalSequence().getLength().
*
* @return coverage of the original sequence by the aligned sequence
*/
double getCoverage();

}