|
| 1 | +package demo; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.util.LinkedHashMap; |
| 5 | + |
| 6 | +import org.biojava.nbio.core.sequence.MultipleSequenceAlignment; |
| 7 | +import org.biojava.nbio.core.sequence.ProteinSequence; |
| 8 | +import org.biojava.nbio.core.sequence.compound.AminoAcidCompound; |
| 9 | +import org.biojava.nbio.core.sequence.compound.AminoAcidCompoundSet; |
| 10 | +import org.biojava.nbio.core.sequence.io.FastaReader; |
| 11 | +import org.biojava.nbio.core.sequence.io.GenericFastaHeaderParser; |
| 12 | +import org.biojava.nbio.core.sequence.io.ProteinSequenceCreator; |
| 13 | +import org.biojava.nbio.phylo.DistanceMatrixCalculator; |
| 14 | +import org.biojava.nbio.phylo.DistanceTreeEvaluator; |
| 15 | +import org.biojava.nbio.phylo.ForesterWrapper; |
| 16 | +import org.biojava.nbio.phylo.TreeConstructor; |
| 17 | +import org.biojava.nbio.phylo.TreeConstructorType; |
| 18 | +import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix; |
| 19 | +import org.forester.evoinference.matrix.distance.DistanceMatrix; |
| 20 | +import org.forester.phylogeny.Phylogeny; |
| 21 | + |
| 22 | +/** |
| 23 | + * This demo contains the CookBook example to create a phylogenetic tree from a |
| 24 | + * given multiple sequence alignment (MSA). |
| 25 | + * |
| 26 | + * @author Scooter Willis |
| 27 | + * @author Aleix Lafita |
| 28 | + * |
| 29 | + */ |
| 30 | +public class DemoDistanceTree { |
| 31 | + |
| 32 | + public static void main(String[] args) throws Exception { |
| 33 | + |
| 34 | + // 0. This is just to load an example MSA from a FASTA file |
| 35 | + InputStream inStream = TreeConstructor.class |
| 36 | + .getResourceAsStream("/PF00104_small.fasta"); |
| 37 | + |
| 38 | + FastaReader<ProteinSequence, AminoAcidCompound> fastaReader = 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 = new MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound>(); |
| 50 | + |
| 51 | + for (ProteinSequence proteinSequence : proteinSequences.values()) { |
| 52 | + msa.addAlignedSequence(proteinSequence); |
| 53 | + } |
| 54 | + |
| 55 | + long readT = System.currentTimeMillis(); |
| 56 | + |
| 57 | + // 1. Calculate the evolutionary distance matrix |
| 58 | + DistanceMatrix DM = DistanceMatrixCalculator.kimuraDistance(msa); |
| 59 | + |
| 60 | + // 2. Construct a distance tree using the NJ algorithm |
| 61 | + Phylogeny phylo = TreeConstructor.distanceTree( |
| 62 | + (BasicSymmetricalDistanceMatrix) DM, TreeConstructorType.NJ); |
| 63 | + |
| 64 | + long treeT = System.currentTimeMillis(); |
| 65 | + String newick = ForesterWrapper.getNewickString(phylo, true, true); |
| 66 | + System.out.println(newick); |
| 67 | + System.out.println("Tree Construction: " + (treeT - readT) + " ms."); |
| 68 | + |
| 69 | + // 3. Evaluate the goodness of fit of the tree |
| 70 | + double cv = DistanceTreeEvaluator.evaluate(phylo, DM); |
| 71 | + System.out.println("CV of the tree: " + (int) (cv*100) + " %"); |
| 72 | + |
| 73 | + } |
| 74 | +} |
0 commit comments