void CChemEq::addElement(CCopasiVector < CChemEqElement > & structure, const CChemEqElement & element, CChemEq::MetaboliteRole role) { unsigned C_INT32 i; std::string key = element.getMetaboliteKey(); if (key == "") return; // don�t add empty element for (i = 0; i < structure.size(); i++) if (key == structure[i]->getMetaboliteKey()) break; if (i >= structure.size()) { CChemEqElement * Element = new CChemEqElement(element); if (role == CChemEq::SUBSTRATE) Element->setMultiplicity(- Element->getMultiplicity()); structure.add(Element, true); } else if (role == CChemEq::SUBSTRATE) structure[i]->addToMultiplicity(- element.getMultiplicity()); else structure[i]->addToMultiplicity(element.getMultiplicity()); }
void WriteDot(char *Title, CCopasiVector < CGene > &gene) { unsigned C_INT32 i; C_INT32 j; char strtmp[1024]; ofstream fout; sprintf(strtmp, "%s.dot", Title); fout.open(strtmp, ios::out); // dot file header fout << "digraph \"" << Title << "\" {\n"; fout << "\tgraph\n\t[\n"; fout << "\t\tcenter=\"true\"\n"; fout << "\t\toverlap=\"false\"\n"; fout << "\t\tDamping=0.999\n"; fout << "\t\tfontname=\"Helvetica\"\n"; fout << "\t\tmaxiter=1000000\n"; fout << "\t\tsplines=\"true\"\n"; fout << "\t\tsep=0.8\n"; fout << "\t\tepsilon=0.0000001\n"; fout << "\t\tlabel=\"" << Title << "\"\n"; fout << "\t\tratio=\"auto\"\n"; fout << "\t]\n\n"; fout << "\tnode\n\t[\n"; fout << "\t\tfontsize=9\n"; fout << "\t\tfontname=\"Helvetica-bold\"\n"; fout << "\t\tshape=\"circle\"\n"; fout << "\t\tstyle=\"bold\"\n"; fout << "\t]\n\n"; fout << "\tedge\n\t[\n"; fout << "\t\tfontsize=9\n"; fout << "\t\tfontname=\"Helvetica\"\n"; fout << "\t\tcolor=\"blue\"\n"; fout << "\t\tarrowhead=\"normal\"\n"; if (gene.size() < 15) fout << "\t\tstyle=\"bold\"\n"; fout << "\t\tlen=2.5\n"; fout << "\t]\n\n"; // graph description for (i = 0; i < gene.size(); i++) for (j = 0; j < gene[i]->getModifierNumber(); j++) { fout << "\t" << gene[i]->getModifier(j)->getName() << " -> " << gene[i]->getName() << endl; if (gene[i]->getModifierType(j) == 0) fout << "\t\t[arrowhead=\"tee\"\n\t\tcolor=\"red\"]" << endl; } fout << "\n}"; fout.close(); }
void WriteDistri(char *Title, CCopasiVector < CGene > &gene) { C_INT32 i, size; char strtmp[1024]; ofstream fout; vector <C_INT32> idegree, odegree, tdegree; size = gene.size(); // create a vector to count all the degrees // there are N or less degrees // (this could be more efficient on memory done in a different way...) idegree.resize(size + 1); odegree.resize(size + 1); tdegree.resize(2*(size + 1)); for (i = 0; i <= size; i++) { idegree[i] = 0; odegree[i] = 0; tdegree[i] = 0; } for (i = size; i < 2*(size + 1); i++) tdegree[i] = 0; // count over all genes for (i = 0; i < size; i++) { idegree[gene[i]->getInDegree()]++; odegree[gene[i]->getOutDegree()]++; tdegree[gene[i]->getTotalDegree()]++; } // now write the data in a gnuplot file sprintf(strtmp, "%s.distri.plt", Title); fout.open(strtmp, ios::out); // fout << "set data style linespoints" << endl; fout << "set linestyle 1 lw 2\nset linestyle 2 lw 2\nset linestyle 3 lw 2" << endl; fout << "set title \'degree distribution for " << Title << "'" << endl; fout << "set xlabel \'# of links\'" << endl; fout << "set ylabel \'comulative distribution\'" << endl; fout << "plot \'-\' t \'in-degree\' ls 1, \'-\' t \'out-degree\' ls 2, \'-\' t \'all-degree\' ls 3" << endl; for (i = 0; i <= size; i++) fout << i << "\t" << idegree[i] << endl; fout << "e" << endl; for (i = 0; i <= size; i++) fout << i << "\t" << odegree[i] << endl; fout << "e" << endl; for (i = 0; i <= size; i++) fout << i << "\t" << tdegree[i] << endl; fout << "e" << endl; fout.close(); }
// calculates the single source shortest-path for gene g // it also stores all the shortest distances in the vector dist C_INT32 SSSP(CCopasiVector < CGene > &gene, C_INT32 g, C_INT32 *dist) { C_INT32 i, j, n, u, cnt, depth; vector < C_INT32 > *distList, *nextList, nbrs; n = gene.size(); // set all distances to the maximum for (i = 0; i < n; i++) dist[i] = n; // check if valid gene index if (g >= n) return n; // distance to self is zero dist[g] = 0; cnt = 0; distList = new vector < C_INT32 >; distList->push_back(g); for (depth = 1; distList->size() > 0; depth++) { nextList = new vector < C_INT32 >; for (i = 0; i < distList->size(); i++) { // fill nbrs with the neighbors nbrs.clear(); for (j = 0; j < gene[(*distList)[i]]->getModifierNumber(); j++) nbrs.push_back(gene[(*distList)[i]]->getModifierIndex(j)); for (j = 0; j < nbrs.size(); j++) { u = nbrs[j]; if (dist[u] == n) { cnt++; dist[u] = depth; nextList->push_back(u); } } } delete distList; distList = nextList; } delete nextList; return cnt == n - 1 ? depth - 2 : n; }
void WritePajek(char *Title, CCopasiVector < CGene > &gene) { C_INT32 i, j, l, size; char strtmp[1024]; ofstream fout; size = gene.size(); sprintf(strtmp, "%s.net", Title); fout.open(strtmp, ios::out & ~ios::binary); fout << "*Network " << Title << endl; fout << "*Vertices " << size << endl; for (i = 0; i < size; i++) fout << i + 1 << " \"" << gene[i]->getName() << "\"" << endl; fout << "*Arcs" << endl; for (i = 0; i < size; i++) for (j = 0; j < gene[i]->getModifierNumber(); j++) for (l = 0; l < size; l++) if (gene[i]->getModifier(j) == gene[l]) fout << l + 1 << " " << i + 1 << " 1 c " << (gene[i]->getModifierType(j) == 0 ? "Red" : "Blue") << endl; fout.close(); }
void SliderDialog::removeSlider() { if (mpCurrSlider) { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); CCopasiVector<CSlider>* pSliderList = (*CCopasiRootContainer::getDatamodelList())[0]->getGUI()->getSliderList(); size_t i, maxCount = pSliderList->size(); for (i = 0; i < maxCount; ++i) { CSlider* pTmpSlider = (*pSliderList)[i]; if (pTmpSlider == mpCurrSlider->getCSlider()) { pSliderList->remove(i); break; } } deleteSlider(mpCurrSlider); mpCurrSlider = NULL; } }
bool CCopasiSimpleSelectionTree::isMetaboliteNameUnique(const std::string & name, const CCopasiVector<CMetab> & metabolites) { bool unique = true; bool found = false; unsigned int counter; for (counter = 0; counter < metabolites.size(); ++counter) { const std::string& thisName = metabolites[counter]->getObjectName(); if (name == thisName) { if (found) { unique = false; break; } found = true; } } return unique; }
void SliderDialog::removeSlider() { if (mpCurrSlider) { CCopasiDataModel * pDataModel = mpCurrSlider->getCSlider()->getObjectDataModel(); assert(pDataModel != NULL); CCopasiVector<CSlider>* pSliderList = pDataModel->getGUI()->getSliderList(); size_t i, maxCount = pSliderList->size(); for (i = 0; i < maxCount; ++i) { CSlider* pTmpSlider = &pSliderList->operator[](i); if (pTmpSlider == mpCurrSlider->getCSlider()) { pSliderList->remove(i); break; } } deleteSlider(mpCurrSlider); mpCurrSlider = NULL; } }
void GraphMetrics(CCopasiVector < CGene > &gene, C_INT32 tedges, char *Title) { C_INT32 i, j, k, l, n; CMatrix<C_FLOAT64> Adj; CEigen *eigen; C_FLOAT64 ssRes, *eigen_r, *eigen_i; C_INT32 **dist; ofstream fout; char fname[1024]; vector <C_FLOAT64> mpl, tpl, b; C_FLOAT64 cpl, cl, cll, cbl, acc, density, netcl, tmp, tmp2, netbc; C_INT32 idcenter, odcenter, clcenter, diameter, id, od, bcenter; // open the file for statistics sprintf(fname, "%s.netstat", Title); fout.open(fname, ios::out & ~ios::binary); n = gene.size(); density = (C_FLOAT64) tedges / (C_FLOAT64) (n * n); fout << "number of vertices\t" << n << endl; fout << "number of arcs\t" << tedges << endl; fout << "density\t" << density << endl; #ifdef XXXX // create the adjacency matrix Adj.newsize(n, n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) Adj[j][i] = 0.0; for (l = 0; l < gene[i]->getModifierNumber(); l++) { for (j = 0; j < n; j++) if (gene[i]->getModifier(l) == gene[j]) { Adj[j][i] = 1.0; // if(gene[i]->getModifierType(l) == 0) Adj[j][i] = -1.0; // else Adj[j][i] = 1.0; break; } } } // calculate its eigenvalues eigen = new CEigen(); ssRes = 1e-9; eigen->CalcEigenvalues(ssRes, Adj); eigen_r = eigen->getEigen_r(); eigen_i = eigen->getEigen_i(); // TODO: linear regression on positive real parts delete eigen; #endif if (n < LARGE_GRAPH) { // find the diameter diameter = 0; dist = new C_INT32 * [n]; for (i = 0; i < n; i++) { dist[i] = new C_INT32[n]; l = SSSP(gene, i, dist[i]); if (l > diameter) diameter = l; } fout << "diameter\t" << diameter << endl; // calculate total and mean path lengths mpl.reserve(n); for (i = 0; i < n; i++) { cpl = 0.0; for (j = 0; j < n; j++) cpl += dist[i][j]; cll = (C_FLOAT64) (n - 1) / cpl; cpl /= n - 1; mpl.push_back(cpl); tpl.push_back(cll); } } // find the degree centers for (id = od = idcenter = odcenter = i = 0; i < n; i++) { k = gene[i]->getOutDegree(); if (k > od) { odcenter = i; od = k; } k = gene[i]->getInDegree(); if (k > id) { idcenter = i; id = k; } } cl = (C_FLOAT64) id / (C_FLOAT64)(n - 1); fout << "in-degree center\t" << gene[idcenter]->getName() << endl; fout << "center's in-degree\t" << id << endl; fout << "center's relative in-degree\t" << cl << endl; cl = (C_FLOAT64) od / (C_FLOAT64)(n - 1); fout << "out-degree center\t" << gene[odcenter]->getName() << endl; fout << "center's out-degree\t" << od << endl; fout << "center's relative out-degree\t" << cl << endl; if (n < LARGE_GRAPH) { // find the closeness center, degree centralization, // and closeness centralization for (cll = 0.0, clcenter = -1, i = 0; i < n; i++) { if (tpl[i] > cll) { cll = tpl[i]; clcenter = i; } } for (netcl = 0.0, i = 0; i < n; i++) netcl += cll - tpl[i]; netcl /= ((C_FLOAT64)n - 1.0) * ((C_FLOAT64)n - 2.0) / (2.0 * (C_FLOAT64)n - 3.0); fout << "closeness center\t" << gene[clcenter]->getName() << endl; fout << "center's relative closeness\t" << cll << endl; fout << "network closeness centralization\t" << netcl << endl; // calculate betweeness centralities b.reserve(n); Betweeness(gene, b); // for (cbl = 0.0, bcenter = 0, i = 0; i < n; i++) { if (b[i] > cbl) { cbl = b[i]; bcenter = i; } } for (netbc = 0.0, i = 0; i < n; i++) netbc += cbl - b[i]; fout << "betweenness center\t" << gene[bcenter]->getName() << endl; fout << "center's relative betweenness\t" << cbl << endl; fout << "network betweenness centralization\t" << netbc << endl; // calculate characteristic path length sort(mpl.begin(), mpl.end()); if (n / 2 == 0) cpl = (mpl[n / 2] + mpl[1 + n / 2]) * 0.5; else cpl = mpl[n / 2]; fout << "characteristic path length\t" << cpl << endl; // calculate clustering coefficients for (acc = 0.0, i = 0; i < n; i++) { tmp = cluster(gene, i); acc += tmp; } acc /= n; fout << "average clustering coefficient\t" << acc << endl; } fout.close(); }
// calculates the betweeness centrality for each gene // uses the algorithm of Brandes (J Math Sociol 25, 163-177, 2001) void Betweeness(CCopasiVector < CGene > &gene, vector <C_FLOAT64> &b) { C_INT32 i, n, s, tot, v, w; vector < C_INT32 > sigma, d; vector < C_FLOAT64 > delta; stack < C_INT32 > S; queue < C_INT32 > Q; typedef list < C_INT32 > INTLIST; vector < INTLIST* > P; n = gene.size(); sigma.reserve(n); delta.reserve(n); d.reserve(n); P.reserve(n); for (i = 0; i < n; i++) P[i] = new INTLIST; // reset variables for (i = 0; i < n; i++) b[i] = 0.0; // iterate over all genes for (s = 0; s < n; s++) { // reset variables while (!S.empty()) S.pop(); while (!Q.empty()) Q.pop(); for (i = 0; i < n; i++) { sigma[i] = 0; d[i] = -1; if (!P[i]->empty()) P[i]->clear(); } sigma[s] = 1; d[s] = 1; // add s to queue Q.push(s); while (! Q.empty()) { v = Q.front(); Q.pop(); S.push(v); tot = gene[v]->getModifierNumber(); for (i = 0; i < tot; i++) { w = gene[v]->getModifierIndex(i); // w found for the first time? if (d[w] < 0) { Q.push(w); d[w] = d[v] + 1; } // shortest path to w via v? if (d[w] == d[v] + 1) { sigma[w] += sigma[v]; P[w]->push_back(v); } } } for (i = 0; i < n; i++) delta[i] = 0.0; // S returns vertices in order of non-increasing distance from s while (!S.empty()) { w = S.top(); S.pop(); INTLIST::iterator it; for (it = P[w]->begin(); it != P[w]->end(); it++) delta[*it] += sigma[*it] * (1 + delta[w]) / sigma[w]; if (w != s) { C_FLOAT64 tmp = delta[w]; b[w] += delta[w]; } } } for (i = 0; i < n; i++) { // make the measures relative b[i] /= ((C_FLOAT64)n - 1.0) * ((C_FLOAT64)n - 2.0); // delete the lists allocated delete P[i]; } }