forked from emreg00/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_converter.py
163 lines (140 loc) · 4.96 KB
/
file_converter.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import network_utilities
import TsvReader
def main():
input_file = "test.txt"
mapping_file = "node_mapping.txt.genesymbol"
output_file = "test.txt.genesymbol"
#convert_ids_using_mapping_file(input_file, mapping_file, output_file)
return
def convert_ids_using_mapping_file(input_file, mapping_file, output_file, one_gene_per_node=True, delim="\t"):
nodes, dummy, node_to_data, dummy = network_utilities.get_nodes_and_edges_from_sif_file(file_name = input_file, store_edge_type = False)
id_to_mapped_ids = get_id_to_mapped_id_mapping(mapping_file, delim=delim)
values = []
#for node, d in node_to_data.iteritems():
for node in nodes:
if node not in id_to_mapped_ids:
continue
if one_gene_per_node:
genes = [ id_to_mapped_ids[node][0] ]
else:
genes = id_to_mapped_ids[node]
for gene in genes:
#values.append((d, gene))
values.append(gene)
values.sort()
values.reverse()
#i = 1
f = open(output_file, 'w')
#f2 = open(output_file + ".ranks", 'w')
#for d, gene in values:
for gene in values:
f.write("%s\n" % (gene))
#f.write("%s\t%s\n" % (gene, str(score)))
#f2.write("%s\t%d\n" % (gene, i))
#i += 1
f.close()
#f2.close()
return
def convert_mapping_file_to_reversed_mapping_file(node_mapping_file, delim="\t"):
id_to_mapped_ids = get_id_to_mapped_id_mapping(node_mapping_file, delim=delim)
#f = open(node_mapping_file + ".single", 'w')
#f.write("user entity id\tgene symbol\n")
f2 = open(node_mapping_file + ".reversed", 'w')
words = open(node_mapping_file).readline().strip().split(delim)
words.reverse()
f2.write("%s\n" % delim.join(words))
for node, mapped_ids in id_to_mapped_ids.iteritems():
#f.write("%s\t%s\n" % (node, mapped_ids[0]))
f2.write("%s\t%s\n" % (mapped_ids[0], node))
#f.close()
f2.close()
return
def get_id_to_mapped_id_mapping(node_mapping_file, delim="\t", inner_delim = ","):
reader = TsvReader.TsvReader(node_mapping_file, delim=delim, inner_delim = inner_delim)
columns, id_to_mapped_ids = reader.read(fields_to_include = None, merge_inner_values = True)
id_to_mapped_ids_formatted = {}
for node, vals in id_to_mapped_ids.iteritems():
vals = reduce(lambda x,y: x+y, vals)
if "-" in vals:
vals.remove("-")
if len(vals) < 1:
continue
id_to_mapped_ids_formatted[node] = vals
return id_to_mapped_ids_formatted
def create_id_mapping_file_from_gene_info(gene_info_file, gene_ids, output_file, one_gene_per_node=True):
reader = TsvReader.TsvReader(gene_info_file, delim="\t", inner_delim = ",")
columns, id_to_mapped_ids = reader.read(fields_to_include = None, keys_to_include=gene_ids, merge_inner_values = True)
f = open(output_file, 'w')
f.write("geneid\tgenesymbol\n")
for node, vals in id_to_mapped_ids.iteritems():
vals = reduce(lambda x,y: x+y, vals)
if "-" in vals:
vals.remove("-")
if len(vals) < 1:
continue
if one_gene_per_node:
f.write("%s\t%s\n" % (node, vals[0]))
else:
[ f.write("%s\t%s\n" % (node, val)) for val in vals ]
f.close()
return
def output_mapped_node_id_scores(output_scores_file, node_mapping_file, one_gene_per_node=True, output_file=None):
"""
Output mapped ids of nodes
"""
dummy, dummy, node_to_score, dummy = network_utilities.get_nodes_and_edges_from_sif_file(file_name = output_scores_file, store_edge_type = False)
id_to_mapped_ids = get_id_to_mapped_id_mapping(node_mapping_file)
values = []
for node, score in node_to_score.iteritems():
if node not in id_to_mapped_ids:
continue
if one_gene_per_node:
genes = [ id_to_mapped_ids[node][0] ]
else:
genes = id_to_mapped_ids[node]
for gene in genes:
values.append((score, gene))
values.sort()
values.reverse()
included = set()
i = 1
if output_file is not None:
f = open(output_file, 'w')
f2 = open(output_file + ".ranks", 'w')
f3 = open(output_file + ".unique", 'w')
for score, gene in values:
f.write("%s\t%s\n" % (gene, str(score)))
f2.write("%s\t%d\n" % (gene, i))
if gene not in included:
f3.write("%s\t%s\n" % (gene, str(score)))
included.add(gene)
i += 1
f.close()
f2.close()
f3.close()
else:
print "%s\t%f" % (gene, score)
return
def convert_node_scores_to_edge_scores(network_file, node_file, output_file):
f = open(node_file)
geneid_to_score = {}
for line in f:
geneid, score = line.strip().split("\t")
geneid_to_score[geneid] = float(score)
f.close()
f = open(network_file)
interactions = []
for line in f:
geneid1, dummy, geneid2 = line.strip().split(" ")
score = (geneid_to_score[geneid1] + geneid_to_score[geneid2]) / 2
interactions.append((score, geneid1, geneid2))
f.close()
interactions.sort()
interactions.reverse()
f_out = open(output_file, 'w')
for score, geneid1, geneid2 in interactions:
f_out.write("%s\t%s\t%f\n" % (geneid1, geneid2, score))
f_out.close()
return
if __name__ == "__main__":
main()