Debian Codegraph @

src/dr/evolution/io/PhylipSequentialImporter.java

raw symbols SCIP
1 /*
2 * PhylipSequentialImporter.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
40 /**
41 * Class for importing PHYLIP sequential file format
42 *
43 * @version $Id: PhylipSequentialImporter.java,v 1.5 2005/05/24 20:25:56 rambaut Exp $
44 *
45 * @author Andrew Rambaut
46 * @author Alexei Drummond
47 */
48 public class PhylipSequentialImporter extends Importer implements SequenceImporter {
49
50 /**
51 * Constructor
52 */
53 public PhylipSequentialImporter(Reader reader, DataType dataType, int maxNameLength) {
54 super(reader);
55 setCommentDelimiters('\0', '\0', '\0');
56
57 this.dataType = dataType;
58 this.maxNameLength = maxNameLength;
59 }
60
61 public PhylipSequentialImporter(Reader reader, Writer commentWriter, DataType dataType, int maxNameLength) {
62 super(reader, commentWriter);
63 setCommentDelimiters('\0', '\0', '\0');
64
65 this.dataType = dataType;
66 this.maxNameLength = maxNameLength;
67 }
68
69 /**
70 * importAlignment.
71 */
72 public Alignment importAlignment() throws IOException, Importer.ImportException
73 {
74 SimpleAlignment alignment = null;
75
76 try {
77
78 int taxonCount = readInteger();
79 int siteCount = readInteger();
80
81 String firstSeq = null;
82
83 for (int i = 0; i < taxonCount; i++) {
84 StringBuffer name = new StringBuffer();
85
86 char ch = read();
87 int n = 0;
88 while (!Character.isWhitespace(ch) && (maxNameLength < 1 || n < maxNameLength)) {
89 name.append(ch);
90 ch = read();
91 n++;
92 }
93
94 StringBuffer seq = new StringBuffer(siteCount);
95 readSequence(seq, dataType, "", siteCount, "-", "?", ".", firstSeq);
96
97 if (firstSeq == null) { firstSeq = seq.toString(); }
98
99
100 if (alignment == null) {
101 alignment = new SimpleAlignment();
102 }
103
104 alignment.addSequence(new Sequence(new Taxon(name.toString()), seq.toString()));
105 }
106
107
108 } catch (EOFException e) { }
109
110 return alignment;
111 }
112
113 /**
114 * importSequences.
115 */
116 public SequenceList importSequences() throws IOException, ImportException {
117 return importAlignment();
118 }
119
120 private DataType dataType;
121 private int maxNameLength = 10;
122 }