-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeBruijn.cs
More file actions
413 lines (362 loc) · 12.4 KB
/
DeBruijn.cs
File metadata and controls
413 lines (362 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Text;
namespace DeBruijn {
class DeBruijn {
static void Main(string[] args) {
// Test run
/*
Graph graph = new(5);
graph.AddNode(1);
graph.AddNode(2);
graph.AddNode(3);
graph.AddNode(4);
graph.AddEdge(graph.vertices[0], graph.vertices[1]);
graph.vertices[0].Display();
graph.vertices[1].Display();
graph.edges[0].Display();
*/
Console.WriteLine("Enter Sequence k: ");
string sequence = Console.ReadLine();
int k = 3;
Console.WriteLine();
// k-mers
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("k-mers: ");
Console.ResetColor();
string[] kmers = IsolateKMers(sequence, k);
foreach (string kmer in kmers) {
Console.WriteLine(kmer);
}
Console.ResetColor();
Console.WriteLine();
// (k-1)-mers
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("(k-1)-mers: ");
Console.ResetColor();
string[] k1mers = IsolateKMers(sequence, k - 1);
// Console.WriteLine(k1mers.Length);
foreach (string kmer in k1mers) {
Console.WriteLine(kmer);
}
Console.WriteLine();
// De Bruijn graph
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("De Bruijn graph: ");
Console.ResetColor();
Graph graph = GenerateGraph(sequence, k);
// Display vertices
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\nVertices: ");
Console.ResetColor();
for (int i = 0; i < graph.v; i++) {
graph.vertices[i].Display();
}
// Display edges
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\nEdges: ");
Console.ResetColor();
for (int i = 0; i < graph.e; i++) {
graph.edges[i].Display();
}
// Display adjacency list
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nAdjacency list: ");
Console.ResetColor();
graph.Display();
}
static string[] IsolateKMers(string k, int length) {
string[] kmers = new string[k.Length];
for (int i = 0; i < kmers.Length; i++) {
var builder = new System.Text.StringBuilder();
if (i + length > k.Length) {
builder.Append(k.Substring(i, k.Length - i));
builder.Append(k.Substring(0, (i + length) - k.Length));
} else {
builder.Append(k.Substring(i, length));
}
kmers[i] = builder.ToString();
}
return RemoveDuplicates(kmers);
}
static string[] RemoveDuplicates(string[] kmers) {
// Allocate a new array to store the unique kmers. Need to know the size
// of the new array.
// O(2 * n^2)
int size = 0;
for (int i = 0; i < kmers.Length; i++) {
bool isUnique = true;
for (int j = i; j < kmers.Length; j++) {
if (i != j && kmers[i] == kmers[j]) {
isUnique = false;
break;
}
}
if (isUnique) {
size++;
}
}
// Console.WriteLine(size);
string[] uniqueKmers = new string[size];
for (int i = 0, j = 0; i < kmers.Length; i++) {
bool isUnique = true;
for (int k = 0; k < uniqueKmers.Length; k++) {
if (i != k && kmers[i] == uniqueKmers[k]) {
isUnique = false;
break;
}
}
if (isUnique) {
uniqueKmers[j++] = kmers[i];
}
}
return uniqueKmers;
}
// TODO: Implement the following methods
// Generate the de Bruijn graph
static Graph GenerateGraph(string sequence, int k) {
string[] kmers = IsolateKMers(sequence, k);
string[] k1mers = IsolateKMers(sequence, k - 1);
Dictionary<string, List<string>> table = new(kmers.Length);
for (int i = 0; i < k1mers.Length; i++) {
string k1mer = k1mers[i];
for (int j = 0; j < kmers.Length; j++) {
if (Overlap(k1mer, kmers[j])) {
string overlap = GetOverlap(k1mer, kmers[j]);
if (table.ContainsKey(k1mer)) {
table[k1mer].Add(overlap);
} else {
table.Add(k1mer, [overlap]);
}
}
}
}
Graph graph = new(k1mers.Length);
// Add vertices
for (int i = 0; i < k1mers.Length; i++) {
graph.AddNode(k1mers[i]);
}
// Add edges
// TODO: This is O(n^3), however, building a De Bruijn graph should take O(n) time.
for (int i = 0; i < k1mers.Length; i++) {
List<string> values = table[k1mers[i]];
foreach (string value in values) {
for (int j = 0; j < graph.vertices.Length; j++) {
if (graph.vertices[j].data == value) {
string kmerWeight = graph.vertices[i].data.Substring(0, graph.vertices[i].data.Length - 1) + "" + graph.vertices[j].data;
graph.AddEdge(graph.vertices[i], graph.vertices[j], new Edge.Weight(kmerWeight));
}
}
}
}
return graph;
}
// Function to see if there are overlaps between the k-mers
static bool Overlap(string k1mer, string kmer) {
if (kmer.Substring(0, k1mer.Length) == k1mer) {
return true;
}
return false;
}
static string GetOverlap(string k1mer, string kmer) {
if (Overlap(k1mer, kmer)) {
return kmer.Substring(kmer.Length - k1mer.Length, k1mer.Length);
}
return "";
}
// Function to find the Eulerian path
static Path EulerianPath(Graph graph) {
return Path.Fleurys(graph);
}
// Function to find the Hamiltonian path
static Path HamiltonianPath(Graph graph) {
return Path.Backtracking(graph);
}
}
class Graph {
public Node? head, tail;
public int size;
public int v, e;
public Node[] vertices;
public Edge[] edges;
public Dictionary<Node, List<Edge>> adjList = [];
public Graph() {
head = tail = null;
size = 0;
v = e = 0;
}
public Graph(int n) : this() {
vertices = new Node[n];
edges = new Edge[n * 2];
}
public void AddNode(string data) {
Node n = new(data) {
data = data
};
if (head == null) {
head = tail = n;
} else {
tail.AddNext(n);
tail = n;
}
vertices[v++] = n;
size++;
}
public void AddEdge(Node from, Node to, Edge.Weight w) {
Edge edge = new(from, to, w);
from.AddNext(to);
edges[e++] = edge;
}
public void AddEdge(Node from, Node to) {
AddEdge(from, to, null);
}
public Dictionary<Node, List<Edge>> GetAdjList() {
for (int i = 0; i < vertices.Length; i++) {
List<Edge> neighbors = new();
for (int j = 0; j < edges.Length; j++) {
if (edges[j] != null && edges[j].From() != null && (edges[j].From() == vertices[i])) {
neighbors.Add(edges[j]);
}
}
adjList.Add(vertices[i], neighbors);
}
return adjList;
}
public void Display() {
Dictionary<Node, List<Edge>> table = GetAdjList();
foreach (KeyValuePair<Node, List<Edge>> pair in table) {
Console.Write(pair.Key.data + " -> ");
foreach (Edge edge in pair.Value) {
Console.Write(edge.To().data + " ");
}
Console.WriteLine();
}
}
}
class Node {
public Node? next, prev;
public string data;
public Node() {
next = prev = null;
}
public Node(string data) : this() {
this.data = data;
}
public void AddNext(Node n) {
next = n;
n.prev = this;
}
public int Value() {
try {
return int.Parse(data);
} catch (FormatException) {
return -1;
}
}
public void Display() {
Console.WriteLine(data);
}
}
class Edge {
public Node? from;
public Node? to;
public Weight? weight;
public Edge() {
from = to = null;
}
public Edge(Node from, Node to) {
this.from = from;
this.to = to;
this.weight = null;
}
public Edge(Node from, Node to, Weight w) {
this.from = from;
this.to = to;
this.weight = w;
}
public void Display() {
if (weight != null) {
Console.WriteLine(from.data + " -> " + to.data + " : " + weight.GetWeight());
} else {
Console.WriteLine(from.data + " -> " + to.data);
}
}
public Node From() {
return from;
}
public Node To() {
return to;
}
public class Weight {
public string weight;
public Weight() {
weight = "";
}
public Weight(string w) {
weight = w;
}
public string GetWeight() {
return weight;
}
public int Value() {
try {
return int.Parse(weight);
} catch (FormatException) {
return -1;
}
}
}
}
class Path {
public Node[] path;
public int size;
public Path() {
path = new Node[100];
size = 0;
}
public Path(int n) {
path = new Node[n];
size = 0;
}
public void AddNode(Node n) {
path[size++] = n;
}
public void Display() {
var builder = new System.Text.StringBuilder();
for (int i = 0; i < size; i++) {
if (i == size - 1) {
builder.Append(path[i].data);
} else {
builder.Append(path[i].data + " -> ");
}
}
Console.WriteLine(builder.ToString());
}
/*
Let's use Fleury's algortihm to find the Eulerian path.
*/
public static Path Fleurys(Dictionary<Node, List<Edge>> list) {
return null;
}
public static Path Fleurys(Graph graph) {
Dictionary<Node, List<Edge>> list = graph.GetAdjList();
return null;
}
/*
Let's use the Backtracking algorithm to find the Hamiltonian path.
*/
public static Path Backtracking(Dictionary<Node, List<Edge>> list) {
return null;
}
public static Path Backtracking(Graph graph) {
Dictionary<Node, List<Edge>> list = graph.GetAdjList();
return null;
}
}
class Pair<T, V>(T key, V value) {
public T key = key;
public V value = value;
static Pair<T, V> Of(T key, V value) {
return new Pair<T, V>(key, value);
}
}
}