    001    package calhoun.analysis.crf.io;
    002
    003    import java.io.IOException;
    004    import java.util.ArrayList;
    005    import java.util.HashMap;
    006    import java.util.List;
    007    import java.util.Map;
    008
    009    import calhoun.seq.FastaIterator;
    010    import calhoun.seq.FastaSequence;
    011    import calhoun.seq.FastaWriter;
    012    import calhoun.util.Assert;
    013
    014    public class FastaInput implements InputComponentIO {
    015            private static final long serialVersionUID = 760405914814389112L;
    016
    017            String header;
    018            String sequence;
    019            Integer numSeqCap = Integer.MAX_VALUE;
    020
    021            public List<String> getComponentNames() {
    022                    List<String> ret = new ArrayList();
    023                    ret.add(header);
    024                    ret.add(sequence);
    025                    return ret;
    026            }
    027
    028            public void readInputSequences(String location, List<Map<String, InputSequence<?>>> inputs) throws IOException {
    029                    FastaIterator it = new FastaIterator(location);
    030                    Assert.a(inputs.size() == 0, "Fasta input always has to go first in the list.");
    031                    int i = 0;
    032                    if (numSeqCap == null) {
    033                            numSeqCap = Integer.MAX_VALUE;
    034                    }
    035                    while(it.hasNext() && i < numSeqCap.intValue()) {
    036                            FastaSequence seq = (FastaSequence) it.next();
    037                            Map<String, InputSequence<?>> input = new HashMap();
    038                            input.put(header, new NameInputSequence(seq.getHeader()));
    039                            input.put(sequence, new InputSequenceCharacter(seq.getSequence().toUpperCase()));
    040                            inputs.add(input);
    041                            i++;
    042                    }
    043            }
    044
    045            public void writeInputSequences(String location, List<? extends Map<String, ? extends InputSequence<?>>> inputComponents) throws IOException {
    046                    FastaWriter w = new FastaWriter(location, false);
    047                    for(Map<String, ? extends InputSequence<?>> input : inputComponents) {
    048                            NameInputSequence nameSeq = (NameInputSequence) input.get(header);
    049                            InputSequenceCharacter seqSeq = (InputSequenceCharacter) input.get(sequence);
    050                            w.writeSeq(nameSeq.getName(), seqSeq.getString());
    051                    }
    052
    053                    w.close();
    054            }
    055
    056            /**
    057             * @return Returns the header.
    058             */
    059            public String getHeader() {
    060                    return header;
    061            }
    062            /**
    063             * @param header The header to set.
    064             */
    065            public void setHeader(String header) {
    066                    this.header = header;
    067            }
    068            /**
    069             * @return Returns the sequence.
    070             */
    071            public String getSequence() {
    072                    return sequence;
    073            }
    074            /**
    075             * @param sequence The sequence to set.
    076             */
    077            public void setSequence(String sequence) {
    078                    this.sequence = sequence;
    079            }
    080
    081            public Integer getNumSeqCap() {
    082                    return numSeqCap;
    083            }
    084
    085            public void setNumSeqCap(Integer numSeqCap) {
    086                    this.numSeqCap = numSeqCap;
    087            }
    088    }