0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import sys, re
|
|
4 import os
|
|
5 import tempfile
|
|
6 import shutil, subprocess, glob
|
|
7 import optparse
|
|
8 from os.path import basename
|
|
9 import zipfile, tarfile, gzip
|
|
10 from galaxy.datatypes.checkers import *
|
|
11 from adlib import *
|
|
12
|
|
13 """
|
|
14
|
|
15 Created by Yvan Le Bras
|
|
16 yvan.le_bras@irisa.fr
|
|
17
|
|
18 Last modifications : 01/10/2014
|
|
19
|
|
20 """
|
|
21
|
|
22
|
|
23 def __main__():
|
|
24
|
|
25
|
|
26 # arguments recuperation
|
|
27 parser = optparse.OptionParser()
|
|
28 parser.add_option("-P")
|
|
29 parser.add_option("--evanno")
|
|
30 parser.add_option("--clumpp")
|
|
31 # multifile management
|
|
32 parser.add_option("--logfile")
|
|
33 # output management
|
|
34 parser.add_option("--id")
|
|
35 parser.add_option("--workdir")
|
|
36 parser.add_option("--compress_output")
|
|
37 # additionnal outputs
|
|
38 parser.add_option("--total_output")
|
|
39 (options, args) = parser.parse_args()
|
|
40
|
|
41 # create the working dir
|
|
42 tmp_dir = tempfile.mkdtemp(dir=options.workdir)
|
|
43 tmp_output_dir = tempfile.mkdtemp(dir=tmp_dir)
|
|
44
|
|
45 print tmp_dir
|
|
46
|
|
47 # Structure_archive
|
|
48
|
|
49 # parse config files
|
|
50 tab_files=galaxy_config_to_tabfiles_for_STACKS(options.P)
|
|
51
|
|
52 # check if zipped files are into the tab
|
|
53 extract_compress_files_from_tabfiles(tab_files, tmp_dir)
|
|
54
|
|
55 # create the structure harvester command input line
|
|
56 cmd_files=" --dir "+tmp_dir+" --out "+tmp_output_dir+" "
|
|
57
|
|
58 # create the populations command line
|
|
59 cmd_options=""
|
|
60
|
|
61 if options.evanno and options.evanno == 'true':
|
|
62 cmd_options+=" --evanno "
|
|
63 if options.clumpp and options.clumpp == 'true':
|
|
64 cmd_options+=" --clumpp "
|
|
65
|
|
66
|
|
67 #print " "+cmd_files+" "+cmd_options
|
|
68
|
|
69 # launch the command line
|
|
70
|
|
71 #dev command
|
|
72 cmd = '/local/galaxy/structureharv/structureHarvester.py'+cmd_files+" "+cmd_options+" 2>&1"
|
|
73
|
|
74 #command with dependencies installed
|
|
75 #cmd = 'structureHarvester.py'+cmd_files+" "+cmd_options+" 2>&1"
|
|
76 proc = subprocess.Popen( args=cmd, shell=True )
|
|
77 returncode = proc.wait()
|
|
78
|
|
79 # postprocesses
|
|
80 if os.path.exists(tmp_output_dir+'/summary.txt'):
|
|
81 os.system('mv '+tmp_output_dir+'/summary.txt '+options.logfile)
|
|
82 else:
|
|
83 sys.stderr.write('Error in StructureHarvester execution; Please read the additional output (stdout)\n')
|
|
84
|
|
85
|
|
86 print "\n[INFO] : "+cmd
|
|
87
|
|
88 # copy all files inside tmp_dir into workdir or into an archive
|
|
89 list_files = glob.glob(tmp_output_dir+'/*')
|
|
90
|
|
91 # if compress output is total
|
|
92 if options.compress_output == 'total':
|
|
93 mytotalzipfile=zipfile.ZipFile(tmp_output_dir+'/total.zip.temp', 'w')
|
|
94
|
|
95 os.chdir(tmp_output_dir)
|
|
96 for i in list_files:
|
|
97 mytotalzipfile.write(os.path.basename(i))
|
|
98
|
|
99 # return the unique archive
|
|
100 os.system("mv "+tmp_output_dir+'/total.zip.temp'+" "+options.total_output)
|
|
101
|
|
102 # if compress output is default
|
|
103 else:
|
|
104 for i in list_files:
|
|
105 command = "mv "+i+" "+options.workdir+ "/primary_" + options.id + "_" + os.path.basename(i).replace("_", ".") + "_visible_tabular"
|
|
106 proc = subprocess.Popen( args=command, shell=True )
|
|
107 returncode = proc.wait()
|
|
108
|
|
109
|
|
110 #clean up temp files
|
|
111 shutil.rmtree( tmp_dir )
|
|
112
|
|
113
|
|
114
|
|
115 if __name__ == "__main__": __main__()
|
|
116
|
|
117
|
|
118
|
|
119
|
|
120
|