-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerger.py
More file actions
180 lines (159 loc) · 4.2 KB
/
merger.py
File metadata and controls
180 lines (159 loc) · 4.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import *
from operator import itemgetter
from fnmatch import fnmatch
import sys, csv
def group_wc_in_full(d_wc, merge_full):
reslist = []
res_no_matches = []
for wc in d_wc:
matches = 0
for full in merge_full:
if match(full[1], wc[1]):
matches += 1
if matches == 1:
cont = 0
for full in merge_full:
if match(full[1], wc[1]):
new_key = full[0] + ", (" + wc[0] + ")"
merge_full[cont] = (new_key,full[1])
cont += 1
if matches > 1:
cont = 0
for full in merge_full:
if match(full[1], wc[1]):
new_key = full[0] + ", [" + wc[0] + "]"
merge_full[cont] = (new_key,full[1])
cont += 1
if matches == 0:
res_no_matches.append(wc)
return merge_full, res_no_matches
def group_no_wc(mergelist):
reslist = []
total_keys = []
for i in range(len(mergelist)):
isMatch = False
compared = False
keys = []
anyval = None
keys.append(mergelist[i][0])
if mergelist[i][0] in total_keys:
continue
for j in range(i + 1, len(mergelist) ):
if mergelist[j][0] in total_keys:
continue
compared = True
if mergelist[i][1] == mergelist[j][1]:
isMatch = True
keys.append(mergelist[j][0])
total_keys.append(mergelist[j][0])
anyval = mergelist[j][1]
if compared:
if isMatch:
str_keys = ", ".join(keys)
new_item = (str_keys, anyval)
reslist.append(new_item)
else:
reslist.append(mergelist[i])
return reslist
def match(v1, v2):
if not "-" in v1 and not "-" in v2:
return v1 == v2
v1_reg = v1.replace("-","?")
cont = 0
for s in v2:
if s == "-":
v1_reg = v1_reg[:cont] + "?" + v1_reg[cont+1:]
cont += 1
return fnmatch(v2, v1_reg)
def group_nonmatched(nonmatched):
pivot = nonmatched[0]
res = []
res.append(pivot)
for i in range(1, len(nonmatched)):
count = 0
keys = []
keys.append(nonmatched[i][0])
for j in range(len(res)):
try:
a = res[j]
except IndexError:
continue
if match(nonmatched[i][1], res[j][1]):
count += 1
keys.append(str(res[j][0]))
del(res[j])
if count == 0:
res.append(nonmatched[i])
else:
new_tuple = (", ".join(keys), nonmatched[i][1] )
res.append(new_tuple)
for i in range(len(res)):
res[i] = ("{" + res[i][0] + "}", res[i][1])
return res
d = { 'k1': 'AABA',
'k2': 'AABA',
'k3': 'ABAA',
'k4': 'AABA',
'k5': 'A--A',
'k6': 'AB-A',
'k7': 'BB-A',# no matched -->
'k8': 'B--A',
'k9': 'B-BA',
'k10':'B--B',
'k11':'BA-B',
'k12':'AAA-',
'k13':'AA-B',
'k14':'AB-B',
'k14':'-B-B',
}
input_file_name = sys.argv[1]
output_file_name = sys.argv[2]
input_file = list(csv.reader(open(input_file_name, 'rb'), delimiter=','))
d = {}
for line in input_file:
str_line = "".join(line[3:len(line)])
d[line[0]] = str_line
d_full = list((key, value) for key, value in d.iteritems())
d_no_wc = list((key,value) for key, value in d.iteritems() if not "-" in value)
d_wc = list((key,value) for key, value in d.iteritems() if "-" in value)
#1- merge_full <= MERGE d_no_wc
merge_full = group_no_wc(d_no_wc)
#2- merge_full_wc <= MERGE d_wc in merge_full
merge_full_wc, wc_no_match = group_wc_in_full(d_wc, merge_full)
#3 merge nonmatched
if len(wc_no_match) > 0:
merged_wc_no_match = group_nonmatched(wc_no_match)
else:
merged_wc_no_match = []
final = merged_wc_no_match + merge_full_wc
idx = 0
for row in final:
positions = {}
ids = row[0]
ids = ids.replace("(","").replace(")","")
ids = ids.replace("[","").replace("]","")
ids = ids.replace("{","").replace("}","")
ids = ids.replace(" ","")
ids = ids.split(',')
for id in ids:
id_clean = id.replace(" ","")
for line in input_file:
if line[0] == id_clean:
positions.setdefault(line[1], []).append( (id_clean,line[2]) )
#get most common element in list (dict keys)
chromosome = max(set(positions.keys()), key=positions.keys().count)
_id, position = min(positions[chromosome], key= lambda x: x[1])
final[idx] += (chromosome, position, _id, )
idx += 1
output_file = open(output_file_name, 'w')
for item in final:
row = []
row.append(item[0]) # ids
row.append(item[2]) #chromosome
row.append(item[3]) #position
row.append(item[4]) #id from position and chr
for c in item[1]:
row.append(c)
output_file.write("\t".join(row) + "\n")