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