-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgbk2folders.py
executable file
·58 lines (54 loc) · 2 KB
/
gbk2folders.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
#!/usr/bin/env python
#This script takes folder of GBK files, reads taxonomy, and moves them into folders based on phylum
#written by Jon Palmer palmer.jona at gmail dot com
import sys, argparse, Bio, glob, os
from Bio import SeqIO
class bcolors:
OKGREEN = '\033[92m'
BLUE = '\033[36m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
parser=argparse.ArgumentParser(
description='''Script that reads folder of GBK files and splits them by taxonomy''',
epilog="""Written by Jon Palmer (2015) [email protected]""")
parser.add_argument('-d', '--directory', default="current", help='folder')
args=parser.parse_args()
if args.directory == "current":
dir = os.getcwd()
else:
dir = os.chdir(args.directory)
os.chdir(dir)
for file in glob.glob("*.gbk"):
file_path = os.path.abspath(file)
dir_path = os.path.abspath(dir)
record = next(SeqIO.parse(file, "genbank"))
tax = record.annotations["taxonomy"]
try:
if "Ascomycota" in tax:
level = "Ascomycota"
if "Basidiomycota" in tax:
level = "Basidiomycota"
if "Microsporidia" in tax:
level = "Microsporidia"
if "Zygomycota" in tax:
level = "Zygomycota"
if "Mucormycotina" in tax:
level = "Zygomycota"
if "Chytridiomycota" in tax:
level = "Chytridiomycota"
if "Rozella" in tax:
level = "Chytridiomycota"
if "Glomeromycota" in tax:
level = "Glomeromycota"
except IndexError:
level = "no_taxonomy"
try:
if not os.path.exists(level):
os.makedirs(level)
except NameError:
print bcolors.WARNING + "Working on: " + bcolors.ENDC + file + bcolors.FAIL + " Tax level not defined, skipping file" + bcolors.ENDC
continue
new_path = dir_path + "/" + level + "/" + file
os.rename(file_path, new_path)
print bcolors.WARNING + "Working on: " + bcolors.ENDC + file + bcolors.OKGREEN + " Moving to: " + bcolors.ENDC + new_path