001    package calhoun.analysis.crf.io;
002    
003    import java.io.BufferedReader;
004    import java.io.IOException;
005    import java.io.Writer;
006    import java.util.ArrayList;
007    import java.util.List;
008    import java.util.Map;
009    
010    import org.apache.commons.logging.Log;
011    import org.apache.commons.logging.LogFactory;
012    
013    import calhoun.analysis.crf.features.supporting.phylogenetic.RootedBinaryPhylogeneticTree;
014    import calhoun.util.Assert;
015    
016    /** an input component that reads in multiple alignment sequences.  Creates 
017     * {@link MultipleAlignmentInputSequence} objects for each sequence.  Can be used as
018     * a regular input or as part of an interleaved input file.
019     */
020    public class MultipleAlignmentInput extends InterleavedInputComponentBase {
021            private static final long serialVersionUID = 1796622784861590659L;
022            private static final Log log = LogFactory.getLog(MultipleAlignmentInput.class);
023    
024            public boolean read(BufferedReader r, Map<String, InputSequence<?>> output) throws IOException {
025                    List<String> speciesNames = new ArrayList<String>();
026                    List<String> consensuses = new ArrayList<String>();
027                    int nSpecies;
028                    
029                    String temp = r.readLine();
030                    if(temp == null)
031                            return false;
032                    
033                    try {
034                            nSpecies = Integer.parseInt(temp);
035                    } catch (Exception e){
036                            log.error("Offending line was : " + temp);
037                            throw new IOException();
038                    }
039                    
040                    temp = r.readLine();
041                    RootedBinaryPhylogeneticTree tree = new RootedBinaryPhylogeneticTree(temp);
042                    
043                    log.debug("Number of species : " + nSpecies);   
044                    
045                    for (int spec=0; spec<nSpecies; spec++) { 
046                            String str1 = r.readLine();
047                            Assert.a(str1 != null);
048    
049                            log.debug("One of the species: " + str1);
050                            speciesNames.add(str1.substring(1));
051    
052                            String str2 = r.readLine();
053                            Assert.a(str2 != null);
054                            consensuses.add(str2);
055                    }
056    
057                    InputSequence<?> inputSeq = new MultipleAlignmentInputSequence(speciesNames, consensuses, speciesNames.get(0), tree);
058                    output.put(name, inputSeq);
059                    return true;
060            }
061            
062    
063            public void write(Writer w, Map<String, ? extends InputSequence<?>> data) throws IOException {
064                    MultipleAlignmentInputSequence inputSeq = (MultipleAlignmentInputSequence) data.get(name);
065                    w.write("" + inputSeq.nSpecies + "\n");
066                    w.write("" + inputSeq.tree.newick() + "\n");
067                    for (int spec=0; spec<inputSeq.nSpecies; spec++) {
068                            w.write(">" + inputSeq.speciesNames.get(spec));
069                            w.write('\n');
070                            w.write(inputSeq.consensuses.get(spec));
071                            w.write('\n');                  
072                    }
073            }
074    }