-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgephi_lib.py
113 lines (105 loc) · 4.41 KB
/
gephi_lib.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
__author__ = 'hervemn'
def data2gexf(output_folder,fragments_list,fragments_contacts):
from xml.dom.minidom import Document
from collections import Counter
import os
doc = Document()
####################Create the <gexf> base element
gexf = doc.createElement("gexf")
gexf.setAttribute("xmlns", "http://www.gexf.net/1/2draft")
gexf.setAttribute("version","1.1")
doc.appendChild(gexf)
##################### Create the main <graph> element
maingraph = doc.createElement("graph")
maingraph.setAttribute("defaultedgetype" , "undirected")
maingraph.setAttribute("mode","static")
gexf.appendChild(maingraph)
#################### define nodes##################################
attributes_node = doc.createElement("attributes")
attributes_node.setAttribute("class" , "node")
attributes_node.setAttribute("mode","static")
maingraph.appendChild(attributes_node)
attribute_size = doc.createElement("attribute")
attribute_size.setAttribute("id" , "size")
attribute_size.setAttribute("title","size(kb)")
attribute_size.setAttribute("type","integer")
attributes_node.appendChild(attribute_size)
attribute_gc = doc.createElement("attribute")
attribute_gc.setAttribute("id" , "gc")
attribute_gc.setAttribute("title","gc_content")
attribute_gc.setAttribute("type","integer")
attributes_node.appendChild(attribute_gc)
attribute_coord = doc.createElement("attribute")
attribute_coord.setAttribute("id" , "coord")
attribute_coord.setAttribute("title","coordinates")
attribute_coord.setAttribute("type","string")
attributes_node.appendChild(attribute_coord)
#################### define edges##################################
attributes_edge = doc.createElement("attributes")
attributes_edge.setAttribute("class" , "edge")
attributes_edge.setAttribute("mode","static")
maingraph.appendChild(attributes_edge)
attribute_weight = doc.createElement("attribute")
attribute_weight.setAttribute("id" , "weight")
attribute_weight.setAttribute("title","weight")
attribute_weight.setAttribute("type","float")
attributes_edge.appendChild(attribute_coord)
########################### writing nodes #########################
nodes_list = doc.createElement("nodes")
maingraph.appendChild(nodes_list)
nodes_list_from_file = open(fragments_list,'r')
line_a = nodes_list_from_file.readline()
id_fragment = 0
dict_fragments = dict()
while 1:
line_a = nodes_list_from_file.readline()
if not line_a:
nodes_list_from_file.close()
break
id_fragment = id_fragment +1
node_ele = doc.createElement("node")
data = line_a.split()
id = data[0]
chrom = data[1]
start_pos = data[2]
end_pos = data[3]
size = data[4]
gc_content = data[5]
dict_fragments[id+chrom] = id_fragment
node_ele.setAttribute("id",str(id_fragment))
node_ele.setAttribute("size",str(size))
node_ele.setAttribute("coordinates",str(start_pos)+','+str(end_pos))
node_ele.setAttribute("label",chrom)
node_ele.setAttribute("gc_content",gc_content)
nodes_list.appendChild(node_ele)
########################### writing edges #########################s
edges_list = doc.createElement("edges")
edges_list_from_file = open(fragments_contacts,'r')
maingraph.appendChild(edges_list)
pool_contacts = Counter()
print 'Pool contacts in dictionnary'
while 1:
line_a = edges_list_from_file.readline()
if not line_a:
edges_list_from_file.close()
break
data = line_a.split()
source = min( int(data[0]),int(data[1]) )
target = max( int(data[0]),int(data[1]) )
pool_contacts[str(source)+'-'+str(target)] += 1
i = 0
for contact in pool_contacts.keys():
i = i+1
weight = pool_contacts[contact]
partners = contact.split('-')
edge_ele = doc.createElement("edge")
edge_ele.setAttribute("id",str(i))
edge_ele.setAttribute("source",partners[0])
edge_ele.setAttribute("target",partners[1])
edge_ele.setAttribute("weight",str(weight))
edges_list.appendChild(edge_ele)
output = open(os.path.join(output_folder,'graph.gexf'),'w')
print 'write gephi file'
doc.writexml(output,indent="\t",addindent="\t",newl="\n")
output.close()
print 'done.'