Skip to content

Commit

Permalink
[cluster] Replace list with slice
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jul 5, 2018
1 parent 947dfd3 commit 92c9f7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
39 changes: 21 additions & 18 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
package allhic

import (
"container/list"
"fmt"
)

// Item is a generic type that stores the merges
type Item struct {
// merge is a generic type that stores the merges
type merge struct {
a int
b int
score float64
Expand Down Expand Up @@ -49,12 +49,12 @@ func Cluster(G [][]float64, nclusters int) map[int][]int {
// The original LACHESIS implementation used a C++ multimap with its use similar
// to a priority queue, however, the performance benefit is not obvious since we
// need to perform updates to all merges (remove old merges and insert new merges)
merges := list.New()
merges := []*merge{}

for i := 0; i < N; i++ {
for j := i + 1; j < N; j++ {
if G[i][j] > MinAvgLinkage {
merges.PushBack(&Item{
merges = append(merges, &merge{
a: i,
b: j,
score: G[i][j],
Expand All @@ -63,19 +63,19 @@ func Cluster(G [][]float64, nclusters int) map[int][]int {
}
}

var bestMerge *Item
nMerges := 0
// The core hierarchical clustering
for {
if merges.Len() == 0 {
if len(merges) == 0 {
log.Notice("No more merges to do since the queue is empty")
break
}
log.Noticef("Inspecting %d potential merges", len(merges))
bestMerge := merges[0]
// Step 1. Find the pairs of the clusters with the highest merge score
bestMerge = merges.Front().Value.(*Item)
for e := merges.Front(); e != nil; e = e.Next() {
if e.Value.(*Item).score > bestMerge.score {
bestMerge = e.Value.(*Item)
for _, merge := range merges {
if merge.score > bestMerge.score {
bestMerge = merge
}
}

Expand Down Expand Up @@ -106,11 +106,12 @@ func Cluster(G [][]float64, nclusters int) map[int][]int {

// Step 3. Calculate new score entries for the new cluster
// Remove all used clusters
newMerges := list.New()
for e := merges.Front(); e != nil; e = e.Next() {
p := e.Value.(*Item)
if clusterActive[p.a] && clusterActive[p.b] {
newMerges.PushBack(p)
newMerges := []*merge{}
for _, merge := range merges {
if clusterActive[merge.a] && clusterActive[merge.b] {
newMerges = append(newMerges, merge)
} else {
fmt.Println("Ignore", merge)
}
}

Expand Down Expand Up @@ -141,11 +142,13 @@ func Cluster(G [][]float64, nclusters int) map[int][]int {
continue
}

newMerges.PushBack(&Item{
p := &merge{
a: min(i, newClusterID),
b: max(i, newClusterID),
score: avgLinkage,
})
}
newMerges = append(newMerges, p)
fmt.Println("Insert", p)
}

// Analyze the current clusters if enough merges occurred
Expand Down
2 changes: 1 addition & 1 deletion extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (r *Extracter) BinSize(i int) int {

// Run calls the distribution steps
func (r *Extracter) Run() {
r.ExtractIntraContigLinks()
r.ExtractInterContigLinks()
r.ExtractIntraContigLinks()
r.Makebins()
r.WriteExtracter("distribution.txt")
r.FindEnrichmentOnContigs("enrichment.txt")
Expand Down
7 changes: 4 additions & 3 deletions partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ func (r *Partitioner) MakeMatrix(edges []ContigPair) [][]float64 {
// ParseDist imports the edges of the contig linkage graph
func (r *Partitioner) ParseDist() []ContigPair {
pairs := ParseDistLines(r.Distfile)
goodPairs := FilterEdges(pairs)
log.Noticef("Edge filtering keeps %s edges",
Percentage(len(goodPairs), len(pairs)))
// goodPairs := FilterEdges(pairs)
// log.Noticef("Edge filtering keeps %s edges",
// Percentage(len(goodPairs), len(pairs)))
goodPairs := pairs
return goodPairs
}

Expand Down

0 comments on commit 92c9f7b

Please sign in to comment.