-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMothurCommandInfoWrapper.py
executable file
·252 lines (197 loc) · 6.76 KB
/
MothurCommandInfoWrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## Wrapper enabling incorporaiton of MOTHUR commands as modules in YAP
#################################################
import sys, tempfile, shlex, glob, os
from subprocess import *
from collections import defaultdict
_author="Sebastian Szpakowski"
_date="2011/09/14"
_version="Version 1"
#################################################
## Classes
##
class MothurCommand():
def __init__(self):
self.name = ""
self.input_files = dict()
self.output_files = dict()
self.args = dict()
self.annotation = dict()
self.text = ""
self.linkbacks = defaultdict(list)
def addArg(self, type, arg, val):
self.text = "%s\n%s\t%s" % (self.text, arg[0:50].ljust(50), val[0:50].ljust(50))
if arg == "commandName":
self.name = val
elif type == "":
self.annotation[arg]=(val)
elif type == "outputTypesNames" and type!=arg:
self.output_files[arg] = ""
elif arg == "inputTypes":
for i in val.split("-"):
self.input_files[i]= ()
else:
self.parseType(arg, val)
def parseType(self, arg, val):
vals = val.strip().split("|")
if len(vals)==5:
vals[2] = self.booleanify(vals[2])
vals[0] = vals[0].strip().split("-")
self.args[arg] = vals
elif arg in self.input_files.keys():
req, imp, one, oneplus, linked, outs = vals
self.input_files[arg] = (req, one, oneplus, linked)
for K in one, oneplus, linked:
if K !="none":
for L in K.strip().split("-"):
self.linkbacks[L].append(arg)
else:
self.args[arg] = vals
#print self.name, ":", arg, "=", val
def booleanify(self, x):
if x in ["T", "t", "True", "true", "TRUE"]:
return True
elif x in ["F", "f", "False", "false", "FALSE"]:
return False
else:
raise ValueError
def getInputs(self):
return self.input_files.keys()
def getArguments(self):
return self.args.keys()
def isAnArgument(self, x):
return (x in self.args.keys())
def getOutputs(self):
return self.output_files.keys()
def isRequired(self, filetype):
return (self.booleanify(self.input_files[filetype][0]))
def getAlternativeInput(self, filetype):
x = set(self.linkbacks[self.input_files[filetype][1]])
x = x.difference(set([filetype]))
return (x)
def getOtherInput(self, filetype):
x = set(self.linkbacks[self.input_files[filetype][2]])
x = x.difference(set([filetype]))
return (x)
def getLinkedInput(self, filetype):
x = set(self.linkbacks[self.input_files[filetype][3]])
x = x.difference(set([filetype]))
return (x)
def getDefault(self, arg):
return (self.args[arg])
def __str__(self):
print self.output_files
otpt = "%s:" % (self.name)
otpt = "%s\n%s\t%s" % (otpt, "input".rjust(10) ,"".join([x.ljust(20) for x in self.input_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "req".rjust(10) ,"".join(["".join("%s" % self.isRequired(x))[:18].ljust(20) for x in self.input_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "alt".rjust(10) ,"".join(["-".join(self.getAlternativeInput(x))[:18].ljust(20) for x in self.input_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "multi".rjust(10) ,"".join(["-".join(self.getOtherInput(x))[:18].ljust(20) for x in self.input_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "link".rjust(10) ,"".join(["-".join(self.getLinkedInput(x))[:18].ljust(20) for x in self.input_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "output".rjust(10) ,"".join([x.ljust(20) for x in self.output_files.keys()]))
otpt = "%s\n%s\t%s" % (otpt, "---".rjust(10) ,"".join([self.output_files[x].ljust(20) for x in self.output_files.keys()]))
otpt = "%s\n%s" % (otpt, self.text)
return otpt
class MothurCommandInfo():
def __init__(self,path=""):
tmpdir = tempfile.mkdtemp(prefix="output")
p = Popen(shlex.split("%smothur #get.commandinfo(output=mothurinfo)"% (path)), stdout=PIPE, stderr=PIPE, cwd=tmpdir)
log = p.stdout.readlines()
info = loadLines("%s/mothurinfo" % (tmpdir))
self.version, self.date = self.parseLog(log)
self.commands = dict()
self.parseInfo(info)
self.cleanUpDir(tmpdir)
def parseLog(self, log):
version = ""
date = ""
for line in log:
if line.startswith("mothur v"):
version = line[9:].strip()
if line.startswith("Last updated"):
date = line[14:].strip()
print ("Found Mothur: %s [%s]" % (version, date))
return (version, date)
def parseInfo(self, info):
current = MothurCommand()
counter = 0
curtype = ""
for line in info:
line = line.strip().split("=")
A = line[0]
if len(line)>1:
B = "=".join(line[1:])
if B.isdigit() or A=="inputTypes":
curtype = A
if A=="commandName":
self.addCommand(current)
current = MothurCommand()
curtype = ""
current.addArg(curtype, A, B)
else:
counter = int(A)
self.addCommand(current)
print "Identified %s commands [reported: %s]" % (len(self.commands.keys()), counter)
def addCommand(self, K):
if K.name!="":
self.commands[K.name] = K
def cleanUpDir(self, path, counter=1):
try:
for file in glob.glob("%s/*" % (path)):
os.remove(file)
os.rmdir(path)
except OSError:
print "Tmp cleanup delayed... x%s [%s]" % (counter, path)
time.sleep(1)
self.cleanUpDir(path, counter=counter+1)
def getCommandInfo(self, x):
return self.commands[x]
#################################################
## Functions
##
################################################
### Read in a file and return a list of lines
###
def loadLines(x):
try:
fp = open(x, "r")
cont=fp.readlines()
fp.close()
#print "%s line(s) loaded." % (len(cont))
except:
cont=""
#print "%s cannot be opened, does it exist? " % ( x )
return cont
def test(path = "./"):
M = MothurCommandInfo(path)
# x = M.getCommandInfo("dist.seqs")
# print x
#
# x = M.getCommandInfo("unique.seqs")
# print x
#
# x = M.getCommandInfo("cluster")
# print x
#
# x = M.getCommandInfo("trim.seqs")
# print x
#
x = M.getCommandInfo("align.seqs")
print x
#
# x = M.getCommandInfo("remove.seqs")
# print x
#################################################
## Arguments
##
#################################################
## Begin
##
#test("/usr/local/devel/ANNOTATION/sszpakow/YAP/bin/mothur-v1.29.2/")
#################################################
## Finish
#################################################