-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_based_scaffolding.py
More file actions
190 lines (151 loc) · 5.59 KB
/
graph_based_scaffolding.py
File metadata and controls
190 lines (151 loc) · 5.59 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
from argparse import ArgumentParser
from Bio import SeqIO
import sys
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from operator import itemgetter
from itertools import combinations
parser = ArgumentParser()
parser.add_argument("efile", help="Error rate file")
parser.add_argument("--mincontigs", type=int, default=2,help="Minimum number of contigs on long read for the read to be considered")
parser.add_argument("--summaryfile", help="Contig Distance Summary file")
parser.add_argument("--blacklistfile", help="File containing long read ids where certain contig mappings should be ignored.")
parser.add_argument("contigfile", help="Contig File")
parser.add_argument("linename", help="Name of cell line")
#parser.add_argument("--maxdev", help="Maximal deviation", type=float, default=2.0)
parser.add_argument("--mindepth", help="Minimal depth", type=int, default=25)
args = parser.parse_args()
# global data structures
contigs = {}
contig2scaf = {}
scaffolds = {}
# in clusters similar scaffolds will be stored, for manual curation
clusters = set()
if args.summaryfile:
with open(args.summaryfile) as f:
for line in f:
sline = line.split()
if sline[1] == "NA":
continue
if int(sline[2]) < args.mindepth:
continue
ctg1 = sline[0].split("_")[0].strip("+").strip("-")
ctg2 = sline[0].split("_")[1].strip("+").strip("-")
ori1 = sline[0].split("_")[0][0]
ori2 = sline[0].split("_")[1][0]
distance = float(sline[1])
dist = int(distance)
if args.blacklistfile:
blacklist = {}
with open(args.blacklistfile) as f:
for line in f:
sline = line.split()
blacklist[sline[0]] = sline[1]
for read in SeqIO.parse(args.contigfile, "fasta"):
contigs[read.id] = len(read.seq)
print("Nr. of scaffolds: " + str(len(contigs)))
class Node:
segments = {} # id1: (lc1, rc1)
sizes_nc = [] # sizes of contigs that determine the average (that were Not Cut)
def get_right_nb(self):
pass
def get_distance_table(self):
pass
def get_avg(self):
return np.mean(self.sizes_nc)
def __init__(self, contiginfo):
#lc1 =
pass
class Edge:
sizes = {} # no need for non cut edges
def get_avg(self):
avg = np.mean(self.sizes.values())
def __init__():
pass
# a scaffold is a connected graph of contig-nodes
class Scaffold:
distanceMatrix = []
length = 0 # length is defined by the average distances and the average node sizes
nodes = set([])
def update_distance_matrix(self):
pass
def compare(self, scaf2):
#compare their distance matrices
pass
def to_graph_string(self):
pass
# give a graph string in order to visualize this ting
def __init__(self):
pass
@classmethod
def init_from_longread(cls,longread):
newinst = cls()
[lrid, lr] = longread
for m in lr["maps"]:
Node(m)
return newinst
# nanopore reads
lreads = {}
with open(args.efile) as f:
for line in f:
#sline = line.split()
[rid, ctg, t2, t3, t4, scr, ecr, lenr, strand, scc, ecc, lenc, t12, t13, t14, t15, t16] = line.split()
data = {"contig":ctg,"strand":int(strand),"scr":int(scr),"ecr":int(ecr),"scc":int(scc),"ecc":int(ecc),"lenc":int(lenc)}
if args.blacklistfile:
if rid in blacklist:
if blacklist[rid] == ctg:
continue
if rid in lreads:
lreads[rid]["maps"].append(data)
else:
lreads[rid] = {}
lreads[rid]["length"] = int(lenr)
lreads[rid]["maps"] = [data]
# get interesting reads
greadst = {}
for rid in lreads:
counter = 0
for item in lreads[rid]["maps"]:
if item["contig"].endswith(args.linename) and item["contig"] != "1036QBL":
counter +=1
if counter >= args.mincontigs:
greadst[rid] = lreads[rid]
break
# turn reads around if necessary
for rid, lr in greadst.items():
bw = 0
fw = 0
for mapping in lr["maps"]:
if mapping["contig"].endswith(args.linename):
if mapping["strand"] == 1:
bw += 1
elif mapping["strand"] == 0:
fw += 1
else:
raise ValueError("strand: " + str(mapping["strand"]))
if bw > fw:
for mapping in lr["maps"]:
if mapping["contig"].endswith(args.linename):
mapping["strand"] = 1 if mapping["strand"] == 0 else 0
mapping["scr"] = lr["length"] - mapping["ecr"]
mapping["ecr"] = lr["length"] - mapping["scr"]
mapping["scc"] = mapping["lenc"] - mapping["ecc"]
mapping["ecc"] = mapping["lenc"] - mapping["scc"]
# turn around and redefine wrong contigs
for mapping in lr["maps"]:
if mapping["contig"].endswith(args.linename):
if mapping["strand"] == 1: #define a new contigname and turn it around
mapping["contig"] = mapping["contig"] + "rc"
mapping["scc"] = mapping["lenc"] - mapping["ecc"]
mapping["ecc"] = mapping["lenc"] - mapping["scc"]
mapping["strand"] = 0
for rid in greadst:
nscaff = Scaffold.init_from_longread((rid,lreads[rid]))
scaffolds[id(nscaff)] = nscaff
scaffolds = {}
# cluster np-reads
print("scaffolding long reads ....")
if args.summaryfile:
print("adding short reads ....")
sys.exit(0)