Debian Codegraph @

src/dr/app/beauti/generator/AlignmentGenerator.java

raw symbols SCIP
1 /*
2 * AlignmentGenerator.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.app.beauti.generator;
27
28 import dr.app.beauti.components.ComponentFactory;
29 import dr.app.beauti.components.dollo.DolloComponentOptions;
30 import dr.app.beauti.options.BeautiOptions;
31 import dr.app.beauti.types.BinaryModelType;
32 import dr.app.beauti.util.XMLWriter;
33 import dr.evolution.alignment.Alignment;
34 import dr.evolution.datatype.DataType;
35 import dr.evolution.datatype.TwoStateCovarion;
36 import dr.evolution.util.Taxon;
37 import dr.evoxml.AlignmentParser;
38 import dr.evoxml.SequenceParser;
39 import dr.evoxml.TaxonParser;
40 import dr.util.Attribute;
41 import dr.xml.XMLParser;
42
43 import java.util.List;
44
45
46 /**
47 * @author Alexei Drummond
48 * @author Andrew Rambaut
49 * @author Walter Xie
50 */
51 public class AlignmentGenerator extends Generator {
52
53 // used as a null sequence if sampling from the prior only. It is
54 // three characters long in case it is codon partitioned.
55 private static final String NULL_SEQUENCE = "???";
56
57 public AlignmentGenerator(BeautiOptions options, ComponentFactory[] components) {
58 super(options, components);
59 }
60
61 /**
62 * Write all Alignments
63 * @param writer XMLWriter
64 */
65 public void writeAlignments(List<Alignment> alignments, XMLWriter writer) {
66 int index = 1;
67 for (Alignment alignment : alignments) {
68 if (alignments.size() > 1) {
69 //if (!options.allowDifferentTaxa) {
70 alignment.setId(AlignmentParser.ALIGNMENT + index);
71 //} else { // e.g. alignment_gene1
72 // alignment.setId("alignment_" + mulitTaxaTagName + index);
73 //}
74 } else {
75 alignment.setId(AlignmentParser.ALIGNMENT);
76 }
77 writeAlignment(alignment, writer);
78 index += 1;
79 writer.writeText("");
80 }
81
82 }
83
84 /**
85 * Generate an alignment block from these beast options
86 *
87 * @param alignment the alignment to write
88 * @param writer the writer
89 */
90 private void writeAlignment(Alignment alignment, XMLWriter writer) {
91
92 writer.writeText("");
93 writer.writeComment("The sequence alignment (each sequence refers to a taxon above).",
94 "ntax=" + alignment.getTaxonCount() + " nchar=" + alignment.getSiteCount());
95 if (options.samplePriorOnly) {
96 writer.writeComment("Null sequences generated in order to sample from the prior only.");
97 }
98
99 if (getAlignmentDataTypeDescription(alignment) != null) {
100 writer.writeOpenTag(
101 AlignmentParser.ALIGNMENT,
102 new Attribute[]{
103 new Attribute.Default<String>(XMLParser.ID, alignment.getId()),
104 new Attribute.Default<String>(DataType.DATA_TYPE, getAlignmentDataTypeDescription(alignment))
105 }
106 );
107 } else {
108 writer.writeOpenTag(
109 AlignmentParser.ALIGNMENT, new Attribute.Default<String>(XMLParser.ID, alignment.getId()));
110
111 writer.writeIDref(DataType.DATA_TYPE, getAlignmentDataTypeIdref(alignment));
112 }
113
114 for (int i = 0; i < alignment.getTaxonCount(); i++) {
115 Taxon taxon = alignment.getTaxon(i);
116
117 writer.writeOpenTag(SequenceParser.SEQUENCE);
118 writer.writeIDref(TaxonParser.TAXON, taxon.getId());
119 if (!options.samplePriorOnly) {
120 // writer.checkText(alignment.getAlignedSequenceString(i));
121 writer.writeText(alignment.getAlignedSequenceString(i));
122
123 // System.out.println(taxon.getId() + ": \n" + alignment.getAlignedSequenceString(i));
124 // System.out.println("len = " + alignment.getAlignedSequenceString(i).length() + "\n");
125 } else {
126 // generate a codon in case there is codon partitioning
127 writer.writeText(NULL_SEQUENCE);
128 }
129 writer.writeCloseTag(SequenceParser.SEQUENCE);
130 }
131 writer.writeCloseTag(AlignmentParser.ALIGNMENT);
132 }
133
134
135 /**
136 * Determine and return the datatype description for these beast options
137 * note that the datatype in XML may differ from the actual datatype
138 *
139 * @param alignment the alignment to get data type description of
140 * @return description
141 */
142
143 private String getAlignmentDataTypeDescription(Alignment alignment) {
144 String description = alignment.getDataType().getDescription();
145
146 switch (alignment.getDataType().getType()) {
147 case DataType.TWO_STATES: // when dataType="binary"
148 // case DataType.COVARION:
149 // description = alignment.getDataType().getDescription();
150
151 // if choose Covarion model then should change into dataType="twoStateCovarion"
152 if (options.getPartitionData(alignment).getPartitionSubstitutionModel().getBinarySubstitutionModel()
153 == BinaryModelType.BIN_COVARION) {
154 description = TwoStateCovarion.INSTANCE.getDescription(); // dataType="twoStateCovarion"
155 } else if (options.getPartitionData(alignment).getPartitionSubstitutionModel().getBinarySubstitutionModel()
156 == BinaryModelType.BIN_DOLLO) {
157 description = null;
158 }
159 break;
160 }
161
162 return description;
163 }
164
165 private String getAlignmentDataTypeIdref(Alignment alignment) {
166 String description = alignment.getDataType().getDescription();
167 switch (alignment.getDataType().getType()) {
168 case DataType.TWO_STATES:
169 if (options.getPartitionData(alignment).getPartitionSubstitutionModel().getBinarySubstitutionModel()
170 == BinaryModelType.BIN_DOLLO) {
171 description = DolloComponentOptions.DATA_NAME;
172 }
173 }
174 return description;
175 }
176 }