Debian Codegraph @

scripts/k3000/K3000_facts_to_fa.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 '''
5 Creation of a FA file from a compacted fact int file.
6 @author pierre peterlongo [email protected]
7 @deprecated
8 '''
9
10
11 __author__ = "Pierre Peterlongo"
12 __email__ = "[email protected]"
13
14 import sys
15 import K3000_common as kc
16 import argparse
17 from collections import defaultdict
18
19
20 rev_sign = lambda s: "+" if s == "-" else "-"
21
22 def store_variant_overlap_length(gfa_file_name):
23 '''
24 Given a graph_final format where overlaps are stored as:
25 L 0 + 400 + 43M OFL:i:1
26
27 Returns a dictionary:
28 key = source_fact_id, value = dictionary:
29 key = target_fact_id, value = overlap length (nucleotides)
30 '''
31 overlap_lengths = defaultdict(dict)
32 with open(gfa_file_name) as gfa_file:
33 while True:
34 line = gfa_file.readline()
35 if not line: break
36 if line[0] != 'L': continue
37 sline = line.strip().split()
38 OL = int(sline[5][:-1])
39 if OL<1: continue
40 # add the Overlap Length (OL)
41 overlap_lengths[sline[2]+sline[1]][sline[4]+sline[3]] = OL
42 # add it for the reverse
43 overlap_lengths[rev_sign(sline[4])+sline[3]][rev_sign(sline[2])+sline[1]] = OL
44 return overlap_lengths
45
46 def store_fact_sequence(gfa_file_name):
47 '''
48 Given a graph_final format where facts are stored as:
49 S 1 gtGCAATAAGAATTGTCTTTCTTATAATAATTGTCCAACTTAGgGTCAATTTCTGTACaaacaaCACCATCCAAt AS:-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;
50
51
52 Returns a dictionary:
53 key = fact_id, value = sequence:
54 '''
55 fact_sequence = defaultdict(dict)
56 with open(gfa_file_name) as gfa_file:
57 while True:
58 line = gfa_file.readline()
59 if not line: break
60 if line[0] != 'S': continue
61 sline = line.strip().split()
62 fact_sequence[sline[1]] = sline[2]
63 return fact_sequence
64
65 def get_sequence(facts_sequence, fact_id):
66 '''
67 fact id is given by \'+12\' or \'-12\'
68 fact sequences are indexed by \'12\' only.
69 if sign is \'+\' returns the corresponding sequence
70 else returns its reverse complement
71 '''
72 forward_sequence = facts_sequence[fact_id[1:]]
73 if fact_id[0] == '+': return forward_sequence
74 else: return kc.get_reverse_complement(forward_sequence)
75
76
77 def formatpathid (pathid: str) -> str:
78 '''
79 given a path id with format mp (eg. m2093)
80 transformt it to format +- (eg. -2093)
81 '''
82 if pathid[0] == 'm': return '-'+pathid[1:]
83 if pathid[0] == 'p': return '+'+pathid[1:]
84 raise ValueError
85
86 def generate_fa(gfa_file_name, path_facts_file_name):
87 '''
88 paths file contains lines as: haplotypeID ccID fact1;fact2;fact3 abundance
89 eg.
90 1 19 m463;p3596;m2093;p2782 583.0
91 we transforme them into a fasta file:
92 >CC_id|Path_id|abundance|Optional stuffs
93 ACGT...
94 '''
95
96 overlap_lengths = store_variant_overlap_length(gfa_file_name)
97 facts_sequence = store_fact_sequence(gfa_file_name)
98
99 with open(path_facts_file_name) as path_facts_file:
100 while True:
101 line = path_facts_file.readline()
102 if not line: break
103 sline = line.strip().split()
104 haplotype_id = sline[0]
105 cc_id = sline[1]
106 facts_path = sline[2]
107 abundance = sline[3]
108 facts_path = facts_path.strip(";") # just in case
109 previous_fact = formatpathid(facts_path.split(";")[0])
110 sequence = get_sequence(facts_sequence, previous_fact)
111 for current_fact in facts_path.split(";")[1:]:
112 current_fact = formatpathid(current_fact)
113 # TODO: remove those asserts when clearly tested.
114 assert (previous_fact in overlap_lengths)
115 assert (current_fact in overlap_lengths[previous_fact])
116 OL = overlap_lengths[previous_fact][current_fact]
117 assert(kc.hamming_near_perfect (get_sequence(facts_sequence, previous_fact)[-OL:], get_sequence(facts_sequence, current_fact)[:OL]))
118 # assert(get_sequence(facts_sequence, previous_fact)[-OL:].upper() == get_sequence(facts_sequence, current_fact)[:OL].upper())
119 sequence += get_sequence(facts_sequence, current_fact)[OL:]
120 previous_fact = current_fact
121 print(f">{cc_id}|{haplotype_id}|{facts_path}|{abundance}\n{sequence}")
122
123
124 def main():
125 '''
126 Creation of a FA file from a compacted fact int file.
127 '''
128
129 parser = argparse.ArgumentParser(description='From phased facts to fasta file.')
130 parser.add_argument("--gfa", type=str,
131 help="File containing the gfa generated by k3000/run.sh (usually named graph_final_X.gfa, with X the read set id)",
132 required=True)
133
134 parser.add_argument("--paths", type=str,
135 help="File containing phased facts (format: \"haplotypeID ccID fact1;fact2;fact3\"",
136 required=True)
137
138
139 args = parser.parse_args()
140 gfa_file_name = str(args.gfa)
141 path_facts_file_name = str(args.paths)
142 generate_fa(gfa_file_name, path_facts_file_name)
143
144
145
146
147
148 if __name__ == "__main__":
149 main()