-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoggenotype.py
More file actions
232 lines (209 loc) · 8.62 KB
/
Copy pathdoggenotype.py
File metadata and controls
232 lines (209 loc) · 8.62 KB
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
# functions for running genotype of gVCF
import sys
import subprocess
import os
import argparse
import time
import socket
import shutil
# Helper function to run commands, handle return values and print to log file
def runCMD(cmd):
val = subprocess.Popen(cmd, shell=True).wait()
if val == 0:
pass
else:
print('command failed')
print(cmd)
sys.exit(1)
###############################################################################
# Helper function to run commands, handle return values and print to log file
def runCMD_output(cmd):
val = subprocess.Popen(cmd, text=True, shell=True, stdout = subprocess.PIPE)
resLines = []
for i in val.stdout:
i = i.rstrip()
resLines.append(i)
return resLines
#############################################################################
#####################################################################
def open_bgzip_write(fileName,threads = 1):
try:
gc = 'bgzip -@ %i > %s ' % (threads, fileName)
outFile = os.popen(gc, 'w')
except:
print("ERROR!! Couldn't open the output file " + fileName+ " (with bgzip)\n")
sys.exit(1)
return outFile
#####################################################################
# setup paths to default programs to use and checks for required programs
def check_prog_paths(myData):
myData['logFile'].write('\nChecking for required programs...\n')
for p in ['gatk']:
if shutil.which(p) is None:
s = p + ' not found in path! please fix (module load?)'
print(s, flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].close()
sys.exit()
else:
myData['logFile'].write('%s\t%s\n' % (p,shutil.which(p)))
myData['logFile'].flush()
#############################################################################
def init_log(myData):
k = list(myData.keys())
k.sort()
myData['startTime'] = time.localtime()
myData['tStart'] = time.time()
t = time.strftime("%a, %d %b %Y %H:%M:%S", myData['startTime'])
myData['logFile'].write(t + '\n')
hn = socket.gethostname()
myData['logFile'].write('Host name: %s\n' % hn)
print('Host name: %s\n' % hn,flush=True)
myData['logFile'].write('\nInput options:\n')
for i in k:
if i in ['logFile']:
continue
myData['logFile'].write('%s\t%s\n' % (i,myData[i]))
myData['logFile'].flush()
#############################################################################
#############################################################################
def check_dir_space(myData,checkSize = True):
myData['logFile'].write('\nchecking file systems\n')
# check tmp dir
if os.path.isdir(myData['tmpDir']) is False:
s = myData['tmpDir'] + ' is not found! making it'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
cmd = 'mkdir -p %s ' % myData['tmpDir']
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
if os.path.isdir(myData['finalDir']) is False:
s = myData['finalDir'] + ' is not found! please check'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
sys.exit()
cmd = 'df -h %s' % myData['tmpDir']
o = runCMD_output(cmd)
myData['logFile'].write(cmd + '\n')
myData['logFile'].write(o[0] + '\n')
myData['logFile'].write(o[1] + '\n')
myData['logFile'].write('\n')
cmd = 'df -h %s' % myData['finalDir']
o = runCMD_output(cmd)
myData['logFile'].write(cmd + '\n')
myData['logFile'].write(o[0] + '\n')
myData['logFile'].write(o[1] + '\n')
stats = os.statvfs(myData['tmpDir'])
freeSpace = stats.f_frsize * stats.f_bavail
freeSpaceGb = freeSpace / (1024**3)
if freeSpaceGb < 20.0:
s = 'less than 20 Gb free in tmpDir! %f\n' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
if checkSize is True: # kill job
s = 'ERROR!! less than 20 Gb free in tmpDir! %f\n Exiting Script!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].close()
sys.exit()
else:
s = 'more than 20 Gb free in tmpDir! %f\n Ok!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
stats = os.statvfs(myData['finalDir'])
freeSpace = stats.f_frsize * stats.f_bavail
freeSpaceGb = freeSpace / (1024**3)
if freeSpaceGb < 50.0:
s = 'less than 50 Gb free in final dir! %f\n!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
if checkSize is True: # kill job
s = 'ERROR less than 50 Gb free in final dir! %f\n! Exiting!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].close()
sys.exit()
else:
s = 'more than 50 Gb free in final dir! %f\n Ok!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].flush()
#############################################################################
def remove_tmp_dir(myData,run=True):
#setup, run, and apply BQSR
s = 'starting remove tmp dir: %s ' % (myData['tmpDir'])
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
check_dir_space(myData,checkSize = False)
if run is True:
shutil.rmtree(myData['tmpDir'])
s = 'removed!'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else:
s = 'skipping rmtree!'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def run_GenomicsDBImport(myData):
s = 'Starting GenomicsDBImport'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['genomicsdb'] = myData['tmpDir'] + 'chunkDB'
cmd = 'gatk --java-options "-Xmx5g -Xms5g" GenomicsDBImport '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' --L %s ' % myData['region']
cmd += ' --sample-name-map %s ' % myData['samples']
cmd += ' --batch-size 50 '
cmd += ' --genomicsdb-workspace-path %s ' % myData['genomicsdb']
cmd += ' --genomicsdb-shared-posixfs-optimizations ' # to check
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def run_GenotypeGVCFs(myData):
s = 'Starting GenotypeGVCFs'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
cmd = 'gatk --java-options "-Xmx5g -Xms5g" GenotypeGVCFs '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' -R %s ' % myData['ref']
cmd += ' -O %s ' % myData['finalVCF']
cmd += ' -V gendb://%s' % myData['genomicsdb']
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################