Skip to content

Commit

Permalink
[graph] Reverse negative oriented path
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jun 10, 2018
1 parent 79d7829 commit 385f539
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
15 changes: 15 additions & 0 deletions anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ type Path struct {
length int // Cumulative length of all contigs
}

// flipOrientations reverses the orientations of all components
func (r *Path) reverse() {
c := r.contigs
for i, j := 0, len(c)-1; i < j; i, j = i+1, j-1 {
c[i], c[j] = c[j], c[i]
}
o := r.orientations
for i, j := 0, len(o)-1; i < j; i, j = i+1, j-1 {
o[i], o[j] = o[j], o[i]
}
for i := range o {
o[i] = -o[i]
}
}

// Node is the scaffold ends, Left or Right (5` or 3`)
type Node struct {
path *Path // List of contigs
Expand Down
7 changes: 7 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func RemoveExt(filename string) string {
return strings.TrimSuffix(filename, path.Ext(filename))
}

// Reverse returns a slice in place
func Reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

// IsNewerFile checks if file a is newer than file b
func IsNewerFile(a, b string) bool {
af, aerr := os.Stat(a)
Expand Down
53 changes: 36 additions & 17 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type Edge struct {
weight float64
}

// isReverse returns the orientation of an edge
func (r *Edge) isReverse() bool {
return r.a.end == 1
}

// isSister returns if the edge is internal to a contig
func (r *Edge) isSister() bool {
return r.weight == 0
}

// updateGraph takes input a list of paths and fuse the nodes
func (r *Anchorer) updateGraph(G Graph) Graph {
return G
Expand Down Expand Up @@ -95,37 +105,46 @@ func (r *Anchorer) generatePathAndCycle(G Graph) {
path1, isCycle = dfs(G, a, path1, visited, true)
if isCycle {
path1 = breakCycle(path1)
printPath(path1, nodeToPath)
mergePath(path1, nodeToPath)
continue
}
delete(visited, a)
path2, _ = dfs(G, a, path2, visited, false)

path1 = append(reversePath(path1), path2...)
printPath(path1, nodeToPath)
mergePath(path1, nodeToPath)
}
log.Noticef("A total of %d nodes mapped to new path", len(nodeToPath))
}

// printPath converts a single edge path into a node path
func printPath(path []Edge, nodeToPath map[*Node]*Path) {
// mergePath converts a single edge path into a node path
func mergePath(path []Edge, nodeToPath map[*Node]*Path) {
p := []string{}
tag := ""
// s := Path{}
// orientation := 0
s := &Path{}
for _, edge := range path {
if edge.weight == 0 { // Sister edge
if edge.a.end == 1 {
tag = "-"
} else {
tag = ""
}
// Special care needed for reverse orientation
for _, contig := range edge.a.path.contigs {
p = append(p, tag+contig.name)
}
if !edge.isSister() {
continue
}
ep := edge.a.path
tag := ""
if edge.isReverse() {
tag = "-"
ep.reverse()
}
// TODO: take orientations into account
s.contigs = append(s.contigs, ep.contigs...)
s.orientations = append(s.orientations, ep.orientations...)
nodeToPath[edge.a] = s
nodeToPath[edge.b] = s

// Special care needed for reverse orientation
for _, contig := range edge.a.path.contigs {
p = append(p, tag+contig.name)
}
}
s.Length()
fmt.Println(path)
fmt.Println(s)
fmt.Println(p)
}

Expand Down

0 comments on commit 385f539

Please sign in to comment.