use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class AnnotationMapping method annotation2BioPaxObject.
public String annotation2BioPaxObject(BioModel bioModel, Identifiable identifiable) {
String name = "";
String type = "";
ArrayList<String> componentInfo = getComponentInfo(bioModel, identifiable);
if (componentInfo.size() > 0) {
name = componentInfo.get(0);
type = componentInfo.get(1);
}
String info = type + " : " + name;
HashMap<String, String> refInfo = getRefInfo(bioModel, identifiable);
// otherwise, create object and add link
if (type.equals(VCID.CLASS_SPECIES)) {
// lookup name by speciesContext
ArrayList<SpeciesContext> speciesContextArrList = new ArrayList<SpeciesContext>(Arrays.asList(new SpeciesContext[] { bioModel.getModel().getSpeciesContext(name) }));
if (speciesContextArrList.get(0) == null) {
speciesContextArrList.clear();
// lookup name by species
for (int i = 0; i < bioModel.getModel().getSpeciesContexts().length; i++) {
if (bioModel.getModel().getSpeciesContexts()[i].getSpecies().getCommonName().equals(name)) {
speciesContextArrList.add(bioModel.getModel().getSpeciesContexts()[i]);
}
}
}
for (int i = 0; i < speciesContextArrList.size(); i++) {
SpeciesContext speciesContext = speciesContextArrList.get(i);
if (bioModel.getRelationshipModel().getRelationshipObjects(speciesContext).size() == 0) {
ArrayList<Xref> xRef = getXrefs(bioModel, refInfo);
ArrayList<String> refName = getNameRef(xRef, name);
BioPaxObject bpObject = bioModel.getPathwayModel().findFromNameAndType(refName.get(0), EntityImpl.TYPE_PHYSICALENTITY);
if (bpObject == null) {
bpObject = createPhysicalEntity(xRef, refName, name);
bioModel.getPathwayModel().add(bpObject);
}
if (!isLinked(bioModel, bpObject, speciesContext)) {
// create linkage
RelationshipObject newRelationship = new RelationshipObject(speciesContext, bpObject);
bioModel.getRelationshipModel().addRelationshipObject(newRelationship);
return info;
}
}
}
} else if (type.equals(VCID.CLASS_BIOMODEL)) {
// BioPaxObject bpObject = bioModel.getPathwayModel().findFromName(refName.get(0), "Pathway");
// if(bpObject == null){
// bpObject = createPathway(componentInfo, refInfo);
// bioModel.getPathwayModel().add(bpObject);
// return info;
// }
} else if (type.equals(VCID.CLASS_REACTION_STEP)) {
ReactionStep reactionStep = bioModel.getModel().getReactionStep(name);
ArrayList<Xref> xRef = getXrefs(bioModel, refInfo);
ArrayList<String> refName = getNameRef(xRef, name);
if (bioModel.getRelationshipModel().getRelationshipObjects(reactionStep).size() == 0) {
BioPaxObject bpObject = bioModel.getPathwayModel().findFromNameAndType(refName.get(0), EntityImpl.TYPE_INTERACTION);
if (bpObject == null) {
bpObject = createInteraction(reactionStep, xRef, refName);
bioModel.getPathwayModel().add(bpObject);
}
if (!isLinked(bioModel, bpObject, reactionStep)) {
// create linkage
RelationshipObject newRelationship = new RelationshipObject(reactionStep, bpObject);
bioModel.getRelationshipModel().addRelationshipObject(newRelationship);
return info;
}
}
}
return null;
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class RbmNetworkGenerator method generateModel.
public static void generateModel(BioModel bioModel, String netfile) throws Exception {
Model model = bioModel.getModel();
Map<String, SpeciesContext> speciesMap = new HashMap<String, SpeciesContext>();
Map<String, ReactionStep> reactionMap = new HashMap<String, ReactionStep>();
List<ReactionLine> reactionLineList = new ArrayList<ReactionLine>();
BufferedReader br = new BufferedReader(new StringReader(netfile));
int reversibleCount = 0;
int reactionCount = 0;
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
line = line.trim();
if (line.equals(BEGIN_PARAMETERS)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_PARAMETERS)) {
break;
}
StringTokenizer st = new StringTokenizer(line2);
String token1 = st.nextToken();
String token2 = st.nextToken();
String token3 = st.nextToken();
ModelParameter mp = model.new ModelParameter(token2, new Expression(token3), Model.ROLE_UserDefined, bioModel.getModel().getUnitSystem().getInstance_TBD());
model.addModelParameter(mp);
}
} else if (line.equals(BEGIN_SPECIES)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_SPECIES)) {
break;
}
StringTokenizer st = new StringTokenizer(line2);
// no
String token1 = st.nextToken();
// pattern
String token2 = st.nextToken();
// initial condition
String token3 = st.nextToken();
String newname = token2.replaceAll("\\.", "_");
newname = newname.replaceAll("[\\(,][a-zA-Z]\\w*", "");
newname = newname.replaceAll("~|!\\d*", "");
newname = newname.replaceAll("\\(\\)", "");
newname = newname.replaceAll("\\)", "");
SpeciesContext sc = model.createSpeciesContext(model.getStructure(0));
sc.setName(newname);
bioModel.getVCMetaData().setFreeTextAnnotation(sc, token2);
bioModel.getVCMetaData().setFreeTextAnnotation(sc.getSpecies(), token2);
speciesMap.put(token1, sc);
}
} else if (line.equals(BEGIN_REACTIONS)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_REACTIONS)) {
break;
}
++reactionCount;
StringTokenizer st = new StringTokenizer(line2);
String token1 = st.nextToken();
// reactants
String token2 = st.nextToken();
// products
String token3 = st.nextToken();
// rate
String token4 = st.nextToken();
String token5 = st.nextToken();
boolean bFoundReversible = false;
Expression rate = new Expression(token4);
for (ReactionLine rl : reactionLineList) {
if (token2.equals(rl.products) && token3.equals(rl.reactants) && token5.equals(rl.ruleLabel + "r")) {
ReactionStep rs = reactionMap.get(rl.no);
((MassActionKinetics) rs.getKinetics()).getReverseRateParameter().setExpression(rate);
reactionLineList.remove(rl);
bFoundReversible = true;
break;
}
}
if (bFoundReversible) {
++reversibleCount;
continue;
}
ReactionLine rl = new ReactionLine(token1, token2, token3, token5);
reactionLineList.add(rl);
SimpleReaction reaction = model.createSimpleReaction(model.getStructure(0));
reactionMap.put(token1, reaction);
reaction.setModel(model);
bioModel.getVCMetaData().setFreeTextAnnotation(reaction, line2);
MassActionKinetics kinetics = new MassActionKinetics(reaction);
reaction.setKinetics(kinetics);
st = new StringTokenizer(token2, ",");
while (st.hasMoreTokens()) {
String t = st.nextToken();
SpeciesContext sc = speciesMap.get(t);
if (sc != null) {
boolean bExists = false;
for (ReactionParticipant rp : reaction.getReactionParticipants()) {
if (rp instanceof Reactant && rp.getSpeciesContext() == sc) {
rp.setStoichiometry(rp.getStoichiometry() + 1);
bExists = true;
break;
}
}
if (!bExists) {
reaction.addReactant(sc, 1);
}
}
}
st = new StringTokenizer(token3, ",");
while (st.hasMoreTokens()) {
String t = st.nextToken();
SpeciesContext sc = speciesMap.get(t);
if (sc != null) {
boolean bExists = false;
for (ReactionParticipant rp : reaction.getReactionParticipants()) {
if (rp instanceof Product && rp.getSpeciesContext() == sc) {
rp.setStoichiometry(rp.getStoichiometry() + 1);
bExists = true;
break;
}
}
if (!bExists) {
reaction.addProduct(sc, 1);
}
}
}
kinetics.getForwardRateParameter().setExpression(rate);
}
}
}
System.out.println(model.getNumSpecies() + " species added");
System.out.println(model.getNumReactions() + " reactions added");
System.out.println(reversibleCount + " reversible reactions found");
if (reactionCount != model.getNumReactions() + reversibleCount) {
throw new RuntimeException("Reactions are not imported correctly!");
}
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class RbmNetworkGenerator method writeSpeciesSortedAlphabetically.
public static void writeSpeciesSortedAlphabetically(PrintWriter writer, Model model, SimulationContext simulationContext, CompartmentMode compartmentMode) {
writer.println(BEGIN_SPECIES);
SpeciesContext[] speciesContexts = model.getSpeciesContexts();
int i = 1;
for (SpeciesContext sc : speciesContexts) {
if (!sc.hasSpeciesPattern()) {
continue;
}
writer.println(i + " " + RbmUtils.toBnglStringSortedAlphabetically(simulationContext, sc, compartmentMode));
i++;
}
writer.println(END_SPECIES);
writer.println();
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class RbmNetworkGenerator method writeSpecies.
public static void writeSpecies(PrintWriter writer, Model model, SimulationContext simulationContext, CompartmentMode compartmentMode) {
writer.println(BEGIN_SPECIES);
SpeciesContext[] speciesContexts = model.getSpeciesContexts();
int i = 1;
for (SpeciesContext sc : speciesContexts) {
if (!sc.hasSpeciesPattern()) {
continue;
}
// writer.println("@" + sc.getStructure().getName() + ":" + RbmUtils.toBnglString(simulationContext, sc, compartmentMode));
writer.println(i + " " + RbmUtils.toBnglString(simulationContext, sc, compartmentMode));
i++;
}
writer.println(END_SPECIES);
writer.println();
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class RbmUtils method findStructure.
// not used; instead of this we should recover the structure of the rule that produced this reaction
@Deprecated
public static Structure findStructure(Model model, HashMap<Integer, String> speciesMap, BNGReaction forwardBNGReaction) {
Structure ours = null;
for (int j = 0; j < forwardBNGReaction.getReactants().length; j++) {
BNGSpecies s = forwardBNGReaction.getReactants()[j];
String scName = speciesMap.get(s.getNetworkFileIndex());
SpeciesContext sc = model.getSpeciesContext(scName);
Structure theirs = sc.getStructure();
if (ours == null || ours.getDimension() > theirs.getDimension()) {
ours = theirs;
}
}
for (int j = 0; j < forwardBNGReaction.getProducts().length; j++) {
BNGSpecies s = forwardBNGReaction.getProducts()[j];
String scName = speciesMap.get(s.getNetworkFileIndex());
SpeciesContext sc = model.getSpeciesContext(scName);
Structure theirs = sc.getStructure();
if (ours == null || ours.getDimension() > theirs.getDimension()) {
ours = theirs;
}
}
if (ours == null) {
ours = model.getStructure(0);
}
return ours;
}
Aggregations