| 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.ancestralstates.AncestralStatesComponentOptions; |
| 30 |
import dr.app.beauti.components.sequenceerror.SequenceErrorModelComponentOptions; |
| 31 |
import dr.app.beauti.options.BeautiOptions; |
| 32 |
import dr.app.beauti.options.PartitionData; |
| 33 |
import dr.app.beauti.options.PartitionPattern; |
| 34 |
import dr.app.beauti.options.PartitionSubstitutionModel; |
| 35 |
import dr.app.beauti.types.BinaryModelType; |
| 36 |
import dr.app.beauti.util.XMLWriter; |
| 37 |
import dr.evolution.alignment.Alignment; |
| 38 |
import dr.evolution.alignment.Patterns; |
| 39 |
import dr.evolution.alignment.SitePatterns; |
| 40 |
import dr.evolution.datatype.DataType; |
| 41 |
import dr.evolution.datatype.Microsatellite; |
| 42 |
import dr.evolution.datatype.Nucleotides; |
| 43 |
import dr.evoxml.*; |
| 44 |
import dr.util.Attribute; |
| 45 |
import dr.xml.XMLParser; |
| 46 |
|
| 47 |
import java.util.ArrayList; |
| 48 |
import java.util.List; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class PatternListGenerator extends Generator { |
| 56 |
|
| 57 |
public PatternListGenerator(BeautiOptions options, ComponentFactory[] components) { |
| 58 |
super(options, components); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public void writePatternList(PartitionData partition, XMLWriter writer) { |
| 68 |
writer.writeText(""); |
| 69 |
|
| 70 |
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options |
| 71 |
.getComponentOptions(AncestralStatesComponentOptions.class); |
| 72 |
|
| 73 |
SequenceErrorModelComponentOptions sequenceErrorOptions = (SequenceErrorModelComponentOptions) options |
| 74 |
.getComponentOptions(SequenceErrorModelComponentOptions.class); |
| 75 |
|
| 76 |
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel(); |
| 77 |
|
| 78 |
String codonHeteroPattern = model.getCodonHeteroPattern(); |
| 79 |
int partitionCount = model.getCodonPartitionCount(); |
| 80 |
|
| 81 |
boolean isAncestralStatesModel = (!ancestralStatesOptions.usingAncestralStates(partition) && |
| 82 |
!sequenceErrorOptions.usingSequenceErrorModel(partition)); |
| 83 |
boolean isCovarionModel = model.getDataType().getType() == DataType.COVARION |
| 84 |
&& model.getBinarySubstitutionModel() == BinaryModelType.BIN_COVARION; |
| 85 |
|
| 86 |
boolean unique = isAncestralStatesModel || isCovarionModel; |
| 87 |
boolean strip = isAncestralStatesModel || isCovarionModel; |
| 88 |
|
| 89 |
if (model.getDataType().getType() == DataType.NUCLEOTIDES && codonHeteroPattern != null && partitionCount > 1) { |
| 90 |
|
| 91 |
if (codonHeteroPattern.equals("112")) { |
| 92 |
writer.writeComment("The " + (unique ? "unique " : "") + "patterns for codon positions 1 & 2"); |
| 93 |
writer.writeOpenTag(MergePatternsParser.MERGE_PATTERNS, |
| 94 |
new Attribute[]{ |
| 95 |
new Attribute.Default<String>(XMLParser.ID, partition.getPrefix() + model.getPrefixCodon(1) + SitePatternsParser.PATTERNS), |
| 96 |
} |
| 97 |
); |
| 98 |
writePatternList(partition, 0, 3, null, unique, strip, writer); |
| 99 |
writePatternList(partition, 1, 3, null, unique, strip, writer); |
| 100 |
|
| 101 |
writer.writeCloseTag(MergePatternsParser.MERGE_PATTERNS); |
| 102 |
|
| 103 |
writer.writeComment("The " + (unique ? "unique " : "") + "patterns for codon position 3"); |
| 104 |
writePatternList(partition, 2, 3, model.getPrefixCodon(2), unique, strip, writer); |
| 105 |
|
| 106 |
} else { |
| 107 |
|
| 108 |
|
| 109 |
for (int i = 1; i <= 3; i++) { |
| 110 |
writer.writeComment("The " + (unique ? "unique " : "") + "patterns for codon position " + i); |
| 111 |
writePatternList(partition, i - 1, 3, model.getPrefixCodon(i), unique, strip, writer); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
} else { |
| 117 |
writePatternList(partition, 0, 1, "", unique, strip, writer); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private void writePatternList(final PartitionData partition, int offset, int every, final String codonPrefix, final boolean unique, final boolean strip, final XMLWriter writer) { |
| 130 |
|
| 131 |
Alignment alignment = partition.getAlignment(); |
| 132 |
int from = partition.getFromSite(); |
| 133 |
int to = partition.getToSite(); |
| 134 |
int partEvery = partition.getEvery(); |
| 135 |
if (partEvery > 1 && every > 1) throw new IllegalArgumentException(); |
| 136 |
|
| 137 |
if (from < 1) from = 1; |
| 138 |
every = Math.max(partEvery, every); |
| 139 |
|
| 140 |
from += offset; |
| 141 |
|
| 142 |
|
| 143 |
SitePatterns patterns = new SitePatterns(alignment, null, from - 1, to - 1, every, strip, unique); |
| 144 |
|
| 145 |
writer.writeComment("The " + (unique ? "unique " : "") + "patterns from " + from + " to " + (to > 0 ? to : "end") + ((every > 1) ? " every " + every : ""), |
| 146 |
"npatterns=" + patterns.getPatternCount()); |
| 147 |
|
| 148 |
List<Attribute> attributes = new ArrayList<Attribute>(); |
| 149 |
|
| 150 |
|
| 151 |
if (codonPrefix != null) { |
| 152 |
attributes.add(new Attribute.Default<String>(XMLParser.ID, partition.getPrefix() + codonPrefix + SitePatternsParser.PATTERNS)); |
| 153 |
} |
| 154 |
|
| 155 |
attributes.add(new Attribute.Default<String>("from", "" + from)); |
| 156 |
if (to >= 0) attributes.add(new Attribute.Default<String>("to", "" + to)); |
| 157 |
|
| 158 |
if (every > 1) { |
| 159 |
attributes.add(new Attribute.Default<String>("every", "" + every)); |
| 160 |
} |
| 161 |
|
| 162 |
if(!unique) { |
| 163 |
attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.UNIQUE, false)); |
| 164 |
} |
| 165 |
|
| 166 |
if (strip) { |
| 167 |
attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.STRIP, false)); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
writer.writeOpenTag(SitePatternsParser.PATTERNS, attributes); |
| 172 |
writer.writeIDref(AlignmentParser.ALIGNMENT, alignment.getId()); |
| 173 |
writer.writeCloseTag(SitePatternsParser.PATTERNS); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public void writePatternList(PartitionPattern partition, List<Microsatellite> microsatList, XMLWriter writer) throws GeneratorException { |
| 183 |
|
| 184 |
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel(); |
| 185 |
|
| 186 |
if (model.getDataType().getType() == DataType.MICRO_SAT) { |
| 187 |
Patterns patterns = partition.getPatterns(); |
| 188 |
|
| 189 |
writer.writeComment("The patterns for microsatellite"); |
| 190 |
writer.writeOpenTag(MicrosatellitePatternParser.MICROSATPATTERN, |
| 191 |
new Attribute[]{ |
| 192 |
new Attribute.Default<String>(XMLParser.ID, partition.getName()), |
| 193 |
}); |
| 194 |
|
| 195 |
if (options.hasIdenticalTaxa() && !patterns.hasMask()) { |
| 196 |
writer.writeIDref(TaxaParser.TAXA, TaxaParser.TAXA); |
| 197 |
} else { |
| 198 |
writer.writeIDref(TaxaParser.TAXA, partition.getName() + "." + TaxaParser.TAXA); |
| 199 |
} |
| 200 |
|
| 201 |
Microsatellite m = model.getMicrosatellite(); |
| 202 |
if (m == null) throw new GeneratorException("Microsatellite is null in partition:\n" + partition.getName()); |
| 203 |
|
| 204 |
if (!microsatList.contains(m)) { |
| 205 |
microsatList.add(m); |
| 206 |
writer.writeTag(MicrosatelliteParser.MICROSAT, |
| 207 |
new Attribute[]{ |
| 208 |
new Attribute.Default<String>(XMLParser.ID, m.getName()), |
| 209 |
new Attribute.Default<Integer>(MicrosatelliteParser.MAX, m.getMax()), |
| 210 |
new Attribute.Default<Integer>(MicrosatelliteParser.MIN, m.getMin()), |
| 211 |
new Attribute.Default<Integer>(MicrosatelliteParser.UNIT_LENGTH, m.getUnitLength()), |
| 212 |
}, true); |
| 213 |
|
| 214 |
} else { |
| 215 |
writer.writeTag(MicrosatelliteParser.MICROSAT, |
| 216 |
new Attribute[]{ |
| 217 |
new Attribute.Default<String>(XMLParser.IDREF, m.getName()), |
| 218 |
}, true); |
| 219 |
} |
| 220 |
|
| 221 |
writer.writeOpenTag(MicrosatellitePatternParser.MICROSAT_SEQ); |
| 222 |
|
| 223 |
String seq = ""; |
| 224 |
int c = 0; |
| 225 |
for (int i = 0; i < patterns.getTaxonCount(); i++) { |
| 226 |
if (!patterns.isMasked(i)) { |
| 227 |
if (c > 0) seq += ","; |
| 228 |
int state = patterns.getPatternState(i, 0); |
| 229 |
if (state == Microsatellite.UNKNOWN_STATE_LENGTH) { |
| 230 |
seq += Microsatellite.UNKNOWN_CHARACTER; |
| 231 |
} else { |
| 232 |
seq += Integer.toString(state); |
| 233 |
} |
| 234 |
c++; |
| 235 |
} |
| 236 |
} |
| 237 |
writer.writeText(seq); |
| 238 |
|
| 239 |
writer.writeCloseTag(MicrosatellitePatternParser.MICROSAT_SEQ); |
| 240 |
|
| 241 |
writer.writeCloseTag(MicrosatellitePatternParser.MICROSATPATTERN); |
| 242 |
|
| 243 |
} else { |
| 244 |
|
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
} |