annotate commet.py @ 0:a6beb4d4c417

Imported from capsule None
author cmonjeau
date Fri, 05 Jun 2015 11:41:26 -0400
parents
children d085f995d556
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
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
3 import os, re, shutil, optparse
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
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
16 or with the galaxy_commet package in the GenOuest toolshed (coming soon)
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
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
28 parser = optparse.OptionParser()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
29 parser.add_option("--input", dest="input")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
30 parser.add_option("-k", dest="kmer")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
31 parser.add_option("-t", dest="minsharedkmer")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
32 parser.add_option("-l", dest="minlengthread")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
33 parser.add_option("-n", dest="maxn")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
34 parser.add_option("-e", dest="minshannonindex")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
35 parser.add_option("-m", dest="maxreads")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
36
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
37 parser.add_option("--output")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
38 parser.add_option("--output_vectors")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
39 parser.add_option("--output_dendro")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
40 parser.add_option("--output_logs")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
41 parser.add_option("--output_matrix")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
42 parser.add_option("--output_heatmap1")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
43 parser.add_option("--output_heatmap2")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
44 parser.add_option("--output_heatmap3")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
45
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
46 (options, args) = parser.parse_args()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
47
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
48
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
49 # copy R script into the current dir
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
50 shutil.copy(os.environ['RSCRIPTS']+"/heatmap.r", os.getcwd())
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
51 shutil.copy(os.environ['RSCRIPTS']+"/dendro.R", os.getcwd())
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
52
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
53 # remove the first line of the input file
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
54 commet_file = open(options.input, "r")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
55 commet_file_clean = open("commet_clean_file", "w")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
56
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
57 # delete the first line
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
58 commet_file.readline()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
59 for line in commet_file:
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
60 commet_file_clean.write(line)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
61
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
62 # close files
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
63 commet_file.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
64 commet_file_clean.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
65
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
66 # edit the command line
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
67 cmd_line=[]
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
68 cmd_line.append("Commet.py")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
69 cmd_line.extend(["commet_clean_file","-b",os.environ['BINARIES'],"-k",options.kmer,"-t",options.minsharedkmer,"-l",options.minlengthread,"-e",options.minshannonindex])
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
70
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
71 # add options
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
72 if options.maxn:
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
73
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
74 #cmd_line += ' -n '+options.maxn+' -m '+options.maxreads+' > '+options.output+' 2>>'+options.output
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
75 cmd_line.extend(["-n",options.maxn,"-m",options.maxreads])
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
76 #else:
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
77 #cmd_line += ' > '+options.output+' 2>>'+options.output
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
78
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
79 # execute job
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
80 p=subprocess.Popen(cmd_line,
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
81 stdout=subprocess.PIPE,stderr=subprocess.PIPE)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
82
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
83 stdoutput, stderror = p.communicate()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
84
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
85 # log file
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
86 logfile=open(options.output, "w")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
87 logfile.write("[COMMAND LINE]"+' '.join(cmd_line)+"\n\n")
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
88 logfile.write(str(stdoutput))
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
89 logfile.write(str(stderror))
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
90 logfile.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
91
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
92 # copy .bv files inside a .bv archive
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
93 tmp_output_dir=os.getcwd()+"/output_commet/"
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
94 os.chdir(tmp_output_dir)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
95
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
96 # create zip outputs
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
97 mybvzipfile=zipfile.ZipFile(tmp_output_dir+'bv.zip.temp', 'w')
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
98 mylogzipfile=zipfile.ZipFile(tmp_output_dir+'log.zip.temp', 'w')
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
99 mymatrixzipfile=zipfile.ZipFile(tmp_output_dir+'matrix.zip.temp', 'w')
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
100
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
101 # write files into the specific archive
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
102 list_files = glob.glob(tmp_output_dir+'/*')
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
103 for i in list_files:
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
104
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
105 if re.search("\.bv$", i):
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
106 mybvzipfile.write(os.path.basename(i))
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
107 if re.search("\.log$", i):
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
108 mylogzipfile.write(os.path.basename(i))
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
109 if re.search(".csv$", i):
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
110 mymatrixzipfile.write(os.path.basename(i))
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
111
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
112 # close zip files
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
113 mybvzipfile.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
114 mylogzipfile.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
115 mymatrixzipfile.close()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
116
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
117 # return the archives
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
118 shutil.move(tmp_output_dir+'bv.zip.temp', options.output_vectors)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
119 shutil.move(tmp_output_dir+'log.zip.temp', options.output_logs)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
120 shutil.move(tmp_output_dir+'matrix.zip.temp', options.output_matrix)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
121
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
122 # outputs
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
123 shutil.move(tmp_output_dir+'dendrogram_normalized.png', options.output_dendro)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
124 shutil.move(tmp_output_dir+'heatmap_normalized.png', options.output_heatmap1)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
125 shutil.move(tmp_output_dir+'heatmap_percentage.png', options.output_heatmap2)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
126 shutil.move(tmp_output_dir+'heatmap_plain.png', options.output_heatmap3)
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
127
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
128 if __name__ == "__main__": __main__()
a6beb4d4c417 Imported from capsule None
cmonjeau
parents:
diff changeset
129