-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline_resdk.py
executable file
·482 lines (368 loc) · 18 KB
/
pipeline_resdk.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python
'''
The MIT License (MIT)
Copyright (c) 2016 Charles Y. Lin, Rocio Dominguez-Vidana, Barbara Jenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
#================================================================================
#=============================DEPENDENCIES=======================================
#================================================================================
import os
import subprocess
import sys
import time
print("using python version %s" % sys.version)
#================================================================================
#=============================LOG INTO RESDK=====================================
#================================================================================
import resdk
res = resdk.Resolwe('admin', 'admin', 'https://torta.bcm.genialis.com')
resdk.start_logging()
#================================================================================
#============================GLOBAL PARAMETERS===================================
#================================================================================
# for testing
# add locations of files and global parameters in this section
collection_slug = 'primary_chordoma'
genome = 'hg19'
# projectFolder = '/home/barbara/gen/linlab/data/chordoma/'
projectFolder = '/grail/projects/chordoma/'
# Path to data path on server
# torta.bcm.genialis.com -> /grail/genialis/data/
# bcm.genialis.com -> /storage/genialis/bcm.genialis.com/data/
data_folder_path = '/grail/genialis/data/'
# Address used to connect to server via ssh
# It can also contains username (i.e. <username>@<hostname>)
ssh_hostname = 'torta.bcmt.bcm.edu'
#================================================================================
#===========================DEFINING THE CLASSES=================================
#================================================================================
#================================================================================
#===================================CLASSES======================================
#================================================================================
class ResCollection(object):
#: list of processing objects that need to be downloaded
to_download = []
#: ssh connection to the server
ssh_connection = None
# this __init__section is called from the get go
def __init__(self, collection_slug, genome, relationship_file=None):
print('Loading collection %s' % (collection_slug))
collection = res.collection.get(collection_slug)
self._collection = collection
self._id = collection.id
self._slug = collection_slug
print("Using genome %s" % (genome))
self._genome = genome
sample_dict = {}
for data in collection.data.filter(type='data:alignment:bam:'):
sample = data.sample or data.presample # get sample or presample of data object
sample_dict[sample.name] = {
'sample': sample,
'unique_id': sample.id, # returns unique id
'slug': sample.slug, # returns slug
'background': 'NONE',
'group': 'NONE',
}
self._sample_dict = sample_dict
if relationship_file: # something provided by user
if os.path.exists(relationship_file):
self.importRelationships(relationship_file)
else:
self.exportRelationships(relationship_file)
def importRelationships(self, input_table):
print('Importing relationship data from %s' % (input_table))
with open(input_table, 'r') as fn:
fn.readline() # skip the header
for line in fn.readlines():
line = line.rstrip().split('\t')
name = line[0]
background = line[3]
group = line[4]
self._sample_dict[name]['background'] = background
self._sample_dict[name]['group'] = group
def exportRelationships(self, output=None):
print('Creating relationship table')
if output:
print('Writing relationships to %s' % (output))
rel_table = [['SAMPLE_NAME', 'SAMPLE_SLUG', 'U_ID', 'BACKGROUND_NAME', 'GROUP']]
for name in self._sample_dict:
sample_data = self._sample_dict[name]
rel_table.append([
name,
sample_data['slug'],
sample_data['unique_id'],
sample_data['background'],
sample_data['group'],
])
if not output:
return rel_table
else:
with open(output, 'w') as outfile:
for line in rel_table:
outfile.write('\t'.join([str(x) for x in line]) + '\n')
def names(self):
# returns all sample names
return sorted(self._sample_dict.keys())
def getGroup(self,name):
# returns all sample names
return self._sample_dict[name]['group']
def getBackground(self, name):
# return the sample name of the background!!!
background_name = self._sample_dict[name]['background']
if background_name == 'NONE' or background_name == '':
return None
else:
return background_name
def getMacs(self, name):
sample = self._sample_dict[name]['sample']
return sample.get_macs()
def getBam(self, name):
sample = self._sample_dict[name]['sample']
return sample.get_bam()
def getCuffquant(self, name):
sample = self._sample_dict[name]['sample']
return sample.get_cuffquant()
def download(self, output=''):
print("Waiting for analysis to finish...")
while True:
new_list = []
for obj in self.to_download:
obj.update()
if obj.status == 'OK':
print('Downloading data for: {}'.format(obj.name))
obj.download(download_dir=output)
elif obj.status == 'ER':
print('Error in object: {}'.format(obj.name))
print(obj.stdout())
print('----------')
else:
new_list.append(obj)
self.to_download = new_list
if new_list == []:
break
time.sleep(1)
def _create_local_link(self, src, dest):
dest_dir = os.path.dirname(dest)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
if os.path.isfile(dest):
os.remove(dest)
os.symlink(src, dest)
def _create_ssh_link(self, src, dest):
if self.ssh_connection is None:
self.ssh_connection = subprocess.Popen(
['ssh', '-tt', ssh_hostname],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=0
)
dest_dir = os.path.dirname(dest)
self.ssh_connection.stdin.write('mkdir -p "{}"\n'.format(dest_dir))
self.ssh_connection.stdin.write('ln -sf "{}" "{}"\n'.format(src, dest))
def create_links(self, links=[], path='resdk_results'):
print('Linking results...')
for link in links:
for data in self._collection.data.filter(status='OK', type=link['type']):
for file_name in data.files(field_name=link['field']):
file_path = os.path.join(data_folder_path, str(data.id), file_name)
link_name = '{:05}_{}_{}'.format(
data.id,
data.sample.slug if data.sample else data.slug,
genome
)
if '.' in file_name:
link_extension = file_name.split('.', 1)[1]
link_name = '{}.{}'.format(link_name, link_extension)
link_path = os.path.join(path, link['subfolder'], link_name)
if os.path.isfile(file_path):
self._create_local_link(file_path, link_path)
else:
self._create_ssh_link(file_path, link_path)
if self.ssh_connection is not None:
self.ssh_connection.stdin.close()
def run_macs(self, sample_name, useBackground=True, p_value='1e-9', watch=False):
sample = self._sample_dict[sample_name]['sample']
if useBackground:
background_name = self.getBackground(sample_name)
if background_name:
background_slug = self._collection.samples.get(name=background_name).slug
else:
print('WARNING: no background dataset found for %s' % (sample_name))
print('INFO: macs will be run without control')
background_slug = None
else:
background_slug = None
macs = sample.run_macs(
use_background=useBackground,
background_slug=background_slug,
p_value=p_value
)
if watch:
self.to_download.append(macs)
return macs
def run_rose2(self, sample_name, useBackground=True, tss=0, stitch=None, watch=False, macs_params={}):
sample = self._sample_dict[sample_name]['sample']
if useBackground:
background_name = self.getBackground(sample_name)
if background_name:
background_slug = self._collection.samples.get(name=background_name).slug
else:
print('WARNING: no background dataset found for %s' % (sample_name))
print('INFO: rose2 will be run without control')
background_slug = None
else:
background_slug = None
genome_string = self._genome.upper()
# get existing macs or create a new one
macs = sample.run_macs(
use_background=useBackground,
background_slug=background_slug,
**macs_params
)
rose = sample.run_rose2(
use_background=useBackground,
background_slug=background_slug,
genome=genome_string,
tss=tss,
stitch=stitch,
beds=macs # run rose only on one macs peaks bed file
)
if watch:
self.to_download.extend(rose) # run_rose returns list
return rose[0] # we only run one rose at the time
def run_bamplot(self, sample_names, input_gff=None, input_region=None, stretch_input=None,
color=None, sense=None, extension=None, rpm=None, yscale=None, names=None,
plot=None, title=None, scale=None, bed=None, multi_page=None, watch=False):
bams = [self.getBam(name) for name in sample_names]
genome_string = self._genome.upper()
bamplot = res.run_bamplot(bams, genome_string, input_gff, input_region, stretch_input,
color, sense, extension, rpm, yscale, names, plot, title, scale, bed, multi_page)
if watch:
self.to_download.append(bamplot)
self._collection.add_data(bamplot)
return bamplot
def run_cuffquant(self, sample_name, gff, watch=False):
sample = self._sample_dict[sample_name]['sample']
cuffquant = sample.run_cuffquant(gff)
if watch:
self.to_download.append(cuffquant)
self._collection.add_data(cuffquant)
return cuffquant
def run_cuffnorm(self, sample_names, watch=False):
replicates = []
labels = []
cuffquants = []
dict_replicates = {}
annotation = None
for sample_name in sample_names:
cuffquant = self.getCuffquant(sample_name)
group = self.getGroup(sample_name)
if annotation is None:
annotation = cuffquant.input['gff']
elif annotation != cuffquant.input['gff']:
raise RuntimeError('Cuffquants objects have different annotations, '
'please select cuffquants with same annotation.')
if group not in dict_replicates:
dict_replicates[group] = str(len(dict_replicates))
labels.append(group)
replicates.append(dict_replicates[group])
cuffquants.append(cuffquant)
inputs = {
'cuffquant': cuffquants,
'annotation': annotation,
'replicates': replicates,
'labels': labels,
}
cuffnorm = res.run_cuffnorm(**inputs)
if watch:
self.to_download.append(cuffnorm)
self._collection.add_data(cuffnorm)
return cuffnorm
def main():
'''
this is where we are building the main run function for the script
all of the work should occur here, but no functions should be defined here
for now it is not executed allowing a check
'''
projectFolder = '/grail/projects/pipeline_resdk/'
#projectFolder = '/home/barbara/gen/linlab/data/pipeline_resdk/' #pipeline_resdk needs write permissions
# ideal situation
res_collection = ResCollection(collection_slug,'hg19','%sCHORDOMA_TABLE.txt' % (projectFolder)) #if foo exists, load it, if not write it out to disk
# all of the datasets that we have from the k27ac group
h3k27ac_list = [name for name in res_collection.names() if res_collection.getGroup(name) == 'H3K27AC']
all_samples = [name for name in res_collection.names()]
#=======================
#schema for how we want to run, record and get back data for any processor
#sample_name = h3k27ac_list[0]
#print(sample_name)
#test macs on this and make sure the collection sample dict is appropriately updated
#this makes sure you can return the macs data id when you run it
#also want to make sure that the collection gets updated appropriately
#macs = res_collection.run_macs(sample_name,useBackground=True,p_value='1e-9', watch=True)
#rose = res_collection.run_rose2(sample_name, useBackground=True, tss=0, stitch=None, macs_params={'p_value': '1e-9'}, watch=True)
#res_collection.download(output='/grail/genialis/pipeline_resdk')
#gff = res.run('upload-gtf', input={'src':'<path/to/gff'}) # upload gff file
#gff = res.data.get() # get gff file onece it is uploaded
#bed = res.run('upload-bed', input={'src':'<path/to/bed>'})
gff_region = 'chr17:+:41468594-41566948'
#bamplot = res_collection.run_bamplot(sample_names=h3k27ac_list, input_region=gff_region, watch=True)
#res_collection.download(output='/grail/genialis/pipeline_resdk')
# if we would like to use bams that are not part of the res_collection
#bam = res.run('upload-bam', input='src':'<path/to/bam>')
#bam1 = res.run('upload-bam', input='src':'<path/to/bam1>')
#bamplot = res.run_bamplot(bam=[bam.id, bam1.id], genome='',... )
#print(macs.id)
#print(rose.id)
#print(res_collection._sample_dict[sample_name])
#now we should be able to retrieve the macs output easily by doing
#print(macs.files())
#========================
# #run macs on everybody w/ background at p of 1e-9 and download to a folder
# macs_parent_folder = utils.formatFolder('%smacsFolder' % (projectFolder),True)
# for sample_name in h3k27ac_list:
# #macs_folder = utils.formatFolder('%s%s_MACS14/' % (macs_parent_folder,sample_name),True)
# #res_collection = run_macs14(res_collection,sample_name,useBackground=True,p_value='1e-9',output=macs_folder)
# res_collection = run_macs14(res_collection,sample_name,useBackground=True,p_value='1e-9')
# gtf = res.run('upload-gtf', input={'src':'/grail/genialis/Homo_sapiens.GRCh38.86.gtf.gz', 'source':'NCBI'}) # upload gff file
# add hg19 annotation file
gtf = res.data.get('hg19gtf-3')
for sample_name in h3k27ac_list:
res_collection.run_rose2(sample_name, useBackground=True, tss=0, stitch=None, macs_params={'p_value': '1e-9'}, watch=True)
for sample_name in all_samples:
res_collection.run_cuffquant(sample_name, gff=gtf, watch=True)
res_collection.run_bamplot(sample_names=h3k27ac_list, input_region=gff_region, watch=True, title='h3k27ac_list')
res_collection.run_cuffnorm(sample_names=all_samples, watch=True)
# Wait for analysis, runed with `watch=True`, to finish and download
# their results.
# res_collection.download(output='/grail/genialis/pipeline_resdk')
# Links can only be created when analysis is finished. So you have
# to wait before running this step or run the script one more time
# when all analysis are finished.
res_collection.create_links([
{'type': 'data:alignment:bam:bowtie2:', 'field': 'bam', 'subfolder': 'bams'},
{'type': 'data:alignment:bam:bowtie2:', 'field': 'bai', 'subfolder': 'bams'},
{'type': 'data:chipseq:macs14:', 'field': 'peaks_bed', 'subfolder': 'macs'},
{'type': 'data:chipseq:macs14:', 'field': 'peaks_xls', 'subfolder': 'macs'},
{'type': 'data:chipseq:rose2:', 'field': 'all_enhancers', 'subfolder': 'roses'},
])
#retrieve an arbitrary macs output
#macs_list = res_collection.get;acs(sample_name)
#then i could get file paths or object ids necessary to run other stuff
if __name__=="__main__":
main()