Mercurial > repos > crs4 > sspace
comparison sspace.py @ 0:b1be94418db9 draft
Uploaded
author | crs4 |
---|---|
date | Tue, 17 Sep 2013 09:41:18 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b1be94418db9 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 SSPACE wrapper | |
4 """ | |
5 | |
6 import logging | |
7 import optparse | |
8 import os | |
9 import shutil | |
10 import subprocess | |
11 import tempfile | |
12 | |
13 | |
14 def which(name, flags=os.X_OK): | |
15 """ Search PATH for executable files with the given name. """ | |
16 result = [] | |
17 exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep)) | |
18 path = os.environ.get('PATH', None) | |
19 if path is None: | |
20 return [] | |
21 for p in os.environ.get('PATH', '').split(os.pathsep): | |
22 p = os.path.join(p, str(name)) | |
23 if os.access(p, flags): | |
24 result.append(p) | |
25 for e in exts: | |
26 pext = p + e | |
27 if os.access(pext, flags): | |
28 result.append(pext) | |
29 return result | |
30 | |
31 | |
32 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s' | |
33 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S' | |
34 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] | |
35 | |
36 | |
37 def __main__(): | |
38 """ main function """ | |
39 parser = optparse.OptionParser() | |
40 parser.add_option('-c', dest='contigs', help='contigs mandatory (-s)') | |
41 parser.add_option('--r1', dest='reads1', help='') | |
42 parser.add_option('--r2', dest='reads2', help='') | |
43 parser.add_option('-i', dest='insert', type='int', help='') | |
44 parser.add_option('-e', dest='error', type='float', help='') | |
45 parser.add_option('-o', dest='orientation', choices=['FF', 'FR', 'RF', 'RR'], help='') | |
46 parser.add_option('-x', action='store_true', dest='extension', help='whether to extend the contigs (-x)') | |
47 parser.add_option('--minoverlap', dest='minoverlap', type='int', help='minoverlap (-m)') | |
48 parser.add_option('--numofreads', dest='numofreads', type='int', help='num of reads to call an extension (-o)') | |
49 parser.add_option('-t', dest='max_trim', type='int', help='maximum number of bases to trim on the contig end when all possibilities have been exhausted for an extension (-t)') | |
50 parser.add_option('-u', dest='unpaired', help='unpaired reads (-u, optional)') | |
51 parser.add_option('-r', dest='min_base_ratio', type='float', help='minimum base ratio used to accept a overhang consensus base (-r)') | |
52 parser.add_option('--minlink', dest='minlink', type='int', help='min link (-k)') | |
53 parser.add_option('--maxratio', dest='maxratio', type='float', help='max ratio (-a)') | |
54 parser.add_option('--contigoverlap', dest='contigoverlap', type='int', help='contigoverlap (-n)') | |
55 parser.add_option('--mincontig', dest='mincontig', type='int', help='mincontig (-z)') | |
56 parser.add_option('-T', dest='n_threads', type='int', help='number of threads to use in Bowtie (-T)') | |
57 parser.add_option('-p', dest='prefix', default='sspace_pre', help='prefix (-b)') | |
58 parser.add_option('--lib', dest='libraryname', default='galx', help='libraryfile') | |
59 parser.add_option('--fe', dest='finalevidence', help='') | |
60 parser.add_option('--fs', dest='finalscaffolds', help='') | |
61 parser.add_option('--lg', dest='logfile', help='') | |
62 parser.add_option('--summ', dest='summaryfile', help='') | |
63 parser.add_option('--loglevel', choices=LOG_LEVELS, help='logging level (default: INFO)', default='DEBUG') | |
64 (options, args) = parser.parse_args() | |
65 if len(args) > 0: | |
66 parser.error('Wrong number of arguments') | |
67 | |
68 log_level = getattr(logging, options.loglevel) | |
69 kwargs = {'format' : LOG_FORMAT, | |
70 'datefmt' : LOG_DATEFMT, | |
71 'level' : log_level} | |
72 if options.logfile: | |
73 kwargs['filename'] = options.logfile | |
74 logging.basicConfig(**kwargs) | |
75 logger = logging.getLogger('SSPACE scaffold assembly') | |
76 | |
77 logger.debug('Creating temp dir') | |
78 contigs = options.contigs | |
79 reads1 = options.reads1 | |
80 reads2 = options.reads2 | |
81 insert = options.insert | |
82 error = options.error | |
83 orientation = options.orientation | |
84 extension = '-x 1' if options.extension else '' | |
85 minoverlap = "-m %d" % options.minoverlap if options.minoverlap is not None else '' | |
86 numofreads = "-o %d" % options.numofreads if options.numofreads is not None else '' | |
87 max_trim = "-t %d" % options.max_trim if options.max_trim is not None else '' | |
88 unpaired = "-u %s" % options.unpaired if options.unpaired else '' | |
89 min_base_ratio = "-r %s" % options.min_base_ratio if options.min_base_ratio is not None else '' | |
90 minlink = "-k %d" % options.minlink if options.minlink is not None else '' | |
91 maxratio = "-a %s" % options.maxratio if options.maxratio is not None else '' | |
92 contigoverlap = "-n %d" % options.contigoverlap if options.contigoverlap is not None else '' | |
93 mincontig = "-z %d" % options.mincontig if options.mincontig is not None else '' | |
94 n_threads = "-T %d" % options.n_threads if options.n_threads is not None else '' | |
95 prefix = options.prefix | |
96 libraryname = options.libraryname | |
97 finalevidence = options.finalevidence | |
98 finalscaffolds = options.finalscaffolds | |
99 summaryfile = options.summaryfile | |
100 exe_name = "SSPACE_Basic_v2.0.pl" | |
101 | |
102 # Set library | |
103 outline = 'lib1 %s %s %d %s %s\n' % (reads1, reads2, insert, error, orientation) | |
104 with open(libraryname, 'w') as out: | |
105 out.write(outline) | |
106 | |
107 exe = which(exe_name, os.R_OK) | |
108 command = "perl %s -l %s -s %s %s %s %s %s %s %s %s %s %s %s %s -b %s" % (exe.pop(), libraryname, contigs, extension, minoverlap, numofreads, max_trim, unpaired, min_base_ratio, minlink, maxratio, contigoverlap, mincontig, n_threads, prefix) | |
109 wd = tempfile.mkdtemp() | |
110 try: | |
111 os.chdir(wd) | |
112 logger.info("SSPACE running") | |
113 logger.debug("executing %s" % command) | |
114 subprocess.check_call(args=command, shell=True) | |
115 | |
116 # Clean environment | |
117 with open("%s.logfile.txt" % os.path.join(wd, prefix), 'rb') as sspace_log_file: | |
118 logger.info("\n".join(["Log from SSPACE", sspace_log_file.read()])) | |
119 logger.info("Moving result files") | |
120 shutil.move("%s.final.evidence" % os.path.join(wd, prefix), finalevidence) | |
121 shutil.move("%s.final.scaffolds.fasta" % os.path.join(wd, prefix), finalscaffolds) | |
122 shutil.move("%s.summaryfile.txt" % os.path.join(wd, prefix), summaryfile) | |
123 logger.debug("Removing temp dir: %s" % wd) | |
124 finally: | |
125 shutil.rmtree(wd) | |
126 | |
127 | |
128 if __name__ == "__main__": | |
129 __main__() |