Search in sources :

Example 6 with BNGOutputSpec

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

the class NetworkTransformer method generateNetwork.

private BNGOutputSpec generateNetwork(SimulationContext simContext, MathMappingCallback mathMappingCallback, NetworkGenerationRequirements networkGenerationRequirements) throws ClassNotFoundException, IOException {
    TaskCallbackMessage tcm;
    BNGOutputSpec outputSpec;
    speciesEquivalenceMap.clear();
    kineticsParameterMap.clear();
    String input = convertToBngl(simContext, true, mathMappingCallback, networkGenerationRequirements);
    for (Map.Entry<FakeSeedSpeciesInitialConditionsParameter, Pair<SpeciesContext, Expression>> entry : speciesEquivalenceMap.entrySet()) {
        FakeSeedSpeciesInitialConditionsParameter key = entry.getKey();
        Pair<SpeciesContext, Expression> value = entry.getValue();
        SpeciesContext sc = value.one;
        Expression initial = value.two;
        System.out.println("key: " + key.fakeParameterName + ",   species: " + sc.getName() + ", initial: " + initial.infix());
    }
    String md5hash = MD5.md5(input);
    if (isBngHashValid(input, md5hash, simContext)) {
        String s = "Previously saved outputSpec is up-to-date, no need to generate network.";
        System.out.println(s);
        // not an error, we just want to show it in red
        tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, s);
        simContext.appendToConsole(tcm);
        if (simContext.isInsufficientIterations()) {
            s = NetworkTransformer.getInsufficientIterationsMessage();
            System.out.println(s);
            tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, s);
            simContext.appendToConsole(tcm);
        }
        if (simContext.isInsufficientMaxMolecules()) {
            s = NetworkTransformer.getInsufficientMaxMoleculesMessage();
            System.out.println(s);
            tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, s);
            simContext.appendToConsole(tcm);
        }
        outputSpec = simContext.getMostRecentlyCreatedOutputSpec();
        return (BNGOutputSpec) BeanUtils.cloneSerializable(outputSpec);
    }
    BNGInput bngInput = new BNGInput(input);
    BNGOutput bngOutput = null;
    try {
        final BNGExecutorService bngService = BNGExecutorService.getInstance(bngInput, networkGenerationRequirements.timeoutDurationMS);
        bngService.registerBngUpdaterCallback(simContext);
        bngOutput = bngService.executeBNG();
    } catch (BNGException ex) {
        ex.printStackTrace(System.out);
        System.out.println("bionetgen exception");
        if (ex.getMessage().contains("was asked to write the network, but no reactions were found")) {
            RuntimeException rex = new RuntimeException("Specified species and reaction rules are not sufficient to define reaction network.");
            throw rex;
        } else {
            // rethrow without losing context
            throw ex;
        }
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.out);
        System.out.println("runtime exception");
        throw ex;
    } catch (Exception ex) {
        ex.printStackTrace(System.out);
        System.out.println("other exception");
        throw new RuntimeException(ex.getMessage());
    }
    // simContext.setInsufficientIterations(false);
    // simContext.setInsufficientMaxMolecules(false);
    String bngConsoleString = bngOutput.getConsoleOutput();
    // TODO: this message we check if insufficient iterations / max molecules
    // DO IT OUTSIDE (in the bng service), we now can
    // tcm = new TaskCallbackMessage(TaskCallbackStatus.DetailBatch, bngConsoleString);
    // simContext.appendToConsole(tcm);
    tcm = new TaskCallbackMessage(TaskCallbackStatus.TaskEndNotificationOnly, "");
    simContext.setNewCallbackMessage(tcm);
    tcm = new TaskCallbackMessage(TaskCallbackStatus.TaskEndAdjustSimulationContextFlagsOnly, "");
    simContext.setNewCallbackMessage(tcm);
    String bngNetString = bngOutput.getNetFileContent();
    outputSpec = BNGOutputFileParser.createBngOutputSpec(bngNetString);
    // prints all output to console
    BNGOutputFileParser.printBNGNetOutput(outputSpec);
    if (mathMappingCallback.isInterrupted()) {
        String msg = "Canceled by user.";
        tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, msg);
        simContext.appendToConsole(tcm);
        // clean the cache if the user interrupts
        simContext.setMd5hash(null);
        throw new UserCancelException(msg);
    }
    if (outputSpec.getBNGSpecies().length > NetworkTransformer.speciesLimit) {
        String message = NetworkTransformer.getSpeciesLimitExceededMessage(outputSpec);
        tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, message);
        simContext.appendToConsole(tcm);
        simContext.setMd5hash(null);
        message = "Unable to generate Math for Application " + simContext.getName() + ".\n" + message;
        throw new RuntimeException(message);
    }
    if (outputSpec.getBNGReactions().length > NetworkTransformer.reactionsLimit) {
        String message = NetworkTransformer.getReactionsLimitExceededMessage(outputSpec);
        tcm = new TaskCallbackMessage(TaskCallbackStatus.Error, message);
        simContext.appendToConsole(tcm);
        simContext.setMd5hash(null);
        message = "Unable to generate Math for Application " + simContext.getName() + ".\n" + message;
        throw new RuntimeException(message);
    }
    // System.out.println("old hash: " + simContext.getMd5hash());
    if (md5hash != null && md5hash.length() != 0 && outputSpec != null) {
        System.out.println("saving hash and output spec");
        synchronized (this) {
            simContext.setMd5hash(md5hash);
            simContext.setMostRecentlyCreatedOutputSpec(outputSpec);
        }
    } else {
        System.out.println("something is wrong with the hash and/or output spec");
    }
    return (BNGOutputSpec) BeanUtils.cloneSerializable(outputSpec);
}
Also used : UserCancelException(org.vcell.util.UserCancelException) SpeciesContext(cbit.vcell.model.SpeciesContext) BNGExecutorService(cbit.vcell.server.bionetgen.BNGExecutorService) BNGOutput(cbit.vcell.server.bionetgen.BNGOutput) FakeSeedSpeciesInitialConditionsParameter(org.vcell.model.rbm.FakeSeedSpeciesInitialConditionsParameter) BNGOutputSpec(cbit.vcell.bionetgen.BNGOutputSpec) PropertyVetoException(java.beans.PropertyVetoException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ParseException(org.vcell.model.bngl.ParseException) BNGException(cbit.vcell.server.bionetgen.BNGException) ModelException(cbit.vcell.model.ModelException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) UserCancelException(org.vcell.util.UserCancelException) BNGException(cbit.vcell.server.bionetgen.BNGException) Expression(cbit.vcell.parser.Expression) BNGInput(cbit.vcell.server.bionetgen.BNGInput) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Pair(org.vcell.util.Pair)

Example 7 with BNGOutputSpec

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

the class ClientTaskManager method copyApplication.

public static AsynchClientTask[] copyApplication(final JComponent requester, final BioModel bioModel, final SimulationContext simulationContext, final boolean bSpatial, final SimulationContext.Application appType) {
    // get valid application name
    String newApplicationName = null;
    String baseName = "Copy of " + simulationContext.getName();
    int count = 0;
    while (true) {
        if (count == 0) {
            newApplicationName = baseName;
        } else {
            newApplicationName = baseName + " " + count;
        }
        if (bioModel.getSimulationContext(newApplicationName) == null) {
            break;
        }
        count++;
    }
    final String newName = newApplicationName;
    AsynchClientTask task1 = new AsynchClientTask("preparing to copy", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            SimulationContext newSimulationContext = SimulationContext.copySimulationContext(simulationContext, newName, bSpatial, appType);
            newSimulationContext.getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT());
            if (newSimulationContext.isSameTypeAs(simulationContext)) {
                MathMappingCallback callback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
                String oldHash = simulationContext.getMd5hash();
                BNGOutputSpec oldSpec = simulationContext.getMostRecentlyCreatedOutputSpec();
                if (oldHash != null && oldSpec != null) {
                    // Warning: the results from "Edit / Test Constraints" are never cached because we want to repeatedly run it
                    // even if nothing changed
                    newSimulationContext.setMd5hash(oldHash);
                    newSimulationContext.setMostRecentlyCreatedOutputSpec(oldSpec);
                    newSimulationContext.setInsufficientIterations(simulationContext.isInsufficientIterations());
                    newSimulationContext.setInsufficientMaxMolecules(simulationContext.isInsufficientMaxMolecules());
                }
                newSimulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
            }
            hashTable.put("newSimulationContext", newSimulationContext);
        }
    };
    AsynchClientTask task2 = new AsynchClientTask("copying application and simulations", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            SimulationContext newSimulationContext = (SimulationContext) hashTable.get("newSimulationContext");
            bioModel.addSimulationContext(newSimulationContext);
            if (newSimulationContext.isSameTypeAs(simulationContext)) {
                // copy simulations to new simContext
                for (Simulation sim : simulationContext.getSimulations()) {
                    Simulation clonedSimulation = new Simulation(sim, false);
                    clonedSimulation.setMathDescription(newSimulationContext.getMathDescription());
                    clonedSimulation.setName(simulationContext.getBioModel().getFreeSimulationName());
                    newSimulationContext.addSimulation(clonedSimulation);
                }
                // copy output functions to new simContext
                ArrayList<AnnotatedFunction> outputFunctions = simulationContext.getOutputFunctionContext().getOutputFunctionsList();
                ArrayList<AnnotatedFunction> newOutputFunctions = new ArrayList<AnnotatedFunction>();
                for (AnnotatedFunction afn : outputFunctions) {
                    newOutputFunctions.add(new AnnotatedFunction(afn));
                }
                newSimulationContext.getOutputFunctionContext().setOutputFunctions(newOutputFunctions);
            } else {
                if (simulationContext.getSimulations().length > 0) {
                    DialogUtils.showWarningDialog(requester, "Simulations are not copied because new application is of different type.");
                }
            }
        }
    };
    return new AsynchClientTask[] { task1, task2 };
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) BNGOutputSpec(cbit.vcell.bionetgen.BNGOutputSpec) GeometryThumbnailImageFactoryAWT(cbit.vcell.geometry.GeometryThumbnailImageFactoryAWT) Simulation(cbit.vcell.solver.Simulation) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 8 with BNGOutputSpec

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

the class BNGExecutorServiceMultipass method doWork.

private CorrectedSR doWork(String oldSpeciesString, String newNetFile) throws ParseException {
    // TODO: make a map to preserve the ancestry of each generated species (rule and iteration that generated it)
    // each species may be generated multiple times, by different rules, at different iterations
    // the key should be the normalized (sorted lexicographically) expression of each species
    // parse the .net file with BNGOutputFileParser
    // seed species at the beginning of the current iteration
    List<BNGSpecies> oldSpeciesList = BNGOutputFileParser.createBngSpeciesOutputSpec(oldSpeciesString);
    // same as above, used internally, as we add new species from the current iteration we put them here;
    // used to speed up the verifications and reduce the isomorphism calls together with the isomorphismSignaturesMap
    Map<String, BNGSpecies> seedSpeciesMap = BNGOutputFileParser.createBngSpeciesSignatureMap(oldSpeciesString);
    // .net file content generated during current iteration
    BNGOutputSpec workSpec = BNGOutputFileParser.createBngOutputSpec(newNetFile);
    // we build here the list of valid (perhaps even corrected) NEW species
    List<BNGSpecies> newSpeciesList = new ArrayList<>();
    // we build here the list of flattened reactions (corrected at need, if the species were corrected)
    ArrayList<BNGReaction> newReactionsList = new ArrayList<BNGReaction>(Arrays.asList(workSpec.getBNGReactions()));
    // ArrayList<ObservableGroup> newObservablesList = new ArrayList<ObservableGroup>();		// here we put all the observables after correction
    // ArrayList<ObservableGroup> newObservablesList = new ArrayList<ObservableGroup>(Arrays.asList(workSpec.getObservableGroups()));
    // identify new products, loop thorough each of them
    Pair<List<BNGSpecies>, List<BNGSpecies>> p = BNGSpecies.diff(oldSpeciesList, workSpec.getBNGSpecies());
    List<BNGSpecies> removed = p.one;
    // after possible needed corrections we may end up with more or less new species for this iteration
    List<BNGSpecies> added = p.two;
    if (!removed.isEmpty()) {
        // this may actually be fired if the BNGSpecies.equals() fails for some reason
        throw new RuntimeException("No existing BNGSpecies may be removed: \n" + removed.get(0));
    }
    if (added.isEmpty()) {
        System.out.println("No new species added this iteration. Finished!");
    }
    // in this map we store which of the indexes of species generated during the current iteration
    // were converted into what after correction
    // an index may exist already in which case we won't save a duplicate or the same new species (that needs repair)
    // may result in more new species after being fixed
    // param1: original index of the newly created species before repairing it
    // param2: list of the indexes of the species resulting from the original after repairing
    Map<String, List<String>> indexesMap = new LinkedHashMap<>();
    // some reactions may end up as identity reactions after corrections caused by anchoring
    // for instance a transport xA -> yA may become xA -> xA after correction if A is anchored to x
    // we make a list of these and get rid of them at the end
    Set<BNGReaction> identityReactions = new HashSet<>();
    // as we validate and we add new species, we use this index to set their network index
    // we can't get the indexes given to the newly generated species for granted, during correction we may
    // lose or gain species
    int firstAvailableIndex = BNGOutputSpec.getFirstAvailableSpeciesIndex(oldSpeciesList);
    int summaryCreated = added.size();
    int summaryRepaired = 0;
    int summaryExisted = 0;
    // species we're adding at the end of this iteration, after repairing and checking for existing
    int summarySpecies = 0;
    for (BNGSpecies s : added) {
        // find the flattened reaction and from the reaction find the rule it's coming from
        for (BNGReaction r : newReactionsList) {
            // console only message
            String message = "";
            // array list with position of this species in the product (may be more than 1)
            List<Integer> reactionProductPositions = r.findProductPositions(s);
            if (reactionProductPositions.isEmpty()) {
                // this species is not a product of this reaction, nothing to do
                continue;
            }
            String ruleName = r.getRuleName();
            if (r.isRuleReversed()) {
                throw new RuntimeException("Reaction " + ruleName + " cannot be 'reverse'.");
            }
            // System.out.println("Species " + s + " found in rule " + r.getRuleName() + ", at index " + position);
            // check the product against the rule to see if it's valid
            // sanity check: only "transport" rules can give incorrect products, any rule with all participants in the same
            // compartment should only give valid products (is that so?)
            String structureName;
            if (model.getStructures().length == 1) {
                structureName = model.getStructure(0).getName();
            } else {
                // this is the structure where the product should be, according to the rule
                structureName = findRuleProductCompartment(s);
            }
            ReactionRule rr = model.getRbmModelContainer().getReactionRule(ruleName);
            // TODO: the code below may be greatly simplified using the more advanced BNGSpecies classes instead of using the strings
            // 'one' is the list of all the compartments mentioned in this product; for single compartment models 'one' will always be 1
            // and thus we'll never attempt to correct anything
            // 'two' is the BNGSpecies string with the compartment info extracted (that is, the AAA sites extracted)
            Pair<List<String>, String> pair = RbmUtils.extractCompartment(s.getName());
            boolean needsRepairing = false;
            if (pair.one.size() > 1) {
                // System.out.println(s.getName() + " multiple compartments, needs repairing.");
                message += s.getName() + " needs repairing... ";
                needsRepairing = true;
                /*	At this point we have the 'probable' structure of this product, extracted from the rule (structureName)
					However, we need to verify against the lists of anchors to make sure is not excluded from the possible list of anchors
					for any of the molecules and choose a compatible structure name, as follows:

					compartments x y z
					molecules A B C		A comes as transport
					rule says that this product belongs in x
					cases:
					1. A~y	 xB.xC.yA ->  yB.yC.yA	- can't use x as the rule says because A must be anchored to y and that takes precedence
													- if B or C can't be in y throw exception, our anchors are too restrictive

					2. A~x~y xB.xC.yA ->  xB.xC.xA	- correct to what the rule wants since x is acceptable as anchor for A

					3. A~z	 xB.xC.yA ->  !!!		- impossible, there is no reactant such that A can get in y (throw exception)
					4. A~x~z xB.xC.yA ->  !!!		- same as above

					
					step 1. look for exceptions: make sets with the compartments for each molecule in the species and see if 
							they all are allowed by the anchor rules (if not, we are in cases 3 or 4 - throw exception
					step 2. see if structureName is acceptable for all anchored molecules - if yes, use it
					step 3. intersect all sets from step 1
						 3.1 - if there's 1 compartment that is acceptable for all, use it
						 3.2 - if there's no compartment then we can't put the species anywhere - throw exception
						 3.3 - if there's more than 1 compartment, it's ambiguous - throw exception
				*/
                // ex of one entry: key T,  value  mem, cyt
                Map<String, Set<String>> speciesAnchorMap = extractSpeciesAnchorMap(s);
                // sanity check, step 1
                for (Map.Entry<String, Set<String>> entry : speciesAnchorMap.entrySet()) {
                    // molecule in the newly generated species (product)
                    String molecule = entry.getKey();
                    if (!anchorsMap.containsKey(molecule)) {
                        // this molecule can be anywhere, it's not anchored - nothing to verify
                        continue;
                    }
                    // 1 or more location where we try to put it (before correction may be more than 1)
                    Set<String> structures = entry.getValue();
                    // these structures MUST all be allowed by the anchor rule, otherwise it would mean they are coming from a reactant that cannot exist (case 3 or 4 above)
                    for (String wantedStructure : structures) {
                        Set<String> allowedStructures = anchorsMap.get(molecule);
                        if (!allowedStructures.contains(wantedStructure)) {
                            // should never happen: no reactant and no rule should have placed this molecule in this structure
                            throw new RuntimeException("Error in " + s.getName() + "\nMolecule " + molecule + " cannot possibly be in Structure " + wantedStructure);
                        }
                    }
                }
                // step 2
                boolean isCandidateStructureAcceptable = true;
                for (Map.Entry<String, Set<String>> entry : speciesAnchorMap.entrySet()) {
                    // molecule in the newly generated species (product)
                    String molecule = entry.getKey();
                    if (!anchorsMap.containsKey(molecule)) {
                        // this molecule can be anywhere, it's not anchored - nothing to verify
                        continue;
                    }
                    Set<String> allowedStructures = anchorsMap.get(molecule);
                    if (!allowedStructures.contains(structureName)) {
                        // the structure 'wanted' by the rule for this product is not acceptable because of some anchor rule
                        isCandidateStructureAcceptable = false;
                        break;
                    }
                }
                // step 3
                if (isCandidateStructureAcceptable == false) {
                    // we try to find one and only one structure that would satisfy all anchored molecules of this species
                    TreeSet<String> intersection = new TreeSet<>();
                    for (Structure str : model.getStructures()) {
                        // we start by adding all structures in the model
                        intersection.add(str.getName());
                    }
                    for (Map.Entry<String, Set<String>> entry : speciesAnchorMap.entrySet()) {
                        // molecule in the newly generated species (product)
                        String molecule = entry.getKey();
                        if (!anchorsMap.containsKey(molecule)) {
                            // this molecule can be anywhere, it's not anchored - nothing to verify
                            continue;
                        }
                        // we already know from step 1 that all structures here are acceptable by the anchor
                        Set<String> structures = anchorsMap.get(molecule);
                        // intersection retains only the elements in 'structures'
                        intersection.retainAll(structures);
                    }
                    if (intersection.size() == 0) {
                        // no structure satisfies the anchor rules for the molecules in this species
                        throw new RuntimeException("Error in " + s.getName() + "\nThe Structures allowed for anchored molecules are mutually exclusive.");
                    } else if (intersection.size() > 1) {
                        // ambiguous, don't know which compartment to pick
                        throw new RuntimeException("Error in " + s.getName() + "\nMultiple Structures allowed by the anchor rules, don't know which to choose.");
                    } else {
                        // found one single structure everybody is happy about
                        structureName = intersection.first();
                    }
                }
            } else {
                // no transport caused by a 'wild card', just direct rule application (no compartment ambiguity, nothing to correct)
                String structure;
                if (model.getStructures().length == 1) {
                    structure = model.getStructure(0).getName();
                } else {
                    structure = pair.one.get(0);
                }
                if (!structure.equals(structureName)) {
                    // the rule (no transport caused by '?' is possible)
                    throw new RuntimeException("Error in " + s.getName() + "\nIf one single structure is present in the species it must match the structure of the rule product");
                }
            // product structure (from rule) should not conflict with the anchor - we should have error issue in the rule itself reporting the conflict
            // TODO: check here too anyway, just in case?
            }
            // new generated species that we may add the list of species for the next iteration
            BNGSpecies candidate;
            // deleted or added previously during earlier iterations through newly created species
            if (needsRepairing) {
                // if not valid, correct the fake "compartment" site to be conform to the compartment of
                // the product pattern
                String speciesRepairedName = RbmUtils.repairCompartment(s.getName(), structureName);
                speciesRepairedName = RbmUtils.resetProductIndex(speciesRepairedName);
                candidate = new BNGComplexSpecies(speciesRepairedName, s.getConcentration(), firstAvailableIndex);
                // System.out.println(candidate.getName() + " repaired!");
                message += "repaired from rule " + rr.getDisplayName() + "... ";
                summaryRepaired++;
                System.out.println("");
            } else {
                String speciesName = s.getName();
                speciesName = RbmUtils.resetProductIndex(speciesName);
                candidate = new BNGComplexSpecies(speciesName, s.getConcentration(), firstAvailableIndex);
            }
            // At this point we have a valid candidate - but we may not need it if it already exist in the list of old seed species or in the list of
            // new seed species we're building now (it may exist in either if correction took place)
            // 
            // We correct the reaction to match the networkFileIndex of this species (useless activity except for the last iteration
            // when we want the valid list of flattened reactions
            // 
            // we set this to an existing species if the candidate matches it (directly or through isomorphism)
            BNGSpecies existingMatch = null;
            long st = System.currentTimeMillis();
            String sig = BNGSpecies.getShortSignature(candidate, sigDetailLevel);
            if (!shortSignaturesSet.contains(sig)) {
            // our candidate has a new short signature, this means it is certainly not among the existing species
            // no point in doing any other search to find it
            } else {
                // short signature match, now we check if the full species exists or is isomorphic with an existing one
                String isomorphSpeciesName = isomorphismSignaturesMap.get(candidate.getName());
                if (isomorphSpeciesName != null) {
                    // name of an existing species that is isomorphic with our candidate
                    // we recover the existing species and will use it instead of the candidate
                    existingMatch = seedSpeciesMap.get(isomorphSpeciesName);
                    if (existingMatch == null) {
                        throw new RuntimeException("Unable to find an isomorph BNGSpecies named " + isomorphSpeciesName + " that should have existed.");
                    }
                } else {
                    // not present among the existing species or their known isomorphisms
                    // try to find an isomorphism the hard way
                    BNGSpecies existingMatchInNew = BNGOutputSpec.findMatch(candidate, newSpeciesList, sigDetailLevel);
                    if (existingMatchInNew != null) {
                        isomorphismSignaturesMap.put(candidate.getName(), existingMatchInNew.getName());
                        existingMatch = existingMatchInNew;
                    } else {
                        BNGSpecies existingMatchInOld = BNGOutputSpec.findMatch(candidate, oldSpeciesList, sigDetailLevel);
                        if (existingMatchInOld != null) {
                            isomorphismSignaturesMap.put(candidate.getName(), existingMatchInOld.getName());
                            existingMatch = existingMatchInOld;
                        }
                    }
                }
            }
            long et = System.currentTimeMillis();
            eltIsomorph += (et - st);
            // if it doesn't exist we add it to the list of seed species we are preparing for the next iteration
            if (existingMatch == null) {
                newSpeciesList.add(candidate);
                message += "Candidate " + candidate.getName() + " added to the seed species list.";
                summarySpecies++;
                firstAvailableIndex++;
                for (int reactionProductPosition : reactionProductPositions) {
                    // correct the reaction
                    r.getProducts()[reactionProductPosition] = candidate;
                }
                manageIndexesMap(indexesMap, s, candidate);
                seedSpeciesMap.put(candidate.getName(), candidate);
                // put self in the list of signature map too
                isomorphismSignaturesMap.put(candidate.getName(), candidate.getName());
                String signature = BNGSpecies.getShortSignature(candidate, sigDetailLevel);
                shortSignaturesSet.add(sig);
            } else {
                message += "Candidate " + candidate.getName() + " already exists, not added.";
                summaryExisted++;
                for (int reactionProductPosition : reactionProductPositions) {
                    // correct the reaction
                    r.getProducts()[reactionProductPosition] = existingMatch;
                }
                manageIndexesMap(indexesMap, s, existingMatch);
            }
            if (needsRepairing && existingMatch != null) {
                if (isIdentityReaction(r)) {
                    // System.out.println("Identity reaction detected after product correction: " + r);
                    identityReactions.add(r);
                }
            }
        }
    // end checking reactions for this species
    }
    // we'll get rid of these
    if (!identityReactions.isEmpty()) {
        // we found some
        newReactionsList.removeAll(identityReactions);
    }
    // System.out.println("------------- Finished checking newly generated species for this iteration. Summary:");
    System.out.println("   Added " + newSpeciesList.size() + " new species");
    System.out.println(" ");
    // String summary = "   " + summaryCreated + " species were created.\n";
    // summary += "   " + summaryRepaired + " species needed repairing.\n";
    // summary += "   " + summaryExisted + " species already present, not added to the seed species list.\n";
    // summary += "   " + summarySpecies + " new species added.\n";
    // consoleNotification(summary);
    // total number of species at this moment
    currentIterationTotalSpecies = previousIterationTotalSpecies + summarySpecies;
    String message = previousIterationTotalSpecies + "," + currentIterationTotalSpecies + "," + needAdjustMaxMolecules;
    TaskCallbackMessage newCallbackMessage = new TaskCallbackMessage(TaskCallbackStatus.AdjustAllFlags, message);
    broadcastCallbackMessage(newCallbackMessage);
    previousIterationTotalSpecies = currentIterationTotalSpecies;
    CorrectedSR sr = new CorrectedSR(newSpeciesList, newReactionsList);
    return sr;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) BNGOutputSpec(cbit.vcell.bionetgen.BNGOutputSpec) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) Structure(cbit.vcell.model.Structure) HashSet(java.util.HashSet) TaskCallbackMessage(cbit.vcell.mapping.TaskCallbackMessage) ReactionRule(cbit.vcell.model.ReactionRule) BNGComplexSpecies(cbit.vcell.bionetgen.BNGComplexSpecies) BNGReaction(cbit.vcell.bionetgen.BNGReaction) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BNGSpecies(cbit.vcell.bionetgen.BNGSpecies)

Aggregations

BNGOutputSpec (cbit.vcell.bionetgen.BNGOutputSpec)8 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ModelException (cbit.vcell.model.ModelException)3 SpeciesContext (cbit.vcell.model.SpeciesContext)3 Expression (cbit.vcell.parser.Expression)3 ExpressionException (cbit.vcell.parser.ExpressionException)3 BNGOutput (cbit.vcell.server.bionetgen.BNGOutput)3 PropertyVetoException (java.beans.PropertyVetoException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 FakeSeedSpeciesInitialConditionsParameter (org.vcell.model.rbm.FakeSeedSpeciesInitialConditionsParameter)3 BNGReaction (cbit.vcell.bionetgen.BNGReaction)2 BNGSpecies (cbit.vcell.bionetgen.BNGSpecies)2 TaskCallbackMessage (cbit.vcell.mapping.TaskCallbackMessage)2 ReactionRule (cbit.vcell.model.ReactionRule)2 Structure (cbit.vcell.model.Structure)2 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)2 BNGExecutorService (cbit.vcell.server.bionetgen.BNGExecutorService)2