Search in sources :

Example 16 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class BeautiBase method assertPriorsEqual.

void assertPriorsEqual(String... ids) {
    System.err.println("assertPriorsEqual");
    CompoundDistribution prior = (CompoundDistribution) doc.pluginmap.get("prior");
    List<Distribution> priors = prior.pDistributions.get();
    for (String id : ids) {
        boolean found = false;
        for (BEASTObject node : priors) {
            if (node.getID().equals(id)) {
                found = true;
            }
        }
        assertThat(found).as("Could not find beastObject with ID " + id).isEqualTo(true);
    }
    List<String> extras = new ArrayList<>();
    for (BEASTObject node : priors) {
        boolean found = false;
        for (String id : ids) {
            if (node.getID().equals(id)) {
                found = true;
            }
        }
        if (!found) {
            extras.add(node.getID());
        }
    }
    if (extras.size() != 0) {
        System.err.println("Extra ids found: " + Arrays.toString(extras.toArray(new String[] {})));
    }
    assertThat(ids.length).as("list of beastObjects do not match").isEqualTo(priors.size());
    ;
}
Also used : CompoundDistribution(beast.core.util.CompoundDistribution) CompoundDistribution(beast.core.util.CompoundDistribution) Distribution(beast.core.Distribution) ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject)

Example 17 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class BeautiBase method pluginListAsString.

private String pluginListAsString(List<?> list) {
    if (list.size() == 0) {
        return "";
    }
    StringBuffer bf = new StringBuffer();
    for (Object o : list) {
        BEASTObject beastObject = (BEASTObject) o;
        bf.append('"');
        bf.append(beastObject.getID());
        bf.append("\", ");
    }
    String str = bf.toString();
    return "(" + str.substring(0, str.length() - 2) + ");";
}
Also used : BEASTObject(beast.core.BEASTObject) BEASTObject(beast.core.BEASTObject)

Example 18 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class BeautiBase method asserListsEqual.

private void asserListsEqual(List<?> list, String[] ids) {
    // check all ids are in list
    for (String id : ids) {
        boolean found = false;
        for (Object o : list) {
            BEASTObject node = (BEASTObject) o;
            if (node.getID().equals(id)) {
                found = true;
                break;
            }
        }
        assertThat(found).as("Could not find beastObject with ID " + id).isEqualTo(true);
    }
    // check all items in list have a unique ie
    Set<String> idsInList = new HashSet<String>();
    Set<String> duplicates = new HashSet<String>();
    for (Object o : list) {
        String id = ((BEASTObject) o).getID();
        if (idsInList.contains(id)) {
            duplicates.add(id);
        } else {
            idsInList.add(id);
        }
    }
    assertThat(duplicates.size()).as("Duplicate ids found: " + Arrays.toString(duplicates.toArray())).isEqualTo(0);
    if (list.size() != ids.length) {
        // list.size > ids.length, otherwise it would have been picked up above
        List<String> extraIDs = new ArrayList<String>();
        for (Object o : list) {
            String id = ((BEASTObject) o).getID();
            boolean found = false;
            for (String id2 : ids) {
                if (id2.equals(id)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                extraIDs.add(id);
            }
        }
        assertThat(ids.length).as("list of beastObjects do not match: found extra items " + Arrays.toString(extraIDs.toArray())).isEqualTo(list.size());
    }
}
Also used : ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject) BEASTObject(beast.core.BEASTObject) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 19 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class BeautiPanelConfig method sync.

@SuppressWarnings("unchecked")
public void sync(int partitionIndex) {
    if (parentInputs.size() > 0 && _input.get() != null) {
        final Input<?> input = parentInputs.get(partitionIndex);
        if (isList) {
            List<Object> list = (List<Object>) _input.get();
            List<Object> targetList = ((List<Object>) input.get());
            // only clear former members
            for (BEASTInterface beastObject : startInputs) {
                targetList.remove(beastObject);
            }
            targetList.addAll(list);
            // sync outputs of items in list
            for (Object o : list) {
                if (o instanceof BEASTInterface) {
                    ((BEASTInterface) o).getOutputs().add(parentBEASTObjects.get(partitionIndex));
                }
            }
        } else {
            try {
                // System.err.println("sync " + parentBEASTObjects.get(partitionIndex) + "[" + input.getName() + "] = " + _input.get());
                input.setValue(_input.get(), parentBEASTObjects.get(partitionIndex));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : BEASTObject(beast.core.BEASTObject) List(java.util.List) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface)

Example 20 with BEASTObject

use of beast.core.BEASTObject in project beast2 by CompEvol.

the class BeautiPanelConfig method resolveInput.

/**
 * Find the input associated with this panel
 * based on the path Input.
 */
@SuppressWarnings("unchecked")
public final Input<?> resolveInput(BeautiDoc doc, int partitionIndex) {
    try {
        // if (parentBEASTObjects != null && parentBEASTObjects.size() > 0 && _input != null)
        // System.err.println("sync " + parentBEASTObjects.get(partitionIndex) + "[?] = " + _input.get());
        List<BEASTInterface> beastObjects;
        if (hasPartitionsInput.get() == Partition.none) {
            beastObjects = new ArrayList<>();
            beastObjects.add(doc.mcmc.get());
        } else {
            beastObjects = doc.getPartitions(hasPartitionsInput.get().toString());
        }
        parentBEASTObjects = new ArrayList<>();
        parentInputs = new ArrayList<>();
        parentBEASTObjects.add(doc);
        parentInputs.add(doc.mcmc);
        type = doc.mcmc.getType();
        isList = false;
        for (int i = 0; i < pathComponents.length; i++) {
            List<BEASTInterface> oldPlugins = beastObjects;
            beastObjects = new ArrayList<>();
            parentBEASTObjects = new ArrayList<>();
            parentInputs = new ArrayList<>();
            for (BEASTInterface beastObject : oldPlugins) {
                final Input<?> namedInput = beastObject.getInput(pathComponents[i]);
                type = namedInput.getType();
                if (namedInput.get() instanceof List<?>) {
                    isList = true;
                    List<?> list = (List<?>) namedInput.get();
                    if (conditionalAttribute[i] == null) {
                        for (Object o : list) {
                            BEASTInterface beastObject2 = (BEASTInterface) o;
                            beastObjects.add(beastObject2);
                            parentBEASTObjects.add(beastObject);
                            parentInputs.add(namedInput);
                        }
                    // throw new Exception ("Don't know which element to pick from the list. List component should come with a condition. " + m_sPathComponents[i]);
                    } else {
                        int matches = 0;
                        for (int j = 0; j < list.size(); j++) {
                            BEASTInterface beastObject2 = (BEASTInterface) list.get(j);
                            if (matches(beastObject2, conditionalAttribute[i], conditionalValue[i])) {
                                beastObjects.add(beastObject2);
                                parentBEASTObjects.add(beastObject);
                                parentInputs.add(namedInput);
                                matches++;
                                break;
                            }
                        }
                        if (matches == 0) {
                            parentInputs.add(namedInput);
                            parentBEASTObjects.add(beastObject);
                        }
                    }
                } else if (namedInput.get() instanceof BEASTInterface) {
                    isList = false;
                    if (conditionalAttribute[i] == null) {
                        beastObjects.add((BEASTInterface) namedInput.get());
                        parentBEASTObjects.add(beastObject);
                        parentInputs.add(namedInput);
                    } else {
                        if (matches(beastObject, conditionalAttribute[i], conditionalValue[i])) {
                            // if ((m_sConditionalAttribute[i].equals("id") && beastObject.getID().equals(m_sConditionalValue[i])) ||
                            // (m_sConditionalAttribute[i].equals("type") && beastObject.getClass().getName().equals(m_sConditionalValue[i]))) {
                            beastObjects.add(beastObject);
                            parentBEASTObjects.add(beastObject);
                            parentInputs.add(namedInput);
                        }
                    }
                } else {
                    throw new IllegalArgumentException("input " + pathComponents[i] + "  is not a beastObject or list");
                }
            }
        }
        if (typeInput.get() != null) {
            type = Class.forName(typeInput.get());
        }
        // sanity check
        if (!isList && (hasPartitionsInput.get() == Partition.none) && beastObjects.size() > 1) {
            Log.warning.println("WARNING: multiple beastObjects match, but hasPartitions=none");
            // this makes sure that all mathing beastObjects are available in one go
            isList = true;
            // this suppresses syncing
            parentInputs.clear();
        }
        inputs.clear();
        startInputs.clear();
        for (BEASTInterface beastObject : beastObjects) {
            inputs.add(beastObject);
            startInputs.add(beastObject);
        }
        if (!isList) {
            _input = new FlexibleInput<>();
        } else {
            _input = new FlexibleInput<>(new ArrayList<>());
        }
        _input.setRule(Validate.REQUIRED);
        syncTo(partitionIndex);
        if (isList) {
            checkForDups((List<Object>) _input.get());
        }
        return _input;
    } catch (Exception e) {
        Log.err.println("Warning: could not find objects in path " + Arrays.toString(pathComponents));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) List(java.util.List) ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject)

Aggregations

BEASTObject (beast.core.BEASTObject)23 ArrayList (java.util.ArrayList)10 BEASTInterface (beast.core.BEASTInterface)7 Input (beast.core.Input)6 Test (org.junit.Test)5 RealParameter (beast.core.parameter.RealParameter)4 Distribution (beast.core.Distribution)3 Parameter (beast.core.parameter.Parameter)3 CompoundDistribution (beast.core.util.CompoundDistribution)3 XMLParserException (beast.util.XMLParserException)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 SAXException (org.xml.sax.SAXException)3 Function (beast.core.Function)2 Logger (beast.core.Logger)2 StateNode (beast.core.StateNode)2 Alignment (beast.evolution.alignment.Alignment)2