annotate Snakefile @ 12:849ab444b4f1 draft

Uploaded
author chrisw
date Wed, 13 Feb 2019 15:51:45 -0500
parents 35ed7314038d
children af1f49e5242d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
1 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
2 Parameters:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
3 - star: arguments to pass to STAR aligner
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
4 - unique_qual: minimum MAPQ needed to be counted in unique BW [default: 10]
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
5 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
6
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
7 STEPS = ['align', 'sort', 'bamcount']
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
8
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
9 FILES = ['sjout.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
10 'bamcount_nonref.csv.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
11 'bamcount_auc.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
12 'bamcount_frag.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
13 'Chimeric.out.junction.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
14 'all.exon_bw_count.zst', 'unique.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
15 'manifest']
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
16
12
849ab444b4f1 Uploaded
chrisw
parents: 11
diff changeset
17 import os
849ab444b4f1 Uploaded
chrisw
parents: 11
diff changeset
18 config['output']=os.path.abspath(config['output'])
849ab444b4f1 Uploaded
chrisw
parents: 11
diff changeset
19
10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
20 import subprocess
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
21 def run_command(cmd_args):
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
22 cmd_args = ' '.join(cmd_args)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
23 try:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
24 subprocess.check_call(args=cmd_args, shell=True)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
25 except subprocess.CalledProcessError as cpe:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
26 sys.stderr.write("error in run_command for command: %s\n" % cmd_args)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
27 raise cpe
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
28
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
29
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
30 import re
11
35ed7314038d Uploaded
chrisw
parents: 10
diff changeset
31 FASTQ_PATT=re.compile(r'([^_\.]+)(_(\d+))?\.((fastq)|fq)(\.gz)?$')
10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
32 def prep_for_galaxy_run():
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
33 try:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
34 os.mkdir(config['temp'])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
35 except OSError as ose:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
36 pass
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
37 fastqs = config['inputs'].split(',')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
38 m = FASTQ_PATT.search(fastqs[0])
11
35ed7314038d Uploaded
chrisw
parents: 10
diff changeset
39 run_acc = 'sample'
35ed7314038d Uploaded
chrisw
parents: 10
diff changeset
40 if m is not None:
35ed7314038d Uploaded
chrisw
parents: 10
diff changeset
41 run_acc = m.group(1)
12
849ab444b4f1 Uploaded
chrisw
parents: 11
diff changeset
42 study_acc = 'study'
10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
43 if 'study' in config:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
44 study_acc = config['study']
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
45 genome = 'hg38'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
46 if 'genome' in config:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
47 genome = config['genome']
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
48 method = 'sra'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
49 # SRR,SRP,genome
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
50 # e.g. SRR1557855,SRP045778,ce10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
51 #create links which will be used in the align step
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
52 i = 1
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
53 for f in fastqs:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
54 newf = '%s/%s_%s_%s_%s_%d.fastq' % (config['temp'], run_acc, study_acc, genome, method, i)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
55 run_command(['zcat',f,'>',newf])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
56 #create fastq 0
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
57 if i == 1:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
58 try:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
59 os.symlink(os.path.abspath(newf), '%s/%s_%s_%s_%s_%d.fastq' % (config['temp'], run_acc, study_acc, genome, method, 0))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
60 except FileExistsError as fee:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
61 pass
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
62 i += 1
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
63 #create fastq 2 if not paired
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
64 if i == 2:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
65 open('%s/%s_%s_%s_%s_%d.fastq' % (config['temp'], run_acc, study_acc, genome, method, 2), "w").close()
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
66 #create expected file structure for annotated exon bed file & reference index
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
67 ref = config['ref']
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
68 config['ref'] = '.'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
69 os.makedirs('%s/%s' % (config['ref'], genome))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
70 os.symlink(ref, '%s/%s/star_idx' % (config['ref'], genome))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
71 os.makedirs('%s/%s/gtf' % (config['ref'], genome))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
72 os.symlink(config['exon_bed'], 'exons.tmp')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
73 os.symlink('../../exons.tmp', '%s/%s/gtf/%s' % (config['ref'], genome, config.get('bw_bed', 'exons.bed')))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
74 return([run_acc, study_acc, genome, method])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
75
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
76
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
77 def get_accessions(wildcards):
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
78 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
79 Grouping of SRRs with the same SRP could happen here
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
80 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
81 #if running under galaxy where the user will input the
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
82 #FASTQs, study, and genome directly
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
83 if 'inputs' in config:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
84 (run_acc, study_acc, genome, method) = prep_for_galaxy_run()
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
85 for ext in FILES:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
86 yield os.path.join(config['output'], '%s_%s_%s_%s.%s' % (run_acc, study_acc, genome, method, ext))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
87 #here to get the make_galaxy_links rule to fire
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
88 yield os.path.join(config['output'], '%s_%s_%s_%s.done' % (run_acc, study_acc, genome, method))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
89 #not running under galaxy, takes a list of accessions
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
90 else:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
91 for fn in config['input'].split():
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
92 with open(fn, 'r') as fh:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
93 for ln in fh:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
94 if ln.count(',') < 2:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
95 continue
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
96 toks = ln.rstrip().split(',')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
97 assert 3 <= len(toks) <= 4
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
98 method = 'sra'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
99 if len(toks) == 4:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
100 method = toks[3]
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
101 # SRR,SRP,genome
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
102 # e.g. SRR1557855,SRP045778,ce10
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
103 for ext in FILES:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
104 yield os.path.join(config['output'], '%s_%s_%s_%s.%s' % (toks[0], toks[1], toks[2], method, ext))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
105
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
106 rule all:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
107 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
108 get_accessions
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
109
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
110
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
111 rule make_manifest:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
112 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
113 config['output'] + '/{quad}.sjout.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
114 config['output'] + '/{quad}.Chimeric.out.junction.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
115 config['output'] + '/{quad}.bamcount_nonref.csv.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
116 config['output'] + '/{quad}.bamcount_auc.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
117 config['output'] + '/{quad}.bamcount_frag.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
118 config['output'] + '/{quad}.all.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
119 config['output'] + '/{quad}.unique.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
120 output:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
121 config['output'] + '/{quad}.manifest'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
122 params:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
123 quad=lambda wildcards: wildcards.quad
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
124 run:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
125 with open(output[0], 'wt') as fh:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
126 for fn in FILES:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
127 fh.write(params.quad + "." + fn + '\n')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
128
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
129 def galaxy_link_files(op):
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
130 a = [op + '/' + f for f in FILES]
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
131 a.extend([op + '/align.log', op + '/bamcount.log'])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
132 return a
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
133
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
134 rule make_galaxy_links:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
135 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
136 config['output'] + '/{quad}.sjout.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
137 config['output'] + '/{quad}.bamcount_nonref.csv.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
138 config['output'] + '/{quad}.bamcount_auc.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
139 config['output'] + '/{quad}.bamcount_frag.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
140 config['output'] + '/{quad}.Chimeric.out.junction.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
141 config['output'] + '/{quad}.all.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
142 config['output'] + '/{quad}.unique.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
143 config['output'] + '/{quad}.manifest'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
144 output:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
145 config['output'] + '/{quad}.done'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
146 params:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
147 quad=lambda wildcards: wildcards.quad,
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
148 out=galaxy_link_files(config['output'])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
149 run:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
150 inputs = input.extend([config['output'] + '/' + params.quad + '.align.log',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
151 config['output'] + '/' + params.quad + '.bamcount.log'])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
152 for (i,fn) in enumerate(input):
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
153 os.symlink(os.path.abspath(fn), params.out[i])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
154 os.symlink(os.path.abspath(input[-3]), output[0])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
155
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
156
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
157 rule bamcount:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
158 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
159 bam=config['temp'] + '/{quad}-sorted.bam',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
160 bamidx=config['temp'] + '/{quad}-sorted.bam.bai',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
161 #exe='/bamcount/bamcount',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
162 exe='bamcount',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
163 bed=lambda wildcards: '%s/%s/gtf/%s' % (config['ref'], wildcards.quad.split('_')[2], config.get('bw_bed', 'exons.bed'))
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
164 output:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
165 nonref=config['output'] + '/{quad}.bamcount_nonref.csv.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
166 auc=config['output'] + '/{quad}.bamcount_auc.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
167 frag=config['output'] + '/{quad}.bamcount_frag.tsv',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
168 all_bw=config['output'] + '/{quad}.all.bw.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
169 unique_bw=config['output'] + '/{quad}.unique.bw.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
170 all_bw_count=config['output'] + '/{quad}.all.exon_bw_count.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
171 unique_bw_count=config['output'] + '/{quad}.unique.exon_bw_count.zst'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
172 log:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
173 config['output'] + '/{quad}.bamcount.log'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
174 params:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
175 srr=lambda wildcards: wildcards.quad.split('_')[0],
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
176 uniq_qual=config.get('unique_qual', 10)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
177 threads: 4
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
178 shell:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
179 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
180 TMP={config[temp]}/{params.srr}_bamcount
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
181 {input.exe} {input.bam} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
182 --threads {threads} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
183 --coverage \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
184 --no-head \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
185 --require-mdz \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
186 --min-unique-qual {params.uniq_qual} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
187 --frag-dist ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
188 --bigwig ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
189 --annotation {input.bed} ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
190 --auc ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
191 --alts ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
192 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
193
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
194 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
195 # --alts
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
196 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
197
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
198 (time zstd ${{TMP}}.alts.tsv -o {output.nonref}) 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
199 size=$(wc -c < {output.nonref})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
200 echo "COUNT_NonrefSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
201 rm -f ${{TMP}}.alts.tsv
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
202
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
203 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
204 # --auc
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
205 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
206 mv ${{TMP}}.auc.tsv {output.auc}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
207 size=$(wc -c < {output.auc})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
208 echo "COUNT_AucSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
209 rm -f ${{TMP}}.auc.tsv
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
210
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
211 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
212 # --frag-dist
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
213 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
214 mv ${{TMP}}.frags.tsv {output.frag}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
215 size=$(wc -c < {output.frag})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
216 echo "COUNT_FragDistSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
217 rm -f ${{TMP}}.frags.tsv
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
218
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
219 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
220 # --bigwig
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
221 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
222
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
223 (time zstd ${{TMP}}.all.bw -o {output.all_bw}) 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
224 size=$(wc -c < {output.all_bw})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
225 echo "COUNT_BwSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
226 rm -f ${{TMP}}.all.bw
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
227
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
228 (time zstd ${{TMP}}.unique.bw -o {output.unique_bw}) 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
229 size=$(wc -c < {output.unique_bw})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
230 echo "COUNT_BwSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
231 rm -f ${{TMP}}.unique.bw
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
232
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
233 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
234 # --annotation
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
235 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
236
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
237 (time zstd ${{TMP}}.all.tsv -o {output.all_bw_count}) 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
238 size=$(wc -c < {output.all_bw_count})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
239 echo "COUNT_BwQuantSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
240 rm -f ${{TMP}}.all.tsv
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
241
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
242 (time zstd ${{TMP}}.unique.tsv -o {output.unique_bw_count}) 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
243 size=$(wc -c < {output.unique_bw_count})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
244 echo "COUNT_BwQuantSize ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
245 rm -f ${{TMP}}.unique.tsv
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
246
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
247 # Check that all temporaries were properly purged
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
248 set +o pipefail ; num_files=$(ls -d ${{TMP}}* 2>/dev/null | wc -l)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
249 if (( $num_files > 0 )) ; then
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
250 echo "Failed to purge files (ignore . and ..): $(ls -ad ${{TMP}}*)"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
251 exit 1
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
252 fi
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
253
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
254 echo "COUNT_BamcountComplete 1"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
255 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
256
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
257 rule sort:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
258 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
259 config['temp'] + '/{quad}.bam'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
260 wildcard_constraints:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
261 quad="[^-]+"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
262 output:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
263 bam=temp(config['temp'] + '/{quad}-sorted.bam'),
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
264 bai=temp(config['temp'] + '/{quad}-sorted.bam.bai')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
265 log:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
266 config['output'] + '/{quad}.sort.log'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
267 params:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
268 srr=lambda wildcards: wildcards.quad.split('_')[0]
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
269 threads: 8
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
270 shell:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
271 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
272 TMP="{config[temp]}/sort_temp.{params.srr}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
273 mkdir -p ${{TMP}}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
274 time samtools sort \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
275 -T ${{TMP}}/samtools_temp \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
276 -@ {threads} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
277 -m 64M \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
278 -o {output.bam} {input} 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
279 rm -rf ${{TMP}}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
280 size=$(wc -c < {output.bam})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
281 echo "COUNT_SortedBAMBytes ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
282
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
283 time samtools index -@ {threads} {output.bam} 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
284 echo "COUNT_SortComplete 1"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
285 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
286
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
287 rule align:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
288 input:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
289 reads0=config['temp'] + '/{quad}_0.fastq',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
290 reads1=config['temp'] + '/{quad}_1.fastq',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
291 reads2=config['temp'] + '/{quad}_2.fastq',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
292 index1=lambda wildcards: '%s/%s/star_idx/SAindex' % (config['ref'], wildcards.quad.split('_')[2]),
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
293 index2=lambda wildcards: '%s/%s/star_idx/SA' % (config['ref'], wildcards.quad.split('_')[2])
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
294 wildcard_constraints:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
295 quad="[^-]+"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
296 output:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
297 bam=temp(config['temp'] + '/{quad}.bam'),
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
298 jxs=config['output'] + '/{quad}.sjout.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
299 chimeric=config['output'] + '/{quad}.Chimeric.out.junction.zst',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
300 unmapped1=config['temp'] + '/{quad}_1.unmappedfastq',
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
301 unmapped2=config['temp'] + '/{quad}_2.unmappedfastq'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
302 log:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
303 config['output'] + '/{quad}.align.log'
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
304 params:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
305 index_base=lambda wildcards: '%s/%s/star_idx' % (config['ref'], wildcards.quad.split('_')[2]),
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
306 srr=lambda wildcards: wildcards.quad.split('_')[0],
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
307 star_params="%s %s" % (config.get('star', ''), '--genomeLoad LoadAndRemove' if 'inputs' not in config else '')
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
308 threads: 16
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
309 shell:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
310 """
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
311 READ_FILES="{input.reads0}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
312 if [[ -s {input.reads2} ]] ; then
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
313 READ_FILES="{input.reads1} {input.reads2}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
314 fi
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
315 TMP="{config[temp]}/align_temp.{params.srr}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
316 rm -rf ${{TMP}}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
317 time STAR \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
318 {params.star_params} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
319 --runMode alignReads \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
320 --runThreadN {threads} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
321 --genomeDir {params.index_base} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
322 --readFilesIn ${{READ_FILES}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
323 --twopassMode None \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
324 --outTmpDir ${{TMP}} \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
325 --outReadsUnmapped Fastx \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
326 --outMultimapperOrder Random \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
327 --outSAMreadID Number \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
328 --outSAMtype BAM Unsorted \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
329 --outSAMmode NoQS \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
330 --outSAMattributes NH MD \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
331 --chimOutType Junctions \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
332 --chimOutJunctionFormat 1 \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
333 --chimSegmentReadGapMax 3 \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
334 --chimJunctionOverhangMin 12 \
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
335 --chimSegmentMin 12 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
336
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
337 # Full set of output files:
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
338 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
339 # Aligned.out.bam
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
340 # Chimeric.out.junction
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
341 # Log.final.out
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
342 # Log.out
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
343 # Log.progress.out
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
344 # SJ.out.tab
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
345 # Unmapped.out.mate1
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
346 # Unmapped.out.mate2 (if any reads were paired-end)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
347
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
348 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
349 # Logs
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
350 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
351 rm -rf ${{TMP}}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
352 cat Log.out >> {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
353 cat Log.final.out >> {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
354 rm -f Log*.out
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
355
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
356 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
357 # Junctions
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
358 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
359 test -f SJ.out.tab
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
360 time zstd SJ.out.tab -o {output.jxs} 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
361 rm -f SJ.out.tab
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
362 size=$(wc -c < {output.jxs})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
363 echo "COUNT_CompressedJxBytes ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
364
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
365 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
366 # Chimerics
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
367 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
368 test -f Chimeric.out.junction
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
369 test -s Chimeric.out.junction
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
370 sort -k1,1 -n -k2,2 Chimeric.out.junction > Chimeric.out.junction.sorted
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
371 time zstd Chimeric.out.junction.sorted -o {output.chimeric} 2>&1 | tee -a {log}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
372 rm -f Chimeric.out.junction Chimeric.out.junction.sorted
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
373 size=$(wc -c < {output.chimeric})
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
374 echo "COUNT_ChimericBytes ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
375
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
376 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
377 # Unmapped
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
378 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
379 touch {output.unmapped2}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
380 test -f Unmapped.out.mate1
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
381 mv Unmapped.out.mate1 {output.unmapped1}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
382 if [[ -f Unmapped.out.mate2 ]] ; then
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
383 mv Unmapped.out.mate2 {output.unmapped2}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
384 fi
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
385
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
386 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
387 # Alignments
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
388 #
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
389 size=$(wc -c < Aligned.out.bam)
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
390 echo "COUNT_BAMBytes ${{size}}"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
391 mv Aligned.out.bam {output.bam}
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
392 echo "COUNT_AlignComplete 1"
f43dd6f7c687 Uploaded
chrisw
parents:
diff changeset
393 """