Skip to content

Commit

Permalink
add setup.py so it pip installs, minor code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
wwood committed Mar 31, 2015
1 parent d053bd7 commit 6132df9
Show file tree
Hide file tree
Showing 22 changed files with 312 additions and 508 deletions.
2 changes: 2 additions & 0 deletions LICENCE.txt
@@ -0,0 +1,2 @@
graftm - Licence
========================
2 changes: 2 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,2 @@
include *.txt
recursive-include examples *.txt *.py
12 changes: 5 additions & 7 deletions bin/graftM
Expand Up @@ -17,8 +17,9 @@ __license__ = "GPL3"
__maintainer__ = "Joel Boyd, Ben Woodcroft"
__email__ = "joel.boyd near uq.net.au, b.woodcroft near uq.edu.au"
__status__ = "Development"
__version__ = "0.4.2"


import graftm
from graftm.Run import Run
import argparse
import sys
Expand Down Expand Up @@ -62,13 +63,14 @@ UTILITIES
e.g. usage:
graftM filter --reads <READS> --graftM_package <GRAFTM_PACKAGE>
=====================================================================================================
""" % (__version__)
""" % (graftm.__version__)



if __name__ == '__main__':

parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--version', action='version', version='graftM v%s' % graftm.__version__)

subparsers = parser.add_subparsers(help="--", dest='subparser_name')

Expand All @@ -90,7 +92,6 @@ if __name__ == '__main__':
graft_parser.add_argument('--seach_and_align_only', action="store_true", help='Stop after reads have been identified and aligned (default=False)', default=False)
graft_parser.add_argument('--search_only', action="store_true", help='Stop after reads have been identified (default=False)', default=False)
graft_parser.add_argument('--check_total_euks', action="store_true", help='Search whole sample for 18S and attempt to estimate relative percentage of Eukaryotes (default=False)', default=False)
graft_parser.add_argument('--version', action='version', version='graftM v%s' % __version__)
graft_parser.add_argument('--output_directory', metavar='reference_package', type=str, help='Output directory name. If unspecified, this file will be named GraftM_proc', default=argparse.SUPPRESS)


Expand All @@ -114,7 +115,6 @@ if __name__ == '__main__':
assemble_parser.add_argument('--graft_run', metavar='File produced ', help='guppy file produced by graftM, comma separated (default=True)', default=True)
assemble_parser.add_argument('--kmer', metavar='k-mer', help='k-mer to use for assembly with velvet', default = '51')
assemble_parser.add_argument('--assembly_type', metavar='type of assembly', help='phrap or velvet assembly',choices = ['phrap', 'velvet', 'finishm'], default='velvet')
assemble_parser.add_argument('--version', action='version', version='graftM v%s' % __version__)
assemble_parser.add_argument('--finish', action = 'store_true', help='finish the velvet assembly with an overlap assembly (default=False)', default=False)

# Manage - various tools that make the management of graftM output easier
Expand All @@ -127,7 +127,6 @@ if __name__ == '__main__':
manage_parser.add_argument('--profile', metavar='graftM_profile', help='GraftM profile produced by graftM graft run (required=True)', required=True)
manage_parser.add_argument('--non_cumil', action = 'store_true', help='Return all sequences exclusively at this taxonomic rank (i.e. do not return sequences that were classified to a higher resolution, default=False)', default=False)
manage_parser.add_argument('--file', action = 'store_true', help='Lineages are in a file, one entry per line (default=False)', default=False)
manage_parser.add_argument('--version', action='version', version='graftM v%s' % __version__)

# Pathfinder - Find a whole pathway of genes in a metagenome/transcriptome.
pathfinder_parser = subparsers.add_parser('pathfinder',
Expand All @@ -138,14 +137,13 @@ if __name__ == '__main__':



if(len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv == '--help'):
if(len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help'):
phelp()
exit(0)
else:
args = parser.parse_args()

Run(args).main()
exit(0)

# Summary of class abbreviations:
# Messenger = M
Expand Down
259 changes: 259 additions & 0 deletions ez_setup.py
@@ -0,0 +1,259 @@

#!python
"""Bootstrap setuptools installation
If you want to use setuptools in your package's setup.py, just include this
file in the same directory with it, and add this to the top of your setup.py::
from ez_setup import use_setuptools
use_setuptools()
If you want to require a specific version of setuptools, set a download
mirror, or use an alternate download directory, you can do so by supplying
the appropriate options to ``use_setuptools()``.
This file can also be run as a script to install or upgrade setuptools.
"""
import os
import shutil
import sys
import tempfile
import tarfile
import optparse
import subprocess

from distutils import log

try:
from site import USER_SITE
except ImportError:
USER_SITE = None

DEFAULT_VERSION = "0.9.6"
DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"

def _python_cmd(*args):
args = (sys.executable,) + args
return subprocess.call(args) == 0

def _install(tarball, install_args=()):
# extracting the tarball
tmpdir = tempfile.mkdtemp()
log.warn('Extracting in %s', tmpdir)
old_wd = os.getcwd()
try:
os.chdir(tmpdir)
tar = tarfile.open(tarball)
_extractall(tar)
tar.close()

# going in the directory
subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
os.chdir(subdir)
log.warn('Now working in %s', subdir)

# installing
log.warn('Installing Setuptools')
if not _python_cmd('setup.py', 'install', *install_args):
log.warn('Something went wrong during the installation.')
log.warn('See the error message above.')
# exitcode will be 2
return 2
finally:
os.chdir(old_wd)
shutil.rmtree(tmpdir)


def _build_egg(egg, tarball, to_dir):
# extracting the tarball
tmpdir = tempfile.mkdtemp()
log.warn('Extracting in %s', tmpdir)
old_wd = os.getcwd()
try:
os.chdir(tmpdir)
tar = tarfile.open(tarball)
_extractall(tar)
tar.close()

# going in the directory
subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
os.chdir(subdir)
log.warn('Now working in %s', subdir)

# building an egg
log.warn('Building a Setuptools egg in %s', to_dir)
_python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)

finally:
os.chdir(old_wd)
shutil.rmtree(tmpdir)
# returning the result
log.warn(egg)
if not os.path.exists(egg):
raise IOError('Could not build the egg.')


def _do_download(version, download_base, to_dir, download_delay):
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
% (version, sys.version_info[0], sys.version_info[1]))
if not os.path.exists(egg):
tarball = download_setuptools(version, download_base,
to_dir, download_delay)
_build_egg(egg, tarball, to_dir)
sys.path.insert(0, egg)
import setuptools
setuptools.bootstrap_install_from = egg


def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
to_dir=os.curdir, download_delay=15):
# making sure we use the absolute path
to_dir = os.path.abspath(to_dir)
was_imported = 'pkg_resources' in sys.modules or \
'setuptools' in sys.modules
try:
import pkg_resources
except ImportError:
return _do_download(version, download_base, to_dir, download_delay)
try:
pkg_resources.require("setuptools>=" + version)
return
except pkg_resources.VersionConflict:
e = sys.exc_info()[1]
if was_imported:
sys.stderr.write(
"The required version of setuptools (>=%s) is not available,\n"
"and can't be installed while this script is running. Please\n"
"install a more recent version first, using\n"
"'easy_install -U setuptools'."
"\n\n(Currently using %r)\n" % (version, e.args[0]))
sys.exit(2)
else:
del pkg_resources, sys.modules['pkg_resources'] # reload ok
return _do_download(version, download_base, to_dir,
download_delay)
except pkg_resources.DistributionNotFound:
return _do_download(version, download_base, to_dir,
download_delay)


def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
to_dir=os.curdir, delay=15):
"""Download setuptools from a specified location and return its filename
`version` should be a valid setuptools version number that is available
as an egg for download under the `download_base` URL (which should end
with a '/'). `to_dir` is the directory where the egg will be downloaded.
`delay` is the number of seconds to pause before an actual download
attempt.
"""
# making sure we use the absolute path
to_dir = os.path.abspath(to_dir)
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
tgz_name = "setuptools-%s.tar.gz" % version
url = download_base + tgz_name
saveto = os.path.join(to_dir, tgz_name)
src = dst = None
if not os.path.exists(saveto): # Avoid repeated downloads
try:
log.warn("Downloading %s", url)
src = urlopen(url)
# Read/write all in one block, so we don't create a corrupt file
# if the download is interrupted.
data = src.read()
dst = open(saveto, "wb")
dst.write(data)
finally:
if src:
src.close()
if dst:
dst.close()
return os.path.realpath(saveto)


def _extractall(self, path=".", members=None):
"""Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
to extract to. `members' is optional and must be a subset of the
list returned by getmembers().
"""
import copy
import operator
from tarfile import ExtractError
directories = []

if members is None:
members = self

for tarinfo in members:
if tarinfo.isdir():
# Extract directories with a safe mode.
directories.append(tarinfo)
tarinfo = copy.copy(tarinfo)
tarinfo.mode = 448 # decimal for oct 0700
self.extract(tarinfo, path)

# Reverse sort directories.
if sys.version_info < (2, 4):
def sorter(dir1, dir2):
return cmp(dir1.name, dir2.name)
directories.sort(sorter)
directories.reverse()
else:
directories.sort(key=operator.attrgetter('name'), reverse=True)

# Set correct owner, mtime and filemode on directories.
for tarinfo in directories:
dirpath = os.path.join(path, tarinfo.name)
try:
self.chown(tarinfo, dirpath)
self.utime(tarinfo, dirpath)
self.chmod(tarinfo, dirpath)
except ExtractError:
e = sys.exc_info()[1]
if self.errorlevel > 1:
raise
else:
self._dbg(1, "tarfile: %s" % e)


def _build_install_args(options):
"""
Build the arguments to 'python setup.py install' on the setuptools package
"""
install_args = []
if options.user_install:
if sys.version_info < (2, 6):
log.warn("--user requires Python 2.6 or later")
raise SystemExit(1)
install_args.append('--user')
return install_args

def _parse_args():
"""
Parse the command line for options
"""
parser = optparse.OptionParser()
parser.add_option(
'--user', dest='user_install', action='store_true', default=False,
help='install in user site package (requires Python 2.6 or later)')
parser.add_option(
'--download-base', dest='download_base', metavar="URL",
default=DEFAULT_URL,
help='alternative URL from where to download the setuptools package')
options, args = parser.parse_args()
# positional arguments are ignored
return options

def main(version=DEFAULT_VERSION):
"""Install or upgrade setuptools and EasyInstall"""
options = _parse_args()
tarball = download_setuptools(download_base=options.download_base)
return _install(tarball, _build_install_args(options))

if __name__ == '__main__':
sys.exit(main())
4 changes: 2 additions & 2 deletions graftm/Alignment_Manager.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python

from Bio import SeqIO
from collections import OrderedDict
import IPython

class Alignment_Manager:

def alignment_correcter(self, alignment_file_list, output_file_name):
Expand Down
5 changes: 2 additions & 3 deletions graftm/DatManip.py
@@ -1,10 +1,9 @@
#!/usr/bin/env python

from graftm.Messenger import Messenger
import IPython
import subprocess
import re
import json
import shutil

# Constants
FORMAT_FASTA = 'FORMAT_FASTA'
FORMAT_FASTQ_GZ = 'FORMAT_FASTQ_GZ'
Expand Down
11 changes: 1 addition & 10 deletions graftm/Extract_Sequences.py
@@ -1,17 +1,8 @@
#!/usr/bin/env python
__author__ = "Joel Boyd, Ben Woodcroft"
__copyright__ = "Copyright 2014"
__credits__ = ["Joel Boyd", "Ben Woodcroft"]
__license__ = "GPL3"
__maintainer__ = "Joel Boyd, Ben Woodcroft"
__email__ = "joel.boyd near uq.net.au, b.woodcroft near uq.edu.au"
__status__ = "Development"
__version__ = "0.4.0"

from graftm.Messenger import Messenger
from graftm.assembler import TaxoGroup
from Bio import SeqIO
import IPython

class Extract:

def __init__(self):
Expand Down
6 changes: 1 addition & 5 deletions graftm/GraftMFiles.py
@@ -1,6 +1,5 @@
#!/usr/bin/env python

import os
import IPython

class GraftMFiles:

Expand Down Expand Up @@ -58,9 +57,6 @@ def conv_output_path(self, out_path):

def comb_aln_fa(self, out_path):
return os.path.join(self.outdir, out_path, "%s_comb.aln.fa" % self.basename)

def conv_output_rev_path(self, out_path):
return os.path.join(self.outdir, out_path, "%s_conv_rev.faa" % self.basename)

def reverse_read_hmmsearch_output_path(self, out_path):
return os.path.join(self.outdir, out_path, "%s_rev.hmmout.csv" % self.basename)
Expand Down
2 changes: 1 addition & 1 deletion graftm/Hmmer.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python

import subprocess
from graftm.GraftMFiles import GraftMFiles
from graftm.Messenger import Messenger
Expand Down

0 comments on commit 6132df9

Please sign in to comment.