stList *getComponents(stList *edges) {
    /*
     * Gets a list of connected components, each connected component
     * being represented as a list of the edges, such that each edge is in exactly one
     * connected component. Allows for multi-graphs (multiple edges connecting two nodes).
     */

    stHash *nodesToEdges = getNodesToEdgesHash(edges);

    /*
     * Traverse the edges greedily
     */
    stList *components =
            stList_construct3(0, (void(*)(void *)) stList_destruct);
    stList *nodes = stHash_getKeys(nodesToEdges);
    while (stList_length(nodes) > 0) {
        stIntTuple *node = stList_pop(nodes);
        stList *edges = stHash_search(nodesToEdges, node);
        if (edges != NULL) { //We have a component to build
            stSortedSet *component = stSortedSet_construct();
            stHash_remove(nodesToEdges, node);
            for (int64_t i = 0; i < stList_length(edges); i++) {
                stIntTuple *edge = stList_get(edges, i);
                getComponentsP(nodesToEdges, stIntTuple_get(edge, 0),
                        component);
                getComponentsP(nodesToEdges, stIntTuple_get(edge, 1),
                        component);
            }
            stList_append(components, stSortedSet_getList(component));
            //Cleanup
            stSortedSet_destruct(component);
            stList_destruct(edges);
        }
        stIntTuple_destruct(node);
    }
    assert(stHash_size(nodesToEdges) == 0);
    stHash_destruct(nodesToEdges);
    stList_destruct(nodes);

    return components;
}
예제 #2
0
파일: sonLibSet.c 프로젝트: adderan/sonLib
int64_t stSet_size(stSet *set) {
    return stHash_size(set->hash);
}
static void test_newBlockHashFromBlock_0(CuTest *testCase) {
    stList *orderList = stList_construct3(0, free);
    stHash *observedHash = createBlockHashFromString("a score=0 test=0\n"
                                                     "s reference.chr0 0 13 + 158545518 gcagctgaaaaca\n"
                                                     "s name.chr1      0 10 +       100 ATGT---ATGCCG\n"
                                                     "s name2.chr1     0 10 +       100 ATGT---ATGCCG\n"
                                                     "s name3.chr1     5 13 -       100 ATGTgggATGCCG\n",
                                                     orderList);
    CuAssertTrue(testCase, stHash_size(observedHash) == 4);
    row_t *key = NULL;
    key = stHash_search(observedHash, "reference");
    CuAssertTrue(testCase, key != NULL);
    CuAssertTrue(testCase, strcmp(key->name, "reference.chr0") == 0);
    CuAssertTrue(testCase, strcmp(key->prevName, "reference.chr0") == 0);
    CuAssertTrue(testCase, key->multipleNames == false);
    CuAssertTrue(testCase, key->start == 0);
    CuAssertTrue(testCase, key->length == 13);
    CuAssertTrue(testCase, key->sourceLength == 158545518);
    CuAssertTrue(testCase, key->prevRightPos == 12);
    CuAssertTrue(testCase, key->strand == '+');
    CuAssertTrue(testCase, key->prevStrand == '+');
    CuAssertTrue(testCase, strcmp(key->sequence, "gcagctgaaaaca") == 0);
    // row 2
    key = stHash_search(observedHash, "name");
    CuAssertTrue(testCase, key != NULL);
    CuAssertTrue(testCase, strcmp(key->name, "name.chr1") == 0);
    CuAssertTrue(testCase, strcmp(key->prevName, "name.chr1") == 0);
    CuAssertTrue(testCase, key->multipleNames == false);
    CuAssertTrue(testCase, key->start == 0);
    CuAssertTrue(testCase, key->length == 10);
    CuAssertTrue(testCase, key->sourceLength == 100);
    CuAssertTrue(testCase, key->prevRightPos == 9);
    CuAssertTrue(testCase, key->strand == '+');
    CuAssertTrue(testCase, key->prevStrand == '+');
    CuAssertTrue(testCase, strcmp(key->sequence, "ATGT---ATGCCG") == 0);
    // row 3
    key = stHash_search(observedHash, "name2");
    CuAssertTrue(testCase, key != NULL);
    CuAssertTrue(testCase, strcmp(key->name, "name2.chr1") == 0);
    CuAssertTrue(testCase, strcmp(key->prevName, "name2.chr1") == 0);
    CuAssertTrue(testCase, key->multipleNames == false);
    CuAssertTrue(testCase, key->start == 0);
    CuAssertTrue(testCase, key->length == 10);
    CuAssertTrue(testCase, key->sourceLength == 100);
    CuAssertTrue(testCase, key->prevRightPos == 9);
    CuAssertTrue(testCase, key->strand == '+');
    CuAssertTrue(testCase, key->prevStrand == '+');
    CuAssertTrue(testCase, strcmp(key->sequence, "ATGT---ATGCCG") == 0);
    // row 4
    key = stHash_search(observedHash, "name3");
    CuAssertTrue(testCase, key != NULL);
    CuAssertTrue(testCase, strcmp(key->name, "name3.chr1") == 0);
    CuAssertTrue(testCase, strcmp(key->prevName, "name3.chr1") == 0);
    CuAssertTrue(testCase, key->multipleNames == false);
    CuAssertTrue(testCase, key->start == 5);
    CuAssertTrue(testCase, key->length == 13);
    CuAssertTrue(testCase, key->sourceLength == 100);
    CuAssertTrue(testCase, key->prevRightPos == 17);
    CuAssertTrue(testCase, key->strand == '-');
    CuAssertTrue(testCase, key->prevStrand == '-');
    CuAssertTrue(testCase, strcmp(key->sequence, "ATGTgggATGCCG") == 0);
    stList_destruct(orderList);
    stHash_destruct(observedHash);
}