-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_io.py
51 lines (46 loc) · 936 Bytes
/
utils_io.py
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
# shared variables and functions
import os, json
from Bio import SeqIO
def read_json(file_name):
try:
handle = open(file_name, 'r')
except IOError:
pass
else:
data = json.load(handle)
handle.close()
return data
def write_json(data, file_name, indent=1):
try:
handle = open(file_name, 'w')
except IOError:
pass
else:
json.dump(data, handle, indent=indent)
handle.write("\n")
handle.close()
def read_fasta(file_name):
alignment = []
try:
handle = open(file_name, 'r')
except IOError:
pass
else:
for record in SeqIO.parse(handle, "fasta"):
v = {
"strain": record.description,
"seq": str(record.seq).upper()
}
alignment.append(v)
handle.close()
return alignment
def write_fasta(viruses, file_name):
try:
handle = open(file_name, 'w')
except IOError:
pass
else:
for v in viruses:
handle.write(">" + v['strain'] + "\n")
handle.write(v['seq'] + "\n")
handle.close()