-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_parser.py
More file actions
56 lines (54 loc) · 2.01 KB
/
data_parser.py
File metadata and controls
56 lines (54 loc) · 2.01 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
from pathlib import Path
import numpy as np
from collections import deque
neg_directory = Path("/results/struct/negatives/negatives/negatives_all")
pos_directory = Path("/results/struct/positives/positives/positives_all")
directories = [neg_directory, pos_directory]
files = []
seq_dicts = []
max=0
for file in files:
with open(file, "r") as f:
lines = f.readlines()
for line in lines:
if line.startswith(">"):
continue
else:
if len(line.strip())>max:
max = len(line.strip())
for directory in directories:
seq_dict = {}
for file in directory.iterdir():
with open(file, "r") as f:
mode =0
if file.name.endswith(".fa"):
mode = 1
elif file.name.endswith(".txt"):
mode = 2
lines = f.readlines()
for i in range(len(lines)):
if mode==0 and i<=0:
continue
if lines[i].startswith(">"):
id = lines[i].split(':')[0][1:]
#smt to trim line and get the sequence idenity
if seq_dict[id] is None:
seq_dict[id] = []
while(i<len(lines) and not lines[i].startswith(">")):
if mode == 0:
line = lines[i].strip().split(" ")[1]
for j in range(len(line),max):
line.append('0.0')
if mode == 1:
line = lines[i].strip().lfit(max,"n")
else:
line = lines[i].strip()
seq_dict[id].append(line)
i+=1
seq_dicts.append((str(directory),seq_dict))
lists = deque()
for key, value in seq_dict.items():
l = [key,list(value)]
lists.append(l)
arr = np.array(lists)
np.savetxt(str(directory)+".csv", arr, delimiter=",", fmt='%s')