void graph_add_edges(graph *g) { node *n1, *n2; int i, j; for (i = 0; i < g->ntemplates; i++) { n1 = g->nodes->node[i]; for (j = i+1; j < g->ntemplates; j++) { int count; double score; n2 = g->nodes->node[j]; score = calc_edge_score(n1->matrix, n2->matrix, g->snp_scores, g->nsnps, &count, g->correlation_offset); if (count) { graph_add_edge(g, g->nodes->node[i], g->nodes->node[j], score); } } } /* Sort edges-out for each node by node number */ for (i = 0; i < g->ntemplates; i++) { node_sort_edges(g->nodes->node[i]); } }
typename SubstEdgeScore<V,D>::value_type SubstEdgeScore<V,D>:: edge_ext_score(const WorkArea& wa, const Data& xx, const Data& yy, const Edge& ix, const Edge& iy, uint i, uint j) const { value_type s=0.0; if (ix.p_pos().first==ix.c_pos().first || iy.p_pos().first==iy.c_pos().first) { // loop region s = calc_edge_score(si_subst_, wa.gap, xx, yy, ix.p_pos().first, ix.p_pos().second, iy.p_pos().first, iy.p_pos().second); } else { // buldge s = calc_edge_score(si_subst_, wa.gap, xx, yy, ix.p_pos().first, ix.c_pos().first-1, iy.p_pos().first, iy.c_pos().first-1); s += calc_edge_score(si_subst_, wa.gap, xx, yy, ix.c_pos().second+1, ix.p_pos().second, iy.c_pos().second+1, iy.p_pos().second); } return s; }
/** * Merges the nodes linked to by edge 'e' and sets any neighbouring * edge / link scores to be invalid. */ void merge_node(graph *g, edge *e) { node *n1, *n2; node_array *na1, *na2, *na; int i, j; if (verbosity >= 2) printf("Merging %d / %d (score %8.2f, link %8.2f) %s / %s\n", e->n1->number, e->n2->number, e->edge_score, e->linkage_score, e->n1->tname, e->n2->tname); /* print_matrix_node(g, e->n1); print_matrix_node(g, e->n2); */ /* Find all the nodes linked to either n1 or n2 where n1 and n2 * are the nodes in this edge. */ n1 = e->n1; n2 = e->n2; na1 = node_neighbours(n1); na2 = node_neighbours(n2); na = node_array_union(na1, na2); node_array_destroy(na1); node_array_destroy(na2); /* Attach n2 to the node_array in n1 - allows traceback */ if (!n1->merged) { n1->merged = node_array_create(); } node_array_add(n1->merged, n2); /* * Merge the matrix rows. */ for (i = 0; i < g->nsnps; i++) { for (j = 0; j < 6; j++) n1->matrix[i][j] += n2->matrix[i][j]; } /* * Forall nodes in our set, find the edges between that and n1 * and/or n2. If it links with both then set the edge score to be * the average of the two values, otherwise use the single score. * Set the linkage score to be undefined. * Reset the edge to be between this node and n1 always (as n2 * will then be disconnected and considered to be merged with n1). */ for (i = 0; i < na->nnodes; i++) { node *n = na->node[i]; edge *e1, *e2; if (n == n1 || n == n2) continue; e1 = edge_find(n, n1); e2 = edge_find(n, n2); if (!e1 && !e2) continue; if (e1 && e2) { /* links to both, so remove edge to n2 */ e1->edge_score = (e1->edge_score + e2->edge_score) / 2; edge_unlink(e2); } else if (e2) { /* links only to n2, so relink edge to n1 */ if (e2->n1 == n) e2->n2 = n1; else e2->n1 = n1; edge_array_add(n1->edges, e2); e1 = e2; } e1->linkage_score = UNDEF_SCORE; e1->edge_score = UNDEF_SCORE; } node_array_destroy(na); edge_unlink(e); for (i = 0; i < g->nodes->nnodes; i++) { if (g->nodes->node[i] == n2) { g->nodes->node[i] = NULL; break; } } /* Recompute all undefined edge scores */ for (i = 0; i < g->edges->nedges; i++) { edge *e; if (!(e = g->edges->edge[i])) continue; if (!e->n1 || !e->n2) continue; e->edge_score = calc_edge_score(e->n1->matrix, e->n2->matrix, g->snp_scores, g->nsnps, NULL, g->correlation_offset); } }