BioJava:CookBook:Core:SequenceCompare
Caveat: This piece of code assumes that the sequences that are to be compared are already pre-aligned.
```java package org.biojava3.cookbook;
import java.util.ArrayList; import org.biojava.nbio.core.sequence.DNASequence; import org.biojava.nbio.core.sequence.compound.AmbiguityDNACompoundSet;
public class SequenceComparer {
private DNASequence seq1;
private DNASequence seq2;
private ArrayList
public SequenceComparer(DNASequence seq1, DNASequence seq2) {
if (seq1.getLength() >= seq2.getLength()) {
this.seq1 = seq1;
this.seq2 = seq2;
} else {
this.seq1 = seq2;
this.seq2 = seq1;
}
}
public int getDifferenceCount() {
for (int i = 0; i < seq1.getLength(); i++) {
if (i > seq2.getLength()-1) {
mismatchIndices.add(i);
} else {
if (!seq1.getCompoundAt(i+1).equals(seq2.getCompoundAt(i+1))) {
mismatchIndices.add(i);
}
}
}
matchingDone = true;
return mismatchIndices.size();
}
public DNASequence getConsensusSequence() {
if (!matchingDone) {
getDifferenceCount();
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < seq1.getLength(); i++) {
if (mismatchIndices.contains(i)) {
try {
sb.append(getConsensus(seq1.getCompoundAt(i+1).getBase().charAt(0), seq2.getCompoundAt(i+1).getBase().charAt(0)));
} catch (IndexOutOfBoundsException ex) {
sb.append('N');
}
} else {
sb.append(seq1.getCompoundAt(i+1).getBase().charAt(0));
}
}
DNASequence result = new DNASequence(sb.toString(), AmbiguityDNACompoundSet.getDNACompoundSet());
result.setOriginalHeader(seq1.getOriginalHeader() + "|" + seq2.getOriginalHeader());
return result;
}
private char getConsensus(char a, char b) {
return lut.valueOf("" + a + b).getConsensusChar();
}
public static void main(String[] args) {
SequenceComparer sc = new SequenceComparer(new DNASequence("ACGT"), new DNASequence("ACC", AmbiguityDNACompoundSet.getDNACompoundSet()));
System.out.println(sc.getDifferenceCount());
System.out.println(sc.getConsensusSequence());
sc = new SequenceComparer(new DNASequence("ACGT"), new DNASequence("ACCN", AmbiguityDNACompoundSet.getDNACompoundSet()));
System.out.println(sc.getDifferenceCount());
System.out.println(sc.getConsensusSequence());
}
} ```