Skip to content

Commit

Permalink
2018.01.26.1200
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgruenstaeudl committed Jan 26, 2018
1 parent ee3e3c3 commit a672fd8
Show file tree
Hide file tree
Showing 23 changed files with 851 additions and 3 deletions.
Binary file added CustomFileOps.pyc
Binary file not shown.
Binary file added CustomPhyloOps.pyc
Binary file not shown.
Binary file added CustomStringOps.pyc
Binary file not shown.
7 changes: 4 additions & 3 deletions WARACS_BayesTraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
__author__ = "Michael Gruenstaeudl, PhD <mi.gruenstaeudl@gmail.com>"
__copyright__ = "Copyright (C) 2015 Michael Gruenstaeudl"
__info__ = "Reconstructing Ancestral Character States using BayesTraits (http://www.evolution.reading.ac.uk/BayesTraits.html)"
__version__ = "2015.12.15.1100"
__version__ = "2018.01.26.1200"

#############
# DEBUGGING #
#############

#import pdb
import pdb
#pdb.set_trace()

####################
Expand Down Expand Up @@ -85,7 +85,8 @@ def main(treedistrFn, plottreeFn, charsFn, charnum, rcnmdl, pathToSoftware, keep
# 1.4. Modify chars-file
reader = csv.reader(open(charsFn, "r"), delimiter=",")
arr = numpy.array(list(reader))
arr = arr[:,[0,charnum]] # Extracting a particular column
#pdb.set_trace()
arr = arr[:,[0,int(charnum)]] # Extracting a particular column
out_handle = CSO.makeprettytable(arr)
CFO.saveFile(charsFnTmp, out_handle)

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
284 changes: 284 additions & 0 deletions suppl/CustomFileOps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
#!/usr/bin/env python
"""General File Operations
"""

#####################
# IMPORT OPERATIONS #
#####################

from __future__ import absolute_import
from __future__ import print_function
from six.moves import map
from six.moves import input

from subprocess import Popen, PIPE
from pkg_resources import WorkingSet, DistributionNotFound
import codecs
import os
import signal
import sys
import time

###############
# AUTHOR INFO #
###############

__author__ = "Michael Gruenstaeudl, PhD"
__copyright__ = "Copyright (C) 2015 Michael Gruenstaeudl"
__email__ = "mi.gruenstaeudl@gmail.com"
__version__ = "2015.12.15.1100"

#################
# COMPATIBILITY #
#################

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

#############
# DEBUGGING #
#############

#import pdb
#pdb.set_trace()

###########
# CLASSES #
###########

class ListFilesInDir:
''' Listing all files in directory.
Args:
string <a>
Returns:
list
'''
def __init__(self, a):
self.dr = a
def go(self):
return os.listdir(self.dr)

class LoadFile:
''' Loading input files.
Args:
string <a>, string <b>, string <c>
Returns:
string
'''
def __init__(self, a):
self.Fn = a
def openFile(self):
infile = open(self.Fn, "r").read()
return infile
def readLines(self):
infile = open(self.Fn, "r").read().splitlines()
return infile
def commaDelim(self):
infile = open(self.Fn, "r").read().split(",")
return infile

class LoadModule:
''' Loading module.
Args:
string <a>
Returns:
none
'''
def __init__(self, a):
self.inStr = a
def go(self):
import importlib
try:
list(map(__import__, [self.inStr]))
# except ImportError: # Not all systems raise the exception "ImportError"; others raise different exception.
except:
print((sys.exc_info()[0]))
sys.exit(" ERROR: Please install the following Python packages: " + self.inStr)
# FUTURE CODE:
# CFO.installPkgs(self.inStr)
return importlib.import_module(self.inStr)

class InstallPkgs:
''' Automatically installing packages.
Args:
list <a> (example: ["dendropy", "numpy"])
Returns:
none
'''
def __init__(self, a):
self.pkgList = a

def go(self):
working_set = WorkingSet()
for pkgName in self.pkgList:
try:
depends = working_set.require(pkgName)
except DistributionNotFound:
from setuptools.command.easy_install import main as install
import urllib2
print("\n Library '" + pkgName + "' needs to be installed.")
allow = input(" May I install the above library? ([y]/n): ") # Prompt for user decision if a package requires installation
allow = allow.upper()
if allow == "N" or allow == "NO":
sys.exit("\n ERROR: Please install package '" + pkgName + "' manually.\n")
else:
try:
response = urllib2.urlopen("http://www.python.org/", timeout=10)
except urllib2.URLError as err:
sys.exit("\n ERROR: No internet connection available.\n")
try:
install(["--user", pkgName]) # Make certain to use the user flag
# ALTERNATIVE: os.system("easy_install-2.7 --user " + pkgName + "==" + pkgVersion)
print("\n Library '" + pkgName + "' installed successfully.")
# ALTERNATIVE: print "\n Library '" + pkgName + "v." + pkgVersion + "' installed successfully."
except:
import os
if sys.platform == "linux" or sys.platform == "linux2":
if isExe("pip2.7"):
os.system("pip2.7 install " + pkgName + " --user")
else:
("\n ERROR: Python setuptools inaccessible.\n")
elif sys.platform == "darwin":
if isExe("pip2.7"):
os.system("pip2.7 install " + pkgName + " --user")
else:
("\n ERROR: Python setuptools inaccessible.\n")
elif sys.platform == "win32":
if isExe("pip2.7.exe"):
os.system("pip2.7.exe install " + pkgName)
else:
("\n ERROR: Python setuptools inaccessible.\n")
try:
exec("import " + pkgName)
except ImportError:
sys.exit("\n Please restart this script.\n") # After installation via easy_install, Python must be restarted for certain new modules to be properly loaded
#import os # For later: Automatic restart
#os.execv(__file__, sys.argv)

class IsExe:
''' Checking if executable exists. '''
def __init__(self, a):
self.fpath = a
def go(self):
return os.path.isfile(self.fpath) and os.access(self.fpath, os.X_OK)

class PopenExtProg:
''' Class for communicating with executable via Popen.
Args:
inlist <a>, flag <b>
Returns:
string
'''
def __init__(self, a, b):
self.cmdL = a
self.flag = b
def go(self):
initCmd = " ".join(self.cmdL)
if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "darwin":
p = Popen(initCmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, preexec_fn=os.setsid)
if self.flag:
time.sleep(float(self.flag))
os.killpg(p.pid, signal.SIGTERM)
if sys.platform == "win32":
p = Popen(initCmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
if self.flag:
time.sleep(float(self.flag))
p.stdin.write("quit\n")
outStream, errorStream = p.communicate()
#if errorStream: # too sensistive with many programs
# sys.exit(" ERROR: ", self.err)
if PY2:
outStream = outStream
if PY3:
outStream = outStream.decode() # in Python 3, files are read as byte-like by default; need to decode to string
return outStream


class RemoveFile:
''' Deleting files.
Args:
string <a>
Returns:
none
'''
def __init__(self, a):
self.Fn = a
def go(self):
os.remove(self.Fn)


class SaveFile:
''' Saving data files.
Args:
string <a>, filehandle <b>, flag <c>
Returns:
none
'''
def __init__(self, a, b, c):
self.Fn = a
self.data = b
self.appendflag = c
def go(self):
if self.appendflag:
outfile = open(self.Fn, "a")
if not self.appendflag:
outfile = open(self.Fn, "w")
outfile.write(self.data)
outfile.close()

class SaveFilewithDFN:
''' Saving data files with filenames.
Args:
string <a>, filehandle <b>, string <c>
Returns:
none
'''
def __init__(self, a, b, c):
self.Fn = a
self.data = str(b)
self.info = c
def go(self):
outfile = open(self.Fn, "a")
outfile.write(self.info+"\n"+self.data+"\n")
outfile.close()

###############
# DEFINITIONS #
###############

def append(a, b):
return SaveFile(a, b, True).go()

def deleteFile(a):
RemoveFile(a).go()

def extprog(a, b=None):
return PopenExtProg(a, b).go()

def installPkgs(pkgList):
InstallPkgs(pkgList).go()

def isExe(a):
return IsExe(a).go()

def listAllFilesInDir(a):
return ListFilesInDir(a).go()

def loadCD(a):
return LoadFile(a).commaDelim()

def saveFile(a, b):
return SaveFile(a, b, False).go()

def saveFn(a, b, c):
return SaveFilewithDFN(a, b, c).go()

def loadR(a):
return LoadFile(a).openFile()

def loadRL(a):
return LoadFile(a).readLines()

def loadModule(a):
return LoadModule(a).go()

Binary file added suppl/CustomFileOps.pyc
Binary file not shown.
78 changes: 78 additions & 0 deletions suppl/CustomPhyloOps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python
"""Custom Phylo Operations
"""

#####################
# IMPORT OPERATIONS #
#####################

from __future__ import absolute_import

import CustomFileOps as CFO
import sys

dendropy = CFO.loadModule("dendropy")

###############
# AUTHOR INFO #
###############

__author__ = "Michael Gruenstaeudl, PhD"
__copyright__ = "Copyright (C) 2015 Michael Gruenstaeudl"
__email__ = "mi.gruenstaeudl@gmail.com"
__version__ = "2015.12.15.1100"

#############
# DEBUGGING #
#############

#import pdb
#pdb.set_trace()

###########
# CLASSES #
###########

class convertNexusToNewick:
''' Convert a phylogenetic tree in NEXUS format to a phylogenetic tree in NEWICK format
Args:
string <a>
Returns:
string
'''
def __init__(self, a):
self.inStr = a
def go(self):
treeH = dendropy.Tree.get_from_string(self.inStr, schema="nexus")
return treeH.as_string(schema='newick')

class getNodeListFromTree:
''' class for generating a list of nodes from a phylogenetic tree
Args:
string <a>
Returns:
str, list
'''
def __init__(self, a):
self.inFn = a
def go(self):
nodespecL, nodeL = [], []
treeH = dendropy.Tree.get_from_path(self.inFn, schema="nexus")
for c, node in enumerate(treeH.nodes(), start=1):
tips = [tip.taxon.label.replace(" ", "_") for tip in node.leaf_nodes()]
if len(tips) > 1:
nodespecL.append("AddNode Node" + str(c) + " " + " ".join(tips))
nodeL.append(c) # generate list of relevant node numbers
node_specs = "\n".join(nodespecL)
return [node_specs, nodeL]

###############
# DEFINITIONS #
###############

def ConvertNexusToNewick(a):
return convertNexusToNewick(a).go()


def GetNodeListFromTree(a):
return getNodeListFromTree(a).go()
Binary file added suppl/CustomPhyloOps.pyc
Binary file not shown.
Loading

0 comments on commit a672fd8

Please sign in to comment.