2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.Sequence;
23 import jalview.datamodel.SequenceI;
25 import java.io.IOException;
26 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
32 * A parser for input or output of MEGA format files. <br>
34 * Tamura K, Stecher G, Peterson D, Filipski A, and Kumar S (2013) MEGA6:
35 * Molecular Evolutionary Genetics Analysis Version 6.0. Molecular Biology and
36 * Evolution 30: 2725-2729. <br>
39 * MEGA file format is supported as described in
40 * http://www.megasoftware.net/manual.pdf <br>
43 * <li>nested comments (marked by [ ]) are accepted but not preserved</li>
44 * <li>to be completed</li>
47 * @see http://www.megasoftware.net/
49 public class MegaFile extends AlignFile
51 private static final int DEFAULT_LINE_LENGTH = 60;
53 private static final String INDENT = " ";
55 private static final String N_SITES = "NSites";
57 private static final String N_SEQS = "NSeqs";
59 private static final String MISSING = "Missing";
61 private static final String IDENTICAL = "Identical";
63 private static final String INDEL = "Indel";
65 private static final String CODETABLE = "CodeTable";
67 private static final String PROTEIN = "Protein";
69 private static final String NUCLEOTIDE = "Nucleotide";
71 private static final String DATATYPE = "DataType";
73 private static final char COMMENT_START = '[';
75 private static final char COMMENT_END = ']';
77 private static final String HASHSIGN = "#";
79 private static final String SEMICOLON = ";";
81 private static final String BANG = "!";
83 private static final String EQUALS = "=";
85 private static final String MEGA_ID = HASHSIGN + "MEGA";
87 private static final String TITLE = "Title";
89 private static final String FORMAT = "Format";
91 private static final String DESCRIPTION = "Description";
93 private static final String GENE = "Gene";
95 private static final String DOMAIN = "Domain";
98 * names of properties to save to the alignment (may affect eventual output
101 static final String PROP_TITLE = "MEGA_TITLE";
103 static final String PROP_INTERLEAVED = "MEGA_INTERLEAVED";
105 static final String PROP_DESCRIPTION = "MEGA_DESCRIPTION";
107 static final String PROP_CODETABLE = "MEGA_CODETABLE";
109 static final String PROP_IDENTITY = "MEGA_IDENTITY";
111 static final String PROP_MISSING = "MEGA_MISSING";
113 static final String PROP_DATATYPE = "MEGA_DATATYPE";
115 // number of bases per line of file (value is inferred)
116 static final String PROP_LINELENGTH = "MEGA_LINELENGTH";
118 // TODO: need a controlled name for Gene as a feature if we want to be able to
119 // output the MEGA file with !Gene headers
120 // WTF do we do if the sequences get realigned?
122 // initial size for sequence data buffer
123 private static final int SEQBUFFERSIZE = 256;
125 private static final String SPACE = " ";
128 * number of sequence positions output per line
130 private int positionsPerLine;
132 private String title;
134 // gap character may be explicitly declared, if not we infer it
135 private Character gapCharacter;
137 // this can be True, False or null (meaning not asserted in file)
138 private Boolean nucleotide;
140 // set once we have seen one block of interleaved data
141 private boolean firstDataBlockRead = false;
143 // this can be True, False or null (meaning we don't know yet)
144 private Boolean interleaved;
146 // write end of line positions as a comment
147 private boolean writePositionNumbers = true;
153 public MegaFile(String inFile, String type) throws IOException
158 public MegaFile(FileParse source) throws IOException
164 * Parse the input stream.
167 public void parse() throws IOException
170 * Read and process MEGA and Title/Format/Description headers if present.
171 * Returns the first data line following the headers.
173 String dataLine = parseHeaderLines();
176 * Temporary store of {sequenceId, positionData} while parsing interleaved
177 * sequences; sequences are maintained in the order in which they are added
178 * i.e. read in the file
180 Map<String, StringBuilder> seqData = new LinkedHashMap<String, StringBuilder>();
183 * The id of the sequence being read (for non-interleaved)
185 String currentId = "";
187 while (dataLine != null)
189 dataLine = dataLine.trim();
190 if (dataLine.length() > 0)
192 if (dataLine.startsWith(BANG + GENE))
196 else if (dataLine.startsWith(BANG + DOMAIN))
198 parseDomain(dataLine);
202 currentId = parseDataLine(dataLine, seqData, currentId);
205 else if (!seqData.isEmpty())
208 * Blank line after processing some data...
210 this.firstDataBlockRead = true;
212 dataLine = nextNonCommentLine();
215 // remember the (longest) line length read in, so we can output the same
216 setAlignmentProperty(PROP_LINELENGTH, String.valueOf(positionsPerLine));
218 setSequences(seqData);
222 * Parse a !Gene command line
226 protected void parseGene(String dataLine)
231 * Parse a !Domain command line
235 private void parseDomain(String dataLine)
240 * Returns the next line that is not a comment, or null at end of file.
241 * Comments in MEGA are within [ ] brackets, and may be nested.
244 * @throws IOException
246 protected String nextNonCommentLine() throws IOException
248 return nextNonCommentLine(0);
252 * Returns the next line that is not a comment, or null at end of file.
253 * Comments in MEGA are within [ ] brackets, and may be nested.
256 * current depth of nesting of comments while parsing
258 * @throws IOException
260 protected String nextNonCommentLine(final int depth) throws IOException
268 System.err.println("Warning: unterminated comment in data file");
272 int leftBracket = data.indexOf(COMMENT_START);
275 * reject unnested comment following data on the same line
277 if (depth == 0 && leftBracket > 0)
279 throw new FileFormatException(
280 "Can't parse comment following data at " + data);
284 * If we are in a (possibly nested) comment after parsing this line, keep
285 * reading recursively until the comment has unwound
287 int newDepth = commentDepth(data, depth);
290 return nextNonCommentLine(newDepth);
295 * not in a comment by end of this line; return what is left (or the next
296 * line if that is empty)
298 String nonCommentPart = getNonCommentContent(data, depth);
299 // if (nonCommentPart.length() > 0)
301 return nonCommentPart;
303 // return nextNonCommentLine(0);
308 * Returns what is left of the input data after removing any comments, whether
309 * 'in progress' from preceding lines, or embedded in the current line
314 * nested depth of comments pending termination
316 * @throws FileFormatException
318 protected static String getNonCommentContent(String data, int depth)
319 throws FileFormatException
321 int len = data.length();
322 StringBuilder result = new StringBuilder(len);
323 for (int i = 0; i < len; i++)
325 char c = data.charAt(i);
350 return result.toString();
354 * Calculates new depth of comment after parsing an input line i.e. the excess
355 * of opening '[' over closing ']' characters. Any excess ']' are ignored (not
356 * treated as comment delimiters).
361 * current comment nested depth before parsing the line
362 * @return new depth after parsing the line
364 protected static int commentDepth(CharSequence data, int depth)
366 int newDepth = depth;
367 int len = data.length();
368 for (int i = 0; i < len; i++)
370 char c = data.charAt(i);
371 if (c == COMMENT_START)
375 else if (c == COMMENT_END && newDepth > 0)
384 * Convert the parsed sequence strings to objects and store them in the model.
388 protected void setSequences(Map<String, StringBuilder> seqData)
390 Set<Entry<String, StringBuilder>> datasets = seqData.entrySet();
392 for (Entry<String, StringBuilder> dataset : datasets)
394 String sequenceId = dataset.getKey();
395 StringBuilder characters = dataset.getValue();
396 SequenceI s = new Sequence(sequenceId, new String(characters));
397 this.seqs.addElement(s);
402 * Process one line of sequence data. If it has no sequence identifier, append
403 * to the current id's sequence. Else parse out the sequence id and append the
404 * data (if any) to that id's sequence. Returns the sequence id (implicit or
405 * explicit) for this line.
411 * @throws IOException
413 protected String parseDataLine(String dataLine,
414 Map<String, StringBuilder> seqData, String currentId)
417 String seqId = getSequenceId(dataLine);
421 * Just character data
423 parseNoninterleavedDataLine(dataLine, seqData, currentId);
426 else if ((HASHSIGN + seqId).trim().equals(dataLine.trim()))
429 * Sequence id only - header line for noninterleaved data
436 * Sequence id followed by data
438 parseInterleavedDataLine(dataLine, seqData, seqId);
444 * Add a line of sequence data to the buffer for the given sequence id. Start
445 * a new one if we haven't seen it before.
450 * @throws IOException
452 protected void parseNoninterleavedDataLine(String dataLine,
453 Map<String, StringBuilder> seqData, String currentId)
456 if (currentId == null)
459 * Oops. Data but no sequence id context.
461 throw new IOException("No sequence id context at: " + dataLine);
464 assertInterleaved(false, dataLine);
466 StringBuilder sb = getSequenceDataBuffer(seqData, currentId);
469 * Add the current line of data to the sequence.
473 setPositionsPerLine(Math.max(positionsPerLine, dataLine.length()));
477 * Get the sequence data for this sequence id, starting a new one if
484 protected StringBuilder getSequenceDataBuffer(
485 Map<String, StringBuilder> seqData, String currentId)
487 StringBuilder sb = seqData.get(currentId);
490 // first data met for this sequence id, start a new buffer
491 sb = new StringBuilder(SEQBUFFERSIZE);
492 seqData.put(currentId, sb);
498 * Parse one line of interleaved data e.g.
501 * #TheSeqId CGATCGCATGCA
507 * @throws IOException
509 protected void parseInterleavedDataLine(String dataLine,
510 Map<String, StringBuilder> seqData, String seqId)
514 * New sequence found in second or later data block - error.
516 if (this.firstDataBlockRead && !seqData.containsKey(seqId))
518 throw new IOException(
519 "Parse error: misplaced new sequence starting at " + dataLine);
522 StringBuilder sb = getSequenceDataBuffer(seqData, seqId);
523 String data = dataLine.substring(seqId.length() + 1).trim();
526 * Do nothing if this line is _only_ a sequence id with no data following.
528 * Remove any internal spaces
530 if (data != null && data.length() > 0)
532 if (data.indexOf(SPACE) != -1)
534 data = data.replace(SPACE, "");
537 setPositionsPerLine(Math.max(positionsPerLine, data.length()));
538 assertInterleaved(true, dataLine);
543 * If the line begins with (e.g.) "#abcde " then returns "abcde" as the
544 * identifier. Else returns null.
549 public static String getSequenceId(String dataLine)
551 // TODO refactor to a StringUtils type class
552 if (dataLine != null)
554 if (dataLine.startsWith(HASHSIGN))
556 int spacePos = dataLine.indexOf(" ");
557 return (spacePos == -1 ? dataLine.substring(1) : dataLine
558 .substring(1, spacePos));
565 * Read the #MEGA and Title/Format/Description header lines (if present).
567 * Save as alignment properties in case useful.
569 * @return the next non-blank line following the header lines.
570 * @throws IOException
572 protected String parseHeaderLines() throws IOException
574 String inputLine = null;
575 while ((inputLine = nextNonCommentLine()) != null)
577 inputLine = inputLine.trim();
582 if (inputLine.length() == 0)
587 if (inputLine.toUpperCase().startsWith(MEGA_ID))
592 if (isTitle(inputLine))
594 this.title = getValue(inputLine);
595 setAlignmentProperty(PROP_TITLE, title);
597 else if (inputLine.startsWith(BANG + DESCRIPTION))
599 parseDescription(inputLine);
602 else if (inputLine.startsWith(BANG + FORMAT))
604 parseFormat(inputLine);
606 else if (!inputLine.toUpperCase().startsWith(MEGA_ID))
610 * Return the first 'data line' i.e. one that is not blank, #MEGA or
620 * Parse a !Format statement. This may be multiline, and is ended by a
624 * @throws IOException
626 protected void parseFormat(String inputLine) throws IOException
628 while (inputLine != null)
630 parseFormatLine(inputLine);
631 if (inputLine.endsWith(SEMICOLON))
635 inputLine = nextNonCommentLine();
640 * Parse one line of a !Format statement. This may contain one or more
641 * keyword=value pairs.
644 * @throws FileFormatException
646 protected void parseFormatLine(String inputLine)
647 throws FileFormatException
649 if (inputLine.startsWith(BANG + FORMAT))
651 inputLine = inputLine.substring((BANG + FORMAT).length());
653 if (inputLine.endsWith(SEMICOLON))
655 inputLine = inputLine.substring(0, inputLine.length() - 1);
657 if (inputLine.length() == 0)
661 String[] tokens = inputLine.trim().split("\\s"); // any whitespace
662 for (String token : tokens)
664 parseFormatKeyword(token);
669 * Parse a Keyword=Value token. Possible keywords are
671 * <li>DataType= DNA, RNA, Nucleotide, Protein</li>
672 * <li>DataFormat= Interleaved, ?</li>
673 * <li>NSeqs= number of sequences (synonym NTaxa)</li>
674 * <li>NSites= number of bases / residues</li>
675 * <li>Property= Exon (or Coding), Intron (or Noncoding), End (of domain)</li>
676 * <li>Indel= gap character</li>
677 * <li>Identical= identity character (to first sequence) (synonym MatchChar)</li>
678 * <li>Missing= missing data character</li>
679 * <li>CodeTable= Standard, other (MEGA supports various)</li>
683 * @throws FileFormatException
684 * if an unrecognised keyword or value is encountered
686 protected void parseFormatKeyword(String token)
687 throws FileFormatException
689 String msg = "Unrecognised Format command: " + token;
690 String[] bits = token.split(EQUALS);
691 if (bits.length != 2)
693 throw new FileFormatException(msg);
695 String keyword = bits[0];
696 String value = bits[1];
699 * Jalview will work out whether nucleotide or not anyway
701 if (keyword.equalsIgnoreCase(DATATYPE))
703 if (value.equalsIgnoreCase("DNA") || value.equalsIgnoreCase("RNA")
704 || value.equalsIgnoreCase("Nucleotide"))
706 this.nucleotide = true;
707 // alignment computes whether or not it is nucleotide when created
709 else if (value.equalsIgnoreCase(PROTEIN))
711 this.nucleotide = false;
715 throw new FileFormatException(msg);
717 setAlignmentProperty(PROP_DATATYPE, value);
721 * accept non-Standard code table but save in case we want to disable
722 * 'translate as cDNA'
724 else if (keyword.equalsIgnoreCase(CODETABLE))
726 setAlignmentProperty(PROP_CODETABLE, value);
730 * save gap char to set later on alignment once created
732 else if (keyword.equalsIgnoreCase(INDEL))
734 this.gapCharacter = value.charAt(0);
737 else if (keyword.equalsIgnoreCase(IDENTICAL)
738 || keyword.equalsIgnoreCase("MatchChar"))
740 setAlignmentProperty(PROP_IDENTITY, value);
741 if (!".".equals(value))
743 System.err.println("Warning: " + token
744 + " not supported, Jalview uses '.' for identity");
748 else if (keyword.equalsIgnoreCase(MISSING))
750 setAlignmentProperty(PROP_MISSING, value);
751 System.err.println("Warning: " + token + " not supported");
754 else if (keyword.equalsIgnoreCase("Property"))
756 // TODO: figure out what to do with this
757 // can it appear more than once in a file?
758 setAlignmentProperty(PROP_MISSING, value);
761 else if (!keyword.equalsIgnoreCase(N_SEQS)
762 && !keyword.equalsIgnoreCase(N_SITES))
764 System.err.println("Warning: " + msg);
769 * Returns the trimmed data on the line following either whitespace or '=',
770 * with any trailing semi-colon removed<br>
773 * <li>Hello World</li>
774 * <li>!Hello: \tWorld;</li>
775 * <li>!Hello=World</li>
777 * should all return "World"
782 protected static String getValue(String inputLine)
784 if (inputLine == null)
789 String s = inputLine.replaceAll("\t", " ").trim();
792 * KEYWORD = VALUE should return VALUE
794 int equalsPos = s.indexOf("=");
797 value = s.substring(equalsPos + 1);
801 int spacePos = s.indexOf(' ');
802 value = spacePos == -1 ? "" : s.substring(spacePos + 1);
804 value = value.trim();
805 if (value.endsWith(SEMICOLON))
807 value = value.substring(0, value.length() - 1).trim();
813 * Returns true if the input line starts with "TITLE" or "!TITLE" (not case
814 * sensitive). The latter is the official format, some older data file
815 * examples have it without the !.
820 protected static boolean isTitle(String inputLine)
822 if (inputLine == null)
826 String upper = inputLine.toUpperCase();
827 return (upper.startsWith(TITLE.toUpperCase()) || upper.startsWith(BANG
828 + TITLE.toUpperCase()));
832 * Reads lines until terminated by semicolon, appending each to the
833 * Description property value.
835 * @throws IOException
837 protected void parseDescription(String firstDescriptionLine)
840 StringBuilder desc = new StringBuilder(256);
841 String line = getValue(firstDescriptionLine);
844 if (line.endsWith(SEMICOLON))
846 desc.append(line.substring(0, line.length() - 1));
849 else if (line.length() > 0)
851 desc.append(line).append(newline);
853 line = nextNonCommentLine();
855 setAlignmentProperty(PROP_DESCRIPTION, desc.toString());
859 * Returns the alignment sequences in Mega format.
862 public String print()
864 return MEGA_ID + newline + print(getSeqsAsArray());
868 * Write out the alignment sequences in Mega format - interleaved unless
869 * explicitly noninterleaved.
871 protected String print(SequenceI[] s)
874 if (this.interleaved != null && !this.interleaved)
876 result = printNonInterleaved(s);
880 result = printInterleaved(s);
886 * Print to string in Interleaved format - blocks of next N characters of each
891 protected String printInterleaved(SequenceI[] s)
893 int maxIdLength = getMaxIdLength(s);
894 int maxSequenceLength = getMaxSequenceLength(s);
895 int numLines = maxSequenceLength / positionsPerLine + 3; // approx
897 int numDataBlocks = (maxSequenceLength - 1) / positionsPerLine + 1;
898 int spaceEvery = this.nucleotide != null && this.nucleotide ? 3 : 10;
899 int chunksPerLine = (positionsPerLine + spaceEvery - 1) / spaceEvery;
902 * Roughly size a buffer to hold the whole output
904 StringBuilder sb = new StringBuilder(numLines
905 * (maxIdLength + positionsPerLine + chunksPerLine + 10));
908 * Output as: #Seqid CGT AGC ACT ... or blocks of 10 for peptide
911 for (int i = 0; i < numDataBlocks; i++)
914 boolean first = true;
916 for (SequenceI seq : s)
919 String seqId = String.format("#%-" + maxIdLength + "s",
923 * output next line for this sequence
926 int lastPos = seqFrom + positionsPerLine; // exclusive
927 for (int j = 0; j < chunksPerLine; j++)
929 char[] subSequence = seq.getSequence(seqFrom,
930 Math.min(lastPos, seqFrom + spaceEvery));
931 if (subSequence.length > 0)
933 sb.append(SPACE).append(subSequence);
935 seqFrom += subSequence.length;
938 // all sequences should be the same length in MEGA
939 advancedBy += subSequence.length;
942 // write last position as a comment
943 if (writePositionNumbers)
945 sb.append(SPACE).append(COMMENT_START).append(from + advancedBy)
946 .append(COMMENT_END);
954 return new String(sb);
958 * Outputs to string the MEGA header and any other known and relevant
959 * alignment properties
963 protected String printHeaders(AlignmentI al)
965 StringBuilder sb = new StringBuilder(128);
966 sb.append(MEGA_ID).append(newline);
967 String propertyValue = (String) al.getProperty(PROP_TITLE);
968 if (propertyValue != null)
970 sb.append(BANG).append(TITLE).append(SPACE)
971 .append(propertyValue)
975 propertyValue = (String) al.getProperty(PROP_DESCRIPTION);
976 if (propertyValue != null)
978 sb.append(BANG).append(DESCRIPTION).append(newline)
979 .append(propertyValue).append(SEMICOLON)
984 * !Format DataType CodeTable
986 sb.append(BANG).append(FORMAT).append(newline);
987 String dataType = (String) al.getProperty(PROP_DATATYPE);
988 if (dataType == null)
990 dataType = al.isNucleotide() ? NUCLEOTIDE : PROTEIN;
992 sb.append(INDENT).append(DATATYPE).append(EQUALS).append(dataType);
993 String codeTable = (String) al.getProperty(PROP_CODETABLE);
994 sb.append(SPACE).append(CODETABLE).append(EQUALS)
995 .append(codeTable == null ? "Standard" : codeTable)
999 * !Format NSeqs NSites
1000 * NSites the length of any sequence (they should all be the same), excluding
1003 sb.append(INDENT).append(N_SEQS).append(EQUALS).append(al.getHeight());
1004 SequenceI seq = al.getSequenceAt(0);
1005 sb.append(SPACE).append(N_SITES).append(EQUALS)
1006 .append(seq.getEnd() - seq.getStart() + 1);
1010 * !Format Indel Identical Missing
1013 sb.append(INDEL).append(EQUALS).append(al.getGapCharacter());
1014 String identity = (String) al.getProperty(PROP_IDENTITY);
1015 if (identity != null)
1017 sb.append(SPACE).append(IDENTICAL).append(EQUALS).append(identity);
1019 String missing = (String) al.getProperty(PROP_MISSING);
1020 if (missing != null)
1022 sb.append(SPACE).append(MISSING).append(EQUALS).append(missing);
1024 sb.append(SEMICOLON).append(newline);
1026 return sb.toString();
1030 * Get the longest sequence id (to allow aligned printout).
1035 protected static int getMaxIdLength(SequenceI[] s)
1037 // TODO pull up for reuse
1039 for (SequenceI seq : s)
1041 int len = seq.getName().length();
1042 if (len > maxLength)
1051 * Get the longest sequence length
1056 protected static int getMaxSequenceLength(SequenceI[] s)
1058 // TODO pull up for reuse
1060 for (SequenceI seq : s)
1062 int len = seq.getLength();
1063 if (len > maxLength)
1072 * Print to string in noninterleaved format - all of each sequence in turn, in
1073 * blocks of 50 characters.
1078 protected String printNonInterleaved(SequenceI[] s)
1080 int maxSequenceLength = getMaxSequenceLength(s);
1082 int numLines = maxSequenceLength / positionsPerLine + 2 + s.length;
1085 * Roughly size a buffer to hold the whole output
1087 StringBuilder sb = new StringBuilder(numLines * positionsPerLine);
1089 int spaceEvery = this.nucleotide != null && this.nucleotide ? 3 : 10;
1090 int chunksPerLine = positionsPerLine / spaceEvery;
1091 for (SequenceI seq : s)
1094 sb.append(HASHSIGN + seq.getName()).append(newline);
1096 while (startPos < seq.getLength())
1098 boolean firstChunk = true;
1100 * print next line for this sequence
1102 int lastPos = startPos + positionsPerLine; // exclusive
1103 for (int j = 0; j < chunksPerLine; j++)
1105 char[] subSequence = seq.getSequence(startPos,
1106 Math.min(lastPos, startPos + positionsPerLine));
1107 if (subSequence.length > 0)
1113 sb.append(subSequence);
1116 startPos += subSequence.length;
1122 return new String(sb);
1126 * Flag this file as interleaved or not, based on data format. Throws an
1127 * exception if has previously been determined to be otherwise.
1131 * @throws IOException
1133 protected void assertInterleaved(boolean isIt, String dataLine)
1134 throws FileFormatException
1136 if (this.interleaved != null && isIt != this.interleaved.booleanValue())
1138 throw new FileFormatException(
1139 "Parse error: mix of interleaved and noninterleaved detected, at line: "
1142 this.interleaved = new Boolean(isIt);
1143 setAlignmentProperty(PROP_INTERLEAVED, interleaved.toString());
1146 public boolean isInterleaved()
1148 return this.interleaved == null ? false : this.interleaved
1153 * Adds saved parsed values either as alignment properties, or (in some cases)
1154 * as specific member fields of the alignment
1157 public void addProperties(AlignmentI al)
1159 super.addProperties(al);
1160 if (this.gapCharacter != null)
1162 al.setGapCharacter(gapCharacter);
1166 * warn if e.g. DataType=DNA but data is protein (or vice versa)
1168 if (this.nucleotide != null && this.nucleotide != al.isNucleotide()) {
1169 System.err.println("Warning: " + this.title + " declared "
1170 + (nucleotide ? "" : " not ") + "nucleotide but it is"
1171 + (nucleotide ? " not" : ""));
1176 * Print the given alignment in MEGA format. If the alignment was created by
1177 * parsing a MEGA file, it should have properties set (e.g. Title) which can
1178 * influence the output.
1181 public String print(AlignmentI al)
1183 this.nucleotide = al.isNucleotide();
1184 String lineLength = (String) al.getProperty(PROP_LINELENGTH);
1185 this.positionsPerLine = lineLength == null ? DEFAULT_LINE_LENGTH : Integer
1186 .parseInt(lineLength);
1187 return printHeaders(al) + print(al.getSequencesArray());
1191 * Returns the number of sequence positions output per line
1195 public int getPositionsPerLine()
1197 return positionsPerLine;
1201 * Sets the number of sequence positions output per line. Note these will be
1202 * formatted in blocks of 3 (nucleotide) or 10 (peptide).
1206 public void setPositionsPerLine(int p)
1208 this.positionsPerLine = p;