-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
executable file
·66 lines (54 loc) · 1.75 KB
/
utils.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
import os,sys,shutil
import subprocess as sp
from time import strftime
from multiprocessing import Pool
from functools import partial
def setwd(wd):
os.chdir(wd)
print "[ cwd ] " + os.getcwd()
def reportDone():
name = "." + sys.argv[0] + '.done'
cmd = ['touch', name]
sp.call(cmd)
print "[ done ] created %s"%name
def call(cmd,dry_run=False):
print "[ cmd ] %s"%cmd
if not dry_run: sp.call(cmd, shell=True)
def check_output(cmd, dry_run=False):
print "[ cmd ] %s"%cmd
if not dry_run:
return sp.check_output(cmd, shell = True)
def readSampleList(sample_list):
with open(sample_list, 'r') as f:
sample_list = [x for x in f.readlines()]
sample_list = [x.strip() for x in sample_list] # remove white space.
print "[ readSampleList ] samples are:"
for sample in sample_list: print sample
print "%i samples"%len(sample_list)
return sample_list
def merge(bams, merged):
bams = " ".join(bams)
cmd = 'samtools merge %s %s'%(merged, bams)
call(cmd)
print "[ merge ] %s finished."%merged
def sort(bam):
cmd = "samtools sort -o %s %s"%(bam.replace('bam','sorted.bam'),bam)
call(cmd)
print "[ sort ] %s finished."%bam
def index(bam):
cmd = "samtools index %s"%bam
call(cmd)
print "[ index ] %s finished."%bam
def compressVcf(sample_dir, sample='raw_variants.g.vcf'):
cmd = "bgzip -c %s/%s > %s/%s"%(sample_dir, sample, sample_dir, sample.replace('vcf','vcf.gz'))
call(cmd)
print "[ compressVcf ] finished."
def indexVcf(sample_dir, sample='raw_variants.g.vcf.gz'):
cmd = "tabix -p vcf %s/%s"%(sample_dir, sample)
call(cmd)
print "[ indexVcf] finished."
def printDateTime(pipe = 'stderr'):
if pipe == 'stderr':
sys.stderr.write("-"*40 + strftime("%Y-%m-%d %H:%M:%S") + "-"*40)
else:
sys.stdout.write("-"*40 + strftime("%Y-%m-%d %H:%M:%S") + "-"*40)