annotate commet.py @ 5:8b2edb8a315e

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