-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05b-clean_sort_addRG_markdup_realign.py
executable file
·172 lines (145 loc) · 6.6 KB
/
05b-clean_sort_addRG_markdup_realign.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
#! /usr/bin/env python
# PBS cluster job submission in Python
# Clean, sort, add Read groups, and Mark duplicates, realign around indels
# By Jean P. Elbers
# Last modified 22 Jan 2015
###############################################################################
Usage = """
05b-clean_sort_addRG_markdup_realign.py - version 1.0
Command:
cd InDir = /work/jelber2/immunome_2014/run1/clean-sort-addRG/
1.Uses samtools merge to combine stampy bam files
~/bin/samtools-1.1/samtools merge \
/work/jelber2/immunome_2014/combined/merged-bams/Sample.bam \
/work/jelber2/immunome_2014/run1/clean-sort-addRG/Sample-CL-RG.bam \
/work/jelber2/immunome_2014/run2/clean-sort-addRG/Sample-CL-RG.bam
2.Uses samtools flagstat to get alignment metrics on stampy aligned bam file
cd /work/jelber2/immunome_2014/combined/merged-bams/
~/bin/samtools-1.1/samtools flagstat \
../merged-bams/Sample.bam > ../merged-bams/Sample.bam.flagstat
3.Clean the initial stampy BAM file:
java -Xmx8g -jar ~/bin/picard-tools-1.128/picard.jar CleanSam \
I=../merged-bams/Sample.bam \
O=../clean-sort-addRG-markdup/Sample-CL2.bam
4.Mark PCR duplicates and optical duplicates:
java -Xmx8g -jar ~/bin/picard-tools-1.128/picard.jar MarkDuplicates \
I=../clean-sort-addRG-markdup/Sample-CL2.bam \
O=../clean-sort-addRG-markdup/Sample-CL2-MD.bam \
METRICS_FILE=../clean-sort-addRG-markdup/Sample-CL2-MD.metrics \
MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=250 \
CREATE_INDEX=true \
ASSUME_SORTED=false \
REMOVE_DUPLICATES=false
5.Find INDEL regions within individual BAM files
java -Xmx8g -jar ~/bin/GATK-3.3.0/GenomeAnalysisTK.jar \
-T RealignerTargetCreator \
-R RefDir/GCF_000241765.3_Chrysemys_picta_bellii-3.0.3_genomic.fna \
-I ../clean-sort-addRG-markdup/Sample-CL2-MD.bam \
--minReadsAtLocus 4 \
-o ../realign-around-indels/Sample.merged.intervals
6.Realign the BAM based on indel intervals:
java -Xmx8g -jar ~/bin/GATK-3.3.0/GenomeAnalysisTK.jar \
-T IndelRealigner \
-R RefDir/GCF_000241765.3_Chrysemys_picta_bellii-3.0.3_genomic.fna \
-I ../clean-sort-addRG-markdup/Sample-CL2-MD.bam \
-targetIntervals ../realign-around-indels/Sample.merged.intervals \
-LOD 3.0 \
-o ../realign-around-indels/Sample-realigned.bam
Directory info:
(1)/work/jelber2/immunome_2014/run1/clean-sort-addRG
(2)/work/jelber2/immunome_2014/combined/merged-bams
(3) /clean-sort-addRG-markdup
(4) /realign-around-indels
InDir = /work/jelber2/immunome_2014/combined/
Input Files = Sample-CL-RG.bam
Usage (execute following code in InDir):
~/scripts/immunome_2014/05b-clean_sort_addRG_markdup_realign *-CL-RG.bam
"""
###############################################################################
import os, sys, subprocess, re #imports os, sys, subprocess, re modules
if len(sys.argv)<2:
print Usage
else:
FileList = sys.argv[1:]
RefDir = "/work/jelber2/reference"
InDir = "/work/jelber2/immunome_2014/combined/"
OutDir1 = "merged-bams"
OutDir2 = "clean-sort-addRG-markdup"
OutDir3 = "realign-around-indels"
os.chdir(InDir)
if not os.path.exists(OutDir1):
os.mkdir(OutDir1) # if OutDir1 does not exist, make it
if not os.path.exists(OutDir2):
os.mkdir(OutDir2) # if OutDir2 does not exist, make it
if not os.path.exists(OutDir3):
os.mkdir(OutDir3) # if OutDir3 does not exist, make it
os.chdir(InDir)
for InFileName in FileList: # so samtools grabs only the file names (i.e., Samples)
FileSuffix = "-CL-RG.bam" # string to remove from InFileName
Sample = InFileName.replace(FileSuffix,'') # creates Sample string
# Customize your job options here
Queue = "single"
Allocation = "hpc_gopo02"
Processors = "nodes=1:ppn=4"
WallTime = "04:00:00"
LogOut = "/work/jelber2/immunome_2014/combined/clean-sort-addRG-markdup"
LogMerge = "oe"
JobName = "clean-sort-addRG-markdup-realign-%s" % (Sample)
Command ="""
~/bin/samtools-1.1/samtools merge \
/work/jelber2/immunome_2014/combined/merged-bams/%s.bam \
/work/jelber2/immunome_2014/run1/clean-sort-addRG/%s-CL-RG.bam \
/work/jelber2/immunome_2014/run2/clean-sort-addRG/%s-CL-RG.bam
cd /work/jelber2/immunome_2014/combined/merged-bams/
~/bin/samtools-1.1/samtools flagstat \
../merged-bams/%s.bam > ../merged-bams/%s.bam.flagstat
java -Xmx8g -jar ~/bin/picard-tools-1.128/picard.jar CleanSam \
I=../merged-bams/%s.bam \
O=../clean-sort-addRG-markdup/%s-CL2.bam
java -Xmx8g -jar ~/bin/picard-tools-1.128/picard.jar MarkDuplicates \
I=../clean-sort-addRG-markdup/%s-CL2.bam \
O=../clean-sort-addRG-markdup/%s-CL2-MD.bam \
METRICS_FILE=../clean-sort-addRG-markdup/%s-CL2-MD.metrics \
MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=250 \
CREATE_INDEX=true \
ASSUME_SORTED=false \
REMOVE_DUPLICATES=false
java -Xmx8g -jar ~/bin/GATK-3.3.0/GenomeAnalysisTK.jar \
-T RealignerTargetCreator \
-R %s/GCF_000241765.3_Chrysemys_picta_bellii-3.0.3_genomic.fna \
-I ../clean-sort-addRG-markdup/%s-CL2-MD.bam \
--minReadsAtLocus 4 \
-o ../realign-around-indels/%s.merged.intervals
java -Xmx8g -jar ~/bin/GATK-3.3.0/GenomeAnalysisTK.jar \
-T IndelRealigner \
-R %s/GCF_000241765.3_Chrysemys_picta_bellii-3.0.3_genomic.fna \
-I ../clean-sort-addRG-markdup/%s-CL2-MD.bam \
-targetIntervals ../realign-around-indels/%s.merged.intervals \
-LOD 3.0 \
-o ../realign-around-indels/%s-realigned.bam""" % \
(Sample, Sample, Sample,
Sample, Sample,
Sample, Sample,
Sample, Sample, Sample,
RefDir, Sample, Sample,
RefDir, Sample, Sample, Sample)
JobString = """
#!/bin/bash
#PBS -q %s
#PBS -A %s
#PBS -l %s
#PBS -l walltime=%s
#PBS -o %s
#PBS -j %s
#PBS -N %s
cd %s
%s\n""" % (Queue, Allocation, Processors, WallTime, LogOut, LogMerge, JobName, InDir, Command)
#Create pipe to qsub
proc = subprocess.Popen(['qsub'], shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
(child_stdout, child_stdin) = (proc.stdout, proc.stdin)
#Print JobString
JobName = proc.communicate(JobString)[0]
print JobString
print JobName