comparison commet.py @ 0:a6beb4d4c417

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