#include #include #include "intset.h" #include "mmapfile.c" #include "readpairs.c" #include "writegraph.c" #define MaxCliqSz (1<<13) #define Symmetrize(op,m,n,i,j) { \ op (BitMatrixRow ((m), (n), (i)), (j)); \ if (!directed) op (BitMatrixRow ((m), (n), (j)), (i)); \ } void usage () { fprintf (stderr, "\ args: [-lnbpR] [-d] [-s #] {adj-list|signed-mtx|num-mtx} []\n\ output: bit matrix\n\ input format spec:\n\ -l list of pairs of names [separated by any of \"#$%&\'():;,]\n\ -n list of pairs of pairs of names, the first element of which is relevant\n\ -b a matrix of \'0\'s and \'1\'s [separated by or ]\n\ -p a matrix of negative and positive 1-digit numbers\n\ -R list of cliques in RNSC output format\n\ -d directed graph\n\ -s # skip the first # lines\n\ in the cases of matrix format, the file name is needed\n"); exit (1); } main (int argc, char **argv) { BitMatrix graph; char *start, *c, *be, ch, *buf; struct stat st; FILE *f; int arg = 1, mode = 0, directed = 0, skip = 0, trace = 0, i, j, k, l, n; while (arg < argc && argv[arg][0] == '-') { switch (argv[arg++][1]) { case 'D': trace = 1; break; case 'd': directed = 1; break; case 'l': mode = 0; break; case 'n': mode = 1; break; case 'b': mode = 2; break; case 'p': mode = 3; break; case 'R': mode = 4; break; case 's': if (arg >= argc || sscanf (argv[arg++], "%d", &skip) != 1 || skip < 0) usage (); break; default: usage (); } } if (arg >= argc) usage (); c = start = mmapFile (f = openFile (argv[arg++]), &st); be = start + st.st_size; for (i = 0; i < skip && c < be; ++i) while (*c++ != '\n'); switch (mode) { case 0: case 1: { while (c < be) { i = getNameIndex (&c, '-'); if (mode) getNameIndex (&c, '-'); j = getNameIndex (&c, '-'); if (mode) getNameIndex (&c, '-'); while (*c++ != '\n'); } n = nNodes; if (trace) fprintf (stderr, "%d nodes seen\n", n); graph = NewEmptyBitMatrix (n, n); for (c = start, i = 0; i < skip && c < be; ++i) while (*c++ != '\n'); int nEdges, nDups, nSelf; for (nEdges = nDups = nSelf = 0; c < be;) { i = getNameIndex (&c, '-'); if (mode) getNameIndex (&c, '-'); j = getNameIndex (&c, '-'); if (mode) getNameIndex (&c, '-'); if (i == j) ++nSelf; else if (Member (BitMatrixRow (graph, n, i), j)) ++nDups; else { Symmetrize (SetAdd, graph, n, i, j); ++nEdges; } while (*c++ != '\n'); } if (trace) fprintf (stderr, "%d edges, %d dups, %d self\n", nEdges, nDups, nSelf); break; } case 2: case 3: { FILE *fn; if (arg+1 >= argc) usage (); c = start = mmapFile (fn = openFile (argv[arg]), &st); be = start + st.st_size; while (c < be) getNameIndex (&c, '-'); munmap (start, st.st_size); fclose (fn); n = nNodes; if (trace) fprintf (stderr, "read %d names\n", n); graph = NewEmptyBitMatrix (n, n); for (i = 0; c < be; ++i) { j = 0; ch = *c++; do { if (ch == '-') ++c; else if (mode == 3 || ch == '1') SetAdd (BitMatrixRow (graph, n, i), j); ++j; while ((ch = *c++) == '\t' || ch == ' '); } while (ch != '\n'); if (j != n) { fprintf (stderr, "matrix row %d: %d entries instead of %d\n", i, j, n); exit (1); } } if (i != n) { fprintf (stderr, "%d rows instead of %d\n", i, n); exit (1); } break; } case 4: { int nod[MaxCliqSz]; l = 1; for (c = start; *c++ != 'C'; ); while (c < be) { while (*c++ != 'z'); c += 2; for (j = 0; (ch = *c++) != '.'; j = j * 10 + ch - '0'); while (*c++ != '\n'); ++l; for (; j--; *(c += 3) == ' ' ? ++c : NULL) getNameIndex (&c, '-'); if (*c++ != '\n' || (c += 10, *c++ != '\n')) { fprintf (stderr, "missing nodes at line %d\n", l); exit (1); } ++l; } n = nNodes; if (trace) fprintf (stderr, "%d nodes seen\n", n); graph = NewEmptyBitMatrix (n, n); int nEdges = 0; for (c = start; *c++ != 'C'; ); while (c < be) { while (*c++ != 'z'); c += 2; for (j = 0; (ch = *c++) != '.'; j = j * 10 + ch - '0'); while (*c++ != '\n'); nEdges += (j*(j-1))>>1; for (k = 0; j--; *(c += 3) == ' ' ? ++c : NULL) { getNameIndex (&c, '-'); nod[k] = i; for (l = 0; l < k; ++l) Symmetrize (SetAdd, graph, n, i, nod[l]); ++k; } c += 12; } break; } } munmap (start, st.st_size); fclose (f); writeGraph (n, graph, (char **) revlist2arrayN (nameList, n)); }