flash2.py 2.36 KB
Newer Older
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
from os import listdir, path, makedirs
from os.path import isfile, join
import sys
import shutil
import gzip
import subprocess
import argparse
import shlex
import os

parser = argparse.ArgumentParser(description='Use FLASH2 read merger to make a new fastq file and keep unmerged reads')

parser.add_argument("flash2_dir", help="path to flash2 executable")
parser.add_argument("file_R1", help="forward read file")
parser.add_argument("file_R2", help="reverse read file")
parser.add_argument("output_file", help="output file")
parser.add_argument("-r1", "--keep_r1", help="keep unmerged forward reads", action="store_true")
parser.add_argument("-r2", "--keep_r2", help="keep unmerged reverse reads", action="store_true")
parser.add_argument("-f", "--flash2-options", help="additional options passed to FLASH2", default="")
parser.add_argument("-k", "--keep", help="keep temporary files (may take lots of disk space in the end)", action = 'store_true')


args  = parser.parse_args()
f_r1  = args.file_R1
f_r2  = args.file_R2
f_out = args.output_file
27
f_opt = args.flash2_options
28
29
30
31

paths =  os.path.split(f_out)
path_head = paths[0]
path_file = paths[1]
32
print( "args: %s" % args)
33
print( "###" )
34
35
36
print( "f_r1: %s" % f_r1)
print( "f_r2: %s" % f_r2)
print( "f_out: %s" % f_out)
37
print( "flash2 options: %s" % f_opt)
38
39
print( "path_head: %s" % path_head)
print( "path_file: %s" % path_file)
40

41
42

cmd = ['%s/flash2' % args.flash2_dir,
43
 f_r1, f_r2,
44
45
 "-d", path_head,
 "-o", path_file,
46
 "-t", "1",
47
]
48
49
50
cmd += shlex.split( f_opt )
print( "# %s" % cmd )

51
exit_code = subprocess.call( cmd )
52
53
54
55
56

if exit_code > 0:
    raise EnvironmentError("Flash2 failed")

try :
flothoni's avatar
flothoni committed
57
    with gzip.open(f_out, 'w') as outFile:
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        with open(f_out+'.extendedFrags.fastq', 'rb') as f1:
            shutil.copyfileobj(f1, outFile)
        if (args.keep_r1):
            with open(f_out+'.notCombined_1.fastq', 'rb') as f2:
                shutil.copyfileobj(f2, outFile)
        if (args.keep_r2):
            with open(f_out+'.notCombined_2.fastq', 'rb') as f3:
                shutil.copyfileobj(f3, outFile)
        if not args.keep:
            os.remove(f_out+'.extendedFrags.fastq')
            os.remove(f_out+'.notCombined_1.fastq')
            os.remove(f_out+'.notCombined_2.fastq')
        ## Remove the histogram provide by Flash2
        os.remove(f_out+'.hist')
        os.remove(f_out+'.histogram')
except IOError :
    os.remove(f_out)
    raise