Skip to content

Commit 7901603

Browse files
committed
Create a Test for Wrapper functions
1 parent 2a8487d commit 7901603

File tree

4 files changed

+111
-65
lines changed

4 files changed

+111
-65
lines changed

biojava-phylo/src/main/java/demo/DemoDistanceTree.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ public static void main(String[] args) throws Exception {
3737
InputStream inStream = TreeConstructor.class
3838
.getResourceAsStream("/PF00104_small.fasta");
3939

40-
FastaReader<ProteinSequence, AminoAcidCompound> fastaReader = new FastaReader<ProteinSequence, AminoAcidCompound>(
40+
FastaReader<ProteinSequence, AminoAcidCompound> fastaReader =
41+
new FastaReader<ProteinSequence, AminoAcidCompound>(
4142
inStream,
4243
new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(),
4344
new ProteinSequenceCreator(AminoAcidCompoundSet
4445
.getAminoAcidCompoundSet()));
4546

46-
LinkedHashMap<String, ProteinSequence> proteinSequences = fastaReader
47-
.process();
47+
LinkedHashMap<String, ProteinSequence> proteinSequences =
48+
fastaReader.process();
4849

4950
inStream.close();
5051

51-
MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound> msa = new MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound>();
52+
MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound> msa =
53+
new MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound>();
5254

5355
for (ProteinSequence proteinSequence : proteinSequences.values()) {
5456
msa.addAlignedSequence(proteinSequence);
@@ -57,8 +59,10 @@ public static void main(String[] args) throws Exception {
5759
long readT = System.currentTimeMillis();
5860

5961
// 1. Calculate the evolutionary distance matrix (can take long)
60-
SubstitutionMatrix<AminoAcidCompound> M = SubstitutionMatrixHelper.getBlosum62();
61-
DistanceMatrix DM = DistanceMatrixCalculator.fractionalDissimilarity(msa);
62+
SubstitutionMatrix<AminoAcidCompound> M = SubstitutionMatrixHelper
63+
.getBlosum62();
64+
DistanceMatrix DM = DistanceMatrixCalculator
65+
.dissimilarityScore(msa, M);
6266

6367
// 2. Construct a distance tree using the NJ algorithm
6468
Phylogeny phylo = TreeConstructor.distanceTree(
@@ -71,7 +75,7 @@ public static void main(String[] args) throws Exception {
7175

7276
// 3. Evaluate the goodness of fit of the tree
7377
double cv = DistanceTreeEvaluator.evaluate(phylo, DM);
74-
System.out.println("CV of the tree: " + (int) (cv*100) + " %");
78+
System.out.println("CV of the tree: " + (int) (cv * 100) + " %");
7579

7680
}
7781
}

biojava-phylo/src/test/java/org/biojava/nbio/phylo/AppTest.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.biojava.nbio.phylo;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.util.LinkedHashMap;
7+
8+
import org.biojava.nbio.core.sequence.MultipleSequenceAlignment;
9+
import org.biojava.nbio.core.sequence.ProteinSequence;
10+
import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
11+
import org.biojava.nbio.core.sequence.compound.AminoAcidCompoundSet;
12+
import org.biojava.nbio.core.sequence.io.FastaReader;
13+
import org.biojava.nbio.core.sequence.io.FastaWriter;
14+
import org.biojava.nbio.core.sequence.io.GenericFastaHeaderParser;
15+
import org.biojava.nbio.core.sequence.io.ProteinSequenceCreator;
16+
import org.biojava.nbio.core.sequence.io.template.FastaHeaderFormatInterface;
17+
import org.forester.msa.Msa;
18+
import org.junit.Test;
19+
20+
import static org.junit.Assert.*;
21+
22+
/**
23+
* Test the BioJava-forester wrapper methods.
24+
*
25+
* @author Aleix Lafita
26+
*
27+
*/
28+
public class TestForesterWrapper {
29+
30+
@Test
31+
public void testMSAconversion() throws Exception {
32+
33+
// Load the msa FASTA file into a BioJava MSA object
34+
InputStream inStream = TestForesterWrapper.class
35+
.getResourceAsStream("/1u6d_symm.fasta");
36+
37+
FastaReader<ProteinSequence, AminoAcidCompound> fastaReader =
38+
new FastaReader<ProteinSequence, AminoAcidCompound>(
39+
inStream,
40+
new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(),
41+
new ProteinSequenceCreator(AminoAcidCompoundSet
42+
.getAminoAcidCompoundSet()));
43+
44+
LinkedHashMap<String, ProteinSequence> proteinSequences = fastaReader
45+
.process();
46+
47+
inStream.close();
48+
49+
MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound> msa =
50+
new MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound>();
51+
52+
String expected = "";
53+
for (ProteinSequence proteinSequence : proteinSequences.values()) {
54+
msa.addAlignedSequence(proteinSequence);
55+
expected += ">" + proteinSequence.getOriginalHeader() + "\n"
56+
+ proteinSequence.toString() + "\n";
57+
}
58+
59+
// Convert the biojava MSA to a FASTA String
60+
OutputStream os = new ByteArrayOutputStream();
61+
FastaWriter<ProteinSequence, AminoAcidCompound> fastaW =
62+
new FastaWriter<ProteinSequence, AminoAcidCompound>(os,
63+
msa.getAlignedSequences(),
64+
new FastaHeaderFormatInterface<ProteinSequence, AminoAcidCompound>() {
65+
@Override
66+
public String getHeader(ProteinSequence sequence) {
67+
return sequence.getAccession().toString();
68+
};
69+
});
70+
fastaW.process();
71+
String biojava = os.toString();
72+
73+
// Convert the biojava MSA to a forester Msa
74+
Msa fMsa = ForesterWrapper.convert(msa);
75+
76+
StringBuilder sb = new StringBuilder();
77+
for (int i = 0; i < fMsa.getNumberOfSequences(); i++) {
78+
sb.append(">" + fMsa.getIdentifier(i) + "\n");
79+
sb.append(fMsa.getSequenceAsString(i) + "\n");
80+
}
81+
String forester = sb.toString();
82+
83+
// Assert that all FASTA files are equal
84+
assertEquals(expected, biojava);
85+
assertEquals(expected, forester);
86+
87+
}
88+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
>1u6d_1
2+
GRLIYTAGGYF-----RQSLSYLEAYNPSDGTWLRLADLQVPRSGLAGCVV
3+
>1u6d_2
4+
GGLLYAVGGRNNspdgNTDSSALDCYNPMTNQWSPCAPMSVPRNRIGVGVI
5+
>1u6d_3
6+
DGHIYAVGGSHG----CIHHNSVERYEPERDEWHLVAPMLTRRIGVGVAVL
7+
>1u6d_4
8+
NRLLYAVGGFDG----TNRLNSAECYYPERNEWRMITAMNTIRSGAGVCVL
9+
>1u6d_5
10+
HNCIYAAGGYDG----QDQLNSVERYDVETETWTFVAPMKHRRSALGITVH
11+
>1u6d_6
12+
QGRIYVLGGYDG----HTFLDSVECYDPDTDTWSEVTRMTSGRSGVGVAVT

0 commit comments

Comments
 (0)