annotate structure-923cc9e6aa30/Structureharvester.py @ 0:2c0b270dae70 draft default tip

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