Skip to content

Commit 385f539

Browse files
committed
[graph] Reverse negative oriented path
1 parent 79d7829 commit 385f539

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

anchor.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ type Path struct {
133133
length int // Cumulative length of all contigs
134134
}
135135

136+
// flipOrientations reverses the orientations of all components
137+
func (r *Path) reverse() {
138+
c := r.contigs
139+
for i, j := 0, len(c)-1; i < j; i, j = i+1, j-1 {
140+
c[i], c[j] = c[j], c[i]
141+
}
142+
o := r.orientations
143+
for i, j := 0, len(o)-1; i < j; i, j = i+1, j-1 {
144+
o[i], o[j] = o[j], o[i]
145+
}
146+
for i := range o {
147+
o[i] = -o[i]
148+
}
149+
}
150+
136151
// Node is the scaffold ends, Left or Right (5` or 3`)
137152
type Node struct {
138153
path *Path // List of contigs

base.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func RemoveExt(filename string) string {
8484
return strings.TrimSuffix(filename, path.Ext(filename))
8585
}
8686

87+
// Reverse returns a slice in place
88+
func Reverse(s []int) {
89+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
90+
s[i], s[j] = s[j], s[i]
91+
}
92+
}
93+
8794
// IsNewerFile checks if file a is newer than file b
8895
func IsNewerFile(a, b string) bool {
8996
af, aerr := os.Stat(a)

graph.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ type Edge struct {
2121
weight float64
2222
}
2323

24+
// isReverse returns the orientation of an edge
25+
func (r *Edge) isReverse() bool {
26+
return r.a.end == 1
27+
}
28+
29+
// isSister returns if the edge is internal to a contig
30+
func (r *Edge) isSister() bool {
31+
return r.weight == 0
32+
}
33+
2434
// updateGraph takes input a list of paths and fuse the nodes
2535
func (r *Anchorer) updateGraph(G Graph) Graph {
2636
return G
@@ -95,37 +105,46 @@ func (r *Anchorer) generatePathAndCycle(G Graph) {
95105
path1, isCycle = dfs(G, a, path1, visited, true)
96106
if isCycle {
97107
path1 = breakCycle(path1)
98-
printPath(path1, nodeToPath)
108+
mergePath(path1, nodeToPath)
99109
continue
100110
}
101111
delete(visited, a)
102112
path2, _ = dfs(G, a, path2, visited, false)
103113

104114
path1 = append(reversePath(path1), path2...)
105-
printPath(path1, nodeToPath)
115+
mergePath(path1, nodeToPath)
106116
}
117+
log.Noticef("A total of %d nodes mapped to new path", len(nodeToPath))
107118
}
108119

109-
// printPath converts a single edge path into a node path
110-
func printPath(path []Edge, nodeToPath map[*Node]*Path) {
120+
// mergePath converts a single edge path into a node path
121+
func mergePath(path []Edge, nodeToPath map[*Node]*Path) {
111122
p := []string{}
112-
tag := ""
113-
// s := Path{}
114-
// orientation := 0
123+
s := &Path{}
115124
for _, edge := range path {
116-
if edge.weight == 0 { // Sister edge
117-
if edge.a.end == 1 {
118-
tag = "-"
119-
} else {
120-
tag = ""
121-
}
122-
// Special care needed for reverse orientation
123-
for _, contig := range edge.a.path.contigs {
124-
p = append(p, tag+contig.name)
125-
}
125+
if !edge.isSister() {
126+
continue
127+
}
128+
ep := edge.a.path
129+
tag := ""
130+
if edge.isReverse() {
131+
tag = "-"
132+
ep.reverse()
133+
}
134+
// TODO: take orientations into account
135+
s.contigs = append(s.contigs, ep.contigs...)
136+
s.orientations = append(s.orientations, ep.orientations...)
137+
nodeToPath[edge.a] = s
138+
nodeToPath[edge.b] = s
139+
140+
// Special care needed for reverse orientation
141+
for _, contig := range edge.a.path.contigs {
142+
p = append(p, tag+contig.name)
126143
}
127144
}
145+
s.Length()
128146
fmt.Println(path)
147+
fmt.Println(s)
129148
fmt.Println(p)
130149
}
131150

0 commit comments

Comments
 (0)