use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.
the class BEAUTiImporter method importFastaFile.
// FASTA
private void importFastaFile(File file) throws IOException, ImportException {
try {
FileReader reader = new FileReader(file);
FastaImporter importer = new FastaImporter(reader, Nucleotides.INSTANCE);
Alignment alignment = importer.importAlignment();
reader.close();
setData(file.getName(), alignment, alignment, null, null, null, null);
} catch (ImportException e) {
throw new ImportException(e.getMessage());
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.
the class NexusImporter method importAlignment.
// **************************************************************
// SequenceImporter IMPLEMENTATION
// **************************************************************
/**
* importAlignment.
*/
public Alignment importAlignment() throws IOException, Importer.ImportException {
boolean done = false;
TaxonList taxonList = null;
Alignment alignment = null;
while (!done) {
try {
NexusImporter.NexusBlock block = findNextBlock();
if (block == NexusImporter.TAXA_BLOCK) {
taxonList = readTaxaBlock();
} else if (block == NexusImporter.CALIBRATION_BLOCK) {
if (taxonList == null) {
throw new MissingBlockException("TAXA block is missing");
}
readCalibrationBlock(taxonList);
} else if (block == NexusImporter.CHARACTERS_BLOCK) {
if (taxonList == null) {
throw new MissingBlockException("TAXA block is missing");
}
alignment = readCharactersBlock(taxonList);
done = true;
} else if (block == NexusImporter.DATA_BLOCK) {
// A data block doesn't need a taxon block before it
// but if one exists then it will use it.
alignment = readDataBlock();
done = true;
} else {
// Ignore the block..
}
} catch (EOFException ex) {
done = true;
}
}
if (alignment == null) {
throw new MissingBlockException("DATA or CHARACTERS block is missing");
}
return alignment;
}
use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.
the class BEAUTiImporter method importBEASTFile.
// xml
private void importBEASTFile(File file) throws IOException, ImportException, JDOMException {
try {
FileReader reader = new FileReader(file);
BeastImporter importer = new BeastImporter(reader);
List<TaxonList> taxonLists = new ArrayList<TaxonList>();
List<Alignment> alignments = new ArrayList<Alignment>();
importer.importBEAST(taxonLists, alignments);
TaxonList taxa = taxonLists.get(0);
int count = 1;
for (Alignment alignment : alignments) {
String name = file.getName();
if (alignment.getId() != null && alignment.getId().length() > 0) {
name = alignment.getId();
} else {
if (alignments.size() > 1) {
name += count;
}
}
setData(name, taxa, alignment, null, null, null, null);
count++;
}
// assume that any additional taxon lists are taxon sets...
for (int i = 1; i < taxonLists.size(); i++) {
Taxa taxonSet = (Taxa) taxonLists.get(i);
options.taxonSets.add(taxonSet);
options.taxonSetsMono.put(taxonSet, false);
options.taxonSetsIncludeStem.put(taxonSet, false);
options.taxonSetsTreeModel.put(taxonSet, options.getPartitionTreeModels().get(0));
}
reader.close();
} catch (JDOMException e) {
throw new JDOMException(e.getMessage());
} catch (ImportException e) {
throw new ImportException(e.getMessage());
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.
the class PatternListGenerator method writePatternList.
/**
* Write a single pattern list
*
* @param partition the partition to write a pattern list for
* @param offset offset by
* @param every skip every
* @param writer the writer
*/
private void writePatternList(final PartitionData partition, int offset, int every, final String codonPrefix, final boolean unique, final boolean strip, final XMLWriter writer) {
Alignment alignment = partition.getAlignment();
int from = partition.getFromSite();
int to = partition.getToSite();
int partEvery = partition.getEvery();
if (partEvery > 1 && every > 1)
throw new IllegalArgumentException();
if (from < 1)
from = 1;
every = Math.max(partEvery, every);
from += offset;
// this object is created solely to calculate the number of patterns in the alignment
SitePatterns patterns = new SitePatterns(alignment, null, from - 1, to - 1, every, strip, unique);
writer.writeComment("The " + (unique ? "unique " : "") + "patterns from " + from + " to " + (to > 0 ? to : "end") + ((every > 1) ? " every " + every : ""), "npatterns=" + patterns.getPatternCount());
List<Attribute> attributes = new ArrayList<Attribute>();
// no 11 of 112 codon, which uses mergePatterns id instead
if (codonPrefix != null) {
attributes.add(new Attribute.Default<String>(XMLParser.ID, partition.getPrefix() + codonPrefix + SitePatternsParser.PATTERNS));
}
attributes.add(new Attribute.Default<String>("from", "" + from));
if (to >= 0)
attributes.add(new Attribute.Default<String>("to", "" + to));
if (every > 1) {
attributes.add(new Attribute.Default<String>("every", "" + every));
}
if (!unique) {
attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.UNIQUE, false));
}
if (strip) {
// default true
attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.STRIP, false));
}
// generate <patterns>
writer.writeOpenTag(SitePatternsParser.PATTERNS, attributes);
writer.writeIDref(AlignmentParser.ALIGNMENT, alignment.getId());
writer.writeCloseTag(SitePatternsParser.PATTERNS);
}
use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.
the class SubstitutionModelGenerator method writeBinaryCovarionModel.
/**
* Write the Binary covarion model XML block
*
* @param writer the writer
* @param model the partition model to write
*/
public void writeBinaryCovarionModel(XMLWriter writer, PartitionSubstitutionModel model) {
// String dataTypeDescription = TwoStateCovarion.INSTANCE.getDescription(); // dataType="twoStateCovarion" for COVARION_MODEL
String prefix = model.getPrefix();
writer.writeComment("The Binary covarion model");
writer.writeOpenTag(BinaryCovarionModelParser.COVARION_MODEL, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, prefix + "bcov") });
if (model.getFrequencyPolicy() == FrequencyPolicyType.EMPIRICAL) {
List<AbstractPartitionData> partitions = options.getDataPartitions(model);
Alignment alignment = ((PartitionData) partitions.get(0)).getAlignment();
// Patterns patterns = new Patterns(partitions.get(0).getAlignment());
// for (int i = 1; i < partitions.size(); i++) {
// patterns.addPatterns(partitions.get(i).getAlignment());
// }
double[] frequencies = alignment.getStateFrequencies();
writer.writeOpenTag(FrequencyModelParser.FREQUENCIES);
writer.writeTag(ParameterParser.PARAMETER, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, prefix + "frequencies"), new Attribute.Default<String>(ParameterParser.VALUE, frequencies[0] + " " + frequencies[1]) }, true);
writer.writeCloseTag(FrequencyModelParser.FREQUENCIES);
} else {
writeFrequencyModelBinary(writer, model, prefix);
}
writeParameter(BinaryCovarionModelParser.HIDDEN_FREQUENCIES, prefix + "hfrequencies", 2, 0.5, 0.0, 1.0, // hfrequencies also 0.5 0.5
writer);
writeParameter(BinaryCovarionModelParser.ALPHA, "bcov.alpha", model, writer);
writeParameter(BinaryCovarionModelParser.SWITCHING_RATE, "bcov.s", model, writer);
writer.writeCloseTag(BinaryCovarionModelParser.COVARION_MODEL);
}
Aggregations