0
|
1 """
|
|
2 @summary: GO enrichment analysis (hotspots)
|
|
3 @author: nanette.coetzer@gmail.com
|
|
4 @version 5
|
|
5
|
|
6 """
|
|
7 import optparse, sys
|
|
8 import subprocess
|
|
9 import tempfile
|
|
10 import os, re
|
|
11
|
|
12 def stop_err( msg ):
|
|
13 sys.stderr.write( "%s\n" % msg )
|
|
14 sys.exit()
|
|
15
|
|
16 def __main__():
|
|
17 #Parse Command Line
|
|
18 parser = optparse.OptionParser()
|
|
19 parser.add_option("-i", "--input1", default=None, dest="input1",
|
|
20 help="genes")
|
|
21 parser.add_option("-o", "--output1", default=None, dest="output1",
|
|
22 help="star genes")
|
8
|
23 parser.add_option("-p", "--output2", default=None, dest="output2",
|
|
24 help="plot")
|
0
|
25 parser.add_option("-m", "--myflag", default=None, dest="myflag",
|
|
26 help="star genes")
|
8
|
27
|
0
|
28 (options, args) = parser.parse_args()
|
|
29
|
|
30 try:
|
|
31 open(options.input1, "r").close()
|
|
32 except TypeError, e:
|
|
33 stop_err("You need to supply the Gene Universe file:\n" + str(e))
|
|
34 except IOError, e:
|
|
35 stop_err("Can not open the Gene Universe file:\n" + str(e))
|
|
36
|
|
37
|
|
38 ##########################################################
|
|
39
|
|
40 infile = open(options.input1,"r")
|
|
41 inlist = []
|
|
42 for line in infile:
|
|
43 inlist.append(line.strip())
|
|
44 infile.close()
|
|
45 outfile = open(options.output1,"w")
|
|
46 for l in inlist:
|
|
47 outfile.write("* "+str(l)+"\n")
|
7
|
48 outfile.write("--- TEST ---" + "\n")
|
0
|
49 outfile.write(options.myflag + "\n")
|
7
|
50 outfile.write("--- TEST ---" + "\n")
|
0
|
51 outfile.close()
|
|
52
|
8
|
53 ##########################################################
|
|
54
|
|
55 # Create temp direcotry
|
|
56 tempdir = tempfile.mkdtemp()
|
|
57 fixdir = options.myflag
|
|
58
|
|
59 # copy INPUT file to the temp directory
|
|
60 # create R script => save in temp directory
|
|
61 # generate new header
|
|
62 new_script = open(tempdir+"/new_script.r","w")
|
|
63 header = "setwd(\"%s\")" %tempdir
|
|
64 new_script.write(header+"\n")
|
|
65
|
|
66 # add script body
|
|
67 script = open(fixdir+"/r_plot.r","r")
|
|
68 for line in script:
|
|
69 new_script.write(line.strip()+"\n")
|
|
70 new_script.close()
|
|
71
|
|
72 # run R script from temp directory
|
|
73 s = "R CMD BATCH %s/new_script.r out.txt" %tempdir
|
|
74 subprocess.call(s, shell=True)
|
|
75
|
|
76 os.system("mv %s/plot.pdf %s" %(tempdir,options.output2))
|
|
77
|
0
|
78 ##############################################
|
|
79
|
|
80 if __name__=="__main__":
|
|
81 __main__()
|