comparison maaslin_wrapper.py @ 8:e9677425c6c3 default tip

Updated the structure of the libraries
author george.weingart@gmail.com
date Mon, 09 Feb 2015 12:17:40 -0500
parents e0b5980139d9
children
comparison
equal deleted inserted replaced
7:c72e14eabb08 8:e9677425c6c3
1 #!/usr/bin/env python
2
3 """
4 Author: George Weingart
5 Description: Wrapper program for maaslin
6 """
7
8 #####################################################################################
9 #Copyright (C) <2012>
10 #
11 #Permission is hereby granted, free of charge, to any person obtaining a copy of
12 #this software and associated documentation files (the "Software"), to deal in the
13 #Software without restriction, including without limitation the rights to use, copy,
14 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 #and to permit persons to whom the Software is furnished to do so, subject to
16 #the following conditions:
17 #
18 #The above copyright notice and this permission notice shall be included in all copies
19 #or substantial portions of the Software.
20 #
21 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
22 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #####################################################################################
28
29 __author__ = "George Weingart"
30 __copyright__ = "Copyright 2012"
31 __credits__ = ["George Weingart"]
32 __license__ = "MIT"
33 __maintainer__ = "George Weingart"
34 __email__ = "george.weingart@gmail.com"
35 __status__ = "Development"
36
37 from cStringIO import StringIO
38 import sys,string
39 import os
40 import tempfile
41 from pprint import pprint
42 import argparse
43
44 ######################################################################################
45 # Parse input parms #
46 ######################################################################################
47 def read_params(x):
48 parser = argparse.ArgumentParser(description='MaAsLin Argparser')
49 parser.add_argument('--lastmeta', action="store", dest='lastmeta',nargs='?')
50 parser.add_argument('--input', action="store", dest='input',nargs='?')
51 parser.add_argument('--output', action="store", dest='output',nargs='?')
52 parser.add_argument('--zip_file', action="store", dest='zip_file',nargs='?')
53 parser.add_argument('--alpha', action="store", type=float,default=0.05,dest='alpha',nargs='?')
54 parser.add_argument('--min_abd', action="store", type=float,default=0.0001,dest='min_abd',nargs='?')
55 parser.add_argument('--min_samp', action="store", type=float,default=0.01,dest='min_samp',nargs='?')
56 parser.add_argument('--tool_option1', action="store", dest='tool_option1',nargs='?')
57 return parser
58
59
60
61 ######################################################################################
62 # Build read config file #
63 ######################################################################################
64 def build_read_config_file(strTempDir,results, DSrc, DMaaslin, root_dir):
65 fname = results.input
66 input_file = open(fname)
67 input_lines = input_file.readlines()
68 LenInput = len(input_lines)
69 input_file.close()
70 TopLimit = int(results.lastmeta)
71 ReadConfigFileName = os.path.join(strTempDir,"Test.read.config")
72 Q = "'"
73
74 #WorkingDir = os.getcwd()
75 WorkingDir = root_dir
76 os.chdir(DMaaslin)
77
78 Limit1 = Q + "2-" + str(TopLimit ) + Q
79 ReadConfigTb1 = [
80 os.path.join(DSrc,"CreateReadConfigFile.R"),
81 "-c",
82 Limit1,
83 ReadConfigFileName,
84 "Metadata"
85 ">/dev/null",\
86 "2>&1"
87 ]
88
89 cmd_config1 = " ".join(ReadConfigTb1)
90
91 os.system(cmd_config1)
92
93 Limit2 = Q + str(TopLimit +1 ) + '-' + Q
94 ReadConfigTb2 = [
95 os.path.join(DSrc,"CreateReadConfigFile.R"),
96 "-a",
97 "-c",
98 Limit2,
99 ReadConfigFileName,
100 "Abundance"
101 ">/dev/null",\
102 "2>&1"
103 ]
104
105 cmd_config2 = " ".join(ReadConfigTb2)
106 os.system(cmd_config2)
107 os.chdir(WorkingDir)
108 return ReadConfigFileName
109
110
111 ######################################################################################
112 # Main Program #
113 ######################################################################################
114
115 # Parse commandline in
116 parser = read_params( sys.argv )
117 results = parser.parse_args()
118 root_dir = os.environ.get('maaslin_SCRIPT_PATH')
119
120
121
122
123
124
125 ### If option 2 is selected inform user on 2 outputs
126 if results.tool_option1 == "2":
127 print "***Please note: 2 output files are generated: Complete zipped results + Summary ***"
128
129 ### Project name
130 strProjectName = os.path.splitext(os.path.basename(results.input))[0]
131
132 ### Define directory locations
133 D = os.path.join(root_dir)
134 DSrc = os.path.join(root_dir,"src")
135 DInput = os.path.join(root_dir,"maaslin","input")
136 DMaaslin = os.path.join(root_dir)
137
138 DMaaslinGalaxy = os.path.join(root_dir)
139
140
141
142 ### Make temporary folder to work in
143 ### Change permissions to make useable
144 strTempDir = tempfile.mkdtemp()
145 cmd_chmod = "chmod 755 /" + strTempDir
146 os.system(cmd_chmod)
147 cmd_mkdir1 = "mkdir -m 755 " + os.path.join(strTempDir,strProjectName)
148 os.system(cmd_mkdir1)
149
150 ### Transpose the pcl file to a tsv file
151 TbCmdTranspose = [\
152 "python",
153 DMaaslinGalaxy + "/transpose.py<" + str(results.input) + ">" + os.path.join(strTempDir,"output.tsv")\
154 ]
155 cmd_transpose = " ".join(TbCmdTranspose)
156 os.system(cmd_transpose)
157
158 ### Make path for target output file
159 OutputFile = os.path.join(strTempDir,strProjectName,strProjectName+".txt")
160
161 ### Make read config file
162 ReadConfigFileName = build_read_config_file(strTempDir,results, DSrc, DMaaslin, root_dir)
163
164 ### Build MaAsLin comamnd
165 CmdsArray = [\
166 os.path.join(DSrc,"Maaslin.R"), \
167 "-d", str(results.alpha),\
168 "-r", str(results.min_abd),\
169 "-p", str(results.min_samp), \
170 "-i", \
171 ReadConfigFileName, \
172 OutputFile, \
173 os.path.join(strTempDir,"output.tsv"), \
174 "-v",\
175 "ERROR",\
176 ">/dev/null",\
177 "2>&1"
178 ]
179
180 invoke_maaslin_cmd = " ".join(CmdsArray)
181
182
183
184
185
186 ### Write to directory cmd line used for troubleshooting
187 #CmdFileName = os.path.join(strTempDir,"cmdfile.txt")
188 #OutFile = open(CmdFileName,"w")
189 #OutputString = invoke_maaslin_cmd + "\n"
190 #OutFile.write(OutputString)
191 #OutFile.close()
192
193 ### Call MaAsLin
194 os.system(invoke_maaslin_cmd)
195
196
197 ### Copy output file to make available to galaxy
198 cmd_copy = "cp " + os.path.join(strTempDir,strProjectName+"/output.txt") + " " + results.output
199 MsgFileName = os.path.join(strTempDir,strProjectName+"/output.txt")
200
201 if not os.path.isfile(MsgFileName):
202 cmd_copy = "cp " + os.path.join(strTempDir,strProjectName+"/output.txt") + " " + results.output
203 OutFile = open(MsgFileName,"w")
204 OutputString = "A MaAsLin error has occurred\n"
205 OutputString = OutputString + "It typically happens when incorrect 'Last metadata row' was selected\n"
206 OutputString = OutputString + "For demo data please choose 'Weight'\n"
207 OutFile.write(OutputString)
208 OutFile.close()
209
210 os.system(cmd_copy)
211
212 ### Zip up output folder
213 cmd_zip = "zip -jr " + os.path.join(strTempDir,strProjectName+".zip") + " " + os.path.join(strTempDir,strProjectName) + ">/dev/null 2>&1"
214
215 os.system(cmd_zip)
216
217 ### Copy output folder to make available to galaxy
218 cmd_copy_zip = "cp " + os.path.join(strTempDir,strProjectName+".zip") + " " + results.zip_file
219 os.system(cmd_copy_zip)
220
221 ### Delete temp directory
222 cmd_del_tempdir = "rm -r " + strTempDir
223 ######os.system(cmd_del_tempdir)