0
|
1 #!/usr/bin/env python
|
|
2 import sys, tempfile, subprocess, glob
|
3
|
3 import os, re, shutil, argparse
|
0
|
4 import zipfile, tarfile, gzip
|
|
5 from os.path import basename
|
|
6
|
|
7 """
|
|
8 WARNING :
|
|
9
|
|
10 commet.py needs commet_exe binaries in your $PATH
|
|
11
|
|
12 commet is available after compiling sources :
|
|
13
|
|
14 http://github.com/pierrepeterlongo/commet
|
|
15
|
3
|
16 or with the package_commet package in the GenOuest toolshed and the main toolshed
|
0
|
17
|
|
18 NOTE :
|
|
19
|
|
20 please add the line #!/usr/bin/env python in top of the Commet.py file if you've a bash error.
|
|
21
|
|
22
|
|
23 """
|
|
24
|
|
25 def __main__():
|
|
26
|
|
27 # arguments recuperation
|
3
|
28 parser = argparse.ArgumentParser()
|
|
29
|
|
30 parser.add_argument("--set", dest="set", action='append')
|
|
31 parser.add_argument("-k", dest="kmer")
|
|
32 parser.add_argument("-t", dest="minsharedkmer")
|
|
33 parser.add_argument("-l", dest="minlengthread")
|
|
34 parser.add_argument("-n", dest="maxn")
|
|
35 parser.add_argument("-e", dest="minshannonindex")
|
|
36 parser.add_argument("-m", dest="maxreads")
|
0
|
37
|
3
|
38 parser.add_argument("--output")
|
|
39 parser.add_argument("--output_vectors")
|
|
40 parser.add_argument("--output_dendro")
|
|
41 parser.add_argument("--output_matrix")
|
|
42 parser.add_argument("--output_heatmap1")
|
|
43 parser.add_argument("--output_heatmap2")
|
|
44 parser.add_argument("--output_heatmap3")
|
0
|
45
|
3
|
46 options = parser.parse_args()
|
0
|
47
|
|
48 # copy R script into the current dir
|
|
49 shutil.copy(os.environ['RSCRIPTS']+"/heatmap.r", os.getcwd())
|
|
50 shutil.copy(os.environ['RSCRIPTS']+"/dendro.R", os.getcwd())
|
|
51
|
3
|
52 # prepare input file
|
|
53 commet_file=open('commet_config_file', 'w')
|
|
54
|
|
55 for set in options.set:
|
|
56 clean_set=set.replace(',', ';').replace('::', ':')
|
|
57 commet_file.write(clean_set+"\n")
|
|
58 commet_file.close()
|
0
|
59
|
|
60 # edit the command line
|
|
61 cmd_line=[]
|
|
62 cmd_line.append("Commet.py")
|
3
|
63 cmd_line.extend(["commet_config_file","-b",os.environ['BINARIES'],"-k",options.kmer,"-t",options.minsharedkmer,"-l",options.minlengthread,"-e",options.minshannonindex])
|
0
|
64
|
|
65 # add options
|
|
66 if options.maxn:
|
|
67 cmd_line.extend(["-n",options.maxn,"-m",options.maxreads])
|
|
68
|
|
69 # execute job
|
3
|
70 p=subprocess.call(cmd_line)
|
0
|
71
|
3
|
72 print "[COMMAND LINE]"+' '.join(cmd_line)
|
0
|
73
|
|
74 # copy .bv files inside a .bv archive
|
|
75 tmp_output_dir=os.getcwd()+"/output_commet/"
|
|
76 os.chdir(tmp_output_dir)
|
|
77
|
|
78 # create zip outputs
|
|
79 mybvzipfile=zipfile.ZipFile(tmp_output_dir+'bv.zip.temp', 'w')
|
|
80 mylogzipfile=zipfile.ZipFile(tmp_output_dir+'log.zip.temp', 'w')
|
|
81 mymatrixzipfile=zipfile.ZipFile(tmp_output_dir+'matrix.zip.temp', 'w')
|
|
82
|
|
83 # write files into the specific archive
|
|
84 list_files = glob.glob(tmp_output_dir+'/*')
|
|
85 for i in list_files:
|
|
86
|
|
87 if re.search("\.bv$", i):
|
|
88 mybvzipfile.write(os.path.basename(i))
|
|
89 if re.search("\.log$", i):
|
|
90 mylogzipfile.write(os.path.basename(i))
|
|
91 if re.search(".csv$", i):
|
|
92 mymatrixzipfile.write(os.path.basename(i))
|
|
93
|
|
94 # close zip files
|
|
95 mybvzipfile.close()
|
|
96 mylogzipfile.close()
|
|
97 mymatrixzipfile.close()
|
|
98
|
|
99 # return the archives
|
|
100 shutil.move(tmp_output_dir+'bv.zip.temp', options.output_vectors)
|
|
101 shutil.move(tmp_output_dir+'log.zip.temp', options.output_logs)
|
|
102 shutil.move(tmp_output_dir+'matrix.zip.temp', options.output_matrix)
|
|
103
|
|
104 # outputs
|
3
|
105 try:
|
|
106 shutil.move(tmp_output_dir+'dendrogram_normalized.png', options.output_dendro)
|
|
107 shutil.move(tmp_output_dir+'heatmap_percentage.png', options.output_heatmap2)
|
|
108 shutil.move(tmp_output_dir+'heatmap_normalized.png', options.output_heatmap1)
|
|
109 shutil.move(tmp_output_dir+'heatmap_plain.png', options.output_heatmap3)
|
|
110 except:
|
|
111 print "There is a problem with gplots execution"
|
0
|
112
|
|
113 if __name__ == "__main__": __main__()
|
|
114
|