-
Notifications
You must be signed in to change notification settings - Fork 3
/
03-flat2ld-bin.py
119 lines (108 loc) · 4.17 KB
/
03-flat2ld-bin.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
110
111
112
113
114
115
116
117
118
119
import os, struct
def exclude_empty_line(INPUT_FILE, OUTPUT_FILE):
input_file = open(INPUT_FILE)
output_file = open(OUTPUT_FILE, 'w')
index = 1
line = input_file.readline()
while line:
line=line.strip()
if line:
output_file.write(line + '\n')
if index%1000 == 0:
print("finish index==" + str(index//1000))
index = index + 1
line = input_file.readline()
input_file.close()
output_file.close()
def split_data_to_file(dir):
hppids_train_ppis = open(dir+"/hppids-train-ppis.txt" , 'w')
hppids_train_labels = open(dir+"/hppids-train-labels.txt", 'w')
hppids_test_ppis = open(dir+"/hppids-test-ppis.txt" , 'w')
hppids_test_labels = open(dir+"/hppids-test-labels.txt" , 'w')
print("Split Supp-A-36630-HPRD-positive-interaction.txt ...")
positive_interaction_file = open(dir+"/Supp-A-36630-HPRD-positive-interaction.txt")
index = 1
line = positive_interaction_file.readline()
while line:
line=line.strip()
if line:
if index<=30000:
hppids_train_ppis.write(line+'\n')
hppids_train_labels.write("1"+'\n')
else:
hppids_test_ppis.write(line+'\n')
hppids_test_labels.write("1"+'\n')
if index%1000 == 0:
print("finish index==" + str(index//1000))
index = index + 1
line = positive_interaction_file.readline()
positive_interaction_file.close()
print("Split Supp-B-36480-HPRD-negative-interaction.txt ...")
negative_interaction_file = open(dir+"/Supp-B-36480-HPRD-negative-interaction.txt")
index = 1
line = negative_interaction_file.readline()
while line:
line=line.strip()
if line:
if index<=30000:
hppids_train_ppis.write(line+'\n')
hppids_train_labels.write("0"+'\n')
else:
hppids_test_ppis.write(line+'\n')
hppids_test_labels.write("0"+'\n')
if index%1000 == 0:
print("finish index==" + str(index//1000))
index = index + 1
line = negative_interaction_file.readline()
negative_interaction_file.close()
hppids_train_ppis.close()
hppids_train_labels.close()
hppids_test_ppis.close()
hppids_test_labels.close()
def convert_txt_to_bin(txt_file_name, bin_file_name, rows, columns, type, fmt):
print("Convert "+txt_file_name+" to "+bin_file_name+" ...")
txt_file = open(txt_file_name)
bin_file = open(bin_file_name, 'wb')
bytes = struct.pack('ii', rows, columns)
bin_file.write(bytes)
bytes_len = len(bytes)
index = 1
line = txt_file.readline()
while line:
line=line.strip()
if line:
strs = line.split(" ")
datas = [type(s) for s in strs]
for data in datas:
bytes = struct.pack(fmt,data)
bin_file.write(bytes)
bytes_len = bytes_len + len(bytes)
if index%1000 == 0:
print("finish index==" + str(index//1000))
index = index + 1
line = txt_file.readline()
print("Converted bytes length= "+str(bytes_len))
txt_file.close()
bin_file.close()
def main():
CWD = os.getcwd()
LDDD = CWD + "/data/04-ld"
LDBDD = CWD + "/data/04-ld-bin"
if not os.path.exists(LDBDD):
os.makedirs(LDBDD)
FILES = [
"Supp-A-36630-HPRD-positive-interaction.txt",
"Supp-B-36480-HPRD-negative-interaction.txt",
]
for FILE in FILES:
print("Processing "+FILE+" ...")
exclude_empty_line(LDDD+"/"+FILE, LDBDD+"/"+FILE)
print("Processed!")
split_data_to_file(LDBDD)
convert_txt_to_bin(LDBDD+"/hppids-train-ppis.txt", LDBDD+"/hppids-train-ppis.bin", 60000, 1260, float, 'f')
convert_txt_to_bin(LDBDD+"/hppids-train-labels.txt", LDBDD+"/hppids-train-labels.bin", 60000, 1, int, 'i')
convert_txt_to_bin(LDBDD+"/hppids-test-ppis.txt", LDBDD+"/hppids-test-ppis.bin", 12915, 1260, float, 'f')
convert_txt_to_bin(LDBDD+"/hppids-test-labels.txt", LDBDD+"/hppids-test-labels.bin", 12915, 1, int, 'i')
print("Finished!!!")
if __name__=="__main__":
main()