Debian Codegraph @

scripts/k3000/K3000_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 a compacted fact int file.
6 '''
7
8
9 __author__ = "Pierre Peterlongo"
10 __email__ = "[email protected]"
11
12 import sys
13 import K3000_common as kc
14
15
16
17
18
19
20 def generate_sequence_paths(sequences, compacted_fact_file_name, int_facts_format):
21 '''
22 Given a set of indexed sequences, the k value and some compacted facts, prints the corresponding fasta seequences
23 Enables also to validate the compacted facts.
24 '''
25 mfile = open(compacted_fact_file_name)
26 nb_non_writen=0
27 for line in mfile:
28 if line[0] == "#": continue
29 # * int_facts_format:
30 # 38772_0;-21479_1;27388_3;-494_28;-45551_36;-11894_10;-50927_7;-66981_10;29405_22;34837_1;20095_5;
31 # * not int_facts_format:
32 # -1000l_0;-136l_-24;-254h_-18;493l_-16; -577h_0;-977h_-26;1354h_-25; => 1
33
34 line = line.split("=")[0].strip() # in case a line contains an abundance, it is indicated by " => 1"
35 # from "-1000l_0;-136l_-24;-254h_-18;493l_-16; -577h_0;-977h_-26;1354h_-25; => 1"
36 # to "-1000l_0;-136l_-24;-254h_-18;493l_-16; -577h_0;-977h_-26;1354h_-25;"
37
38 # if the input fact contains a space it means that this is a paired fact, each part is treated
39 for fact_as_ids in line.split():
40 # print(fact_as_ids)
41 toprint, header, bubble_facts_position_start_stops, full_seq = kc.line2seq(fact_as_ids, sequences, int_facts_format, 0)
42 if toprint:
43 print(f">{header}\t{bubble_facts_position_start_stops}\n{full_seq}")
44 else: nb_non_writen+=1
45
46 if nb_non_writen>0:
47 sys.stderr.write("Warning, "+str(nb_non_writen)+" facts were removed as their sequence concatenation were not coherent or because they contained non coherent predictions\n")
48
49 mfile.close()
50
51 def is_int_fact(file_name):
52 '''
53 Determines if a given file is under the format
54 2468_0;-2708_6;1954_-25;1154_-26; (called 'int_facts')
55 or
56 -577h_0;-977h_-26;1354h_-25; => 1
57 '''
58 with open(file_name) as my_file:
59 while True:
60 line = my_file.readline()
61 if not line:
62 raise RuntimeError (f'input file {file_name} does not contain correctly formated facts')
63 if line[0] == "#" : continue # comment
64 if ';' not in line:
65 raise RuntimeError (f'input file {file_name} does not contain correctly formated facts. The line {line} should contain at least a \';\'')
66 # here we are check the kind of file:
67 line = line.split()[0]
68 if 'h' in line or 'l' in line:
69 return False
70 return True
71
72
73
74 def main():
75 '''
76 Creation of a FA file from a compacted fact int file.
77
78 '''
79 sequences=kc.index_sequences(sys.argv[1]) #for each snp id: sequences[snp_id]=[left_unitig_len, right_unitig_len, upperseq, lowerseq]
80 sequences=kc.index_sequences(sys.argv[2], sequences) #for each snp id: sequences[snp_id]=[left_unitig_len, right_unitig_len, upperseq, lowerseq]
81 int_facts_format = is_int_fact(sys.argv[3])
82 generate_sequence_paths(sequences, sys.argv[3], int_facts_format)
83
84
85
86
87 if __name__ == "__main__":
88 main()