Skip to content

Commit

Permalink
add nexus newick script
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmeaton committed May 19, 2020
1 parent 2761f9e commit 7507f3c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions scripts/nexus2newick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""
@author: Katherine Eaton
Convert Nexus tree to Newick Format.
./nexus2newick.py --nexus tree.nexus --newick tree.nwk
"""

# This program should only be called from the command-line
if __name__ != "__main__": quit()

#-----------------------------------------------------------------------#
# Modules and Packages #
#-----------------------------------------------------------------------#
import argparse # Command-line argument parsing
import dendropy # Read and write trees

#-----------------------------------------------------------------------#
# Argument Parsing #
#-----------------------------------------------------------------------#

parser = argparse.ArgumentParser(description='Convert Nexus tree to Newick Format.',
add_help=True)

# Argument groups for the program

parser.add_argument('--nexus',
help = 'Path to the input nexus tree.',
action = 'store',
dest = 'nexusPath',
required = True)

parser.add_argument('--newick',
help = 'Path to the output newick tree.',
action = 'store',
dest = 'newickPath',
required = True)

# Retrieve user parameters
args = vars(parser.parse_args())

nexus_path = args['nexusPath']
newick_path = args['newickPath']

#------------------------------------------------------------------------------#
# Error Catching #
#------------------------------------------------------------------------------#
post_trees = dendropy.TreeList()

# Check if NEXUS file exists
if not os.path.exists(nexus_path):
print('An error occurred while trying to open', nexus_path)
sys.exit(1)

post_trees.read(
file=open(nexus_path, "r"),
schema="nexus")

post_trees.write(
path=newick_path,
schema="newick")

0 comments on commit 7507f3c

Please sign in to comment.