| 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.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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class AlignmentGenerator extends Generator { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private static final String NULL_SEQUENCE = "???"; |
| 56 |
|
| 57 |
public AlignmentGenerator(BeautiOptions options, ComponentFactory[] components) { |
| 58 |
super(options, components); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 70 |
alignment.setId(AlignmentParser.ALIGNMENT + index); |
| 71 |
|
| 72 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 121 |
writer.writeText(alignment.getAlignedSequenceString(i)); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
} else { |
| 126 |
|
| 127 |
writer.writeText(NULL_SEQUENCE); |
| 128 |
} |
| 129 |
writer.writeCloseTag(SequenceParser.SEQUENCE); |
| 130 |
} |
| 131 |
writer.writeCloseTag(AlignmentParser.ALIGNMENT); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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: |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if (options.getPartitionData(alignment).getPartitionSubstitutionModel().getBinarySubstitutionModel() |
| 153 |
== BinaryModelType.BIN_COVARION) { |
| 154 |
description = TwoStateCovarion.INSTANCE.getDescription(); |
| 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 |
} |