-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_htlv1.py
113 lines (80 loc) · 1.93 KB
/
data_htlv1.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
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import os
#from util import seq_matrix
# In[ ]:
def seq_matrix(seq_list, dim):
tensor = np.zeros((len(seq_list),dim,4))
y = []
for i in range(len(seq_list)):
seq = seq_list[i]
j = 0
for s in seq:
if s == 'A' or s == 'a':
tensor[i][j] = [1,0,0,0]
if s == 'T' or s == 't':
tensor[i][j] = [0,1,0,0]
if s == 'C' or s == 'c':
tensor[i][j] = [0,0,1,0]
if s == 'G' or s == 'g':
tensor[i][j] = [0,0,0,1]
if s == 'N':
tensor[i][j] = [0,0,0,0]
j += 1
return tensor
# In[ ]:
def bed_to_fasta():
beds = ['data/VIS_pos_final.bed','data/VIS_neg_final.bed']
for bed in beds:
os.system("bedtools getfasta -fi ../ref/hg19.fa -bed " + bed + " -fo " + bed + ".fasta")
# In[ ]:
def fasta_to_matrix():
seq_name = ['data/VIS_pos_final.bed' , 'data/VIS_neg_final.bed']
print(seq_name)
dim = 1000
print('seq')
### Seq ###
for name in seq_name:
if 'pos' in name:
print(name)
y = []
seq = []
positive_seq_file = open(name +'.fasta')
lines = positive_seq_file.readlines()
positive_seq_file.close()
for line in lines:
line = line.strip()
if line[0] == '>':
y.append(1)
else:
seq.append(line)
X1 = seq_matrix(seq,dim)
print('pos_ending!')
np.save(name.split('.')[0], X1)
if 'neg' in name:
print(name)
y = []
seq = []
negative_seq_file = open(name +'.fasta')
lines = negative_seq_file.readlines()
negative_seq_file.close()
for line in lines:
line = line.strip()
if line[0] == '>':
y.append(0)
else:
seq.append(line)
X0 = seq_matrix(seq,dim)
print('neg_ending!')
np.save(name.split('.')[0], X0)
X = np.concatenate([X1,X0])
y = np.concatenate([np.ones(len(X1)), np.zeros(len(X0))])
np.save('data/x_VISDB_fulldata.npy', X)
np.save('data/y_VISDB_fulldata.npy', y)
# In[ ]:
if __name__ == '__main__':
bed_to_fasta()
fasta_to_matrix()
# In[ ]: