//*****************************************************************************
//
// File: Results.java
// Package: edu.rit.compbio.phyl
// Unit: Class edu.rit.compbio.phyl.Results
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.compbio.phyl;
import edu.rit.draw.Drawing;
import edu.rit.swing.DisplayableIO;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Date;
import java.util.Formatter;
import java.util.Map;
/**
* Class Results provides a method for reporting the results of a maximum
* parsimony phylogenetic tree construction program. The results are stored in
* files in a specified directory. The following files are created:
*
* -
* "index.html" has all the results. Open this file in a web browser to
* view the results.
*
-
* "tree_001.png", "tree_002.png", and so on have pictures of
* the phylogenetic trees. Each tip node is labeled with a DNA sequence name.
* Each interior node is labeled with the number of state changes at that node.
*
-
* "sequences.phy" has a copy of the input DNA sequences in interleaved
* PHYLIP format.
*
-
* "trees.txt" has all the phylogenetic trees, one per line, in Newick
* Standard format.
*
*
* @author Alan Kaminsky
* @version 25-Oct-2008
*/
public class Results
{
// Prevent construction.
private Results()
{
}
// Exported operations.
/**
* Report the results of a maximum parsimony phylogenetic tree construction
* program.
*
* @param directory
* Directory in which to store the files. If the directory does not
* exist, it is created.
* @param programName
* Name of the program.
* @param hostName
* Name of the host computer, or null if none.
* @param K
* Number of parallel threads, or 0 if none.
* @param infile
* Input file.
* @param originalSeqList
* List of DNA sequences read from the input file, in the order in which
* the sequences appeared in the input file.
* @param sortedSeqList
* List of DNA sequences read from the input file, in the order in which
* the sequences were added to the phylogenetic trees during the search.
* @param initialBound
* Initial bound for branch-and-bound search, or 0 if no initial bound.
* @param treeStoreLimit
* Maximum number of trees to store.
* @param results
* Maximum parsimony results object containing results of the search.
* @param t1
* System clock snapshot at beginning.
* @param t2
* System clock snapshot after preprocessing.
* @param t3
* System clock snapshot after computation.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public static void report
(File directory,
String programName,
String hostName,
int K,
File infile,
DnaSequenceList originalSeqList,
DnaSequenceList sortedSeqList,
int initialBound,
int treeStoreLimit,
MaximumParsimonyResults results,
long t1,
long t2,
long t3)
throws IOException
{
// If directory does not exist, create it.
if (! directory.exists()) directory.mkdirs();
// Begin writing "index.html" file.
File indexfile = new File (directory, "index.html");
PrintStream out =
new PrintStream
(new BufferedOutputStream
(new FileOutputStream (indexfile)));
out.println ("");
out.println ("");
out.print ("Maximum Parsimony Phylogenetic Tree Search Results -- ");
out.print (infile);
out.println (" ");
out.println ("");
out.println ("");
out.println ("");
// Write overview section of "index.html" file.
out.println ("Maximum Parsimony Phylogenetic Tree Search Results
");
out.println ("
Summary
");
out.println ("");
out.println ("
");
out.println ("");
out.println ("Date/time: ");
out.print ("");
out.print (new Date (t3));
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Program: ");
out.print ("");
out.print (programName);
out.println (" ");
out.println (" ");
if (hostName != null)
{
out.println ("");
out.println ("Computer: ");
out.print ("");
out.print (hostName.replaceAll("<","<").replaceAll(">",">"));
out.println (" ");
out.println (" ");
}
if (K > 0)
{
out.println ("");
out.println ("Parallel threads: ");
out.print ("");
out.print (K);
out.println (" ");
out.println (" ");
}
out.println ("");
out.println ("Sequence file: ");
out.print ("");
out.print (infile);
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Number of sequences: ");
out.print ("");
out.print (originalSeqList.length());
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Number of sites: ");
out.print ("");
out.print (originalSeqList.seq(0).length());
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Number of informative sites: ");
out.print ("");
out.print (originalSeqList.informativeSiteCount());
out.println (" ");
out.println (" ");
if (initialBound >= 0)
{
out.println ("");
out.println ("Parsimony score initial bound: ");
out.print ("");
out.print (initialBound);
out.println (" ");
out.println (" ");
}
out.println ("");
out.println ("Maximum trees saved: ");
out.print ("");
out.print (treeStoreLimit);
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Number of trees: ");
out.print ("");
out.print (results.size());
out.println (" ");
out.println (" ");
out.println ("");
out.println ("Number of state changes: ");
out.print ("");
out.print (results.score());
out.println (" ");
out.println (" ");
out.println ("
");
out.println ("");
out.println ("");
out.println ("Sequences");
out.println ("
Distances");
out.println ("
Trees");
out.println ("
");
// Write a copy of the input sequences to "sequences.phy" file.
originalSeqList.write
(new File (directory, "sequences.phy"), 70, true, false);
// Write sequences section of "index.html" file.
out.println ("
Sequences
");
out.println ("Sequences in interleaved PHYLIP format: sequences.phy
");
out.println ("");
out.println ("Notes:");
out.print ("
♦ Number of sequences: ");
out.println (originalSeqList.length());
out.print ("
♦ Number of sites: ");
out.println (originalSeqList.seq(0).length());
out.print ("
♦ Number of informative sites: ");
out.println (originalSeqList.informativeSiteCount());
out.println ("
♦ Informative sites marked");
out.println ("
");
out.println ("");
originalSeqList.write (out, 70, true, true);
out.println ("");
// Write distances section of "index.html" file.
out.println ("
Distances
");
Distance dcalc_H = new HammingDistance();
printDistanceMatrix
(out, "Hamming distances:", "%.0f", originalSeqList, dcalc_H);
Distance dcalc_JC = new JukesCantorDistance();
printDistanceMatrix
(out, "Jukes-Cantor distances:", "%.2f", originalSeqList, dcalc_JC);
// Write trees section of "index.html" file.
out.println ("
Trees
");
out.println ("Trees in Newick Standard format: trees.txt
");
out.println ("");
out.println ("Notes:");
out.println ("
♦ These are unrooted trees");
out.println ("
♦ Each interior node marked with number of state changes at that node");
out.println ("
♦ Each branch marked with least squares branch length, Jukes-Cantor distances");
out.print ("
♦ Number of trees: ");
out.print (results.size());
out.print ("
♦ Total number of state changes: ");
out.print (results.score());
out.println ("
");
// Make an array of all trees in ascending order of squared error and
// average root height.
int L = results.size();
TreeInfo[] allTrees = new TreeInfo [L];
for (int i = 0; i < L; ++ i)
{
// Reconstruct tree.
DnaSequenceTree tree =
sortedSeqList.toTree (results.tree(i));
// Compute parsimony score at each node.
FitchParsimony.computeScore (tree);
// Compute least squares branch lengths.
double sqrerr = LeastSquaresBranchLengths.solve (tree, dcalc_JC);
// Compute average root height.
double avgheight = averageRootHeight (tree);
// Add to array.
allTrees[i] = new TreeInfo (tree, sqrerr, avgheight);
}
Arrays.sort (allTrees);
// Write all trees.
PrintStream treeout =
new PrintStream
(new BufferedOutputStream
(new FileOutputStream
(new File (directory, "trees.txt"))));
Drawing drawing = Drawing.defaultDrawing();
TreeDrawing artist = new TreeDrawing();
for (int i = 0; i < L; ++ i)
{
DnaSequenceTree tree = allTrees[i].tree;
double sqrerr = allTrees[i].sqrerr;
double avgheight = allTrees[i].avgheight;
// Print tree to "index.html" file.
out.print ("Tree ");
out.print (i + 1);
out.print (" of ");
out.print (L);
out.print ("
");
out.println ("");
out.println (tree);
out.println ("");
// Print tree to "trees.txt" file.
treeout.println (tree);
// Write tree drawing to "tree_XXX.png" file.
drawing.clear();
artist.draw (tree);
StringBuilder builder = new StringBuilder();
Formatter formatter = new Formatter (builder);
formatter.format ("tree_%03d.png", i + 1);
String treefilename = builder.toString();
DisplayableIO.writeGrayscalePNGFile
(drawing, new File (directory, treefilename));
// Display tree drawing in "index.html" file.
out.print ("
");
out.println ("");
out.print ("Squared error = ");
out.println (sqrerr);
out.println ("
");
}
treeout.close();
// Finish writing "index.html" file.
long t4 = System.currentTimeMillis();
out.println ("
");
out.println ("");
out.println ("
");
out.println ("");
out.print ("");
out.print (t2 - t1);
out.println (" ");
out.println ("msec preprocessing ");
out.println (" ");
out.println ("");
out.print ("");
out.print (t3 - t2);
out.println (" ");
out.println ("msec calculation ");
out.println (" ");
out.println ("");
out.print ("");
out.print (t4 - t3);
out.println (" ");
out.println ("msec postprocessing ");
out.println (" ");
out.println ("");
out.print ("");
out.print (t4 - t1);
out.println (" ");
out.println ("msec total ");
out.println (" ");
out.println ("
");
out.println ("");
out.println ("");
out.println ("
");
out.println ("");
out.println ("");
out.println ("Powered by Parallel Java: ");
out.println (" ");
out.println ("");
out.println ("http://www.cs.rit.edu/~ark/pj.shtml");
out.println (" ");
out.println (" ");
out.println ("");
out.println ("");
out.println ("Developed by Alan Kaminsky: ");
out.println (" ");
out.println ("");
out.println ("http://www.cs.rit.edu/~ark/");
out.println (" ");
out.println (" ");
out.println ("
");
out.println ("");
out.println ("");
out.println ("");
out.close();
}
// Hidden operations.
/**
* Print a distance matrix.
*
* @param out Where to print.
* @param title Title.
* @param format Format string for printing distances.
* @param seqList DNA sequence list.
* @param dcalc Object to calculate distances between sequences.
*/
private static void printDistanceMatrix
(PrintStream out,
String title,
String format,
DnaSequenceList seqList,
Distance dcalc)
{
out.print ("");
out.print (title);
out.println ("
");
out.println ("");
out.println ("");
out.println (" ");
for (int j = 0; j < seqList.length(); ++ j)
{
DnaSequence seq_j = seqList.seq(j);
out.print ("");
out.print (seq_j.name());
out.println (" ");
}
out.println (" ");
for (int i = 0; i < seqList.length(); ++ i)
{
DnaSequence seq_i = seqList.seq(i);
out.println ("");
out.print ("");
out.print (seq_i.name());
out.println (" ");
for (int j = 0; j < seqList.length(); ++ j)
{
DnaSequence seq_j = seqList.seq(j);
out.print ("");
out.format (format, dcalc.distance (seq_i, seq_j));
out.println (" ");
}
out.println (" ");
}
out.println ("
");
}
/**
* Compute average of branch lengths from every tip node to the root in the
* given tree.
*
* @param tree DNA sequence tree.
*/
private static double averageRootHeight
(DnaSequenceTree tree)
{
double sum = 0.0;
int L = tree.length();
int N = 0;
for (int i = 0; i < L; ++ i)
{
if (tree.child1(i) == -1)
{
sum += rootHeight (tree, i);
++ N;
}
}
return sum/N;
}
/**
* Compute branch length from the given tip node to the root in the given
* tree.
*
* @param tree DNA sequence tree.
* @param i Tip node index.
*/
private static double rootHeight
(DnaSequenceTree tree,
int i)
{
double sum = 0.0;
while (i != -1)
{
Double brlen = tree.branchLength(i);
if (brlen != null) sum += brlen;
i = tree.parent(i);
}
return sum;
}
// Hidden helper classes.
/**
* A record of information about a DNA sequence tree.
*/
private static class TreeInfo
implements Comparable
{
public DnaSequenceTree tree;
public double sqrerr;
public double avgheight;
public TreeInfo
(DnaSequenceTree tree,
double sqrerr,
double avgheight)
{
this.tree = tree;
this.sqrerr = sqrerr;
this.avgheight = avgheight;
}
public int compareTo
(TreeInfo info)
{
if (this.sqrerr < info.sqrerr) return -1;
else if (this.sqrerr > info.sqrerr) return +1;
else if (this.avgheight < info.avgheight) return -1;
else if (this.avgheight > info.avgheight) return +1;
else return 0;
}
}
}
Ads help maintain this website.