Search in sources :

Example 1 with BNGMultiStateSpecies

use of cbit.vcell.bionetgen.BNGMultiStateSpecies in project vcell by virtualcell.

the class BNGExecutorServiceMultipass method findRuleProductCompartment.

private static String findRuleProductCompartment(BNGSpecies s) {
    boolean needsCorrection = false;
    // the structures we encounter in this species (use tree set to avoid duplicates)
    Set<String> structureNameSet = new TreeSet<>();
    // if it's a complex species we break it down in simple ones and populate this list
    List<BNGSpecies> simpleSpeciesList = new ArrayList<>();
    if (s instanceof BNGComplexSpecies) {
        simpleSpeciesList.addAll(Arrays.asList(s.parseBNGSpeciesName()));
    } else {
        // if it's a simple species to begin with we'll only have one element in this list
        simpleSpeciesList.add(s);
    }
    for (BNGSpecies simpleSpecies : simpleSpeciesList) {
        // must be multi state (actually multi-site!), we have at least 2 components (sites): RbmUtils.SiteStruct and RbmUtils.SiteProduct
        if (!(simpleSpecies instanceof BNGMultiStateSpecies)) {
            throw new RuntimeException("Species " + s.getName() + " must be instance of BNGMultiStateSpecies");
        }
        BNGMultiStateSpecies mss = (BNGMultiStateSpecies) simpleSpecies;
        // System.out.println("  " + mss.toString());
        List<BNGSpeciesComponent> componentsList = new ArrayList<>(Arrays.asList(mss.getComponents()));
        String structName = "";
        String prodPosition = "";
        for (BNGSpeciesComponent sc : componentsList) {
            // System.out.println("     " + sc.getComponentName() + "~" + sc.getCurrentState());
            if (sc.getComponentName().equals(RbmUtils.SiteStruct)) {
                structName = sc.getCurrentState();
            } else if (sc.getComponentName().equals(RbmUtils.SiteProduct)) {
                prodPosition = sc.getCurrentState();
            }
        }
        if (structName == null || structName.isEmpty() || structName.equals("null")) {
            throw new RuntimeException("Structure name site missing from BNGSpecies " + mss);
        }
        if (prodPosition == null || prodPosition.isEmpty() || prodPosition.equals("null")) {
            throw new RuntimeException("Index of original rule product site missing from BNGSpecies " + mss);
        }
        switch(prodPosition) {
            case // comes from matching a wildcard with a seed species, its compartment may be wrong
            "0":
                break;
            case // comes by directly applying a rule, this is the correct compartment inherited from the rule product
            "1":
                structureNameSet.add(structName);
                break;
            default:
                throw new RuntimeException("Product position must be 0 or 1 for " + mss.getName());
        }
    }
    if (structureNameSet.size() != 1) {
        throw new RuntimeException("The number of structures for " + s.getName() + " must be exactly 1.");
    }
    String structName = structureNameSet.iterator().next();
    return structName;
}
Also used : BNGSpeciesComponent(cbit.vcell.bionetgen.BNGSpeciesComponent) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) BNGMultiStateSpecies(cbit.vcell.bionetgen.BNGMultiStateSpecies) BNGComplexSpecies(cbit.vcell.bionetgen.BNGComplexSpecies) BNGSpecies(cbit.vcell.bionetgen.BNGSpecies)

Example 2 with BNGMultiStateSpecies

use of cbit.vcell.bionetgen.BNGMultiStateSpecies in project vcell by virtualcell.

the class BNGExecutorServiceMultipass method extractPolymerObservablesAsString.

private String extractPolymerObservablesAsString(String prefix, String sBngInputString) {
    if (polymerEqualObservables.isEmpty() && polymerGreaterObservables.isEmpty()) {
        return prefix;
    }
    String observablesString = "";
    List<BNGSpecies> bngSpeciesList = BNGOutputFileParser.createBngSpeciesOutputSpec(sBngInputString);
    // ordered by the network file index which is also key
    Map<Integer, Map<String, Integer>> masterSignaturesMap = new LinkedHashMap<>();
    // key = network file index, value = compartment of species
    Map<Integer, String> masterCompartmentMap = new LinkedHashMap<>();
    for (BNGSpecies species : bngSpeciesList) {
        List<BNGSpecies> ourList = new ArrayList<>();
        if (species instanceof BNGComplexSpecies) {
            ourList.addAll(Arrays.asList(species.parseBNGSpeciesName()));
        } else {
            // if it's a simple species to begin with we'll only have one element in this list
            ourList.add(species);
        }
        // key = name of molecules, value = number of occurrences
        Map<String, Integer> signatureMap = new HashMap<>();
        for (BNGSpecies mtp : ourList) {
            if (!(mtp instanceof BNGMultiStateSpecies)) {
                throw new RuntimeException("Species " + mtp.getName() + " must be instance of BNGMultiStateSpecies");
            }
            BNGMultiStateSpecies ss = (BNGMultiStateSpecies) mtp;
            String mtpName = ss.extractMolecularTypeName();
            if (signatureMap.containsKey(mtpName)) {
                int value = signatureMap.get(mtpName);
                value += 1;
                signatureMap.put(mtpName, value);
            } else {
                signatureMap.put(mtpName, 1);
            }
        }
        int networkFileIndex = species.getNetworkFileIndex();
        masterSignaturesMap.put(networkFileIndex, signatureMap);
        // we look in first mtp of this seed species, the compartment is the same in all
        BNGMultiStateSpecies ss = (BNGMultiStateSpecies) ourList.get(0);
        String compartment = ss.extractCompartment();
        if (compartment == null) {
            // single compartment, we get it from the model
            if (model.getStructures().length > 1) {
                throw new RuntimeException("BNGExecutorServiceMultipass: Unable to extract compartment");
            }
            compartment = model.getStructure(0).getName();
        }
        masterCompartmentMap.put(networkFileIndex, compartment);
    }
    // we need to find the next available index for observables
    int nextAvailableIndex;
    if (prefix != null) {
        nextAvailableIndex = extractNextAvailableIndexFromObservables(prefix);
    } else {
        nextAvailableIndex = 1;
    }
    // we assume this is an observable made of only 1 sp that contains only one mtp, ex:  A()=xx   where xx is number of occurrences of A
    for (RbmObservable oo : polymerEqualObservables) {
        // for each polymer observable, here we build the string with the network indexes of the species that satisfy the criteria
        String speciesFoundIndexes = "";
        String mtpName = oo.getSpeciesPatternList().get(0).getMolecularTypePatterns().get(0).getMolecularType().getDisplayName();
        int sequenceLength = oo.getSequenceLength();
        System.out.println(mtpName + "=" + sequenceLength);
        // comma counter
        int i = 0;
        boolean found = false;
        // check all seed species for those that satisfy this observable and build comma delimited string of network file indexes
        for (Map.Entry<Integer, Map<String, Integer>> entry : masterSignaturesMap.entrySet()) {
            Integer networkFileIndex = entry.getKey();
            // only interested if compartment is the same for both obs and seed species
            String compartment = masterCompartmentMap.get(networkFileIndex);
            if (!compartment.equals(oo.getStructure().getName())) {
                // seed species in other compartment than our observable
                continue;
            }
            Map<String, Integer> value = entry.getValue();
            if (value.containsKey(mtpName)) {
                int occurences = value.get(mtpName);
                if (sequenceLength == occurences) {
                    if (i > 0) {
                        speciesFoundIndexes += ",";
                    }
                    speciesFoundIndexes += networkFileIndex;
                    found = true;
                    i++;
                }
            }
        }
        if (found == false) {
            // desired number of occurrences for the polymer species has not been found in any seed species
            continue;
        }
        // finished for this observable
        System.out.println("Observable " + oo.getDisplayName() + ": " + mtpName + "()=" + sequenceLength + " found in species " + speciesFoundIndexes + ".");
        observablesString += "\t" + nextAvailableIndex + " " + oo.getDisplayName() + "\t" + speciesFoundIndexes + "\n";
        nextAvailableIndex++;
    }
    for (RbmObservable oo : polymerGreaterObservables) {
        // same as above for the A()>xx polymer observables
        String speciesFoundIndexes = "";
        String mtpName = oo.getSpeciesPatternList().get(0).getMolecularTypePatterns().get(0).getMolecularType().getDisplayName();
        int sequenceLength = oo.getSequenceLength();
        System.out.println(mtpName + "=" + sequenceLength);
        int i = 0;
        boolean found = false;
        // check all seed species for those that satisfy this observable and build comma delimited string of network file indexes
        for (Map.Entry<Integer, Map<String, Integer>> entry : masterSignaturesMap.entrySet()) {
            Integer networkFileIndex = entry.getKey();
            String compartment = masterCompartmentMap.get(networkFileIndex);
            if (!compartment.equals(oo.getStructure().getName())) {
                // seed species in other compartment than our observable
                continue;
            }
            Map<String, Integer> value = entry.getValue();
            if (value.containsKey(mtpName)) {
                int occurences = value.get(mtpName);
                if (sequenceLength < occurences) {
                    if (i > 0) {
                        speciesFoundIndexes += ",";
                    }
                    speciesFoundIndexes += networkFileIndex;
                    found = true;
                    i++;
                }
            }
        }
        if (found == false) {
            continue;
        }
        System.out.println("Observable " + oo.getDisplayName() + ": " + mtpName + "()>" + sequenceLength + " found in species " + speciesFoundIndexes + ".");
        observablesString += "\t" + nextAvailableIndex + " " + oo.getDisplayName() + "\t" + speciesFoundIndexes + "\n";
        nextAvailableIndex++;
    }
    if (observablesString.isEmpty()) {
        // may be null
        return prefix;
    }
    if (prefix != null) {
        observablesString = prefix + observablesString;
    }
    return observablesString;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RbmObservable(cbit.vcell.model.RbmObservable) ArrayList(java.util.ArrayList) BNGComplexSpecies(cbit.vcell.bionetgen.BNGComplexSpecies) LinkedHashMap(java.util.LinkedHashMap) BNGMultiStateSpecies(cbit.vcell.bionetgen.BNGMultiStateSpecies) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BNGSpecies(cbit.vcell.bionetgen.BNGSpecies)

Aggregations

BNGComplexSpecies (cbit.vcell.bionetgen.BNGComplexSpecies)2 BNGMultiStateSpecies (cbit.vcell.bionetgen.BNGMultiStateSpecies)2 BNGSpecies (cbit.vcell.bionetgen.BNGSpecies)2 ArrayList (java.util.ArrayList)2 BNGSpeciesComponent (cbit.vcell.bionetgen.BNGSpeciesComponent)1 RbmObservable (cbit.vcell.model.RbmObservable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1