It is interesting to observe the following crash of libSBML. I loaded two models into memory and tried the following (using libSBML 3.4 and Scientific Linux as OS):
// model 1: m1
// model 2: m2
for (i = 0; i < m1.getNumSpecies(); i++) {
Species s1 = m1.getSpecies(i);
Species s2 = findSpecies(m2, s1);
if (s2 == null) {
System.out.println("no mapping found for Species " + s1.getId());
} else {
System.out.println("mapping Species " + s1.getId() + " to " + s2.getId());
}
}
Please find the referenced methods below. Interestingly, this code snippet crashes if I change the if-clause as follows:
if (s2 != null) {
System.out.println("mapping Species " + s1.getId() + " to " + s2.getId());
} else {
System.out.println("no mapping found for Species " + s1.getId());
}
LibSBML then states an unexpected error occured and the program was terminated. It took a lot of time to identify the source of this error but I still do not know why this actually happens.
Cheers
Andreas
// The referenced methods:
public static Species findSpecies(Model m, Species s) {
for (int i = 0; i < m.getNumSpecies(); i++)
if (equals(m.getSpecies(i), s))
return m.getSpecies(i);
return null;
}
/**
* Returns true if both species have the identical id, the identical name,
* or if the MIRIAM annotation of both species is identical.
*
* @param r
* @param p
* @return
*/
public static boolean equals(Species r, Species p) {
if (r.getId().equals(p.getId())
|| (r.isSetName() && p.isSetName() && r.getName().equals(
p.getName())))
return true;
// annotation check
for (int i = 0; i < r.getNumCVTerms(); i++) {
CVTerm cvtR = r.getCVTerm(i);
boolean equal = false;
for (int j = 0; j < p.getNumCVTerms(); j++) {
CVTerm cvtP = p.getCVTerm(j);
// run over all
if (cvtR.getQualifierType() == cvtP.getQualifierType())
for (int k = 0; k < cvtR.getNumResources(); k++) {
for (int l = 0; l < cvtP.getNumResources(); l++)
if (cvtR.getResources().getValue(k).equals(
cvtP.getResources().getValue(l)))
equal = true;
}
}
if (equal)
return true;
}
return false;
}