forked from phageParser/phageParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.py
103 lines (80 loc) · 2.76 KB
/
interactions.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
## Making interactions.txt file from sorted BLAST outputs
import json,os,csv
def csv_to_list(csv_file, delimiter=","):
with open (csv_file, 'r') as csv_con:
reader = csv.reader(csv_con, delimiter=delimiter)
return list(reader)
def convert_cells_to_floats(csv_cont):
for row in range(len(csv_cont)):
for cell in range(len(csv_cont[row])):
try:
csv_cont[row][cell] = float(csv_cont[row][cell])
except ValueError:
pass
def sort_by_column(csv_cont, col, reverse=False):
header = csv_cont[0]
body = csv_cont[1:]
if isinstance(col,str):
col_index = header.index(col)
else:
col_index = col
body = sorted(body,
key=operator.itemgetter(col_index),
reverse=reverse)
body.insert(0,header)
return body
def print_csv(csv_content):
print (50*'-')
for row in csv_content:
row=[str(e) for e in row]
print ('\t' .join(row))
print (50*'_')
def write_csv(dest, csv_cont):
with open(dest, 'w') as out_file:
writer= csv.writer(out_file, delimiter=',')
for row in csv_cont:
writer.writerow(row)
elements = []
phages = []
edges = []
for fn in os.listdir("output/sorted"):
# made node for each bacteria and enter in elements
bac_name = fn[7:16]
obj = {
"group": 'nodes',
"data": {"id":bac_name},
"classes": 'bacteria'
}
elements.append(obj)
csv_cont = csv_to_list("output/sorted/"+fn)[1:]
for i in range(len(csv_cont)):
#only make phage node if it doesn't already exists
if (not(csv_cont[i][1] in phages)):
obj = {
"group": 'nodes',
"data": { "id": csv_cont[i][1]},
"classes": 'phage'
}
elements.append(obj)
phages.append(csv_cont[i][1])
for i in range(len(csv_cont)):
if (not(bac_name+"_to_"+csv_cont[i][1] in edges)):
obj ={
"group": 'edges',
"data": {"id": bac_name+"_to_"+csv_cont[i][1],
"source": bac_name,
"target": csv_cont[i][1]}
}
elements.append(obj)
edges.append(bac_name+"_to_"+csv_cont[i][1])
# open each sorted file in output/sorted and get the phages, if phage is not phages list, add and create node, create edge
"""
json_file = open("json.txt","w")
json_file.write("cytoscape({" +"\n")
json_file.write("container: document.getElementById('cy')," +"\n")
json_file.write("elements: [" + "\n")
"""
with open("json.txt","w") as outfile:
json.dump(elements,outfile)
#for thing in elements:
# json.write(thing)