annotate DiffExpAnal/wrappGSNAP.py @ 1:b8887c180d45 draft

Uploaded
author yufei-luo
date Tue, 22 Jan 2013 10:31:35 -0500
parents 63799b789162
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
2 """
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
3 Yufei LUO
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
4 """
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
5
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
6
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
7 import os, sys, subprocess,tempfile
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
8 from optparse import OptionParser
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
9
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
10 def stop_err(msg):
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
11 sys.stderr.write('%s\n' % msg)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
12 sys.exit()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
13
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
14 def __main__():
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
15 #Parse Command Line
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
16 description = "GMAP/GSNAP version:2012-12-20."
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
17 parser = OptionParser(description = description)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
18 parser.add_option("-d", "--genomeName", dest="genomeName", help="Define the reference genome name.[compulsory]")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
19 parser.add_option("-D", "--workingDir", dest="workingDir", help="Define the directory of writing reference genome index.[compulsory]")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
20 parser.add_option("-k", "--kmer", dest="kmer", defaut=12, help="Choose kmer value (<=16), a big kmer value can take more RAM(4Go).[compulsory]")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
21 parser.add_option("-i", "--inputFasta", dest="inputFastaFile", help="Reference genome file, fasta format.[compulsory]")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
22 parser.add_option("-q", "--inputFastq", dest="inputFastqFile", help="Input fastq file.")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
23 parser.add_option("-p", "--pairedEnd", dest="pairedEndFile", defaut=None, help="Input paired-end fastq file.")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
24 parser.add_option("-A", "--outputFormat", dest="outputFormat", defaut="sam", help="Choose an output format [sam, goby (need to re-compile with appropriate options)].")
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
25 (options, args) = parser.parse_args()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
26
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
27 #If workingDir dose not exist, should create before run the job.
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
28 dir = options.workingdir
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
29 if not os.path.exists(dir):
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
30 os.makedirs(dir)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
31
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
32 cmds = []
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
33 cmd_setup = "gmap_setup -d %s -D %s -k %s %s" % (options.genomeName, options.workingdir, options.kmer, options.inputFastaFile)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
34 cmds.append(cmd_setup)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
35 cmd_make_coords = "make Makefile.%s coords" % options.genomeName
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
36 cmds.append(cmd_make_coords)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
37 cmd_make_gmapdb = "make Makefile.%s gmapdb" % options.genomeName
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
38 cmds.append(cmd_make_gmapdb)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
39 cmd_make_install = "make Makefile.%s install" % options.genomeName
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
40 cmds.append(cmd_make_install)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
41 cmd_run = "gsnap -d %s -D %s -A %s %s " % (options.genomeName, options.workingdir, options.outputFormat, options.inputFastqFile)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
42 if options.pairedEndFile != None:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
43 cmd_run += "%s" % options.pairedEndFile
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
44 cmds.append(cmd_run)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
45
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
46 tmp_files = []
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
47 for i in range(len(cmds)):
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
48 try:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
49 tmp_out = tempfile.NamedTemporaryFile().name
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
50 tmp_files.append(tmp_out)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
51 tmp_stdout = open(tmp_out, 'wb')
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
52 tmp_err = tempfile.NamedTemporaryFile().name
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
53 tmp_files.append(tmp_err)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
54 tmp_stderr = open(tmp_err, 'wb')
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
55 proc = subprocess.Popen(args=cmds[i], shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
56 returncode = proc.wait()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
57 tmp_stderr.close()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
58 #get stderr, allowing for case where it's very large
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
59 tmp_stderr = open(tmp_err, 'rb')
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
60 stderr = ''
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
61 buffsize = 1048576
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
62 try:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
63 while True:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
64 stderr += tmp_stderr.read(buffsize)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
65 if not stderr or len(stderr) % buffsize != 0:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
66 break
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
67 except OverflowError:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
68 pass
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
69 tmp_stdout.close()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
70 tmp_stderr.close()
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
71 if returncode != 0:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
72 raise Exception, stderr
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
73 except Exception, e:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
74 stop_err('Error in :\n' + str(e))
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
75
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
76 for tmp_file in tmp_files:
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
77 os.remove(tmp_file)
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
78
63799b789162 Uploaded
yufei-luo
parents:
diff changeset
79 if __name__=="__main__":__main__()