Debian Codegraph @

src/dr/evolution/io/FastaImporter.java

raw symbols SCIP
1 /*
2 * FastaImporter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.io;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.alignment.SimpleAlignment;
30 import dr.evolution.datatype.DataType;
31 import dr.evolution.sequence.Sequence;
32 import dr.evolution.sequence.SequenceList;
33 import dr.evolution.util.Taxon;
34
35 import java.io.EOFException;
36 import java.io.IOException;
37 import java.io.Reader;
38 import java.io.Writer;
39 import java.util.StringTokenizer;
40
41 /**
42 * Class for importing PHYLIP sequential file format
43 *
44 * @version $Id: PhylipSequentialImporter.java,v 1.5 2005/05/24 20:25:56 rambaut Exp $
45 *
46 * @author Andrew Rambaut
47 * @author Alexei Drummond
48 */
49 public class FastaImporter extends Importer implements SequenceImporter {
50
51 public static final char FASTA_FIRST_CHAR = '>';
52
53 /**
54 * Constructor
55 */
56 public FastaImporter(Reader reader, DataType dataType) {
57 this(reader, null, dataType);
58 }
59
60 public FastaImporter(Reader reader, Writer commentWriter, DataType dataType) {
61 super(reader, commentWriter);
62 setCommentDelimiters('\0', '\0', '\0');
63
64 this.dataType = dataType;
65 }
66
67 /**
68 * importAlignment.
69 */
70 public Alignment importAlignment() throws IOException, ImportException
71 {
72 SimpleAlignment alignment = null;
73
74 try {
75 // find fasta line start
76 while (read() != FASTA_FIRST_CHAR) {
77 }
78
79 do {
80 final String name = readLine().trim();
81 StringBuffer seq = new StringBuffer();
82
83 readSequence(seq, dataType, "" + FASTA_FIRST_CHAR, Integer.MAX_VALUE, "-", "?", "", "");
84
85 if (alignment == null) {
86 alignment = new SimpleAlignment();
87 }
88
89 alignment.addSequence(new Sequence(new Taxon(name.toString()), seq.toString()));
90
91 } while (getLastDelimiter() == FASTA_FIRST_CHAR);
92 } catch (EOFException e) {
93 // catch end of file the ugly way.
94 }
95
96 return alignment;
97 }
98
99 /**
100 * importSequences.
101 */
102 public SequenceList importSequences() throws IOException, ImportException {
103 return importAlignment();
104 }
105
106 private DataType dataType;
107 private int maxNameLength = 10;
108 }