-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |