0
|
1 #!/usr/bin/python3
|
|
2 import shutil
|
|
3 import os
|
|
4 import sys
|
|
5 import tempfile
|
|
6 import subprocess
|
|
7
|
|
8 # This scripts installs the PointFinder database for using KMA
|
|
9 # KMA should be installed before running this script
|
|
10 # The scripts assumes that it is placed together with the PointFinder species
|
|
11 # directories
|
|
12 #
|
|
13 # First clone the repository:
|
|
14 # git clone https://bitbucket.org/genomicepidemiology/pointfinder_db.git
|
|
15
|
|
16 # Check if executable kma_index program is installed, if not promt the user for
|
|
17 # path
|
|
18
|
|
19
|
|
20 # Function for easy error printing
|
|
21 def eprint(*args, **kwargs):
|
|
22 print(*args, file=sys.stderr, **kwargs)
|
|
23
|
|
24
|
|
25 # KMA version
|
|
26 KMA_VERSION = "1.0.1"
|
|
27
|
|
28
|
|
29 interactive = True
|
|
30 if len(sys.argv) >= 2:
|
|
31 kma_index = sys.argv[1]
|
|
32 if "non_interactive" in sys.argv:
|
|
33 interactive = False
|
|
34 else:
|
|
35 kma_index = "kma_index"
|
|
36
|
|
37 print(str(sys.argv))
|
|
38
|
|
39 while shutil.which(kma_index) is None:
|
|
40 eprint("KMA index program, {}, does not exist or is not executable"
|
|
41 .format(kma_index))
|
|
42 ans = None
|
|
43 if(interactive):
|
|
44 ans = input("Please input path to executable kma_index program or"
|
|
45 "choose one of the options below:\n"
|
|
46 "\t1. Install KMA using make, index db, then remove KMA.\n"
|
|
47 "\t2. Exit\n")
|
|
48
|
|
49 if(ans == "2" or ans == "q" or ans == "quit" or ans == "exit"):
|
|
50 eprint("Exiting!\n\n"
|
|
51 "Please install executable KMA programs in order to install"
|
|
52 "this database.\n\n"
|
|
53 "KMA can be obtained from bitbucked:\n\n"
|
|
54 "git clone -b {:s} "
|
|
55 "https://bitbucket.org/genomicepidemiology/kma.git"
|
|
56 .format(KMA_VERSION))
|
|
57 sys.exit()
|
|
58
|
|
59 if(ans == "1" or ans is None):
|
|
60 if(shutil.which("git") is None):
|
|
61 sys.exit("Attempt to automatically install KMA failed.\n"
|
|
62 "git does not exist or is not executable.")
|
|
63 org_dir = os.getcwd()
|
|
64
|
|
65 # Create temporary directory
|
|
66 tempdir = tempfile.TemporaryDirectory()
|
|
67 os.chdir(tempdir.name)
|
|
68
|
|
69 try:
|
|
70 subprocess.run(
|
|
71 ["git", "clone", "-b", KMA_VERSION,
|
|
72 "https://bitbucket.org/genomicepidemiology/kma.git"],
|
|
73 check=True)
|
|
74 os.chdir("kma")
|
|
75 except subprocess.CalledProcessError:
|
|
76 eprint("Installation in temporary directory with make failed "
|
|
77 "at the git cloning step")
|
|
78 os.chdir(org_dir)
|
|
79
|
|
80 try:
|
|
81 subprocess.run(["make"])
|
|
82 except subprocess.CalledProcessError:
|
|
83 eprint("Installation in temporary directory with make failed "
|
|
84 "at the make step.")
|
|
85 os.chdir(org_dir)
|
|
86
|
|
87 kma_index = "{}/kma/kma_index".format(tempdir.name)
|
|
88 os.chdir(org_dir)
|
|
89 if shutil.which(kma_index) is None:
|
|
90 eprint("Installation in temporary directory with make failed "
|
|
91 "at the test step.")
|
|
92 os.chdir(org_dir)
|
|
93 kma_index = "kma_index"
|
|
94 if(not interactive):
|
|
95 ans = "2"
|
|
96
|
|
97 if(ans is not None and ans != "1" and ans != "2"):
|
|
98 kma_index = ans
|
|
99 if shutil.which(kma_index) is None:
|
|
100 eprint("Path, {}, is not an executable path. Please provide "
|
|
101 "absolute path\n".format(ans))
|
|
102
|
|
103
|
|
104 # Index databases
|
|
105 os.makedirs("kma_indexing", exist_ok=True)
|
|
106 # Use config_file to go through database dirs
|
|
107 config_file = open("config", "r")
|
|
108 for line in config_file:
|
|
109 if line.startswith("#"):
|
|
110 continue
|
|
111 else:
|
|
112 line = line.rstrip().split("\t")
|
|
113 species_dir = line[0].strip()
|
|
114 # for each dir index the fasta files
|
|
115 os.system("{0} -i {1}/*.fsa -o {1}/{1}".format(kma_index, species_dir))
|
|
116
|
|
117 config_file.close()
|
|
118
|
|
119 eprint("Done")
|