| 1 |
import sys |
| 2 |
import K3000_common as kc |
| 3 |
def update_closests(closests, source, target, distance): |
| 4 |
|
| 5 |
if source in closests: |
| 6 |
if distance < closests[source][1]: |
| 7 |
closests[source] = (target, distance) |
| 8 |
|
| 9 |
else: |
| 10 |
closests[source] = (target, distance) |
| 11 |
|
| 12 |
|
| 13 |
def store_closest_variant_id(gfa_file_name): |
| 14 |
""" |
| 15 |
Given a gfa file, store for each variant id (eg 12) the closest next variant (eg -42). In this case: |
| 16 |
closests[12]=-42 and |
| 17 |
closests[42]=[-12] |
| 18 |
|
| 19 |
|
| 20 |
The distance is computed from the BP field |
| 21 |
|
| 22 |
S 1 -577h;-977l;1354l; SP:0_44;11_64;32_75; BP:0_41;-26_41;-25_41; EV:0 FC:i:52 min:17 max:410 mean:180.0 AC:410;17;113; |
| 23 |
|
| 24 |
|
| 25 |
closests[-577]=-977 |
| 26 |
closests[-977]=1354 |
| 27 |
closests[-1354]=977 |
| 28 |
closests[977]=577 |
| 29 |
|
| 30 |
returns closests, that actualy contains alsoe the relative distance eg: |
| 31 |
closests[-577]=(-977,-26) |
| 32 |
closests[-977]=(1354,-25) |
| 33 |
closests[-1354]=(977,-25) |
| 34 |
closests[977]=(577,-26) |
| 35 |
. |
| 36 |
""" |
| 37 |
closests={} |
| 38 |
with open(gfa_file_name) as gfa_file: |
| 39 |
for line in gfa_file.readlines(): |
| 40 |
if line[0]!='S': continue |
| 41 |
line = line.split() |
| 42 |
ids = line[2].strip(';').split(';') |
| 43 |
distances = line[4].strip(';').split(':')[1].split(';') |
| 44 |
previous = int(ids[0][:-1]) |
| 45 |
for i in range(1,len(ids)): |
| 46 |
|
| 47 |
current = int(ids[i][:-1]) |
| 48 |
distance = int(distances[i].split('_')[0]) |
| 49 |
|
| 50 |
update_closests(closests, previous, current, distance) |
| 51 |
|
| 52 |
update_closests(closests, -current, -previous, distance) |
| 53 |
previous = current |
| 54 |
return closests |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def store_fact_extreme_snp_ids(gfa_file_name): |
| 65 |
""" |
| 66 |
Given a gfa file, store the id of each extreme SNP (without h or l) |
| 67 |
""" |
| 68 |
mfile = open(gfa_file_name) |
| 69 |
leftmost_snp_to_fact_id={} |
| 70 |
rightmost_snp_to_fact_id={} |
| 71 |
|
| 72 |
|
| 73 |
for line in mfile: |
| 74 |
|
| 75 |
|
| 76 |
if line[0]!="S": continue |
| 77 |
line=line.strip().split() |
| 78 |
compactedfact_id = int(line[1]) |
| 79 |
|
| 80 |
|
| 81 |
leftmost_snp_id=int(line[2].strip().split(';')[0][:-1]) |
| 82 |
if leftmost_snp_id not in leftmost_snp_to_fact_id: leftmost_snp_to_fact_id[leftmost_snp_id] = set() |
| 83 |
leftmost_snp_to_fact_id[leftmost_snp_id].add(compactedfact_id) |
| 84 |
if -leftmost_snp_id not in rightmost_snp_to_fact_id: rightmost_snp_to_fact_id[-leftmost_snp_id] = set() |
| 85 |
rightmost_snp_to_fact_id[-leftmost_snp_id].add(-compactedfact_id) |
| 86 |
|
| 87 |
|
| 88 |
rightmost_snp_id=int(line[2].strip(';').split(';')[-1][:-1]) |
| 89 |
if rightmost_snp_id not in rightmost_snp_to_fact_id: rightmost_snp_to_fact_id[rightmost_snp_id] = set() |
| 90 |
rightmost_snp_to_fact_id[rightmost_snp_id].add(compactedfact_id) |
| 91 |
if -rightmost_snp_id not in leftmost_snp_to_fact_id: leftmost_snp_to_fact_id[-rightmost_snp_id] = set() |
| 92 |
leftmost_snp_to_fact_id[-rightmost_snp_id].add(-compactedfact_id) |
| 93 |
|
| 94 |
mfile.close() |
| 95 |
return leftmost_snp_to_fact_id, rightmost_snp_to_fact_id |
| 96 |
|
| 97 |
|
| 98 |
def get_uppercase_sequence(sequence): |
| 99 |
"""given a sequence lBr with l and r in acgt and B in ACGT, return B""" |
| 100 |
res="" |
| 101 |
for l in sequence: |
| 102 |
if l>='A' and l<='Z': |
| 103 |
res+=l |
| 104 |
return res |
| 105 |
|
| 106 |
|
| 107 |
def store_remarkable_kmers(fa_file_name, k, leftmost_snp_to_fact_id, rightmost_snp_to_fact_id, LOs = {}, LIs = {}, RIs = {}, ROs = {}): |
| 108 |
""" |
| 109 |
Given a disco output, store for SNPs the remarkable (k-1)mers. |
| 110 |
|
| 111 |
Remarkable (k-1)mers are |
| 112 |
LO (Left Out) Leftmost (k-1)mer on the left unitig |
| 113 |
LI (Left In) Leftmost (k-1)mer not on the SNP (corresponds to the first k-1 upper case characters) |
| 114 |
RI (Right In) Rightmost (k-1)mer not on the SNP (corresponds to the last k-1 upper case characters) |
| 115 |
RO (Right Out) Rightmost (k-1)mer on the right unitig |
| 116 |
|
| 117 |
We store only LO and LI for SNPs that are a left most SNP of at least a fact |
| 118 |
We store only RO and RI for SNPs that are a right most SNP of at least a fact |
| 119 |
""" |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
mfile = open(fa_file_name) |
| 127 |
while True: |
| 128 |
line1 = mfile.readline() |
| 129 |
if not line1: break |
| 130 |
line1 = line1.strip() |
| 131 |
line2 = mfile.readline().strip() |
| 132 |
mfile.readline().strip() |
| 133 |
mfile.readline().strip() |
| 134 |
|
| 135 |
if not line1.startswith(">SNP"): continue |
| 136 |
|
| 137 |
snp_id = int(line1.split("|")[0].split('_')[-1]) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if snp_id in leftmost_snp_to_fact_id: |
| 143 |
|
| 144 |
LO = line2[:k-1].upper() |
| 145 |
central_sequence = get_uppercase_sequence(line2) |
| 146 |
LI = central_sequence[:k-1] |
| 147 |
|
| 148 |
if LO not in LOs: LOs[LO] = set() |
| 149 |
LOs[LO] = LOs[LO].union(leftmost_snp_to_fact_id[snp_id]) |
| 150 |
if LI not in LIs: LIs[LI] = set() |
| 151 |
LIs[LI] = LIs[LI].union(leftmost_snp_to_fact_id[snp_id]) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if snp_id in rightmost_snp_to_fact_id: |
| 158 |
|
| 159 |
RO = line2[-k+1:].upper() |
| 160 |
central_sequence = get_uppercase_sequence(line2) |
| 161 |
RI = central_sequence[-k+1:] |
| 162 |
|
| 163 |
if RO not in ROs: ROs[RO] = set() |
| 164 |
ROs[RO] = ROs[RO].union(rightmost_snp_to_fact_id[snp_id]) |
| 165 |
if RI not in RIs: RIs[RI] = set() |
| 166 |
RIs[RI] = RIs[RI].union(rightmost_snp_to_fact_id[snp_id]) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if -snp_id in leftmost_snp_to_fact_id: |
| 173 |
|
| 174 |
line2 = kc.get_reverse_complement(line2) |
| 175 |
LO = line2[:k-1].upper() |
| 176 |
central_sequence = get_uppercase_sequence(line2) |
| 177 |
LI = central_sequence[:k-1] |
| 178 |
|
| 179 |
if LO not in LOs: LOs[LO] = set() |
| 180 |
LOs[LO] = LOs[LO].union(leftmost_snp_to_fact_id[-snp_id]) |
| 181 |
if LI not in LIs: LIs[LI] = set() |
| 182 |
LIs[LI] = LIs[LI].union(leftmost_snp_to_fact_id[-snp_id]) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if -snp_id in rightmost_snp_to_fact_id: |
| 189 |
|
| 190 |
line2 = kc.get_reverse_complement(line2) |
| 191 |
RO = line2[-k+1:].upper() |
| 192 |
central_sequence = get_uppercase_sequence(line2) |
| 193 |
RI = central_sequence[-k+1:] |
| 194 |
|
| 195 |
if RO not in ROs: ROs[RO] = set() |
| 196 |
ROs[RO] = ROs[RO].union(rightmost_snp_to_fact_id[-snp_id]) |
| 197 |
if RI not in RIs: RIs[RI] = set() |
| 198 |
RIs[RI] = RIs[RI].union(rightmost_snp_to_fact_id[-snp_id]) |
| 199 |
|
| 200 |
|
| 201 |
mfile.close() |
| 202 |
return LOs, LIs, RIs, ROs |
| 203 |
|
| 204 |
def add_canonical(canonical_links, left_fact_id, right_fact_id): |
| 205 |
if abs(left_fact_id) <= abs(right_fact_id): |
| 206 |
if left_fact_id not in canonical_links: canonical_links[left_fact_id] = set() |
| 207 |
canonical_links[left_fact_id].add(right_fact_id) |
| 208 |
else: |
| 209 |
if -right_fact_id not in canonical_links: canonical_links[-right_fact_id] = set() |
| 210 |
canonical_links[-right_fact_id].add(-left_fact_id) |
| 211 |
|
| 212 |
|
| 213 |
def store_sequence_link_facts(LOs, LIs, RIs, ROs, k, rightmost_snp_to_fact_id, fa_file_name, canonical_links = {}): |
| 214 |
""" given the association (k-1)mers -> leftfacts, we may derive the links between facts |
| 215 |
1. RIA == LOB and ROA == LIB -> A+ -> B+ |
| 216 |
2. RIA == rc(ROB) and ROA == rc(RIB) -> A+ -> B- |
| 217 |
3. LOA == rc(LIB) and LIA == rc(LOB) -> A- -> B+ |
| 218 |
4. LOA == RIB and LIA == ROB -> A- -> B- (eq B+ -> A+) |
| 219 |
|
| 220 |
In this function we traverse again all variants, |
| 221 |
for those that are the END of a fact : |
| 222 |
check if their RI and RO may lead to one of the previous link |
| 223 |
for those whose reverse complement is the END of a fact : |
| 224 |
Reverse the sequence and then check if their RI and RO may lead to one of the previous link |
| 225 |
|
| 226 |
Case co-mapped: |
| 227 |
Finaly, two successive facts can exist if their extreme variants (eg r and l) were seen at least |
| 228 |
once co-mapped and if there exists no other variant (eg m) between r, and l. |
| 229 |
This information is stored in thie closests dictionary |
| 230 |
""" |
| 231 |
|
| 232 |
mfile = open(fa_file_name) |
| 233 |
while True: |
| 234 |
line1 = mfile.readline() |
| 235 |
if not line1: break |
| 236 |
line1 = line1.strip() |
| 237 |
line2 = mfile.readline().strip() |
| 238 |
mfile.readline().strip() |
| 239 |
mfile.readline().strip() |
| 240 |
|
| 241 |
if not line1.startswith(">SNP"): continue |
| 242 |
|
| 243 |
snp_id = int(line1.split("|")[0].split('_')[-1]) |
| 244 |
|
| 245 |
|
| 246 |
if snp_id in rightmost_snp_to_fact_id: |
| 247 |
ROA = line2[-k+1:].upper() |
| 248 |
central_sequence = get_uppercase_sequence(line2) |
| 249 |
RIA = central_sequence[-k+1:] |
| 250 |
|
| 251 |
|
| 252 |
if RIA in LOs and ROA in LIs: |
| 253 |
set_of_right_facts_compatible = LOs[RIA].intersection(LIs[ROA]) |
| 254 |
for right_fact_id in set_of_right_facts_compatible: |
| 255 |
for left_fact_id in rightmost_snp_to_fact_id[snp_id]: |
| 256 |
add_canonical(canonical_links, left_fact_id, right_fact_id) |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if kc.get_reverse_complement(RIA) in ROs and kc.get_reverse_complement(ROA) in RIs: |
| 262 |
set_of_right_facts_compatible = ROs[kc.get_reverse_complement(RIA)].intersection(RIs[kc.get_reverse_complement(ROA)]) |
| 263 |
for right_fact_id in set_of_right_facts_compatible: |
| 264 |
for left_fact_id in rightmost_snp_to_fact_id[snp_id]: |
| 265 |
add_canonical(canonical_links, left_fact_id, -right_fact_id) |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
mfile.close() |
| 276 |
return canonical_links |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
def store_link_facts_from_comapped(leftmost_snp_to_fact_id, rightmost_snp_to_fact_id, closests, canonical_links): |
| 282 |
""" |
| 283 |
given a set of rightmost snp to fact ids and leftmost snp to fact id, and the closests variants dictionary: |
| 284 |
add links between facts |
| 285 |
""" |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
for source, target in closests.items(): |
| 293 |
if target[1]<0: continue |
| 294 |
if source in rightmost_snp_to_fact_id and target[0] in leftmost_snp_to_fact_id: |
| 295 |
for left_fact_id in rightmost_snp_to_fact_id[source]: |
| 296 |
for right_fact_id in leftmost_snp_to_fact_id[target[0]]: |
| 297 |
add_canonical(canonical_links, left_fact_id, right_fact_id) |
| 298 |
return canonical_links |
| 299 |
|
| 300 |
|
| 301 |
sign = lambda s: "+" if s >=0 else "-" |
| 302 |
revsign = lambda s: "-" if s >=0 else "+" |
| 303 |
def print_canonical_successive_links(canonical_links): |
| 304 |
for source, targets in canonical_links.items(): |
| 305 |
for target in targets: |
| 306 |
print(f"L\t{abs(source)}\t{sign(source)}\t{abs(target)}\t{sign(target)}\t-1M") |
| 307 |
|
| 308 |
|
| 309 |
def print_original_gfa(gfa_file_name): |
| 310 |
mfile = open(gfa_file_name) |
| 311 |
print ("H\t#################") |
| 312 |
print ("H\t# GFA of variants") |
| 313 |
print ("H\t#################") |
| 314 |
print ("H\t# Nodes are (compacted) facts with their read mapping coverage. Eg. \"S 1 -577h;-977l;1354l; SP:0_44;11_64;32_75; BP:0_41;-26_41;-25_41; FC:i:52 min:17 max:410 mean:180.0 AC:17;410;113;\".") |
| 315 |
print ("H\t# * field SP stands for \"Sequence Position\". A bit useless in this file. It indicates for each allele of the fact its starting and ending position in the ACGT sequence, not shown here") |
| 316 |
print ("H\t# * field BP stands for \"Bubble Position\". For each allele of the fact it indicates:") |
| 317 |
print ("H\t# - first: the relative position of first nucleotide of the bubble with respect to the position of the last nucleotide of the bubble of the previous allele. This value is equal to zero for the first allele") |
| 318 |
print ("H\t# - second: the length of the bubble of the allele") |
| 319 |
print ("H\t# * field EV strands for \"Extreme Variant\". Facts having at least one variant with no input or no output edge are considered as facts having an extreme variants. Their value is set to EV:1. Else, the value is set to EV:0") |
| 320 |
print ("H\t# * field FC is the coverage of the fact, as provided by the total number of reads that phased at least two alleles of the fact") |
| 321 |
print ("H\t# * fields min, max, and mean stand resp. for the min, max and mean of the read coverage of all alleles") |
| 322 |
print ("H\t# * field AC stands for \"Allele Coverage\". The number of reads that map each allele is given in the same order as the variant ids (eg. \"17;410;113;\" are respectively, the coverages of variants \"-577h;-977l;1354l\")") |
| 323 |
print ("H\t# Four types of edges:") |
| 324 |
print ("H\t# 1. Overlap between facts. These links have an overlap length >0. Eg, \"L 1 - 29384 + 3M\", with:") |
| 325 |
print ("H\t# \"S 1 10011l;23229h;-21935l;-8929l;-24397l;10011h; RC:i:24\", and") |
| 326 |
print ("H\t# \"S 29384 21935l;-23229h;-10011l;24397l;-23229l;-25549h;-10011h; RC:i:43\".") |
| 327 |
print ("H\t# 2. Facts linked by paired end reads. Eg \"L 10735 + 29384 + 0M FC:i:5\".") |
| 328 |
print ("H\t# The coverage indicates the number of pairend read linking the two facts") |
| 329 |
print ("H\t# These links have an overlap of length 0.") |
| 330 |
print ("H\t# 3. Facts linked by unitigs. The unitig finishing a fact overlaps the unitig starting another fact. Eg \"L 19946 + 11433 + -1M\".") |
| 331 |
print ("H\t# These links are directed and validate the facts orientation. ") |
| 332 |
print ("H\t# These links have an overlap of length -1.") |
| 333 |
print ("H\t# 4. Facts sharing at least one SNP identifier.") |
| 334 |
print ("H\t# These links have an overlap of length -2.") |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
for l in mfile: |
| 339 |
print(l,end='') |
| 340 |
mfile.close() |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
def main (gfa_file_name, co_fa_file_name, unco_fa_file_name): |
| 346 |
|
| 347 |
sys.stderr.write("#Print original gfa\n") |
| 348 |
print_original_gfa(gfa_file_name) |
| 349 |
|
| 350 |
|
| 351 |
sys.stderr.write("#Store extreme left and right variant id for each fact\n") |
| 352 |
leftmost_snp_to_fact_id, rightmost_snp_to_fact_id = store_fact_extreme_snp_ids(gfa_file_name) |
| 353 |
k = kc.determine_k(co_fa_file_name) |
| 354 |
sys.stderr.write("#Store remarkable kmers for each such variant\n") |
| 355 |
LOs, LIs, RIs, ROs = store_remarkable_kmers(co_fa_file_name, k,leftmost_snp_to_fact_id, rightmost_snp_to_fact_id) |
| 356 |
LOs, LIs, RIs, ROs = store_remarkable_kmers(unco_fa_file_name, k,leftmost_snp_to_fact_id, rightmost_snp_to_fact_id, LOs, LIs, RIs, ROs) |
| 357 |
|
| 358 |
sys.stderr.write("#Compute successive links obtained from kmers\n") |
| 359 |
canonical_links = store_sequence_link_facts(LOs, LIs, RIs, ROs, k, rightmost_snp_to_fact_id, co_fa_file_name) |
| 360 |
canonical_links = store_sequence_link_facts(LOs, LIs, RIs, ROs, k, rightmost_snp_to_fact_id, unco_fa_file_name, canonical_links) |
| 361 |
del LOs, LIs, RIs, ROs |
| 362 |
|
| 363 |
sys.stderr.write("#Store distances between co-mapped variants\n") |
| 364 |
closests = store_closest_variant_id(gfa_file_name) |
| 365 |
|
| 366 |
sys.stderr.write("#Compute successive links obtained from co-mapped variants\n") |
| 367 |
canonical_links = store_link_facts_from_comapped(leftmost_snp_to_fact_id, rightmost_snp_to_fact_id, closests, canonical_links) |
| 368 |
|
| 369 |
sys.stderr.write(f"#Write successive links\n") |
| 370 |
print_canonical_successive_links(canonical_links) |
| 371 |
|
| 372 |
|
| 373 |
if __name__ == "__main__": |
| 374 |
main(sys.argv[1], sys.argv[2], sys.argv[3]) |