001 package calhoun.analysis.crf.io;
002
003 import java.io.IOException;
004 import java.util.ArrayList;
005 import java.util.List;
006 import java.util.Map;
007
008 import calhoun.analysis.crf.features.supporting.phylogenetic.RootedBinaryPhylogeneticTree;
009 import calhoun.util.Assert;
010 import calhoun.util.FileUtil;
011
012 public class AlignmentTree implements InputComponentIO {
013 private static final long serialVersionUID = 760405914814389112L;
014
015 String component;
016
017 public List<String> getComponentNames() {
018 List<String> ret = new ArrayList();
019 ret.add(component);
020 return ret;
021 }
022
023 public void readInputSequences(String location, List<Map<String, InputSequence<?>>> inputs) throws IOException {
024 Assert.a(inputs.size() > 0, "AlignmentTree can't be the first input.");
025
026 // Read the tree from the file
027 String[] contents = FileUtil.readFile(location).split("\n");
028
029 RootedBinaryPhylogeneticTree tree = new RootedBinaryPhylogeneticTree(contents[0]);
030
031 // Add it into every input sequence
032 for(Map<String, InputSequence<?>> input : inputs) {
033 input.put(component, new MultipleAlignmentInputSequence(contents[1].trim(), tree));
034 }
035 }
036
037 public void writeInputSequences(String location, List<? extends Map<String, ? extends InputSequence<?>>> inputComponents) throws IOException {
038 MultipleAlignmentInputSequence alignment = (MultipleAlignmentInputSequence) inputComponents.get(0).get(component);
039 String tree = alignment.getTree().newick();
040 FileUtil.writeFile(location, tree);
041 }
042
043 /**
044 * @return Returns the header.
045 */
046 public String getComponent() {
047 return component;
048 }
049 /**
050 * @param header The header to set.
051 */
052 public void setComponent(String header) {
053 this.component = header;
054 }
055 }