Debian Codegraph @

scripts/k3000/K3000_phased_paths_to_fa.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 '''
5 Creation of a FA file from set of phased facts, as obtained by the ILP model:
6 #ID #path ("variant id"_"distance between the end of the previous bubble and the start of the current one) #abundance
7 1 1054l_0;-826l_-26;-914l_-31;-434h_-36;-369h_-15;675h_-5;-543l_-17;-386l_-27;326l_-1;88h_-10;359l_-21;542l_-28;-540h_-36;-153h_-2;382l_-3;1077h_-37;40h_56;413l_-40;-744h_-32;-510h_2;-636l_-22;-1076l_-22;646h_39;897l_-32;968h_-34;1130h_18;578l_-28;812l_-8;-547h_-10;412h_-14;435h_-3;113h_-16;-975l_-35;-807l_8;-1094l_-8;-447h_-40;-802l_-23;-779l_-19;1066h_-25;-429l_11;701h_-35;180l_-30;-563h_6;67h_5;169h_-36;-864l_-21;-219l_-14;282h_5;567h_-31;453l_-24;740l_-35;-255l_-29;723h_-12;1050h_-24;-204h_17;303h_-12;-237l_-34;236h_-30;-408h_-32;-102l_-6;-1128l_-29;-474h_-12;1092h_-14;-491h_-35;889h_-35;45l_-8;1182l_-40;477l_-28;-172l_-13;1060h_-13;221h_-26;157h_-31;1091l_-17;-1021l_-2;846h_-33;1014h_-36;964h_-38; 162
8
9 Outputs:
10 >path_ID|abundance|info about starting and ending positions of each allele on the global sequence (SP = Sequence positions), and the Bubble Positions (BP). For each allele in the fact we store the distance between the bubble start (upper case letter and the end of the previous bubble (also upper case letter). We add the length of the bubble (upper case letter).
11 ACGTN* sequence
12 '''
13
14
15 __author__ = "Pierre Peterlongo"
16 __email__ = "[email protected]"
17
18
19 import sys
20 import K3000_common as kc
21
22
23
24
25
26 def generate_sequence_paths(sequences, compacted_fact_file_name, hamming_max=1):
27 '''
28 Given a set of indexed sequences, the k value and some compacted facts, prints the corresponding fasta seequences
29 Enables also to validate the compacted facts.
30 '''
31 non_writen_list=""
32 mfile = open(compacted_fact_file_name)
33 nb_non_writen=0
34 for line in mfile:
35 # 1 1054l_0;-826l_-26;-914l_-31;-434h_-36;-369h_-15;675h_-5;-543l_-17;-386l_-27;326l_-1;88h_-10;359l_-21;542l_-28;-540h_-36;-153h_-2;382l_-3;1077h_-37;40h_56;413l_-40;-744h_-32;-510h_2;-636l_-22;-1076l_-22;646h_39;897l_-32;968h_-34;1130h_18;578l_-28;812l_-8;-547h_-10;412h_-14;435h_-3;113h_-16;-975l_-35;-807l_8;-1094l_-8;-447h_-40;-802l_-23;-779l_-19;1066h_-25;-429l_11;701h_-35;180l_-30;-563h_6;67h_5;169h_-36;-864l_-21;-219l_-14;282h_5;567h_-31;453l_-24;740l_-35;-255l_-29;723h_-12;1050h_-24;-204h_17;303h_-12;-237l_-34;236h_-30;-408h_-32;-102l_-6;-1128l_-29;-474h_-12;1092h_-14;-491h_-35;889h_-35;45l_-8;1182l_-40;477l_-28;-172l_-13;1060h_-13;221h_-26;157h_-31;1091l_-17;-1021l_-2;846h_-33;1014h_-36;964h_-38; 162
36
37 if line[0] == "#": continue
38 line = line.strip().split()
39 path_id=int(line[0])
40 abundance=int(line[2])
41
42 toprint, _, _, full_seq = kc.line2seq(line[1], sequences, False, hamming_max)
43 if toprint:
44 # print(f">path_{path_id}|{abundance}|{header}|{bubble_facts_position_start_stops}\n{full_seq}")# TMI
45 print(f">path_{path_id}|{abundance}\n{full_seq}")
46 else:
47 nb_non_writen+=1
48 non_writen_list+=f"path_{path_id}|{abundance}, "
49
50 if nb_non_writen>0:
51 sys.stderr.write(f"Warning, {nb_non_writen} facts were removed as their sequence concatenation were not coherent or because they contained non coherent predictions\n")
52 sys.stderr.write(f"Removed paths: {non_writen_list[:-2]}\n")
53 mfile.close()
54
55
56
57
58
59 def main():
60 '''
61 Creation of a FA file from a compacted fact int file.
62 '''
63 sequences=kc.index_sequences(sys.argv[1]) #for each snp id: sequences[snp_id]=[left_unitig_len, right_unitig_len, upperseq, lowerseq]
64 sequences=kc.index_sequences(sys.argv[2], sequences) #for each snp id: sequences[snp_id]=[left_unitig_len, right_unitig_len, upperseq, lowerseq]
65 generate_sequence_paths(sequences, sys.argv[3])
66
67
68
69
70 if __name__ == "__main__":
71 main()