0
|
1 # -*- coding: utf-8 -*-
|
|
2 """
|
|
3 SSAKE wrapper
|
|
4 """
|
|
5
|
|
6 import logging
|
|
7 import optparse
|
|
8 import os
|
|
9 import shutil
|
|
10 import subprocess
|
|
11 import tempfile
|
|
12
|
|
13 def execute(cmd):
|
|
14 """ """
|
|
15 subprocess.check_call(args=cmd, stdout=open(os.devnull, 'w'), shell=True)
|
|
16
|
|
17
|
|
18 def which(name, flags=os.X_OK):
|
|
19 """
|
|
20 Search PATH for executable files with the given name.
|
|
21 """
|
|
22 result = []
|
|
23 exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
|
|
24 path = os.environ.get('PATH', None)
|
|
25 if path is None:
|
|
26 return []
|
|
27 for p in os.environ.get('PATH', '').split(os.pathsep):
|
|
28 p = os.path.join(p, str(name))
|
|
29 if os.access(p, flags):
|
|
30 result.append(p)
|
|
31 for e in exts:
|
|
32 pext = p + e
|
|
33 if os.access(pext, flags):
|
|
34 result.append(pext)
|
|
35 return result
|
|
36
|
|
37
|
|
38 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s'
|
|
39 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
|
|
40 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
|
|
41
|
|
42
|
|
43 def __main__():
|
|
44 parser = optparse.OptionParser()
|
|
45 parser.add_option('--if_unpaired', dest='if_unpaired', help='Unpaired FASTA input file name')
|
|
46 parser.add_option('--if_paired_r1', dest='if_paired_r1', help='Paired FASTA reads 1 input file name')
|
|
47 parser.add_option('--if_paired_r2', dest='if_paired_r2', help='Paired FASTA reads 2 input file name')
|
|
48 parser.add_option('-s', dest='seeds_file', help='FASTA as seeds, input file name')
|
|
49 parser.add_option('-w', dest='mindepthofcoverage', type='int', help='minimum depth of coverage allowed for contigs')
|
|
50 parser.add_option('-m', dest='minoverlappingbases', type='int', default=20, help='Minimum number of overlapping bases with the seed/contig during overhang consensus build up (default -m 20)')
|
|
51 parser.add_option('-o', dest='mincall', type='int', default=2, help='mincall -o ')
|
|
52 parser.add_option('-r', dest='baseratio', type='float', default=0.7, help='baseratio -r')
|
|
53 parser.add_option('-k', dest='minnumlinks', type='int', default=4, help='Minimum number of links (read pairs) to compute scaffold -k')
|
|
54 parser.add_option('-e', dest='error', type='float', default=0.75, help='Error (%) allowed on mean distance -e')
|
|
55 parser.add_option('-a', dest='maxlinkratio', type='float', default=0.5, help='Maximum link ratio between two best contig pairs -a')
|
|
56 parser.add_option('-x', dest='minoverlap', type='int', default=20, help='Minimum overlap required between contigs to merge adjacent contigs in a scaffold -x')
|
|
57 parser.add_option('--ignore_header', dest='ignore_header', choices=['0', '1'], default='1', help='Ignore read name/header *will use less RAM if set to 1* -h')
|
|
58 parser.add_option('--kind_of_reads', dest='kind_of_reads', choices=['0', '1', '2'], help='Kind of reads (-p)')
|
|
59 parser.add_option('--iz', dest='insert_size', type='int', help='Library insert size')
|
|
60 parser.add_option('--prefix', dest='prefix', default='ssake_pre', help='prefix')
|
|
61 parser.add_option('--out1', dest='contigs', help='contig file')
|
|
62 parser.add_option('--out2', dest='short', help='short file')
|
|
63 parser.add_option('--out3', dest='singlets', help='singlets file')
|
|
64 parser.add_option('--loglevel', choices=LOG_LEVELS, default='INFO', help='logging level (default: INFO)')
|
|
65 parser.add_option('--logfile', help='log file (default=stderr)')
|
|
66 (options, args) = parser.parse_args()
|
|
67 if len(args) > 0:
|
|
68 parser.error('Wrong number of arguments')
|
|
69
|
|
70 log_level = getattr(logging, options.loglevel)
|
|
71 kwargs = {'format': LOG_FORMAT,
|
|
72 'datefmt': LOG_DATEFMT,
|
|
73 'level': log_level}
|
|
74 if options.logfile:
|
|
75 kwargs['filename'] = options.logfile
|
|
76 logging.basicConfig(**kwargs)
|
|
77 logger = logging.getLogger('SSAKE scaffold assembly')
|
|
78
|
1
|
79 executables = ('SSAKE', 'makePairedOutput2EQUALfiles.pl', 'makePairedOutput2UNEQUALfiles.pl')
|
|
80 logger.debug(which(executables[0]))
|
|
81 logger.debug(which(executables[1]))
|
|
82 logger.debug(which(executables[2]))
|
|
83 logger.debug('Creating temp dir')
|
|
84 kind_of_reads = int(options.kind_of_reads)
|
|
85 if not (kind_of_reads):
|
|
86 infile = options.if_unpaired
|
|
87 paired = 0
|
|
88 else:
|
|
89 infile_r1 = options.if_paired_r1
|
|
90 infile_r2 = options.if_paired_r2
|
|
91 paired = 1
|
|
92 insert_size = options.insert_size
|
|
93 minnumlinks = options.minnumlinks
|
|
94 error = options.error
|
|
95 maxlinkratio = options.maxlinkratio
|
|
96 minoverlap = options.minoverlap
|
|
97 mindepthofcoverage = options.mindepthofcoverage
|
|
98 minoverlappingbases = options.minoverlappingbases
|
|
99 mincall = options.mincall
|
|
100 baseratio = options.baseratio
|
|
101 ignore_header = options.ignore_header
|
|
102 prefix = options.prefix
|
|
103 contigs = options.contigs
|
|
104 short = options.short
|
|
105 singlets = options.singlets
|
|
106 seeds = " -s %s" % options.seeds_file if options.seeds_file else ''
|
|
107 wd = tempfile.mkdtemp()
|
|
108 try:
|
|
109 os.chdir(wd)
|
|
110 if kind_of_reads == 1:
|
|
111 cmd = "%s %s %s %d" % (
|
|
112 executables[1], infile_r1, infile_r2,
|
|
113 insert_size)
|
|
114 logger.info("Preparing data")
|
|
115 execute(cmd)
|
|
116 paired_file = "%s/paired.fa" % wd
|
|
117 command = "%s -f %s -k %d -e %s -a %s -x %d" % (executables[0], paired_file, minnumlinks, error, maxlinkratio, minoverlap)
|
|
118 elif kind_of_reads == 2:
|
|
119 cmd = "%s %s %s %d" % (
|
|
120 executables[2], infile_r1, infile_r2,
|
|
121 insert_size)
|
|
122 logger.info("Preparing data")
|
|
123 execute(cmd)
|
|
124 paired_file = "%s/paired.fa" % wd
|
|
125 unpaired_file = "%s/unpaired.fa" % wd
|
|
126 command = "%s -f %s -g %s -k %d -e %s -a %s -x %d" % (executables[0], paired_file, unpaired_file, minnumlinks, error, maxlinkratio, minoverlap)
|
|
127 else:
|
|
128 command = "%s -f %s" % (executables[0], infile)
|
|
129 command += " %s -w %d -m %d -o %d -r %s -h %s -b %s -p %s" % (seeds, mindepthofcoverage, minoverlappingbases, mincall, baseratio, ignore_header, prefix, paired)
|
|
130 logger.debug(command)
|
|
131 logger.info("Executing SSAKE")
|
|
132 execute(command)
|
|
133
|
|
134 with open("%s.log" % os.path.join(wd, prefix), 'rb') as ssake_log_file:
|
|
135 logger.info("\n".join(["Log from SSAKE", ssake_log_file.read()]))
|
|
136 logger.info("Moving result files")
|
|
137 shutil.move("%s.contigs" % os.path.join(wd, prefix), contigs)
|
|
138 shutil.move("%s.short" % os.path.join(wd, prefix), short)
|
|
139 shutil.move("%s.singlets" % os.path.join(wd, prefix), singlets)
|
|
140 finally:
|
|
141 shutil.rmtree(wd)
|
|
142
|
0
|
143
|
|
144 if __name__ == "__main__":
|
|
145 __main__()
|