-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHapFasta.py
More file actions
178 lines (143 loc) · 6.09 KB
/
HapFasta.py
File metadata and controls
178 lines (143 loc) · 6.09 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import csv
__description__ = '''Prepare traits for building haplotype network.'''
ARGS = [
('-input', dict(metavar='<str>', type=str, help='''aligned fasta sequences''', required=True)),
('-output', dict(metavar='<str>', type=str, help='''name prefix of output files''', required=True))
]
def run(input_file, result_file):
names = []
org_seq = []
AA = "ACGTU"
new_line = False
with open(input_file, 'r') as f:
for line in f:
line = line.strip()
if line.startswith(">"):
new_line = True
names.append(line[1:])
else:
if new_line:
org_seq.append(line.upper())
new_line = False
else:
org_seq[len(org_seq) - 1] += line.upper()
if not org_seq:
print("No valid sequence found for building haplotype table.")
return
org_seq_clean = org_seq.copy()
is_var = bytearray(max(map(len, org_seq)))
for i, seq in enumerate(org_seq):
print(f"Checking wobbles in seq {names[i]}", end='\r')
for j, c in enumerate(seq):
is_var[j] = is_var[j] or (c not in AA)
for i in range(len(org_seq_clean)):
temp = list(org_seq_clean[i])
print(f"Cleaning sequence {names[i]}", end='\r')
for j in range(len(temp)):
if is_var[j]:
temp[j] = '#'
org_seq_clean[i] = ''.join(temp).replace('#', '')
hap_seq = []
hap_name = []
for i, line in enumerate(org_seq_clean):
print(f"Calculating haplotype for {names[i]}", end='\r')
try:
hap_index = hap_seq.index(line)
hap_name.append(hap_index)
except ValueError:
hap_seq.append(line)
hap_name.append(len(hap_seq) - 1)
with open(result_file + "_seq2hap.csv", "w", newline='') as rf:
writer = csv.writer(rf, delimiter=',')
writer.writerow(["id", "hap", "name", "trait"])
for i, line in enumerate(org_seq_clean):
name_trait = names[i].upper().split('|', 1)
writer.writerow([str(i),
f'Hap_{hap_name[i]}',
name_trait[0],
name_trait[1] if len(name_trait) > 1 else '[NULL]'])
traits = set()
for i, name in enumerate(names):
name_trait = name.upper().split('|', 1)
s = name_trait[1] if len(name_trait) > 1 else '[NULL]'
traits.add(s)
hap_seq = [""] + hap_seq
trait_list = [""] + list(sorted(traits))
hap_trait_table = [[0 for i in range(len(trait_list))] for j in range(len(hap_seq))]
for i in range(1, len(trait_list)):
hap_trait_table[0][i] = trait_list[i]
for i in range(1, len(hap_seq)):
hap_trait_table[i][0] = f'Hap_{hap_name[i - 1]}'
hap_trait_table[0][0] = ""
for i, name in enumerate(names):
name_trait = name.upper().split('|', 1)
s = name_trait[1] if len(name_trait) > 1 else '[NULL]'
hap_trait_table[hap_name[i] + 1][trait_list.index(s)] += 1
with open(result_file + "_hap_trait.csv", "w", newline='') as rf:
writer = csv.writer(rf, delimiter=',')
for row in hap_trait_table:
writer.writerow(row)
with open(result_file + "_seq_trait.csv", "w", newline='') as rf:
writer = csv.writer(rf, delimiter=',')
writer.writerow(trait_list)
for name in names:
name_trait = name.upper().split('|', 1)
s = name_trait[1] if len(name_trait) > 1 else '[NULL]'
writer.writerow([name_trait[0]] + [int(t in s.upper()) for t in trait_list[1:]])
reduced_seq = hap_seq.copy()
is_var = bytearray(max(map(len, hap_seq[1:])))
print("Checking haplotype site ...")
for j in range(len(is_var)):
is_var[j] = (
all(len(seq) > j and (seq[j] in AA) for seq in hap_seq[1:]) and
len(set(seq[j] for seq in hap_seq[1:])) > 1)
for i in range(1, len(reduced_seq)):
temp = list(reduced_seq[i])
for j in range(len(temp)):
if not is_var[j]:
temp[j] = '#'
reduced_seq[i] = ''.join(temp).replace('#', '')
reduced_seq1 = org_seq_clean.copy()
for i in range(len(reduced_seq1)):
temp = list(reduced_seq1[i])
for j in range(len(temp)):
if not is_var[j]:
temp[j] = '#'
reduced_seq1[i] = ''.join(temp).replace('#', '')
with open(result_file + "_hap.fasta", "w") as rf:
for i in range(1, len(reduced_seq)):
rf.write(f'>Hap_{i - 1}\n')
rf.write(f'{reduced_seq[i]}\n')
with open(result_file + "_hap.phy", "w") as rf:
rf.write(f'{len(reduced_seq) - 1} {len(reduced_seq[1])}\n')
for i in range(1, len(reduced_seq)):
rf.write(f'Hap_{i - 1} {reduced_seq[i]}\n')
with open(result_file + "_seq.phy", "w") as rf:
rf.write(f'{len(reduced_seq1)} {len(reduced_seq1[0])}\n')
for i, seq in enumerate(reduced_seq1):
name = names[i].upper().split('|', 1)[0]
rf.write(f'{name} {seq}\n')
with open(result_file + "_seq.fasta", "w") as rf:
for i, seq in enumerate(reduced_seq1):
name = names[i].upper().split('|', 1)[0]
rf.write(f'>{name}\n')
rf.write(f'{seq}\n')
with open(result_file + ".meta", "w") as rf:
for i, seq in enumerate(reduced_seq1):
name_trait = names[i].upper().split('|', 1)
s = name_trait[1] if len(name_trait) > 1 else '[NULL]'
rf.write(f'{name_trait[0]} {s}\n')
print(f'Finished creating haplotypes, {len(hap_seq) - 1} haplotypes and {len(reduced_seq[1])} variation sites in total.')
def main(pars, args):
input_file = args.input
result_file = args.output
run(input_file, result_file)
if __name__ == "__main__":
pars = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=__description__)
for param in ARGS:
pars.add_argument(param[0], **param[1])
args = pars.parse_args()
main(pars, args)