| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class FastaImporter extends Importer implements SequenceImporter { |
| 50 |
|
| 51 |
public static final char FASTA_FIRST_CHAR = '>'; |
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 69 |
|
| 70 |
public Alignment importAlignment() throws IOException, ImportException |
| 71 |
{ |
| 72 |
SimpleAlignment alignment = null; |
| 73 |
|
| 74 |
try { |
| 75 |
|
| 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 |
|
| 94 |
} |
| 95 |
|
| 96 |
return alignment; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public SequenceList importSequences() throws IOException, ImportException { |
| 103 |
return importAlignment(); |
| 104 |
} |
| 105 |
|
| 106 |
private DataType dataType; |
| 107 |
private int maxNameLength = 10; |
| 108 |
} |