int main ( int argc , const char* argv[] ) { int a = 0 ; mcxTing* argtxt = mcxTingEmpty(NULL, 10) ; if (0) mclxIOsetQMode("MCLXIOVERBOSITY", MCL_APP_VB_NO) ; opInitialize() /* symtable etc */ ; globInitialize() /* hdltable etc */ ; if (argc == 1) { mcxTing* ops = mcxTingEmpty(NULL, 20) ; mcxIO *xfin = mcxIOnew("-", "r") ; mcxIOopen(xfin, EXIT_ON_FAIL) ; fprintf ( stdout , "At your service: " "'[/<op>] help', '[/<str>] grep', 'ops', 'info', and 'quit'.\n" ) ; while (1) { int ok ; mcxTing* line = mcxTingEmpty(NULL, 30) ; fprintf(stdout, "> ") ; fflush(stdout) ; if (STATUS_OK != mcxIOreadLine(xfin, line, MCX_READLINE_BSC)) { fprintf(stdout, "curtains!\n") ; mcxTingFree(&line) ; break ; } mcxTingAppend(ops, line->str) ; mcxTingFree(&line) ; ok = zsDoSequence(ops->str) ; if (ok && (v_g & V_STACK)) zsList(0) ; mcxTingEmpty(ops, 20) ; } } else { for (a=1;a<argc;a++) { mcxTingWrite(argtxt, argv[a]) ; if (!zgUser(argtxt->str)) mcxExit(1) ; } } mcxTingFree(&argtxt) ; return 0 ; }
void mcxIOrelease ( mcxIO* xf ) { if (xf) { mcxIOclose(xf) ; if (xf->fn) mcxTingFree(&(xf->fn)) ; if (xf->mode) mcxFree(xf->mode) ; } }
static mcxstatus handle_label ( mcxTing** keypp , unsigned long* z , map_state* map_z , mcxbits bits , const char* mode ) { mcxbool strict = bits & MCLXIO_STREAM_GTAB_STRICT ; mcxbool warn = bits & MCLXIO_STREAM_WARN ; mcxbool ro = bits & (MCLXIO_STREAM_GTAB_STRICT | MCLXIO_STREAM_GTAB_RESTRICT) ; mcxbool debug = bits & MCLXIO_STREAM_DEBUG ; mcxstatus status = STATUS_OK ; mcxKV* kv = mcxHashSearch(*keypp, map_z->map, ro ? MCX_DATUM_FIND : MCX_DATUM_INSERT) ; if (!kv) /* ro and not found */ { if (strict) { mcxErr (module, "label <%s> not found (%s strict)", (*keypp)->str, mode) ; status = STATUS_FAIL ; } else { if (warn) /* restrict */ mcxTell (module, "label <%s> not found (%s restrict)", (*keypp)->str, mode) ; status = STATUS_IGNORE ; } } else if (kv->key != *keypp) /* seen */ { mcxTingFree(keypp) ; *z = VOID_TO_ULONG kv->val /* fixme theoretical signedness issue */ ; } else /* INSERTed */ { if (debug) mcxTell ( module , "label %s not found (%s extend %lu)" , (*keypp)->str , mode , (map_z->max_seen + 1) ) ; map_z->n_seen++ ; map_z->max_seen++ ; kv->val = ULONG_TO_VOID map_z->max_seen ; *z = map_z->max_seen /* fixme theoretical signedness issue */ ;if(DEBUG3)fprintf(stderr, "hl insert %s <%s> to %d\n", mode, keypp[0]->str, (int) *z) ; status = STATUS_NEW ; } ;if(DEBUG2)fprintf(stderr, "hl final map to %d\n", (int) *z) ; return status ; }
void mcxIOfree ( mcxIO** xfpp ) { if (*xfpp) { mcxIO* xf = *xfpp ; mcxIOrelease(xf) ; mcxTingFree(&(xf->buffer)) ; if (xf->usr && xf->usr_free) xf->usr_free(xf->usr) ; mcxFree(xf) ; *xfpp = NULL ; } }
static mcxstatus get_interface ( mclAlgParam** mlpp , const char* fn_input /* Use this as input or mx_input */ , const char* arg_shared , const char* arg_extra , mclx* mx_input /* Use this as input or fn_input */ , mcxbits CACHE , mcxOnFail ON_FAIL ) { mcxTing* spec = mcxTingNew(arg_shared) ; int argc1 = 0 ; char** argv1 ; mcxstatus status ; mclAlgParam* mymlp = NULL ; mclAlgParam** mymlpp = mlpp ? mlpp : &mymlp ; if (arg_extra) mcxTingPrintAfter(spec, " %s", arg_extra) /* warning this clobbers spec->str */ ; argv1 = mcxOptParseString(spec->str, &argc1, ' ') ; status = mclAlgInterface ( mymlpp , argv1 , argc1 , fn_input , mx_input , CACHE ) ; if (status && ON_FAIL == EXIT_ON_FAIL) mcxExit(1) ; mcxFree(argv1) ; mcxTingFree(&spec) /* fixfixfixmefixmefffixme: mclAlgInterface might use opt->val * which points to somewhere in spec->str. Check. */ ; if (!mlpp) mclAlgParamFree(mymlpp, TRUE) ; return status ; }
void write_clustering ( mclx* cl , const mclx* clprev , mcxIO* xfcone , mcxIO* xfstack , const char* plexprefix , int multiplex_idx , const mclAlgParam* mlp ) { /* this branch is also taken for dispatch mode */ if (plexprefix) { mcxTing* clname = mcxTingPrint(NULL, "%s.%03d", plexprefix, multiplex_idx) ; mcxIO* xfout = mcxIOnew(clname->str, "w") ; if (dispatch_g && mlp && !mcxIOopen(xfout, RETURN_ON_FAIL)) fprintf(xfout->fp, "# %s\n", mlp->cline->str) ; mclxaWrite(cl, xfout, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; mcxTingFree(&clname) ; mcxIOfree(&xfout) ; } if (subcluster_g || dispatch_g) return ; if (xfstack) mclxaWrite(cl, xfstack, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; if (xfcone && !clprev) mclxaWrite(cl, xfcone, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; else if (xfcone) { mclx* clprevt = mclxTranspose(clprev) ; mclx* contracted = mclxCompose(clprevt, cl, 0) ; mclxMakeCharacteristic(contracted) ; mclxaWrite(contracted, xfcone, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; mclxFree(&clprevt) ; mclxFree(&contracted) ; } }
mcxstatus mcxIOreadLine ( mcxIO *xf , mcxTing *dst , mcxbits flags ) { int a ; dim ll ; mcxbool chomp = flags & MCX_READLINE_CHOMP ? TRUE : FALSE ; mcxbool skip = flags & MCX_READLINE_SKIP_EMPTY ? TRUE : FALSE ; mcxbool par = flags & MCX_READLINE_PAR ? TRUE : FALSE ; mcxbool dot = flags & MCX_READLINE_DOT ? TRUE : FALSE ; mcxbool bsc = flags & MCX_READLINE_BSC ? TRUE : FALSE ; mcxbool repeat = dot || par || bsc ? TRUE : FALSE ; mcxbool continuation = FALSE ; mcxTing* line ; mcxstatus stat = STATUS_OK ; if (!xf->fp && mcxIOopen(xf, RETURN_ON_FAIL)) { mcxIOerr(xf, "mcxIOreadLine", "is not open") ; return STATUS_FAIL ; } if (xf->ateof) return STATUS_DONE ; if (!dst || !mcxTingEmpty(dst, 1)) return STATUS_NOMEM ; if (skip || par) { while((a = mcxIOstep(xf)) == '\n') NOTHING ; if (xf->ateof) return STATUS_DONE ; else mcxIOstepback(a, xf) ; } if (!(line = repeat ? mcxTingEmpty(NULL, 1) : dst)) return STATUS_NOMEM ; while (1) { ofs d = mcxIO__rl_rl__(xf, line) ; if (IO_MEM_ERROR == d) { stat = STATUS_NOMEM /* fixme grainify error/status */ ; break ; } ll = line->len ; if (!repeat) break ; else /* must append line to dst */ { if ( dot && !continuation && line->str[0] == '.' && ( ll == 2 || (ll == 3 && line->str[1] == '\r') ) /* fixme still not fully covering */ ) break /* do not attach the single-dot-line */ ; if (par && !continuation && ll == 1) break /* do not attach the second newline */ ; if (!mcxTingNAppend(dst, line->str, line->len)) { stat = STATUS_NOMEM ; break ; } continuation = bsc && (ll > 1 && *(line->str+ll-2) == '\\') ; if (continuation) mcxTingShrink(dst, -2) ; if (!par && !dot && (bsc && !continuation)) break ; if (xf->ateof) break ; } } if (repeat) mcxTingFree(&line) ; if (stat) return stat /* fixme; should we not check chomp first ? */ /* fixme _: \n\r ? */ ; if (chomp && dst->len && *(dst->str+dst->len-1) == '\n') mcxTingShrink(dst, -1) ; if (xf->ateof && !dst->len) return STATUS_DONE ; return STATUS_OK ; }
static mclx* process_queries ( mcxIO* xq , mclx* mx , mclx* mxtp , mcxIO* xfmx , mclTab* tab , mcxIO* xfout , mcxIO* xfpath , mcxIO* xfstep ) { mcxTing* line = mcxTingEmpty(NULL, 100) ; mcxTing* sa = mcxTingEmpty(NULL, 100) ; mcxTing* sb = mcxTingEmpty(NULL, 100) ; SSPxy* sspo = mclgSSPxyNew(mx, mxtp) ; mcxIOopen(xq, EXIT_ON_FAIL) ; while (1) { long a = -1, b = -2, ns = 0 ; mcxbool query = FALSE ; if (isatty(fileno(xq->fp))) fprintf ( stdout , "\n(ready (expect two %s or : directive))\n" , tab ? "labels" : "graph indices" ) ; if ( STATUS_OK != mcxIOreadLine(xq, line, MCX_READLINE_CHOMP) || !strcmp(line->str, ".") ) break ; query = (u8) line->str[0] == ':' ; if (query && (line->len == 1 || isspace((unsigned char) line->str[1]))) { fprintf(xfout->fp, "-->\n") ; fprintf(xfout->fp, ":tf <tf-spec>\n") ; fprintf(xfout->fp, ":top <num>\n") ; fprintf(xfout->fp, ":list <node>\n") ; fprintf(xfout->fp, ":clcf <node>\n") ; fprintf(xfout->fp, ":reread>\n") ; fprintf(xfout->fp, "<--\n") ; continue ; } mcxTingEnsure(sa, line->len) ; mcxTingEnsure(sb, line->len) ; ns = sscanf(line->str, "%s %s", sa->str, sb->str) ; if (ns == 2) sa->len = strlen(sa->str) , sb->len = strlen(sb->str) ; else sa->len = strlen(sa->str) , sb->len = 0 , sb->str[0] = '\0' ; if (!query && ns != 2) { if (line->len) fprintf(stderr, "(error expect two nodes or : directive)\n") ; continue ; } if (query) { mx = handle_query(mx, xfmx, sa, sb) ; sspo->mx = mx /* fixme improve ownership handling */ ; sspo->mxtp = mx ; fprintf(xfout->fp, "%s\n\n", line->str) ; continue /* fixme improve flow */ ; } else if (tab) { mcxKV* kv ; if ((kv = mcxHashSearch(sa, hsh_g, MCX_DATUM_FIND))) a = VOID_TO_ULONG kv->val /* fixme (> 2G labels) */ ; else { label_not_found(sa) ; continue ; } if ((kv = mcxHashSearch(sb, hsh_g, MCX_DATUM_FIND))) b = VOID_TO_ULONG kv->val /* fixme (> 2G labels) */ ; else { label_not_found(sb) ; continue ; } } else if (mcxStrTol(sa->str, &a, NULL) || mcxStrTol(sb->str, &b, NULL)) { fprintf(stderr, "(error failed-reading-number)\n") ; continue ; } if (check_bounds(mx, a)) continue ; if (check_bounds(mx, b)) continue ; fprintf ( xfout->fp , "\n(lattice\n" " (anchors %s %s)\n" , sa->str , sb->str ) ; if (0 && a == b) { fprintf ( xfout->fp , " (path-length 0)\n" "(data\n" ) ; } else { mcxstatus thestat = mclgSSPxyQuery(sspo, a, b) ; dim t ; if (thestat) fprintf(xfout->fp, " (path-length -2)\n(data\n") ; else if (sspo->length < 0) /* not in same component */ fprintf(xfout->fp, " (path-length -1)\n(data\n") ; else { fprintf ( xfout->fp , " (path-length %d)\n" "(data\n" , (int) sspo->length ) ; if (sspo->length == 1) { if (tab) fprintf(xfout->fp, "((%s %s))\n", sa->str, sb->str) ; else fprintf(xfout->fp, "((%ld %ld))\n", (long) a, (long) b) ; } else for (t=0; t< N_COLS(sspo->pathmx)-1; t++) erdos_link_together(xfout, mx, sspo->pathmx->cols+t, sspo->pathmx->cols+t+1) ; fputs(")\n", xfout->fp) ; fprintf(xfout->fp, " (anchors %s %s)\n", sa->str, sb->str) ; fprintf(xfout->fp, " (considered %d)\n", (int) sspo->n_considered) ; fprintf(xfout->fp, " (participants %d)\n", (int) sspo->n_involved) ; fprintf(xfout->fp, " (path-length %d)\n", (int) sspo->length) ; } } fprintf(xfout->fp, ")\n\n") ; if (xfpath) mclxWrite(sspo->pathmx, xfpath, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; if (xfstep) mclxWrite(sspo->stepmx, xfstep, MCLXIO_VALUE_GETENV, RETURN_ON_FAIL) ; mclgSSPxyReset(sspo) ; } mcxTingFree(&sa) ; mcxTingFree(&sb) ; mcxTingFree(&line) ; mclgSSPxyFree(&sspo) ; return mx ; }
mclx* mclxIOstreamIn ( mcxIO* xf , mcxbits bits , mclpAR* transform , void (*ivpmerge)(void* ivp1, const void* ivp2) , mclxIOstreamer* streamer , mcxOnFail ON_FAIL ) { mcxstatus status = STATUS_FAIL ; const char* me = module ; mcxbool symmetric = bits & MCLXIO_STREAM_SYMMETRIC ; mcxbool mirror = bits & MCLXIO_STREAM_MIRROR ; mcxbool abc = bits & MCLXIO_STREAM_ABC ? TRUE : FALSE ; mcxbool one23 = bits & MCLXIO_STREAM_123 ? TRUE : FALSE ; mcxbool etc = bits & (MCLXIO_STREAM_ETC | MCLXIO_STREAM_ETC_AI) ? TRUE : FALSE ; mcxbool longlist = bits & (MCLXIO_STREAM_ETCANY | MCLXIO_STREAM_235ANY) ? TRUE : FALSE ; mcxTing* linebuf = mcxTingEmpty(NULL, 100) ; map_state map_c = { NULL, NULL, -1 , 0} ; map_state map_r = { NULL, NULL, -1 , 0} ; stream_state iface ; etc_state etcstate ; unsigned long n_ite = 0 ; mclx* mx = NULL ; if (!ivpmerge) ivpmerge = mclpMergeMax ; if (symmetric) iface.map_c = &map_c /* this bit of hidgery-pokery */ , iface.map_r = &map_c /* is a crucial interfacummathingy */ ; else iface.map_c = &map_c , iface.map_r = &map_r ;if(DEBUG2)fprintf(stderr, "%s abc\n", abc ? "yes" : "no") ; etcstate.etcbuf = NULL ; etcstate.etcbuf_ofs = 0 ; etcstate.etcbuf_check = 0 ; etcstate.x_prev = ULONG_MAX /* note we depend on ULONG_MAX + 1 == 0 */ ; etcstate.n_y = 0 /* fixme incomplete and distributed initialization of iface */ ; iface.pars = NULL ; iface.pars_n_alloc = 0 ; iface.pars_n_used = 0 ;if(DEBUG3)fprintf(stderr, "1 + max c %lu\n", (ulong) (iface.map_c->max_seen+1)) /* fixme: put the block below in a subroutine */ ; while (1) { if (abc + one23 + longlist > TRUE) /* OUCH */ { mcxErr(module, "multiple stream formats specified") ; break ; } if (!symmetric && streamer->tab_sym_in) { mcxErr(module, "for now disallowed, single tab, different domains") ; break ; } if ((!one23 && !abc && !longlist)) { mcxErr(module, "not enough to get going") ; break ; } /* These have maps associated with them. * Note that bitsp may be changed (by filling in * somewhat underspecified settings). * todo hierverder: etc case supported below ? */ if (abc || etc) stream_state_set_map(symmetric, &iface, streamer, &bits) ; if (xf->fp == NULL && (mcxIOopen(xf, ON_FAIL) != STATUS_OK)) { mcxErr(me, "cannot open stream <%s>", xf->fn->str) ; break ; } status = STATUS_OK ; break ; } iface.bits = bits ; if (!status) while (1) { unsigned long x = 876543210, y = 876543210 ; double value = 0 ; n_ite++ ; iface.x = 0 ; iface.y = 0 ; if (n_ite % 20000 == 0) fputc('.', stderr) /* fixme conditional to sth */ ; if (n_ite % 1000000 == 0) fprintf(stderr, " %ldM\n", (long) (n_ite / 1000000)) /* * - the read routines largely manage iface, including * map_c->max_seen and map_r->max_seen. It would be * nice to encapsulate that management in a single * place. Note the read_abc requirement that sometimes * a label may need to be deleted from a hash. The fact * that handle_label (called by read_etc and read_abc) * also manages max_seen complicate encapsulation though. * * - read_etc manages its line buffer. */ ; status = one23 ? read_123(xf, linebuf, &iface, streamer, &value, bits) : abc ? read_abc(xf, linebuf, &iface, &value) : longlist ? read_etc(xf, &iface, &etcstate, &value) : STATUS_FAIL ; x = iface.x ; y = iface.y /* considerme: etc status ignore could still expand column range. * do we change the status and deal with not incorporating the row, * or do we keep status, and change realloc/ignore logic below? */ ;if(0)fprintf(stderr, "#x now %lu status %s\n", (ulong) (iface.map_c->max_seen+1), MCXSTATUS(status)) /* etc/235 are special in that with NEW x and IGNORE y * we respect x * fixme: should not do that for auto-increment */ ; if (status == STATUS_IGNORE) /* maybe restrict mode */ { if ( longlist && iface.statusx == STATUS_NEW && iface.map_c->max_seen+1 > iface.pars_n_used /* note mixed-sign comparison */ ) { if ((status = pars_realloc(&iface, iface.map_c->max_seen+1))) break ; } continue ; } else if (status) /* FAIL or DONE */ break ; if ( iface.map_c->max_seen >= iface.pars_n_used /* note mixed-sign comparison */ && (status = pars_realloc(&iface, iface.map_c->max_seen+1)) ) break ; status = STATUS_FAIL /* fixme restructure logic, mid-re-initialization is ugly */ ; if ( bits & (MCLXIO_STREAM_LOGTRANSFORM | MCLXIO_STREAM_NEGLOGTRANSFORM) ) { if (bits & MCLXIO_STREAM_LOGTRANSFORM) value = value > 0 ? log(value) : -PVAL_MAX ; else if (bits & MCLXIO_STREAM_NEGLOGTRANSFORM) value = value > 0 ? -log(value) : PVAL_MAX ; if (bits & MCLXIO_STREAM_LOG10) value /= log(10) ; } if (transform) { mclp bufivp ; bufivp.idx = 0 ; bufivp.val = value ; value = mclpUnary(&bufivp, transform) ; } /* fixme: below we have canonical dependence, index as offset */ if (value) { if(DEBUG3)fprintf(stderr, "attempt to extend %d\n", (int) x) ; if (mclpARextend(iface.pars+x, y, value)) { mcxErr(me, "x-extend fails") ; break ; } if (mirror && mclpARextend(iface.pars+y, x, value)) { mcxErr(me, "y-extend fails") ; break ; } } status = STATUS_OK ; } if (n_ite >= 1000000 && n_ite % 5000000) fputc('\n', stderr) ; mcxTingFree(&(etcstate.etcbuf)) ; if (status == STATUS_FAIL || ferror(xf->fp)) mcxErr(me, "error occurred (status %d lc %d)", (int) status, (int) xf->lc) ; else { mx = make_mx_from_pars(streamer, &iface, ivpmerge, bits) ; status = mx ? STATUS_OK : STATUS_FAIL ; } mcxTingFree(&linebuf) ; free_pars(&iface) ; if (status == STATUS_FAIL) { if (ON_FAIL == EXIT_ON_FAIL) mcxDie(1, me, "fini") ; } /* with 123, etcai there is simply no column tab * todo: perhaps create a dummy one (integers). */ if ( !status && (abc || (bits & (MCLXIO_STREAM_ETC | MCLXIO_STREAM_ETC_AI))) ) { if (symmetric) streamer->tab_sym_out = make_tab(iface.map_c) ; else { if (!(bits & MCLXIO_STREAM_ETC_AI)) streamer->tab_col_out = make_tab(iface.map_c) ;if(0)fprintf(stderr, "%p x %p\n", (void*) iface.map_c->map, (void*) iface.map_c->tab) ;if(0)mcxHashStats(stdout, iface.map_c->map) ; streamer->tab_row_out = make_tab(iface.map_r) ; } } mcxHashFree(&(iface.map_c->map), mcxTingRelease, NULL) ; if (!symmetric) mcxHashFree(&(iface.map_r->map), mcxTingRelease, NULL) ; return mx ; }
static mcxstatus read_abc ( mcxIO* xf , mcxTing* buf , stream_state *iface , double* value ) { mcxstatus status = mcxIOreadLine(xf, buf, MCX_READLINE_CHOMP) ; mcxTing* xkey = mcxTingEmpty(NULL, buf->len) ; mcxTing* ykey = mcxTingEmpty(NULL, buf->len) ; mcxbits bits = iface->bits ; mcxbool strict = bits & MCLXIO_STREAM_STRICT ; mcxbool warn = bits & MCLXIO_STREAM_WARN ; mcxbool label_cbits = bits & (MCLXIO_STREAM_CTAB_STRICT | MCLXIO_STREAM_CTAB_RESTRICT) ; mcxbool label_rbits = bits & (MCLXIO_STREAM_RTAB_STRICT | MCLXIO_STREAM_RTAB_RESTRICT) ; mcxbool label_dbits = bits & (MCLXIO_STREAM_WARN | MCLXIO_STREAM_DEBUG) ; const char* printable ; int cv = 0 ; iface->statusx = STATUS_OK ; iface->statusy = STATUS_OK ; do { int xlen = 0 ; int ylen = 0 ; if (status) break ; printable = mcxStrChrAint(buf->str, isspace, buf->len) ; if (printable && (uchar) printable[0] == '#') { status = mcxIOreadLine(xf, buf, MCX_READLINE_CHOMP) ; continue ; } mcxTingEnsure(xkey, buf->len) /* fixme, bit wasteful */ ; mcxTingEnsure(ykey, buf->len) /* fixme, bit wasteful */ ; cv = strchr(buf->str, '\t') ? sscanf(buf->str, "%[^\t]\t%[^\t]%lf", xkey->str, ykey->str, value) : sscanf(buf->str, "%s%s%lf", xkey->str, ykey->str, value) /* WARNING: [xy]key->len have to be set. * we first check sscanf return value */ ; if (cv == 2) *value = 1.0 ; else if (cv != 3) { if (warn || strict) mcxErr ( module , "abc-parser chokes at line %ld [%s]" , (long) xf->lc , buf->str ) ; if (strict) { status = STATUS_FAIL ; break ; } status = mcxIOreadLine(xf, buf, MCX_READLINE_CHOMP) ; continue ; } else if (!(*value <= FLT_MAX)) /* should catch nan, inf */ *value = 1.0 ; xlen = strlen(xkey->str) ; ylen = strlen(ykey->str) ; xkey->len = xlen ; ykey->len = ylen ; status = iface->statusx = handle_label(&xkey, &(iface->x), iface->map_c, label_cbits | label_dbits, "col") ; if (status == STATUS_FAIL || status == STATUS_IGNORE) break ; status = iface->statusy = handle_label(&ykey, &(iface->y), iface->map_r, label_rbits | label_dbits, "row") ; if (status == STATUS_FAIL || status == STATUS_IGNORE) break ; status = STATUS_OK /* Note: status can never be STATUS_NEW */ ; break ; } while (1) ;if(DEBUG2) fprintf(stderr, "read_abc status %s\n", MCXSTATUS(status)) ; if (status == STATUS_NEW) mcxErr(module, "read_abc panic, because status == STATUS_NEW") /* Below we remove the key from the map if it should be * ignored. It will be freed in the block following this one. */ ; if ( iface->statusx == STATUS_NEW && (iface->statusy == STATUS_FAIL || iface->statusy == STATUS_IGNORE) ) { mcxHashSearch(xkey, iface->map_c->map, MCX_DATUM_DELETE) ; iface->map_c->max_seen-- ; iface->statusx = STATUS_IGNORE ; } else if /* Impossible (given that we break when iface->statusx) but defensive */ ( iface->statusy == STATUS_NEW && (iface->statusx == STATUS_FAIL || iface->statusx == STATUS_IGNORE) ) { mcxHashSearch(ykey, iface->map_r->map, MCX_DATUM_DELETE) ; iface->map_r->max_seen-- ; iface->statusy = STATUS_IGNORE ; } /* NOTE handle_label might have set either to NULL but * that's OK. This is needed because handle_label(&xkey) * might succeed and free xkey (because already present in * map_c->map); then when handle_label(&ykey) fails we need to * clean up. */ ; if (status) { mcxTingFree(&xkey) /* kv deleted if iface->statusx == STATUS_NEW */ ; mcxTingFree(&ykey) /* kv deleted if iface->statusy == STATUS_NEW */ ; } return status ; }
/* Purpose: read a single x/y combination. The x may be cached * due to the etc format, where a single line always refers to the same x * and that x is listed only at the start or line, or omitted with * the etc-ai and 235-ai formats. * * state->x_prev may be used by read_etc in order to obtain the * current x index. */ static mcxstatus read_etc ( mcxIO* xf , stream_state *iface , etc_state *state , double* value ) { mcxbits bits = iface->bits ; FILE* stdbug = stdout ; mcxstatus status = STATUS_OK ; mcxTing* ykey = NULL ; mcxTing* xkey = NULL ; const char* printable ; mcxbool label_cbits = bits & (MCLXIO_STREAM_CTAB_STRICT | MCLXIO_STREAM_CTAB_RESTRICT) ; mcxbool label_rbits = bits & (MCLXIO_STREAM_RTAB_STRICT | MCLXIO_STREAM_RTAB_RESTRICT) ; mcxbool label_dbits = bits & (MCLXIO_STREAM_WARN | MCLXIO_STREAM_DEBUG) ; iface->statusx = STATUS_OK ; iface->statusy = STATUS_OK ; iface->x = state->x_prev ; *value = 1.0 ;if(DEBUG)fprintf(stdbug, "read_etc initially set x to %d\n", (int) iface->x) ; if (!state->etcbuf) state->etcbuf = mcxTingEmpty(NULL, 100) ; do { int n_char_read = 0 ; if (state->etcbuf->len != state->etcbuf_check) { mcxErr ( module , "read_etc sanity check failed %ld %ld" , (long) state->etcbuf->len , (long) state->etcbuf_check ) ; status = STATUS_FAIL ; break ; } /* do we need to read a line ? */ /* -> then set iface->x */ /* fixmefixme: funcify this */ /* iface->x can only be changed in this branch */ /* ************************************************************************** */ if (state->etcbuf_ofs >= state->etcbuf->len) { state->etcbuf_ofs = 0 ; state->n_y = 0 ; if ((status = mcxIOreadLine(xf, state->etcbuf, MCX_READLINE_CHOMP))) break ; state->etcbuf_check = state->etcbuf->len ; if ( !(printable = mcxStrChrAint(state->etcbuf->str, isspace, -1)) || (unsigned char) *printable == '#' ) { state->etcbuf_ofs = state->etcbuf->len ; iface->statusy = STATUS_IGNORE ; break /* fixme: ^ statusx seems to work as well. cleanify design */ ; } ; if (bits & (MCLXIO_STREAM_ETC_AI | MCLXIO_STREAM_235_AI)) { } /* In this branch we do not issue handle_label, so we take care of max_seen. */ else if (bits & MCLXIO_STREAM_235) { if (1 != sscanf(state->etcbuf->str, "%lu%n", &(iface->x), &n_char_read)) { iface->statusx = STATUS_FAIL ; break ; } state->etcbuf_ofs += n_char_read ; if (iface->map_c->max_seen+1 < iface->x+1) /* note mixed-sign comparison */ iface->map_c->max_seen = iface->x ; state->x_prev = iface->x ; } else if (bits & MCLXIO_STREAM_ETC) { xkey = mcxTingEmpty(NULL, state->etcbuf->len) ; if (1 != sscanf(state->etcbuf->str, "%s%n", xkey->str, &n_char_read)) break ; state->etcbuf_ofs += n_char_read ; xkey->len = strlen(xkey->str) ; xkey->str[xkey->len] = '\0' ;if(DEBUG3)fprintf(stderr, "max %lu\n", (ulong) iface->map_c->max_seen) ; iface->statusx = handle_label(&xkey, &(iface->x), iface->map_c, label_cbits | label_dbits, "col") ;if(DEBUG3)fprintf(stderr, "max %lu x %lu\n", (ulong) iface->map_c->max_seen, (ulong) iface->x) ; if (iface->statusx == STATUS_IGNORE || iface->statusx == STATUS_FAIL) { /* iface->x = 141414 recentlyadded */ ;if(DEBUG3)fprintf(stderr, "max %lu\n", (ulong) iface->map_c->max_seen) ; break ; } /* ^ Consider what happens when we break here (x label not * accepted) with map_c->max_seen. Basically x label is * indepedent of y, so we never need to undo the * handle_label action. */ state->x_prev = iface->x ; } else mcxDie(1, module, "strange, really") ; } /* ************************************************************************** */ if ( !( printable = mcxStrChrAint(state->etcbuf->str+state->etcbuf_ofs, isspace, -1) ) || (uchar) *printable == '#' ) { state->etcbuf_ofs = state->etcbuf->len ; /* iface->y = 141414 recentlyadded */ ; iface->statusy = STATUS_IGNORE ; break ; } if (bits & (MCLXIO_STREAM_235_AI | MCLXIO_STREAM_235)) { if (1 != sscanf(state->etcbuf->str+state->etcbuf_ofs, "%lu%n", &(iface->y), &n_char_read)) { char* s = state->etcbuf->str+state->etcbuf_ofs ; while(isspace((uchar) s[0])) s++ ; mcxErr ( module , "unexpected string starting with <%c> on line %lu" , (int) ((uchar) s[0]) , xf->lc ) ; iface->statusy = STATUS_FAIL ; } else { ;if(DEBUG3)fprintf(stdbug, "hit at %d\n", (int) state->etcbuf_ofs); state->etcbuf_ofs += n_char_read ; if (iface->map_r->max_seen+1 < iface->y+1) /* note mixed-sign comparison */ iface->map_r->max_seen = iface->y ; } } else /* ETCANY */ { ykey = mcxTingEmpty(NULL, state->etcbuf->len) ; if (1 != sscanf(state->etcbuf->str+state->etcbuf_ofs, "%s%n", ykey->str, &n_char_read)) break ; ykey->len = strlen(ykey->str) ; ykey->str[ykey->len] = '\0' ; state->etcbuf_ofs += n_char_read ; iface->statusy = handle_label(&ykey, &(iface->y), iface->map_r, label_rbits | label_dbits, "row") ; } /* this won't scale well in terms of organisation if and when * tabs are allowed with 235 mode, because in that case, * with 235-ai and restrict-tabr and extend-tabc we will * need the stuff below duplicated in the 235 branch above. * what happens here is that we only decide now whether * the auto-increment is actually happening. It depends * on there being at least one y that was not rejected. */ ; if ( (bits & (MCLXIO_STREAM_ETC_AI | MCLXIO_STREAM_235_AI)) && (iface->statusy == STATUS_OK || iface->statusy == STATUS_NEW) && !state->n_y ) { iface->x = state->x_prev + 1 /* works first time around */ ; iface->map_c->max_seen = state->x_prev + 1 ; state->n_y++ ; state->x_prev = iface->x ; } ;if(DEBUG2)fprintf(stdbug, "etc handle label we have y %d status %s\n", (int) iface->y, MCXSTATUS(iface->statusy)) ; } while (0) ;if(DEBUG2)fprintf(stdbug, "status %s\n", MCXSTATUS(status)) ; do { if (status) /* e.g. STATUS_DONE (readline) [or STATUS_IGNORE (#)]*/ break /* below iface->statusy == STATUS_NEW should be impossible * given this clause and the code sequence earlier. */ ; if (iface->statusx == STATUS_FAIL || iface->statusx == STATUS_IGNORE) { mcxTingFree(&xkey) ; status = iface->statusx ; break ; } /* case iface->statusx == STATUS_NEW is *always* honored */ ; if (iface->statusy == STATUS_FAIL || iface->statusy == STATUS_IGNORE) { mcxTingFree(&ykey) ; status = iface->statusy ; break ; } } while (0) ; if (status == STATUS_IGNORE || status == STATUS_FAIL) mcxTingFree(&ykey) /* fixme, the action in this branch is done in other places too. cleanify design */ ; if ( iface->statusx == STATUS_IGNORE || !mcxStrChrAint(state->etcbuf->str+state->etcbuf_ofs, isspace, -1) ) state->etcbuf_ofs = state->etcbuf->len ;if(DEBUG3)fprintf ( stdbug, "read_etc %s return x(%s -> %d stat=%s) y(%s -> %d stat=%s) status %s buf %d %d c_max_seen %lu\n" , MCXSTATUS(status) , (xkey ? xkey->str : "-"), (int) iface->x, MCXSTATUS(iface->statusx) , (ykey ? ykey->str : "-"), (int) iface->y, MCXSTATUS(iface->statusy) , MCXSTATUS(status), (int) state->etcbuf->len, (int) state->etcbuf_ofs , (ulong) iface->map_c->max_seen ) ; return status ; }
int main ( int argc , const char* argv[] ) { mcxIO* xf_tab = NULL ; mcxIO* xf_tabr = NULL ; mcxIO* xf_tabc = NULL ; mcxIO* xf_restrict_tab = NULL ; mcxIO* xf_restrict_tabr = NULL ; mcxIO* xf_restrict_tabc = NULL ; mcxIO* xf_mx = mcxIOnew("-", "r") ; mcxIO* xfout = NULL ; const char* fndump = "-" ; mclTab* tabr = NULL ; mclTab* tabc = NULL ; mclTab* restrict_tabr = NULL ; mclTab* restrict_tabc = NULL ; mcxbool transpose = FALSE ; mcxbool lazy_tab = FALSE ; mcxbool write_tabc = FALSE ; mcxbool write_tabr = FALSE ; mcxbool cat = FALSE ; mcxbool tree = FALSE ; mcxbool skel = FALSE ; mcxbool newick = FALSE ; mcxbits newick_bits = 0 ; mcxbits cat_bits = 0 ; dim catmax = 1 ; dim n_max = 0 ; dim table_nlines = 0 ; dim table_nfields = 0 ; int split_idx = 1 ; int split_inc = 1 ; const char* split_stem = NULL ; const char* sort_mode = NULL ; mcxTing* line = mcxTingEmpty(NULL, 10) ; mcxbits modes = MCLX_DUMP_VALUES ; mcxbits mode_dump = MCLX_DUMP_PAIRS ; mcxbits mode_part = 0 ; mcxbits mode_loop = MCLX_DUMP_LOOP_ASIS ; mcxbits mode_matrix = 0 ; int digits = MCLXIO_VALUE_GETENV ; mcxOption* opts, *opt ; mcxstatus parseStatus = STATUS_OK ; mcxLogLevel = MCX_LOG_AGGR | MCX_LOG_MODULE | MCX_LOG_IO | MCX_LOG_GAUGE | MCX_LOG_WARN ; mclxIOsetQMode("MCLXIOVERBOSITY", MCL_APP_VB_YES) ; mclx_app_init(stderr) ; mcxOptAnchorSortById(options, sizeof(options)/sizeof(mcxOptAnchor) -1) ; opts = mcxOptParse(options, (char**) argv, argc, 1, 0, &parseStatus) ; if (!opts) exit(0) ; for (opt=opts;opt->anch;opt++) { mcxOptAnchor* anch = opt->anch ; switch(anch->id) { case MY_OPT_HELP : case MY_OPT_APROPOS : mcxOptApropos(stdout, me, syntax, 0, 0, options) ; return 0 ; case MY_OPT_VERSION : app_report_version(me) ; return 0 ; case MY_OPT_TAB : xf_tab = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_TABC : xf_tabc = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_TABR : xf_tabr = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_OUTPUT : fndump = opt->val ; break ; case MY_OPT_SEP_LEAD : sep_lead_g = opt->val ; break ; case MY_OPT_SEP_FIELD : sep_row_g = opt->val ; break ; case MY_OPT_SEP_CAT : sep_cat_g = opt->val ; break ; case MY_OPT_SEP_VAL : sep_val_g = opt->val ; break ; case MY_OPT_PREFIXC : prefixc_g = opt->val ; break ; case MY_OPT_RESTRICT_TAB : xf_restrict_tab = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_RESTRICT_TABC : xf_restrict_tabc = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_RESTRICT_TABR : xf_restrict_tabr = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_LAZY_TAB : lazy_tab = TRUE ; break ; case MY_OPT_NO_VALUES : BIT_OFF(modes, MCLX_DUMP_VALUES) ; break ; case MY_OPT_DUMP_RLINES : mode_dump = MCLX_DUMP_LINES ; BIT_ON(modes, MCLX_DUMP_NOLEAD) ; break ; case MY_OPT_DUMP_VLINES : mode_dump = MCLX_DUMP_LINES ; BIT_ON(modes, MCLX_DUMP_LEAD_VALUE) ; break ; case MY_OPT_DUMP_LINES : mode_dump = MCLX_DUMP_LINES ; break ; case MY_OPT_OMIT_EMPTY : BIT_ON(modes, MCLX_DUMP_OMIT_EMPTY) ; break ; case MY_OPT_SORT : sort_mode = opt->val ; break ; case MY_OPT_NO_LOOPS : mode_loop = MCLX_DUMP_LOOP_NONE ; break ; case MY_OPT_CAT_LIMIT : n_max = atoi(opt->val) ; break ; case MY_OPT_SPLIT_STEM : split_stem = opt->val ; sep_cat_g = NULL ; break ; case MY_OPT_FORCE_LOOPS : mode_loop = MCLX_DUMP_LOOP_FORCE ; break ; case MY_OPT_SKEL : skel = TRUE ; break ; case MY_OPT_WRITE_TABC : write_tabc = TRUE ; break ; case MY_OPT_DIGITS : digits = strtol(opt->val, NULL, 10) ; break ; case MY_OPT_WRITE_TABR : write_tabr = TRUE ; break ; case MY_OPT_DUMP_RDOM : transpose = TRUE ; skel = TRUE ; mode_dump = MCLX_DUMP_LINES ; break ; case MY_OPT_DUMP_CDOM : skel = TRUE ; mode_dump = MCLX_DUMP_LINES ; break ; case MY_OPT_IMX : mcxIOnewName(xf_mx, opt->val) ; break ; case MY_OPT_ICL : mcxIOnewName(xf_mx, opt->val) ; mode_dump = MCLX_DUMP_LINES ; BIT_ON(modes, MCLX_DUMP_NOLEAD) ; BIT_OFF(modes, MCLX_DUMP_VALUES) ; break ; case MY_OPT_TREECAT : mcxIOnewName(xf_mx, opt->val) ; tree = TRUE ; cat_bits |= MCLX_PRODUCE_DOMSTACK ; break ; case MY_OPT_CAT : mcxIOnewName(xf_mx, opt->val) ; cat = TRUE ; break ; case MY_OPT_DUMP_MATRIX : mode_matrix |= MCLX_DUMP_MATRIX ; break ; case MY_OPT_TRANSPOSE : transpose = TRUE ; break ; case MY_OPT_DUMP_UPPER : mode_part = MCLX_DUMP_PART_UPPER ; break ; case MY_OPT_DUMP_UPPERI : mode_part = MCLX_DUMP_PART_UPPERI ; break ; case MY_OPT_DUMP_LOWER : mode_part = MCLX_DUMP_PART_LOWER ; break ; case MY_OPT_DUMP_LOWERI : mode_part = MCLX_DUMP_PART_LOWERI ; break ; case MY_OPT_DUMP_NOLEAD : BIT_ON(modes, MCLX_DUMP_NOLEAD) ; break ; case MY_OPT_NEWICK_MODE : if (strchr(opt->val, 'N')) newick_bits |= (MCLX_NEWICK_NONL | MCLX_NEWICK_NOINDENT) ; if (strchr(opt->val, 'I')) newick_bits |= MCLX_NEWICK_NOINDENT ; if (strchr(opt->val, 'B')) newick_bits |= MCLX_NEWICK_NONUM ; if (strchr(opt->val, 'S')) newick_bits |= MCLX_NEWICK_NOPTHS ; newick = TRUE ; break ; case MY_OPT_DUMP_NEWICK : newick = TRUE ; break ; case MY_OPT_DUMP_TABLE : mode_dump = MCLX_DUMP_TABLE ; break ; case MY_OPT_TABLE_NFIELDS : table_nfields = atoi(opt->val) ; break ; case MY_OPT_TABLE_NLINES : table_nlines = atoi(opt->val) ; break ; case MY_OPT_DUMP_PAIRS : mode_dump = MCLX_DUMP_PAIRS ; break ; } } ; if (skel) cat_bits |= MCLX_READ_SKELETON ; modes |= mode_loop | mode_dump | mode_part | mode_matrix ; xfout = mcxIOnew(fndump, "w") ; mcxIOopen(xfout, EXIT_ON_FAIL) ; mcxIOopen(xf_mx, EXIT_ON_FAIL) ; if (cat || tree) catmax = n_max ? n_max : 0 ; if ((write_tabc || write_tabr) && !xf_tab) mcxDie(1, me, "need a single tab file (-tab option) with --write-tabc or --write-tabr") ; if (xf_tab && mcxIOopen(xf_tab, RETURN_ON_FAIL)) mcxDie(1, me, "no tab") ; else { if (xf_tabr && mcxIOopen(xf_tabr, RETURN_ON_FAIL)) mcxDie(1, me, "no tabr") ; if (xf_tabc && mcxIOopen(xf_tabc, RETURN_ON_FAIL)) mcxDie(1, me, "no tabc") ; } { if (xf_restrict_tab && mcxIOopen(xf_restrict_tab, RETURN_ON_FAIL)) mcxDie(1, me, "no restriction tab") ; else { if (xf_restrict_tabr && mcxIOopen(xf_restrict_tabr, RETURN_ON_FAIL)) mcxDie(1, me, "no restriction tabr") ; if (xf_restrict_tabc && mcxIOopen(xf_restrict_tabc, RETURN_ON_FAIL)) mcxDie(1, me, "no restriction tabc") ; } /* fixme: below is pretty boilerplate, happens in other places as well */ if (xf_restrict_tab) { if (!(restrict_tabr = mclTabRead (xf_restrict_tab, NULL, RETURN_ON_FAIL))) mcxDie(1, me, "error reading restriction tab") ; restrict_tabc = restrict_tabr ; mcxIOclose(xf_restrict_tab) ; } else { if (xf_restrict_tabr) { if (!(restrict_tabr = mclTabRead(xf_restrict_tabr, NULL, RETURN_ON_FAIL))) mcxDie(1, me, "error reading restriction tabr") ; mcxIOclose(xf_restrict_tabr) ; } if (xf_restrict_tabc) { if (!(restrict_tabc = mclTabRead(xf_restrict_tabc, NULL, RETURN_ON_FAIL))) mcxDie(1, me, "error reading restriction tabc") ; mcxIOclose(xf_restrict_tabc) ; } } } /* fixme: restructure code to include bit below */ if (write_tabc || write_tabr) { mclv* dom_cols = mclvInit(NULL) ; mclv* dom_rows = mclvInit(NULL) ; mclv* dom = write_tabc ? dom_cols : dom_rows ; if (!(tabc = mclTabRead(xf_tab, NULL, RETURN_ON_FAIL))) mcxDie(1, me, "error reading tab file") ; if (mclxReadDomains(xf_mx, dom_cols, dom_rows)) mcxDie(1, me, "error reading matrix file") ; mcxIOclose(xf_mx) /* fixme check status */ ; mclTabWrite(tabc, xfout, dom, RETURN_ON_FAIL) ; mcxIOclose(xfout) ; return 0 ; } if (newick) { mcxTing* thetree ; mclxCat cat ; if (xf_tab && !(tabr = mclTabRead(xf_tab, NULL, RETURN_ON_FAIL))) mcxDie(1, me, "error reading tab file") ; mclxCatInit(&cat) ; if ( mclxCatRead ( xf_mx , &cat , 0 , NULL , tabr ? tabr->domain : NULL , MCLX_CATREAD_CLUSTERTREE | MCLX_ENSURE_ROOT ) ) mcxDie(1, me, "failure reading file") ; thetree = mclxCatNewick(&cat, tabr, newick_bits) ; fwrite(thetree->str, 1, thetree->len, xfout->fp) ; fputc('\n', xfout->fp) ; mcxIOclose(xfout) ; return 0 ; } while (1) { mclxIOdumper dumper ; mclxCat cat ; dim i ; if (xf_tab && !lazy_tab) cat_bits |= MCLX_REQUIRE_GRAPH ; mclxCatInit(&cat) ; if (mclxCatRead(xf_mx, &cat, catmax, NULL, NULL, cat_bits)) break ; for (i=0;i<cat.n_level;i++) { mclx* mx = cat.level[i].mx ; if (restrict_tabr || restrict_tabc) { mclx* sub ; sub = mclxSub ( mx , restrict_tabc ? restrict_tabc->domain : mx->dom_cols , restrict_tabr ? restrict_tabr->domain : mx->dom_rows ) ; mx = sub ; } /* noteme fixme dangersign mx now may violate some 'cat' invariant */ if (sort_mode) { if (!strcmp(sort_mode, "size-ascending")) mclxColumnsRealign(mx, mclvSizeCmp) ; else if (!strcmp(sort_mode, "size-descending")) mclxColumnsRealign(mx, mclvSizeRevCmp) ; else mcxErr(me, "unknown sort mode <%s>", sort_mode) ; if (catmax != 1) mcxErr(me, "-sort option and cat mode may fail or corrupt") ; } if (xf_tab && !tabr) { if (!( tabr = mclTabRead (xf_tab, lazy_tab ? NULL : mx->dom_rows, RETURN_ON_FAIL) ) ) mcxDie(1, me, "consider using --lazy-tab option") ; tabc = tabr ; mcxIOclose(xf_tab) ; } else { if (!tabr && xf_tabr) { if (!(tabr = mclTabRead (xf_tabr, lazy_tab ? NULL : mx->dom_rows, RETURN_ON_FAIL) ) ) mcxDie(1, me, "consider using --lazy-tab option") ; mcxIOclose(xf_tabr) ; } if (!tabc && xf_tabc) { if (!( tabc = mclTabRead (xf_tabc, lazy_tab ? NULL : mx->dom_cols, RETURN_ON_FAIL) ) ) mcxDie(1, me, "consider using --lazy-tab option") ; mcxIOclose(xf_tabc) ; } } ; if (transpose) { mclx* tp = mclxTranspose(mx) ; mclxFree(&mx) ; mx = tp ; if (tabc || tabr) { mclTab* tabt = tabc ; tabc = tabr ; tabr = tabt ; } } if (mode_dump == MCLX_DUMP_TABLE) BIT_ON(modes, MCLX_DUMP_TABLE_HEADER) ; mclxIOdumpSet(&dumper, modes, sep_lead_g, sep_row_g, sep_val_g) ; dumper.table_nlines = table_nlines ; dumper.table_nfields = table_nfields ; dumper.prefixc = prefixc_g ; if (split_stem) { mcxTing* ting = mcxTingPrint(NULL, "%s.%03d", split_stem, split_idx) ; mcxIOclose(xfout) ; mcxIOrenew(xfout, ting->str, "w") ; split_idx += split_inc ; } if ( mclxIOdump ( mx , xfout , &dumper , tabc , tabr , digits , RETURN_ON_FAIL ) ) mcxDie(1, me, "something suboptimal") ; mclxFree(&mx) ; if (sep_cat_g && i+1 < cat.n_level) fprintf(xfout->fp, "%s\n", sep_cat_g) ; } break ; } mcxIOfree(&xf_mx) ; mcxIOfree(&xfout) ; mcxIOfree(&xf_tab) ; mcxIOfree(&xf_tabr) ; mcxIOfree(&xf_tabc) ; mcxTingFree(&line) ; return 0 ; }
mcxstatus sharedArgHandle ( int optid , const char* val , mcxDispHook* hook , mcxDispBundle* bundle ) { mcxTing* full_syntax = mcxTingPrint(NULL, "%s %s", "clm", hook->syntax) ; switch(optid) { case CLM_DISP_HELP : case CLM_DISP_APROPOS : case CLM_DISP_AMOIXA : mcxOptApropos ( stdout , hook->name , full_syntax->str , 15 , 0 , bundle->disp_shared ) ; mcxOptApropos ( stdout , hook->name , NULL , 15 , MCX_OPT_DISPLAY_SKIP | ( optid == CLM_DISP_AMOIXA ? MCX_OPT_DISPLAY_HIDDEN : 0 ) , hook->options ) ; mcxExit(0) ; break ; case CLM_DISP_VERSION : bundle->disp_version(hook->name) ; mcxExit(0) ; break ; case CLM_DISP_NOP : NOTHING ; break ; case CLM_DISP_TEST : mcx_test_g = TRUE ; break ; case CLM_DISP_PROGRESS : mcx_progress_g = atoi(val) ; break ; case CLM_DISP_PROGRESS2 : mcx_progress_g = 1 ; break ; case CLM_DISP_DEBUG : mcx_debug_g = atoi(val) ; break ; case CLM_DISP_DEBUG2 : mcx_debug_g = -1u ; break ; case CLM_DISP_SET : mcxSetenv(val) ; break ; default : break ; } mcxTingFree(&full_syntax) ; return STATUS_OK ; }
int main ( int argc , const char* argv[] ) { mcxIO *xfcl = NULL , *xfctrl = NULL , *xfcoarse= NULL , *xfbase = NULL , *xfcone = NULL , *xfstack = NULL ; mclx* mxbase, *cl, *cl_coarse, *clprev, *clctrl = NULL ; mcxTing* shared = mcxTingNew("-I 4 -overlap split") ; mcxbool root = TRUE ; mcxbool have_bootstrap = FALSE ; const char* plexprefix = NULL ; const char* stem = "mcl" ; mcxbool same = FALSE ; mcxbool plex = TRUE ; mcxbool add_transpose = FALSE ; const char* b2opts = NULL ; const char* b1opts = NULL ; mcxbits write_modes = 0 ; mclAlgParam* mlp = NULL ; mcxstatus status = STATUS_OK ; mcxstatus parse_status = STATUS_OK ; int multiplex_idx = 1 ; int N = 0 ; int n_ite = 0 ; dim n_components = 0, n_cls = 0 ; int a = 1, i= 0 ; int n_arg_read = 0 ; int delta = 0 ; mcxOption* opts, *opt ; mcxTing* cline = mcxOptArgLine(argv+1, argc-1, '\'') ; mclgTF* transform = NULL ; mcxTing* transform_spec = NULL ; double iaf = 0.84 ; mclx_app_init(stderr) ; if (0) mcxLogLevel = MCX_LOG_AGGR | MCX_LOG_MODULE | MCX_LOG_IO | MCX_LOG_GAUGE | MCX_LOG_WARN ; else mcxLogLevelSetByString("xf4g1") ; mcxOptAnchorSortById(options, sizeof(options)/sizeof(mcxOptAnchor) -1) ; if (argc == 2 && argv[1][0] == '-' && mcxOptIsInfo(argv[1], options)) delta = 1 ; else if (argc < 2) { help(options, shared) ; exit(0) ; } opts = mcxOptExhaust (options, (char**) argv, argc, 2-delta, &n_arg_read, &parse_status) ; if (parse_status != STATUS_OK) { mcxErr(me, "initialization failed") ; exit(1) ; } ; for (opt=opts;opt->anch;opt++) { mcxOptAnchor* anch = opt->anch ; switch(anch->id) { case MY_OPT_HELP : help(options, shared) ; exit(0) ; case MY_OPT_APROPOS : help(options, shared) ; exit(0) ; break ; case MY_OPT_NMAX : N = atoi(opt->val) ; break ; case MY_OPT_Z : help(NULL, shared) ; exit(0) ; break ; case MY_OPT_SHARED : mcxTingPrintAfter(shared, " %s", opt->val) ; break ; case MY_OPT_TRANSFORM : transform_spec = mcxTingNew(opt->val) ; break ; case MY_OPT_B1 : b1opts = opt->val ; break ; case MY_OPT_B2 : b2opts = opt->val ; break ; case ALG_OPT_SETENV : mcxSetenv(opt->val) ; break ; case ALG_OPT_QUIET : mcxLogLevelSetByString(opt->val) ; break ; case MY_OPT_HDP : hdp_g = atof(opt->val) ; break ; case MY_OPT_ADDTP : add_transpose = TRUE ; break ; case MY_OPT_ANNOT /* only used in command-line copying */ : break ; case MY_OPT_IAF : iaf = atof(opt->val) / 100 ; break ; case MY_OPT_WRITE : if (strstr(opt->val, "stack")) write_modes |= OUTPUT_STACK ; if (strstr(opt->val, "cone")) write_modes |= OUTPUT_CONE ; if (strstr(opt->val, "levels")) write_modes |= OUTPUT_STEPS ; if (strstr(opt->val, "coarse")) write_modes |= OUTPUT_COARSE ; if (strstr(opt->val, "base")) write_modes |= OUTPUT_BASE ; break ; case MY_OPT_BASENAME : xfbase = mcxIOnew(opt->val, "w") ; break ; case MY_OPT_COARSE : xfcoarse = mcxIOnew(opt->val, "w") ; break ; case MY_OPT_CONE : xfcone = mcxIOnew(opt->val, "w") ; break ; case MY_OPT_ROOT : root = strchr("1yY", (u8) opt->val[0]) ? TRUE : FALSE ; break ; case MY_OPT_STACK : xfstack = mcxIOnew(opt->val, "w") ; break ; case MY_OPT_STEM : stem = opt->val ; break ; case MY_OPT_MULTIPLEX : plex = strchr("yY1", (unsigned char) opt->val[0]) ? TRUE : FALSE ; break ; case MY_OPT_DISPATCH : dispatch_g = TRUE ; break ; case MY_OPT_INTEGRATE : integrate_g = TRUE ; break ; case MY_OPT_CONTRACT : break ; case MY_OPT_SUBCLUSTERX : subclusterx_g = TRUE, subcluster_g = TRUE ; break ; case MY_OPT_SUBCLUSTER : subcluster_g = TRUE ; break ; case MY_OPT_CONTROL : xfctrl = mcxIOnew(opt->val, "r") ; break ; case MY_OPT_CL : xfcl = mcxIOnew(opt->val, "r") ; have_bootstrap = TRUE ; break ; case MY_OPT_VERSION : app_report_version(me) ; exit(0) ; default : mcxExit(1) ; } } mcxOptFree(&opts) ; a = 2 + n_arg_read ; if (a < argc) { if (strcmp(argv[a], "--")) mcxDie ( 1 , me , "trailing %s options require standalone '--' separator (found %s)" , integrate_g ? "integrate" : "mcl" , argv[a] ) ; a++ ; } if (subcluster_g + dispatch_g + integrate_g > 1) mcxDie(1, me, "too many modes!") ; if (N && N < argc-a) mcxErr(me, "-n argument leaves spurious option specifications") ; srandom(mcxSeed(89315)) ; signal(SIGALRM, mclSigCatch) ; if (dispatch_g) plexprefix = "dis" ; else if (!write_modes || (write_modes & OUTPUT_STEPS)) plexprefix = stem ; { mcxTing* tg = mcxTingEmpty(NULL, 30) ; if ((write_modes & OUTPUT_COARSE) && !xfcoarse) mcxTingPrint(tg, "%s.%s", stem, "coarse") , xfcoarse = mcxIOnew(tg->str, "w") ; if ((write_modes & OUTPUT_BASE) && !xfbase) mcxTingPrint(tg, "%s.%s", stem, "base") , xfbase = mcxIOnew(tg->str, "w") ; if ( (!write_modes || (write_modes & OUTPUT_CONE)) && !xfcone ) { mcxTingPrint(tg, "%s.%s", stem, "cone") ; xfcone = mcxIOnew(tg->str, "w") ; mcxIOopen(xfcone, EXIT_ON_FAIL) ; fprintf(xfcone->fp, "# %s %s\n", argv[0], cline->str) ; } if ((write_modes & OUTPUT_STACK) && !xfstack) { mcxTingPrint(tg, "%s.%s", stem, "stack") ; xfstack = mcxIOnew(tg->str, "w") ; mcxIOopen(xfstack, EXIT_ON_FAIL) ; fprintf(xfstack->fp, "# %s %s\n", argv[0], cline->str) ; } mcxTingFree(&tg) ; } if (integrate_g) { for (i=a;i<argc;i++) { mcxIO* xf = mcxIOnew(argv[i], "r") ; mclx* cl = mclxRead(xf, EXIT_ON_FAIL) ; mclxCatPush(&stck_g, cl, NULL, NULL, mclxCBdomStack, NULL, "dummy-integrate", n_cls++) ; } integrate_results(&stck_g) ; if (xfstack) mclxCatWrite(xfstack, &stck_g, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; if (xfcone) mclxCatConify(&stck_g) , mclxCatWrite(xfcone, &stck_g, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; return 0 ; } for (i=a;i<argc;i++) { if (get_interface(NULL, argv[1], shared->str, argv[i], NULL, 0, RETURN_ON_FAIL)) mcxDie(1, me, "error while testing mcl options viability (%s)", argv[i]) ; } mcxLog(MCX_LOG_APP, me, "pid %ld", (long) getpid()) /* make sure clusters align with this cluster * status: does not seem promising. */ ; if (xfctrl) clctrl = mclxRead(xfctrl, EXIT_ON_FAIL) ; /* * Below: compute cl and mxbase. */ ; if (xfcl) { cl = mclxRead(xfcl, EXIT_ON_FAIL) ; write_clustering (cl, NULL, xfcone, xfstack, plexprefix, multiplex_idx++, NULL) ; if (subcluster_g || dispatch_g) mclxCatPush(&stck_g, cl, NULL, NULL, mclxCBdomStack, NULL, "dummy-mclcm", n_cls++) ; mcxIOfree(&xfcl) ; if (!b1opts && !b2opts) b1opts = "" ; mxbase = get_base(argv[1], NULL, b1opts, b2opts) ; } else { mcxbits CACHE = b1opts || b2opts ? ALG_CACHE_INPUT /* cache, transform later */ : ALG_CACHE_START ; get_interface ( &mlp , argv[1] , shared->str , a < argc ? argv[a] : NULL , NULL , CACHE , EXIT_ON_FAIL ) ; if (a < argc) a++ ; if ((status = mclAlgorithm(mlp)) == STATUS_FAIL) { mcxErr(me, "failed at initial run") ; exit(1) ; } cl_coarse = mclAlgParamRelease(mlp, mlp->cl_result) ; cl_coarse = control_test(cl_coarse, clctrl) ; write_clustering (cl_coarse, NULL, xfcone, xfstack, plexprefix, multiplex_idx++, mlp) ; if (subcluster_g || dispatch_g) mclxCatPush(&stck_g, cl_coarse, NULL, NULL, mclxCBdomStack, NULL, "dummy-mclcm", n_cls++) ; cl = cl_coarse ; n_ite++ ; if (b1opts || b2opts) { mclx* mx_input = mclAlgParamRelease(mlp, mlp->mx_input) ; mxbase = get_base(NULL, mx_input, b1opts, b2opts) /* ^ get_base frees mx_input */ ; } else mxbase = mclAlgParamRelease(mlp, mlp->mx_start) ; } clprev = cl ; mclAlgParamFree(&mlp, TRUE) ; if (xfbase) { dim nre = mclxNrofEntries(mxbase) ; mcxLog(MCX_LOG_APP, me, "base has %lu entries", (ulong) nre) ; mclxaWrite(mxbase, xfbase, MCLXIO_VALUE_GETENV, EXIT_ON_FAIL) ; mcxIOclose(xfbase) ; } if (subcluster_g || dispatch_g) iaf = iaf ? 1/iaf : 1.414 ; while ( (!dispatch_g && (!N || n_ite < N)) || (dispatch_g && a < argc) ) { mclx* mx_coarse = NULL, *clnext = NULL ; dim dist_new_prev = 0, dist_prev_new = 0 ; mclx* clnew = NULL ; mcxbool faith = FALSE ; double inflation = -1.0 ; if (subcluster_g) mx_coarse = subclusterx_g ? mclxBlockPartition(mxbase, clprev, 50) : mclxBlockUnion(mxbase, clprev) /* have to copy mxbase as mx_coarse is freed. * Even if it were not freed, it is probably transformed. */ ; else if (dispatch_g) mx_coarse = mclxCopy(mxbase) ; else { mx_coarse = get_coarse(mxbase, clprev, add_transpose) ; if (n_ite == 1) { mclx* cc = clmUGraphComponents(mx_coarse, NULL) /* fixme; mx_coarse garantueed UD ? */ ; n_components = N_COLS(cc) ; mclxFree(&cc) ; } } if (xfcoarse) write_coarse(xfcoarse, mx_coarse) ; get_interface ( &mlp , NULL , shared->str , a < argc ? argv[a] : NULL , mx_coarse , ALG_CACHE_START , EXIT_ON_FAIL ) ; inflation = mlp->mpp->mainInflation ; BIT_OFF(mlp->modes, ALG_DO_SHOW_PID | ALG_DO_SHOW_JURY) ; if ((status = mclAlgorithm(mlp)) == STATUS_FAIL) { mcxErr(me, "failed") ; mcxExit(1) ; } cl_coarse = mclAlgParamRelease(mlp, mlp->cl_result) ; if (xfcoarse) mclxaWrite(cl_coarse, xfcoarse, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; if (dispatch_g || subcluster_g) clnext = cl_coarse ; else clnext = mclxCompose(clprev, cl_coarse, 0) , clnext = control_test(clnext, clctrl) , mclxFree(&cl_coarse) ; clmSJDistance (clprev, clnext, NULL, NULL, &dist_prev_new, &dist_new_prev) ; if (dist_prev_new + dist_new_prev) { write_clustering (clnext, clprev, xfcone, xfstack, plexprefix, multiplex_idx++, mlp) ; clnew = clnext ; if (subcluster_g || dispatch_g) mclxCatPush(&stck_g, clnext, NULL, NULL, mclxCBdomStack, NULL, "dummy-mclcm", n_cls++) ; else mclxFree(&clprev) ; clprev = clnew ; } else if ( N_COLS(clnext) > n_components && inflation * iaf > 1.2 && inflation * iaf < 10 ) { mclxFree(&clnext) ; inflation *= iaf ; mcxTingPrintAfter(shared, " -I %.2f", inflation) ; mcxLog(MCX_LOG_APP, me, "setting inflation to %.2f", inflation) ; faith = TRUE ; } /* i.e. vanilla mode, contraction */ else if (!subcluster_g && !dispatch_g) { mclx* cc ; mclxFree(&clnext) ; mclxAddTranspose(mx_coarse, 1.0) ; cc = clmUGraphComponents(mx_coarse, NULL) ; if (N_COLS(cc) < N_COLS(clprev)) { mclx* ccback = mclxCompose(clprev, cc, 0) ; write_clustering (ccback, clprev, xfcone, xfstack, plexprefix, multiplex_idx++, NULL) ; mclxFree(&clprev) ; clprev = ccback ; mcxTell(me, "connected components added as root clustering") ; } if (root && N_COLS(cc) > 1) { mclx* root = mclxCartesian ( mclvCanonical(NULL, 1, 0) , mclvCopy(NULL, mxbase->dom_cols) , 1.0 ) ; write_clustering (root, clprev, xfcone, xfstack, plexprefix, multiplex_idx++, NULL) ; mclxFree(&clprev) ; mcxTell(me, "universe added as root clustering") ; clprev = root ; clnew = NULL ; } mclxFree(&cc) ; } else if (subcluster_g || dispatch_g) mclxFree(&clnext) ; mclAlgParamFree(&mlp, TRUE) /* frees mx_coarse */ ; if (!clnew && !faith) { same = TRUE ; break ; } a++ ; if (dispatch_g && a == argc) break ; n_ite++ ; } if (same) mcxLog(MCX_LOG_MODULE, me, "no further contraction: halting") ; if (dispatch_g) integrate_results(&stck_g) ; else if (subcluster_g) mclxCatReverse(&stck_g) ; if (dispatch_g || subcluster_g) { dim j ; if (xfstack) mclxCatWrite(xfstack, &stck_g, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; if (xfcone && ! mclxCatConify(&stck_g)) mclxCatWrite(xfcone, &stck_g, MCLXIO_VALUE_NONE, RETURN_ON_FAIL) ; for (j=0;j<stck_g.n_level;j++) { mclxAnnot* an = stck_g.level+j ; mclxFree(&an->mx) ; } mcxFree(stck_g.level) ; } mcxIOfree(&xfcoarse) ; mcxIOfree(&xfbase) ; mcxIOfree(&xfcone) ; mcxIOfree(&xfstack) ; mcxTingFree(&shared) ; if (!dispatch_g && !subcluster_g) /* fixme fixme fixme */ mclxFree(&clprev) ; mclxFree(&mxbase) ; mclvFree(&start_col_sums_g) ; mcxTingFree(&cline) ; helpful_reminder() ; return STATUS_OK ; }