-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter10Xbam.py
209 lines (153 loc) · 5.31 KB
/
filter10Xbam.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
##############################################################################
#
# Copyright (C) 2024 Jakub Scaber
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
###############################################################################
"""===========================
Pipeline template
===========================
:Author: Jakub Scaber
:Release: $Id$
:Date: |today|
:Tags: Python
.. Replace the documentation below with your own description of the
pipeline's purpose
Overview
========
This pipeline computes the word frequencies in the configuration
files :file:``pipeline.ini` and :file:`conf.py`.
Usage
=====
See :ref:`PipelineSettingUp` and :ref:`PipelineRunning` on general
information how to use CGAT pipelines.
Configuration
-------------
The pipeline requires a configured :file:`pipeline.ini` file.
CGATReport report requires a :file:`conf.py` and optionally a
:file:`cgatreport.ini` file (see :ref:`PipelineReporting`).
Default configuration files can be generated by executing:
python <srcdir>/pipeline_rMATS.py config
Input files
-----------
None required except the pipeline configuration files.
Requirements
------------
The pipeline requires the results from
:doc:`pipeline_annotations`. Set the configuration variable
:py:data:`annotations_database` and :py:data:`annotations_dir`.
On top of the default CGAT setup, the pipeline requires the following
software to be in the path:
.. Add any additional external requirements such as 3rd party software
or R modules below:
Requirements:
* samtools >= 1.1
Pipeline output
===============
.. Describe output files of the pipeline here
Glossary
========
.. glossary::
Code
====
"""
from ruffus import transform, mkdir, follows, merge, regex, suffix, \
jobs_limit, files, collate, add_inputs, formatter, \
active_if, originate, subdivide
import sys
import os
import glob
import sqlite3
import cgatcore.experiment as E
import cgatcore.iotools as iotools
from cgatcore import pipeline as P
# load options from the config file
PARAMS = P.get_parameters(
["%s/pipeline.yml" % os.path.splitext(__file__)[0], "pipeline.yml"])
# if necessary, update the PARAMS dictionary in any modules file.
# e.g.:
#
# import CGATPipelines.PipelineGeneset as PipelineGeneset
# PipelineGeneset.PARAMS = PARAMS
#
# Note that this is a hack and deprecated, better pass all
# parameters that are needed by a function explicitely.
# -----------------------------------------------
# Utility functions
def connect():
'''utility function to connect to database.
Use this method to connect to the pipeline database.
Additional databases can be attached here as well.
Returns an sqlite3 database handle.
'''
dbh = sqlite3.connect(PARAMS["database"])
statement = '''ATTACH DATABASE '%s' as annotations''' % (
PARAMS["annotations_database"])
cc = dbh.cursor()
cc.execute(statement)
cc.close()
return dbh
TRACKS = glob.glob("*_bam.bam")
# ---------------------------------------------------
# Specific pipeline tasks
@transform(TRACKS,
suffix(".bam"),
".sam")
def filter(infile, outfile):
stem = P.snip(infile, "_possorted_genome_bam.bam")
statement = '''samtools view %(infile)s| LC_ALL=C grep -F -f Donor.csv > %(outfile)s ''' % locals()
P.run(statement, job_memory="2G")
@transform(filter,
suffix("possorted_genome_bam.sam"),
"mn.sam")
def merge(infile, outfile):
header = P.snip(infile, "bam.sam") + "samheader"
statement = '''cat %(header)s %(infile)s > %(outfile)s ''' % locals()
P.run(statement, job_memory="2G")
@transform(merge,
suffix(".sam"),
".bam")
def makeBam(infile, outfile):
statement = '''samtools view -b %(infile)s > %(outfile)a''' % locals()
P.run(statement, job_memory="2G")
# ---------------------------------------------------
# Generic pipeline tasks
@follows(makeBam)
def full():
pass
@follows(mkdir("report"))
def build_report():
'''build report from scratch.
Any existing report will be overwritten.
'''
E.info("starting report build process from scratch")
P.run_report(clean=True)
@follows(mkdir("report"))
def update_report():
'''update report.
This will update a report with any changes inside the report
document or code. Note that updates to the data will not cause
relevant sections to be updated. Use the cgatreport-clean utility
first.
'''
E.info("updating report")
P.run_report(clean=False)
@follows(update_report)
def publish_report():
'''publish report in the CGAT downloads directory.'''
E.info("publishing report")
P.publish_report()
if __name__ == "__main__":
sys.exit(P.main(sys.argv))