utils.py 3.92 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
import argparse
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
2
import ctypes
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
3
from datetime import date
4
import os
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
5
import sys
6
from pathlib import Path
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
7

8
9
10
11
if 'CONDA_PREFIX' in os.environ:
    CONDA_ENV_BINARY_PATH= Path(os.environ['CONDA_PREFIX']) / 'bin'
else:
    CONDA_ENV_BINARY_PATH = Path('/bin')
12

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
13
14
def add_data_args(parser: argparse.ArgumentParser):
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
15
        '--uniref90_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
16
17
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
18
        '--mgnify_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
19
20
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
21
        '--pdb70_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
22
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
23
24
25
    parser.add_argument(
        '--pdb_seqres_database_path', type=str, default=None,
    )
26
27
28
    parser.add_argument(
        '--uniref30_database_path', type=str, default=None,
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
29
    parser.add_argument(
Gustaf Ahdritz's avatar
Fixes  
Gustaf Ahdritz committed
30
        '--uniclust30_database_path', type=str, default=None,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
31
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
32
33
34
    parser.add_argument(
        '--uniprot_database_path', type=str, default=None,
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
35
36
37
38
    parser.add_argument(
        '--bfd_database_path', type=str, default=None,
    )
    parser.add_argument(
39
        '--jackhmmer_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'jackhmmer'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
40
41
    )
    parser.add_argument(
42
        '--hhblits_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'hhblits'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
43
44
    )
    parser.add_argument(
45
        '--hhsearch_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'hhsearch'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
46
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
47
    parser.add_argument(
48
        '--hmmsearch_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'hmmsearch'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
49
50
    )
    parser.add_argument(
51
        '--hmmbuild_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'hmmbuild'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
52
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
53
    parser.add_argument(
54
        '--kalign_binary_path', type=str, default=str(CONDA_ENV_BINARY_PATH / 'kalign'),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
55
56
    )
    parser.add_argument(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
57
        '--max_template_date', type=str,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
58
59
60
61
62
        default=date.today().strftime("%Y-%m-%d"),
    )
    parser.add_argument(
        '--obsolete_pdbs_path', type=str, default=None
    )
63
64
65
    parser.add_argument(
        '--release_dates_path', type=str, default=None
    )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
66
67
68
69
70
71
72
73


def get_nvidia_cc():
    """
    Returns a tuple containing the Compute Capability of the first GPU
    installed in the system (formatted as a tuple of strings) and an error
    message. When the former is provided, the latter is None, and vice versa.

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
74
    Adapted from script by Jan Schlüte t
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
75
76
77
78
    https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549
    """
    CUDA_SUCCESS = 0

79
80
81
82
    libnames = [
        'libcuda.so', 
        'libcuda.dylib', 
        'cuda.dll',
83
        '/usr/local/cuda/compat/libcuda.so', # For Docker
84
    ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
85
86
87
88
89
90
91
92
    for libname in libnames:
        try:
            cuda = ctypes.CDLL(libname)
        except OSError:
            continue
        else:
            break
    else:
93
        return None, "Could not load any of: " + ' '.join(libnames)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
94
95
96
97
98
99
100
101
102
103
104

    nGpus = ctypes.c_int()
    cc_major = ctypes.c_int()
    cc_minor = ctypes.c_int()

    result = ctypes.c_int()
    device = ctypes.c_int()
    error_str = ctypes.c_char_p()

    result = cuda.cuInit(0)
    if result != CUDA_SUCCESS:
105
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
106
107
108
109
        if error_str.value:
            return None, error_str.value.decode()
        else:
            return None, "Unknown error: cuInit returned %d" % result
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
110
111
    result = cuda.cuDeviceGetCount(ctypes.byref(nGpus))
    if result != CUDA_SUCCESS:
112
113
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        return None, error_str.value.decode()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
114

115
    if nGpus.value < 1:
116
        return None, "No GPUs detected"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
117
118
119

    result = cuda.cuDeviceGet(ctypes.byref(device), 0)
    if result != CUDA_SUCCESS:
120
121
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        return None, error_str.value.decode()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
122
123
124
125
126
127
128
129

    if cuda.cuDeviceComputeCapability(ctypes.byref(cc_major), ctypes.byref(cc_minor), device) != CUDA_SUCCESS:
        return None, "Compute Capability not found"

    major = cc_major.value
    minor = cc_minor.value

    return (major, minor), None