/usr/share/pyshared/cogent/app/mothur.py is in python-cogent 1.5.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | #!/usr/bin/env python
"""Provides an application controller for the commandline version of
mothur Version 1.6.0
"""
from __future__ import with_statement
from os import path, getcwd, mkdir, remove, listdir
from shutil import copyfile
from subprocess import Popen
from cogent.app.parameters import ValuedParameter
from cogent.app.util import CommandLineApplication, ResultPath, \
CommandLineAppResult, ApplicationError
from cogent.parse.mothur import parse_otu_list
__author__ = "Kyle Bittinger"
__copyright__ = "Copyright 2007-2011, The Cogent Project"
__credits__ = ["Kyle Bittinger"]
__license__ = "GPL"
__version__ = "1.5.1"
__maintainer__ = "Kyle Bittinger"
__email__ = "kylebittinger@gmail.com"
__status__ = "Prototype"
class Mothur(CommandLineApplication):
"""Mothur application controller
"""
_options = {
# Clustering algorithm. Choices are furthest, nearest, and
# average
'method': ValuedParameter(
Name='method', Value='furthest', Delimiter='=', Prefix=''),
# Cutoff distance for the distance matrix
'cutoff': ValuedParameter(
Name='cutoff', Value=None, Delimiter='=', Prefix=''),
# Minimum pairwise distance to consider for clustering
'precision': ValuedParameter(
Name='precision', Value=None, Delimiter='=', Prefix=''),
}
_parameters = {}
_parameters.update(_options)
_input_handler = '_input_as_multiline_string'
_command = 'mothur'
def __init__(self, params=None, InputHandler=None, SuppressStderr=None,
SuppressStdout=None, WorkingDir=None, TmpDir='/tmp',
TmpNameLen=20, HALT_EXEC=False):
"""Initialize a Mothur application controller
params: a dictionary mapping the Parameter id or synonym to its
value (or None for FlagParameters or MixedParameters in flag
mode) for Parameters that should be turned on
InputHandler: this is the method to be run on data when it is
passed into call. This should be a string containing the
method name. The default is _input_as_string which casts data
to a string before appending it to the command line argument
SuppressStderr: if set to True, will route standard error to
/dev/null, False by default
SuppressStdout: if set to True, will route standard out to
/dev/null, False by default
WorkingDir: the directory where you want the application to run,
default is the current working directory, but is useful to
change in cases where the program being run creates output
to its current working directory and you either don't want
it to end up where you are running the program, or the user
running the script doesn't have write access to the current
working directory
WARNING: WorkingDir MUST be an absolute path!
TmpDir: the directory where temp files will be created, /tmp
by default
TmpNameLen: the length of the temp file name
HALT_EXEC: if True, raises exception w/ command output just
before execution, doesn't clean up temp files. Default False.
"""
super(Mothur, self).__init__(
params=params, InputHandler=InputHandler,
SuppressStderr=SuppressStderr, SuppressStdout=SuppressStdout,
WorkingDir='', TmpDir='', TmpNameLen=TmpNameLen,
HALT_EXEC=HALT_EXEC)
# Prevent self.WorkingDir from being explicitly cast as a
# FilePath object. This behavior does not seem necessary in
# the parent's __init__() method, since the casting is
# repeated in _set_WorkingDir().
if WorkingDir is not None:
working_dir = WorkingDir
else:
working_dir = self._working_dir or getcwd()
self.WorkingDir = working_dir
self.TmpDir = TmpDir
@staticmethod
def getHelp():
"""Returns link to online manual"""
help = (
'See manual, available on the MOTHUR wiki:\n'
'http://schloss.micro.umass.edu/mothur/'
)
return help
def __call__(self, data=None, remove_tmp=True):
"""Run the application with the specified kwargs on data
data: anything that can be cast into a string or written out to
a file. Usually either a list of things or a single string or
number. input_handler will be called on this data before it
is passed as part of the command-line argument, so by creating
your own input handlers you can customize what kind of data
you want your application to accept
remove_tmp: if True, removes tmp files
"""
# Process the input data. Input filepath is stored in
# self._input_filename
getattr(self, self.InputHandler)(data)
if self.SuppressStdout:
outfile = None
else:
outfile = open(self.getTmpFilename(self.TmpDir), 'w')
if self.SuppressStderr:
errfile = None
else:
errfile = open(self.getTmpFilename(self.TmpDir), 'w')
args = [self._command, self._compile_mothur_script()]
process = Popen(
args, stdout=outfile, stderr=errfile, cwd=self.WorkingDir)
exit_status = process.wait()
if not self._accept_exit_status(exit_status):
raise ApplicationError(
'Unacceptable application exit status: %s, command: %s' % \
(exit_status, args))
if outfile is not None:
outfile.seek(0)
if errfile is not None:
errfile.seek(0)
result = CommandLineAppResult(
outfile, errfile, exit_status, result_paths=self._get_result_paths())
# Clean up the input file if one was created
if remove_tmp:
if self._input_filename:
remove(self._input_filename)
self._input_filename = None
return result
def _accept_exit_status(self, status):
return int(status) == 0
def _compile_mothur_script(self):
"""Returns a Mothur batch script as a string"""
def format_opts(*opts):
"""Formats a series of options for a Mothur script"""
return ', '.join(filter(None, map(str, opts)))
vars = {
'in': self._input_filename,
'unique': self._derive_unique_path(),
'dist': self._derive_dist_path(),
'names': self._derive_names_path(),
'cluster_opts' : format_opts(
self.Parameters['method'],
self.Parameters['cutoff'],
self.Parameters['precision'],
),
}
script = (
'#'
'unique.seqs(fasta=%(in)s); '
'dist.seqs(fasta=%(unique)s); '
'read.dist(column=%(dist)s, name=%(names)s); '
'cluster(%(cluster_opts)s)' % vars
)
return script
def _get_result_paths(self):
paths = {
'distance matrix': self._derive_dist_path(),
'otu list': self._derive_list_path(),
'rank abundance': self._derive_rank_abundance_path(),
'species abundance': self._derive_species_abundance_path(),
'unique names': self._derive_names_path(),
'unique seqs': self._derive_unique_path(),
'log': self._derive_log_path(),
}
return dict([(k, ResultPath(v)) for (k,v) in paths.items()])
# Methods to derive/guess output pathnames produced by MOTHUR.
# TODO: test for input files that do not have a filetype extension
def _derive_log_path(self):
"""Guess logfile path produced by Mothur
This method checks the working directory for log files
generated by Mothur. It will raise an ApplicationError if no
log file can be found.
Mothur 1.6.0 and 1.7.2 generate logfiles named
'mothur.logFile'. Mothur 1.9.0 generates log files named in a
nondeterministic way, using the current time, with a lowercase
extension. To work around this problem, we search the working
directory for files with the either extension. If the working
directory contains two valid Mothur log files, this method may
inadvertently return the incorrect log file for Mothur >=
1.9.0.
"""
input_dir = path.dirname(path.abspath(self._input_filename))
possible_logfiles = listdir(input_dir)
matching_logfiles = filter(
lambda x: x.endswith('.logfile') or x.endswith('.logFile'),
possible_logfiles)
try:
log_path = matching_logfiles[0]
except IndexError:
raise ApplicationError(
'No log file detected in directory %s. Contents: \n\t%s' % (
input_dir, '\n\t'.join(possible_logfiles)))
return path.join(input_dir, log_path)
def _derive_unique_path(self):
"""Guess unique sequences path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.unique%s' % (base, ext)
def _derive_dist_path(self):
"""Guess distance matrix path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.unique.dist' % base
def _derive_names_path(self):
"""Guess unique names file path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.names' % base
def __get_method_abbrev(self):
"""Abbreviated form of clustering method parameter.
Used to guess output filenames for MOTHUR.
"""
abbrevs = {
'furthest': 'fn',
'nearest': 'nn',
'average': 'an',
}
if self.Parameters['method'].isOn():
method = self.Parameters['method'].Value
else:
method = self.Parameters['method'].Default
return abbrevs[method]
def _derive_list_path(self):
"""Guess otu list file path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.unique.%s.list' % (base, self.__get_method_abbrev())
def _derive_rank_abundance_path(self):
"""Guess rank abundance file path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.unique.%s.rabund' % (base, self.__get_method_abbrev())
def _derive_species_abundance_path(self):
"""Guess species abundance file path produced by Mothur"""
base, ext = path.splitext(self._input_filename)
return '%s.unique.%s.sabund' % (base, self.__get_method_abbrev())
def getTmpFilename(self, tmp_dir='/tmp', prefix='tmp', suffix='.txt'):
"""Returns a temporary filename
Similar interface to tempfile.mktmp()
"""
# Override to change default constructor to str(). FilePath
# objects muck up the Mothur script.
return super(Mothur, self).getTmpFilename(
tmp_dir=tmp_dir, prefix=prefix, suffix=suffix,
result_constructor=str)
# Temporary input file needs to be in the working directory, so we
# override all input handlers.
def _input_as_multiline_string(self, data):
"""Write multiline string to temp file, return filename
data: a multiline string to be written to a file.
"""
self._input_filename = self.getTmpFilename(
self.WorkingDir, suffix='.fasta')
with open(self._input_filename, 'w') as f:
f.write(data)
return self._input_filename
def _input_as_lines(self, data):
"""Write sequence of lines to temp file, return filename
data: a sequence to be written to a file, each element of the
sequence will compose a line in the file
* Note: '\n' will be stripped off the end of each sequence
element before writing to a file in order to avoid
multiple new lines accidentally be written to a file
"""
self._input_filename = self.getTmpFilename(
self.WorkingDir, suffix='.fasta')
with open(self._input_filename, 'w') as f:
# Use lazy iteration instead of list comprehension to
# prevent reading entire file into memory
for line in data:
f.write(str(line).strip('\n'))
f.write('\n')
return self._input_filename
def _input_as_path(self, data):
"""Copys the provided file to WorkingDir and returns the new filename
data: path or filename
"""
self._input_filename = self.getTmpFilename(
self.WorkingDir, suffix='.fasta')
copyfile(data, self._input_filename)
return self._input_filename
def _input_as_paths(self, data):
raise NotImplementedError('Not applicable for MOTHUR controller.')
def _input_as_string(self, data):
raise NotImplementedError('Not applicable for MOTHUR controller.')
# FilePath objects muck up the Mothur script, so we override the
# property methods for self.WorkingDir
def _get_WorkingDir(self):
"""Gets the working directory"""
return self._curr_working_dir
def _set_WorkingDir(self, path):
"""Sets the working directory
"""
self._curr_working_dir = path
try:
mkdir(self.WorkingDir)
except OSError:
# Directory already exists
pass
WorkingDir = property(_get_WorkingDir, _set_WorkingDir)
def mothur_from_file(file):
app = Mothur(InputHandler='_input_as_lines')
result = app(file)
# Force evaluation, so we can safely clean up files
otus = list(parse_otu_list(result['otu list']))
result.cleanUp()
return otus
|