-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reannotation.Snakemake
517 lines (439 loc) · 27.8 KB
/
Reannotation.Snakemake
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#run snaxli kemake
#
#snakemake -d `pwd` -s `pwd`/Reannotation.Snakemake --stats snakemake.stats -j 100 --cluster 'sbatch {params.cluster}'
#########################################################################
## prepare directories and necessary files: #
# maindirectory: Reannotation #
# files: genome.fasta, annotation.gff3, #
# subdirectory "reads" - files: samples.fastq or samples.bam
# # necessary programs:
# # introncalc2.0.pl, Star, cufflinks, Trinity, PASA
# # clustermodules:
# # module load python/3.4.2
#
#########################################################################
#################################
# #
# Import modules #
# #
#################################
import math
import os
#################################
# #
# Own function definition #
# #
#################################
def getFileNumber(file_name, entryNumber):
f = open(file_name)
total = 0
for line in f:
if ">" in line:
total += 1
f.close()
return math.floor(2*total/entryNumber+1);
#get bam file
def getBamFile(dir,species):
list=[];
for file in os.listdir(dir+'/'+species+'.reads/.'):
if fnmatch.fnmatch(file,'*.*.bam'):
list.append(file);
return list
#uniquify ids
def f10(seq, idfun=None):
seen = set()
results=[]
if idfun is None:
for x in seq:
if x in seen:
continue
seen.add(x)
results.append(x)
else:
for x in seq:
x = idfun(x)
if x in seen:
continue
seen.add(x)
results.append(x)
return results
#################################
# #
# Variables setup #
# #
#################################
#################################
#Environment #
#################################
WORKDIR="/media/work/genomes/exoDer/annotation/reAnnotation/"
#WORKDIR="/media/ckustor/work/Reannotation"
#COMPUTEDIR="/tmp"
COMPUTEDIR="/global/lv70539/htafer"
READS="/global/lv70539/htafer/reads"
#COMPUTEDIR="/scratch"
#################################
#THREADS #
#################################
THREADS=16
##################################
#FILES #
##################################
#GENOME
ID="exoDer";
REF=ID+".fasta"
#BAMFILES
BAMFILES="/global/lv70539/htafer/reads/{samples}.fastq"
BAMS ,= glob_wildcards(BAMFILES);
ANNOT=ID+".Protein.gff3"
BLAST="/home/htafer/share/database/UniProt90pSaccharomyceta.fasta"
PFAM="/home/htafer/bin/PfamScan/db/"
##################################
#BIOLOGY #
##################################
#BIOLOGY
INTRON=2000
#Which rules are run locally # depends on which computer the pipeline is running
#localrules: all, clean, STAR, STARIdx, bamToFastq, mergeBam, cufflinks, composeMerge, mergeAssemblies, normalization, trinityAlignment, trinityDeNovo, PASA, PASAtrainingset, PASAhints, mergeFastq, Annotcompare
localrules: all, clean, composeMerge, mergeAssemblies, PASAtrainingset, PASAhints, mergeFastq, STARIdx
##################################
## #
## ALL #
## #
##################################
rule all:
input: annot=ID+".AnnotationUpdate", pasa=ID+".PASA", introns=ID+".introns.gff", transdecoder=ID+".PASAtrainingset", newProt=ID+".newProtein.gff3", otherTranscript=ID+".noKnownFunc.gff3"
##################################################################################
# #
# Get The Rest #
# #
##################################################################################
rule otherTranscript:
input: lnc=ID+".lncRNA.gff3", coding=ID+".allProtein.gff3", pasaAssemblies=ID+".PASA/"+ID+".pasa_assemblies.gff3"
output: ID+".noKnownFunc.gff3"
shell:"""
mergealign.pl < {input.pasaAssemblies} | cut -f 1-9 > merged.gff
bedtools intersect -s -v -a merged.gff -b {input.lnc} | bedtools intersect -s -v -a - -b {input.coding} > {output}
"""
#################################################################################
# #
# Get lncRNAs #
# #
#################################################################################
########################
#### lncRNA ####
########################
rule lncRNA:
input: ids="./CPAT/cpat.results", blast="BLAST/blast.coding.candidates", pfam="PFAM/"+ID+".pfam.coding.candidates", pasaAssemblies=ID+".PASA/"+ID+".pasa_assemblies.gff3"
output: lnc=ID+".lncRNA.gff3"
threads: THREADS
params:
shell:"""
cat {WORKDIR}/{input.blast} {WORKDIR}/{input.pfam} | sort -u > coding.candidates
cut -f 1 {WORKDIR}/{input.ids} > id.cpat.results
cat id.cpat.results | perl -lane 'my $count=`grep -cw $F[0] coding.candidates`; chomp($count); if($count==0){{print $F[0]}};' | sort -u | fgrep -w -f - {WORKDIR}/{input.pasaAssemblies} > {WORKDIR}/{output.lnc}
"""
########################
#### BLAST ####
########################
rule blast:
input: cpat="./CPAT/"+ID+".cpat.fasta"
output: blast="BLAST/blast.coding.candidates"
threads: THREADS
params:
shell:"""
cd BLAST
formatdb -i {BLAST} -p T -n uniprot90S
blastx -query {WORKDIR}/{input.cpat} -db uniprot90S -num_threads {threads} -outfmt 7 -out blast.out
cat blast.out | perl -lane 'if($F[7]>$F[6] && $F[10]<0.001){{print;}}' | cut -f 1 | sort -u | sed -r 's/asmbl/>asmbl/' > {WORKDIR}/{output.blast}
"""
########################
#### PFAM ####
########################
rule pfam:
input: "./CPAT/"+ID+".cpat.fasta"
output: "PFAM/"+ID+".pfam.coding.candidates"
threads: THREADS
shell: """
mkdir -p PFAM
cd PFAM
pfam_scan.pl -fasta {WORKDIR}/{input} -dir {PFAM} -cpu {threads} -outfile pfam.out
cat pfam.out | grep -v '#' | perl -lane 'if($F[2]>$F[1] && $F[12]<0.001){{print;}}' | cut -f 1 | sort -u | sed -r 's/asmbl/>asmbl/' > {WORKDIR}/{output}
"""
################
#### CPAT ####
################
rule cpat:
input: gene="noOverlap.fasta", fa="assemblies.fa", nonc="nonconding.fasta", coding="codingseq.fasta"
output: fasta="./CPAT/"+ID+".cpat.fasta", results="CPAT/cpat.results"
threads: THREADS
shell:"""
mkdir -p CPAT
cd CPAT
make_hexamer_tab.py -c {WORKDIR}/{input.coding} -n {WORKDIR}/{input.nonc} > hexamer.tab
make_logitModel.py -c {WORKDIR}/{input.coding} -n {WORKDIR}/{input.nonc} -x hexamer.tab -o {ID}
cpat.py -d {ID}.logit.RData -x hexamer.tab -g {WORKDIR}/{input.gene} -o {ID}.cpat
cat {ID}.cpat | perl -lane 'if($F[5]<0.01){{print;}}' | sort -k 2,2gr | sed -r 's/ASMBL/asmbl/' > {WORKDIR}/{output.results}
parallel -j {threads} 'grep -P "{{}}$" {WORKDIR}/{input.fa} -A 1 ' ::: `cat {WORKDIR}/{output.results} | cut -f 1 | grep asmbl` > {WORKDIR}/{output.fasta}
"""
rule prepareFiles:
input: fa="assemblies.fa", intron=ID+".introns.gff", updateAnn=ID+".allProtein.gff3"
output: coding="codingseq.fasta", nonc="nonconding.fasta"
params:
threads: THREADS
shell:"""
#We have to prepare a training data set in order to train CPAT
#Start with the protein dataset
getAnnoFasta.pl {input.updateAnn} --seqfile {REF}
cat {ID}.allProtein3.cdsexons | sed -r 's/.cds[0-9]//g' | perl -lane 'BEGIN{{my $previousId="";}} if($F[0]=~/>/){{if($F[0] eq $previousId){{next;}}else{{$previousId=$F[0]; print "\n",$F[0]}}}}else{{printf $F[0]}}' > {output.coding}
#Now generate the non coding dataset with the intron
cat {input.intron} | sed -r 's/;src=E//g' > temp.introns
parallel -j {threads} 'grep -P "{{}}$" {input.fa} -A 1 ' ::: `cat temp.introns | cut -f 9 | sed -r 's/.+Target=([^$]+)/\\1/' | sort -u` > {output.nonc}
"""
################
# INTERSECT #
################
rule intersect:
input: transAnn=ID+".PASAtrainingset/"+ID+".assemblies.fasta.transdecoder.genome.gff3", pasaAssemblies=ID+".PASA/"+ID+".pasa_assemblies.gff3", pasaSequence=ID+".PASA/"+ID+".assemblies.fasta", updateAnn=ID+".allProtein.gff3"
output: fasta="noOverlap.fasta", fa="assemblies.fa"
params:
threads: THREADS
shell:"""
#put the gene together
mergealign.pl < {input.pasaAssemblies} | cut -f 1-9 > merged.gff
#check which gene overlap neither with the updated protein annotation nor with the pasa protein annotation
intersectBed -v -s -a merged.gff -b {input.updateAnn} > no.gff3
intersectBed -v -s -a no.gff3 -b {input.transAnn} > noOverlap.gff
perl ~/bin/rmenterdb.pl < {input.pasaSequence} > {output.fa}
#Fetch the sequences
parallel -j {threads} 'grep -P "{{}}$" {output.fa} -A 1 ' ::: `cat noOverlap.gff | cut -f 9 | sed -r 's/.+Target=([^$]+)/\\1/' | sort -u` > {output.fasta}
"""
#################################################################################
# #
# Get New Proteins #
# #
#################################################################################
rule newProtein:
input: transAnn=ID+".PASAtrainingset/"+ID+".assemblies.fasta.transdecoder.genome.gff3", updateAnn=ID+".AnnotationUpdate/"+ID+".update.gff3"
output:newProtein=ID+".newProtein.gff3", allProtein=ID+".allProtein.gff3"
shell:"""
grep -P "\tCDS\t" {input.transAnn} | mergealign.pl > {ID}.transdecoder.CDS.gff3
grep -P "\tCDS\t" {input.updateAnn} | mergealign.pl > {ID}.update.CDS.gff3
bedtools intersect -v -a {ID}.transdecoder.CDS.gff3 -b {ID}.update.CDS.gff3 | cut -f 9 | sed -r 's/.+Parent=//' | fgrep -f - {input.transAnn} | grep -P "\tCDS\t" > {ID}.newProtein.CDS.gff3
cut -f 9 {ID}.newProtein.CDS.gff3 | sed -r 's/.+Parent=//' | fgrep -f - {input.transAnn} | perl -lane 'if($F[2]=~/mRNA/){{$anno=$F[8]; $anno=~s/ID=.+Parent=/ID=/; $anno=~/=(asmbl[^g]+g[^;]+)/; my $id=$1; $id=~s/g\./m./; $anno=~s/ORF/$id/; print "$F[0]\t$F[1]\tgene\t$F[3]\t$F[4]\t$F[5]\t$F[6]\t$F[7]\t$anno"; $anno=$F[8]; $anno=~s/ORF/$id/; print join("\t",@F[0..7]),"\t$ann\
o";}}else{{print;}}' > {output.newProtein}
cat {input.updateAnn} {output.newProtein} > {output.allProtein}
"""
#################################################################################
# #
# Annotation Update #
# #
#################################################################################
rule Annotcompare:
input: genome=ID+".fasta", annot=ANNOT, dn="./reads/"+ID+".trinityDN", gg="./reads/"+ID+".trinityGG",dirIn=ID+".PASA"
output: dir=ID+".AnnotationUpdate", updateGFF3=ID+".AnnotationUpdate/"+ID+".update.gff3"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell: """
#mysql start
cd $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/scripts/mysql_install_db --default-files=./my-new.cnf --datadir=$HOME/bin/mysql-5.6.22/data
cp $HOME/my-new.cnf $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/bin/mysqld_safe --defaults-file=./my-new.cnf --skip-grant-tables &
sleep 10
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/PASA${{prefix}}
cd {COMPUTEDIR}/PASA${{prefix}}
#check compatibility
#$PASAHOME/misc_utilities/pasa_gff3_validator.pl {WORKDIR}/{input.annot}
cat {WORKDIR}/{input.dn}/Trinity.fasta {WORKDIR}/{input.gg}/Trinity-GG.fasta > {COMPUTEDIR}/PASA${{prefix}}/transcripts.fasta
#Prepare annotCompare.config
DBNAME={ID}
cat $PASAHOME/pasa_conf/pasa.annotationCompare.Template.txt | sed -r "s/DBNAME/${{DBNAME}}/" > ./annotCompare.config
$PASAHOME/scripts/Launch_PASA_pipeline.pl -c ./annotCompare.config -A -L --annots_gff3 {WORKDIR}/{input.annot} -g {WORKDIR}/{input.genome} -t ./transcripts.fasta
mv {COMPUTEDIR}/PASA${{prefix}} {WORKDIR}/{output.dir}
cd {WORKDIR}/{output.dir}
mv *gene_structures_post_PASA_updates.*.gff3 {output.updateGFF3}
#mysql shutdown
cd $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/bin/mysqladmin --defaults-file=./my-new.cnf shutdown
"""
##################################
## #
## PASA FIRST PASS #
## #
##################################
rule PASA:
input: genome=ID+".fasta", dn="./reads/"+ID+".trinityDN", gg="./reads/"+ID+".trinityGG", cf=ID+".cufflinks.gtf"
output: dir=ID+".PASA"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
#mysql start
cd $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/scripts/mysql_install_db --default-files=./my-new.cnf --datadir=$HOME/bin/mysql-5.6.22/data
cp $HOME/my-new.cnf $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/bin/mysqld_safe --defaults-file=./my-new.cnf --skip-grant-tables &
sleep 10
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/PASA${{prefix}}
cd {COMPUTEDIR}/PASA${{prefix}}
#Prepare files
cat {WORKDIR}/{input.dn}/Trinity.fasta {WORKDIR}/{input.gg}/Trinity-GG.fasta > {COMPUTEDIR}/PASA${{prefix}}/transcripts.fasta
# NOT genome-guided, only de novo
$PASAHOME/misc_utilities/accession_extractor.pl < {WORKDIR}/{input.dn}/Trinity.fasta > {COMPUTEDIR}/PASA${{prefix}}/tdn.accs
#Prepare alignAssembly.config
DBNAME={ID}
cat $PASAHOME/pasa_conf/pasa.alignAssembly.Template.txt | sed -r "s/DBNAME/${{DBNAME}}/" > ./alignAssembly.config
$PASAHOME/scripts/Launch_PASA_pipeline.pl -c ./alignAssembly.config -C -r -R -g {WORKDIR}/{input.genome} --ALIGNERS blat,gmap -t ./transcripts.fasta --transcribed_is_aligned_orient --TDN ./tdn.accs --cufflinks_gtf {WORKDIR}/{input.cf} -I {INTRON} --stringent_alignment_overlap 30.0 --CPU {threads}
$PASAHOME/scripts/build_comprehensive_transcriptome.dbi -c ./alignAssembly.config -t ./transcripts.fasta --min_per_ID 95 --min_per_aligned 30
mv {COMPUTEDIR}/PASA${{prefix}} {WORKDIR}/{output.dir}
#shutdown mysql
cd $HOME/bin/mysql-5.6.22/
$HOME/bin/mysql-5.6.22/bin/mysqladmin --defaults-file=./my-new.cnf shutdown
"""
rule PASAtrainingset:
input: ID+".PASA"
output: dir=ID+".PASAtrainingset"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
cd {WORKDIR}/{input}
$PASAHOME/scripts/pasa_asmbls_to_training_set.dbi --pasa_transcripts_fasta {WORKDIR}/{input}/{ID}.assemblies.fasta --pasa_transcripts_gff3 {WORKDIR}/{input}/{ID}.pasa_assemblies.gff3
mkdir {WORKDIR}/{output}
mv {ID}.assemblies.fasta.transdecoder.* {WORKDIR}/{output}
"""
rule PASAhints:
input: ID+".PASA"
output: ID+".introns.gff"
params: cluster="-cwd -V"
threads: THREADS
shell:"""
perl {WORKDIR}/introncalc2.0.pl < {WORKDIR}/{input}/{ID}.pasa_assemblies.gff3 > {WORKDIR}/{output}
"""
##################################
## #
## TRINITY #
## #
##################################
rule mergeFastq:
input: expand("./reads/{samples}.fastq", samples=BAMS)
output: "./reads/"+ID+".merged.fq"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
cat {WORKDIR}/{input} > {WORKDIR}/{output}
"""
rule normalization:
input: "./reads/"+ID+".merged.fq"
output: "./reads/"+ID+".trinityIn"
params: cluster="--partition=mem_0256 --qos=normal_0256"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/trinityIn${{prefix}}
$HOME/bin/trinityrnaseq-2.0.6/util/insilico_read_normalization.pl --seqType fq --single {input} --SS_lib_type F --JM 200G --max_cov 30 --CPU {threads} --output {COMPUTEDIR}/trinityIn${{prefix}}
mv {COMPUTEDIR}/trinityIn${{prefix}} {WORKDIR}/{output}
"""
rule trinityDeNovo:
input: "./reads/"+ID+".trinityIn"
output: "./reads/"+ID+".trinityDN"
params: cluster="--partition=mem_0256 --qos=normal_0256"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/trinity${{prefix}}
fastQName=`ls {WORKDIR}/{input}/*.ok | sed -r 's/.ok//'`
Trinity --seqType fq --single ${{fastQName}} --SS_lib_type F --CPU {threads} --output {COMPUTEDIR}/trinity${{prefix}} --max_memory 240G
mv {COMPUTEDIR}/trinity${{prefix}} {WORKDIR}/{output}
"""
rule trinityAlignment:
input: "./reads/"+ID+".merged.bam"
output: dire="./reads/"+ID+".trinityGG"
params: cluster="--partition=mem_0128 --qos=normal_0128"
threads: THREADS
shell:"""
#prefix=`date --rfc-3339=ns | md5sum | head -c 16`
prefix="cc3498d43dd4cabb"
mkdir -p {COMPUTEDIR}/trinity${{prefix}}
Trinity --genome_guided_bam {input} --genome_guided_max_intron {INTRON} --SS_lib_type F --max_memory 120G --CPU {threads} --output {COMPUTEDIR}/trinity${{prefix}}
mv {COMPUTEDIR}/trinity${{prefix}} {WORKDIR}/{output}
"""
##################################
## #
## CUFFLINKS #
## #
##################################
rule mergeAssemblies:
input: "./reads/assemblies.txt"
output: dir="./reads", file=ID+".cufflinks.gtf"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
cuffmerge -o {WORKDIR}/{output.dir} -s {REF} {input} -p {threads}
mv {WORKDIR}/{output.dir}/merged.gtf {output.file}
"""
rule composeMerge:
input: expand("./reads/{samples}.cufflinks/transcripts.gtf", samples=BAMS)
output: txt="./reads/assemblies.txt"
shell:"""
ls {WORKDIR}/{input} > {output.txt}
"""
rule cufflinks:
input: "./reads/{samples}.mapped.sam"
output: dir="./reads/{samples}.cufflinks", file="./reads/{samples}.cufflinks/transcripts.gtf"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/${{prefix}}
cufflinks -o {COMPUTEDIR}/${{prefix}} -p {threads} -u -I {INTRON} --max-bundle-length 10000 --library-type ff-firststrand --min-intron-length 30 {WORKDIR}/{input}
mv {COMPUTEDIR}/${{prefix}}/* {WORKDIR}/{output.dir}
"""
##################################
## #
## Spliced RNAseq mapping #
## #
##################################
rule mergeBam:
input: expand("./reads/{samples}.mapped.bam", samples=BAMS)
output: "./reads/"+ID+".merged.bam"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/${{prefix}}
samtools merge {COMPUTEDIR}/${{prefix}}/merged.bam {input}
samtools sort {COMPUTEDIR}/${{prefix}}/merged.bam {COMPUTEDIR}/${{prefix}}/merged.sorted
#mergeBam INPUT={WORKDIR}/{input} SORT_ORDER=coordinate OUTPUT={WORKDIR}/{output}
mv {COMPUTEDIR}/${{prefix}}/merged.sorted.bam {WORKDIR}/{output}
rm -rf {COMPUTEDIR}/${{prefix}}
"""
rule STAR:
input: reads="./reads/{samples}.fastq", idx=ID+".idx"
output: bam="./reads/{samples}.mapped.bam", sam="./reads/{samples}.mapped.sam"
params: cluster="--partition=mem_0064 --qos=normal_0064"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/${{prefix}}
cd {COMPUTEDIR}/${{prefix}}
STAR --genomeDir {WORKDIR}/{input.idx} --readFilesIn {WORKDIR}/{input.reads} --runThreadN {threads} --twopassMode Basic --outReadsUnmapped None --chimSegmentMin 12 --chimJunctionOverhangMin 12 --alignSJDBoverhangMin 10 --alignIntronMax {INTRON} --chimSegmentReadGapMax parameter 3 --alignSJstitchMismatchNmax 5 -1 5 5
#sort sam files
samtools view -bS {COMPUTEDIR}/${{prefix}}/Aligned.out.sam | samtools sort - {COMPUTEDIR}/${{prefix}}/Aligned.sorted
samtools view -h {COMPUTEDIR}/${{prefix}}/Aligned.sorted.bam | awk 'BEGIN {{OFS="\t"}} {{split($6,C,/[0-9]*/); split($6,L,/[SMDIN]/); if (C[2]=="S") {{$10=substr($10,L[1]+1); $11=substr($11,L[1]+1)}}; if (C[length(C)]=="S") {{L1=length($10)-L[length(L)-1]; $10=substr($10,1,L1); $11=substr($11,1,L1); }}; gsub(/[0-9]*S/,"",$6); print}}' > {COMPUTEDIR}/${{prefix}}/Aligned.sorted.sam
#mv results to final directory
mv {COMPUTEDIR}/${{prefix}}/Aligned.sorted.sam {WORKDIR}/{output.sam}
mv {COMPUTEDIR}/${{prefix}}/Aligned.sorted.bam {WORKDIR}/{output.bam}
#clean up
rm -rf {COMPUTEDIR}/${{prefix}}
"""
rule STARIdx:
input: genome=ID+".fasta", gff=ID+".Protein.gff"
output: dir=ID+".idx"
threads: THREADS
shell:"""
prefix=`date --rfc-3339=ns | md5sum | head -c 16`
mkdir -p {COMPUTEDIR}/${{prefix}}
STAR -sjdbGTFfile {input.gff} --sjdbOverhang 100 --runMode genomeGenerate --genomeDir {COMPUTEDIR}/${{prefix}} --genomeFastaFiles {WORKDIR}/{input} --runThreadN {threads}
mv {COMPUTEDIR}/${{prefix}} {WORKDIR}/{output.dir}
"""