-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmergeFilesVariant.py
More file actions
executable file
·347 lines (322 loc) · 14.5 KB
/
mergeFilesVariant.py
File metadata and controls
executable file
·347 lines (322 loc) · 14.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import sys,string,os
sys.path.insert(1, os.path.join(sys.path[0], '..')) ### import parent dir dependencies
import os.path
import unique
import itertools
dirfile = unique
############ File Import Functions #############
def filepath(filename):
fn = unique.filepath(filename)
return fn
def read_directory(sub_dir):
dir_list = unique.read_directory(sub_dir)
return dir_list
def returnDirectories(sub_dir):
dir_list = unique.returnDirectories(sub_dir)
return dir_list
class GrabFiles:
def setdirectory(self,value): self.data = value
def display(self): print self.data
def searchdirectory(self,search_term):
#self is an instance while self.data is the value of the instance
files = getDirectoryFiles(self.data,search_term)
if len(files)<1: print 'files not found'
return files
def returndirectory(self):
dir_list = getAllDirectoryFiles(self.data)
return dir_list
def getAllDirectoryFiles(import_dir):
all_files = []
dir_list = read_directory(import_dir) #send a sub_directory to a function to identify all files in a directory
for data in dir_list: #loop through each file in the directory to output results
data_dir = import_dir[1:]+'/'+data
if '.' in data:
all_files.append(data_dir)
return all_files
def getDirectoryFiles(import_dir,search_term):
dir_list = read_directory(import_dir) #send a sub_directory to a function to identify all files in a directory
matches=[]
for data in dir_list: #loop through each file in the directory to output results
data_dir = import_dir[1:]+'/'+data
if search_term not in data_dir and '.' in data: matches.append(data_dir)
return matches
def cleanUpLine(line):
line = string.replace(line,'\n','')
line = string.replace(line,'\c','')
data = string.replace(line,'\r','')
data = string.replace(data,'"','')
return data
def combineAllLists(files_to_merge,original_filename,includeColumns=False):
headers =[]; all_keys={}; dataset_data={}; files=[]; filter_keys={}
for filename in files_to_merge:
print filename
duplicates=[]
fn=filepath(filename); x=0; combined_data ={}; files.append(filename)
if '/' in filename:
file = string.split(filename,'/')[-1][:-4]
else:
file = string.split(filename,'\\')[-1][:-4]
for line in open(fn,'rU').xreadlines():
data = cleanUpLine(line)
t = string.split(data,'\t')
if x==0:
if data[0]!='#':
x=1
try: t = t[1:]
except Exception: t = ['null']
if includeColumns==False:
headers.append(file)
#for i in t: headers.append(i+'.'+file); #headers.append(i)
else:
headers.append(t[includeColumns]+'.'+file)
else: #elif 'FOXP1' in data or 'SLK' in data or 'MBD2' in data:
key = t[0]
try: key = t[0]+':'+t[1]+' '+t[3]+'|'+t[5]; t = [key,t[2]]
except Exception: print key;sys.exit()
if includeColumns==False:
try: values = t[1:]
except Exception: values = ['null']
try:
if original_filename in filename and len(original_filename)>0: key = t[1]; values = t[2:]
except IndexError: print original_filename,filename,t;kill
else:
values = [t[includeColumns]]
#key = string.replace(key,' ','')
if len(key)>0 and key != ' ' and key not in combined_data: ### When the same key is present in the same dataset more than once
try: all_keys[key] += 1
except KeyError: all_keys[key] = 1
v = float(values[0])
if permform_all_pairwise == 'yes':
try: combined_data[key].append(values); duplicates.append(key)
except Exception: combined_data[key] = [values]
else:
combined_data[key] = values
if float(t[1])>0.15: filter_keys[key]=[]
#print duplicates
dataset_data[filename] = combined_data
for i in dataset_data:
print len(dataset_data[i]), i
print 'filter_keys',len(filter_keys)
###Add null values for key's in all_keys not in the list for each individual dataset
combined_file_data = {}
for filename in files:
combined_data = dataset_data[filename]
###Determine the number of unique values for each key for each dataset
null_values = []; i=0
for key in combined_data:
number_of_values = len(combined_data[key][0]); break
while i<number_of_values: null_values.append('0'); i+=1
for key in all_keys:
if key in filter_keys:
include = 'yes'
if combine_type == 'intersection':
if all_keys[key]>(len(files_to_merge)-1): include = 'yes'
else: include = 'no'
if include == 'yes':
try: values = combined_data[key]
except KeyError:
values = null_values
if permform_all_pairwise == 'yes':
values = [null_values]
if permform_all_pairwise == 'yes':
try:
val_list = combined_file_data[key]
val_list.append(values)
combined_file_data[key] = val_list
except KeyError: combined_file_data[key] = [values]
else:
try:
previous_val = combined_file_data[key]
new_val = previous_val + values
combined_file_data[key] = new_val
except KeyError: combined_file_data[key] = values
original_filename = string.replace(original_filename,'1.', '1.AS-')
export_file = output_dir+'/MergedVariants.txt'
fn=filepath(export_file);data = open(fn,'w')
title = string.join(['uid']+headers,'\t')+'\n'; data.write(title)
for key in combined_file_data:
#if key == 'ENSG00000121067': print key,combined_file_data[key];kill
new_key_data = string.split(key,'-'); new_key = new_key_data[0]
if permform_all_pairwise == 'yes':
results = getAllListCombinations(combined_file_data[key])
for result in results:
merged=[]
for i in result: merged+=i
merged2 = map(float,merged)
if max(merged2)>1:
merged=[]
max_val = max(merged2)
for i in merged2:
try: i = i/max_val
except Exception: pass
merged.append(str(i))
values = string.join([key]+merged,'\t')+'\n'; data.write(values) ###removed [new_key]+ from string.join
else:
try:
values = string.join([key]+combined_file_data[key],'\t')+'\n'; data.write(values) ###removed [new_key]+ from string.join
except Exception: print combined_file_data[key];sys.exit()
data.close()
print "exported",len(dataset_data),"to",export_file
def customLSDeepCopy(ls):
ls2=[]
for i in ls: ls2.append(i)
return ls2
def getAllListCombinationsLong(a):
ls1 = ['a1','a2','a3']
ls2 = ['b1','b2','b3']
ls3 = ['c1','c2','c3']
ls = ls1,ls2,ls3
list_len_db={}
for ls in a:
list_len_db[len(x)]=[]
print len(list_len_db), list_len_db;sys.exit()
if len(list_len_db)==1 and 1 in list_len_db:
### Just simply combine non-complex data
r=[]
for i in a:
r+=i
else:
#http://code.activestate.com/recipes/496807-list-of-all-combination-from-multiple-lists/
r=[[]]
for x in a:
t = []
for y in x:
for i in r:
t.append(i+[y])
r = t
return r
def combineUniqueAllLists(files_to_merge,original_filename):
headers =[]; all_keys={}; dataset_data={}; files=[]
for filename in files_to_merge:
print filename
fn=filepath(filename); x=0; combined_data ={}; files.append(filename)
if '/' in filename:
file = string.split(filename,'/')[-1][:-4]
else:
file = string.split(filename,'\\')[-1][:-4]
for line in open(fn,'rU').xreadlines():
data = cleanUpLine(line)
t = string.split(data,'\t')
if x==0:
if data[0]!='#':
x=1
try: t = t[1:]
except Exception: t = ['null']
for i in t:
headers.append(i+'.'+file)
if x==0:
if data[0]!='#':
x=1;
headers+=t[1:] ###Occurs for the header line
headers+=['null']
else: #elif 'FOXP1' in data or 'SLK' in data or 'MBD2' in data:
key = t[0]
try: values = t[1:]
except Exception: values = ['null']
try:
if original_filename in filename and len(original_filename)>0: key = t[1]; values = t[2:]
except IndexError: print original_filename,filename,t;kill
#key = string.replace(key,' ','')
combined_data[key] = values
if len(key)>0 and key != ' ':
try: all_keys[key] += 1
except KeyError: all_keys[key] = 1
dataset_data[filename] = combined_data
###Add null values for key's in all_keys not in the list for each individual dataset
combined_file_data = {}
for filename in files:
combined_data = dataset_data[filename]
###Determine the number of unique values for each key for each dataset
null_values = []; i=0
for key in combined_data: number_of_values = len(combined_data[key]); break
while i<number_of_values: null_values.append('0'); i+=1
for key in all_keys:
include = 'yes'
if combine_type == 'intersection':
if all_keys[key]>(len(files_to_merge)-1): include = 'yes'
else: include = 'no'
if include == 'yes':
try: values = combined_data[key]
except KeyError: values = null_values
try:
previous_val = combined_file_data[key]
new_val = previous_val + values
combined_file_data[key] = new_val
except KeyError: combined_file_data[key] = values
original_filename = string.replace(original_filename,'1.', '1.AS-')
export_file = output_dir+'/MergedFiles.txt'
fn=filepath(export_file);data = open(fn,'w')
title = string.join(['uid']+headers,'\t')+'\n'; data.write(title)
for key in combined_file_data:
#if key == 'ENSG00000121067': print key,combined_file_data[key];kill
new_key_data = string.split(key,'-'); new_key = new_key_data[0]
values = string.join([key]+combined_file_data[key],'\t')+'\n'; data.write(values) ###removed [new_key]+ from string.join
data.close()
print "exported",len(dataset_data),"to",export_file
def getAllListCombinations(a):
#http://www.saltycrane.com/blog/2011/11/find-all-combinations-set-lists-itertoolsproduct/
""" Nice code to get all combinations of lists like in the above example, where each element from each list is represented only once """
list_len_db={}
for x in a:
list_len_db[len(x)]=[]
if len(list_len_db)==1 and 1 in list_len_db:
### Just simply combine non-complex data
r=[]
for i in a:
r.append(i[0])
return [r]
else:
return list(itertools.product(*a))
def joinFiles(files_to_merge,CombineType,unique_join,outputDir):
""" Join multiple files into a single output file """
global combine_type
global permform_all_pairwise
global output_dir
output_dir = outputDir
combine_type = string.lower(CombineType)
permform_all_pairwise = 'yes'
print 'combine type:',combine_type
print 'join type:', unique_join
#g = GrabFiles(); g.setdirectory(import_dir)
#files_to_merge = g.searchdirectory('xyz') ###made this a term to excluded
if unique_join:
combineUniqueAllLists(files_to_merge,'')
else:
combineAllLists(files_to_merge,'')
return output_dir+'/MergedFiles.txt'
if __name__ == '__main__':
dirfile = unique
includeColumns=-2
includeColumns = False
output_dir = filepath('output')
combine_type = 'union'
permform_all_pairwise = 'yes'
print "Analysis Mode:"
print "1) Batch Analysis"
print "2) Single Output"
inp = sys.stdin.readline(); inp = inp.strip()
if inp == "1": batch_mode = 'yes'
elif inp == "2": batch_mode = 'no'
print "Combine Lists Using:"
print "1) Grab Union"
print "2) Grab Intersection"
inp = sys.stdin.readline(); inp = inp.strip()
if inp == "1": combine_type = 'union'
elif inp == "2": combine_type = 'intersection'
if batch_mode == 'yes': import_dir = '/batch/general_input'
else: import_dir = '/input'
g = GrabFiles(); g.setdirectory(import_dir)
files_to_merge = g.searchdirectory('xyz') ###made this a term to excluded
if batch_mode == 'yes':
second_import_dir = '/batch/primary_input'
g = GrabFiles(); g.setdirectory(second_import_dir)
files_to_merge2 = g.searchdirectory('xyz') ###made this a term to excluded
for file in files_to_merge2:
temp_files_to_merge = customLSDeepCopy(files_to_merge)
original_filename = string.split(file,'/'); original_filename = original_filename[-1]
temp_files_to_merge.append(file)
if '.' in file:
combineAllLists(temp_files_to_merge,original_filename)
else:
combineAllLists(files_to_merge,'',includeColumns=includeColumns)
print "Finished combining lists. Select return/enter to exit"; inp = sys.stdin.readline()