-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter_CLI.py
102 lines (83 loc) · 4.24 KB
/
converter_CLI.py
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
#!/usr/bin/env python2.7
### If you use this script, please place your hands together and bow in thanks to Charles Foster from MEEP, the University of Sydney ###
### Note: the script relies on your file extensions accurately mirroring the file contents
### Acceptable extensions: .nex (nexus), .fasta .fas .fa (fasta), .phy (phylip)
### Usage: ./converter_CLI.py <original_file_type> <desired_file_type>
### Requires biopython (and its dependencies)
from os import getcwd, listdir, rename
from os.path import isfile, join
from sys import argv
import os
from Bio import SeqIO
from Bio import AlignIO
from Bio.Alphabet import generic_dna
print "\n*****************\n\nHello, and welcome to my terminal. I will be your converter for today.\n"
mypath = getcwd()
print "Your path is: %s\n" % mypath
fas_files = [f for f in listdir(mypath) if isfile(join(mypath, f)) if f.endswith("fasta") or f.endswith("fas") or f.endswith("fa")]
nex_files = [f for f in listdir(mypath) if isfile(join(mypath, f)) if f.endswith("nex") or f.endswith("nexus")]
phy_files = [f for f in listdir(mypath) if isfile(join(mypath, f)) if f.endswith("phy")]
all_files = fas_files + nex_files + phy_files
print "You have the following files:\n\nFasta: %s\nNexus: %s\nPhylip: %s\n" % (fas_files,nex_files,phy_files)
option = argv[1] + "_" + argv[2]
if option == "fasta_nexus":
print "\nGood choice! I'll do it now...\n"
for file in fas_files:
count = SeqIO.convert(file, "fasta", file+".nex", "nexus", generic_dna)
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif option == "fasta_phy":
print "\nGood choice! I'll do it now...\n"
for file in fas_files:
count = SeqIO.convert(file, "fasta", file+".phy", "phylip-relaxed")
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif option == "nexus_fasta":
print "\nGood choice! I'll do it now...\n"
for file in nex_files:
count = SeqIO.convert(file, "nexus", file+".fas", "fasta")
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif option == "nexus_phy":
print "\nGood choice! I'll do it now...\n"
for file in nex_files:
count = SeqIO.convert(file, "nexus", file+".phy", "phylip-relaxed")
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif option == "phylip_fasta":
print "\nGood choice! I'll do it now...\n"
for file in phy_files:
count = SeqIO.convert(file, "phylip-relaxed", file+".fasta", "fasta")
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif option == "phylip_nexus":
print "\nGood choice! However, I should warn you that any gene boundaries in a concatenated file will be lost. I'll do the conversion now...\n"
for file in phy_files:
count = SeqIO.convert(file, "phylip-relaxed", file+".nex", "nexus", generic_dna)
print "\nConverting: %s" % file
print "Converted %i taxa\n" % count
elif not option == "fasta_nexus" or option == "fasta_phy" or option == "nexus_fasta" or option == "nexus_phy" or option == "phylip_fasta" or option == "phylip_nexus":
print "That was not an option. This is why we can't have nice things. I quit!\a\a\a"
quit()
for file in os.listdir('.'):
if file.endswith('.fas.nex'):
rename(file, file.replace(".fas.nex", ".nex"))
elif file.endswith('.fasta.nex'):
rename(file, file.replace(".fasta.nex", ".nex"))
elif file.endswith('.fa.nex'):
rename(file, file.replace(".fa.nex", ".nex"))
elif file.endswith('.phy.nex'):
rename(file, file.replace(".phy.nex", ".nex"))
elif file.endswith('.fas.phy'):
rename(file, file.replace(".fas.phy", ".phy"))
elif file.endswith('.fasta.phy'):
rename(file, file.replace(".fasta.phy", ".phy"))
elif file.endswith('.nex.phy'):
rename(file, file.replace(".nex.phy", ".phy"))
elif file.endswith('.nex.fasta'):
rename(file, file.replace(".nex.fasta", ".fasta"))
elif file.endswith('.nex.fas'):
rename(file, file.replace(".nex.fas", ".fasta"))
elif file.endswith('.phy.fasta'):
rename(file, file.replace(".phy.fasta", ".fasta"))
print "\nI am now finished converting your files. Have a great day!\n"