annotate resfinder/db_resfinder/INSTALL.py @ 0:55051a9bc58d draft default tip

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