utils.py 3.84 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
CONDA_ENV_BINARY_PATH= Path(os.environ['CONDA_PREFIX']) / 'bin'

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


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
72
    Adapted from script by Jan Schlüte t
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
73
74
75
76
    https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549
    """
    CUDA_SUCCESS = 0

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

    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:
103
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
104
105
106
107
        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
108
109
    result = cuda.cuDeviceGetCount(ctypes.byref(nGpus))
    if result != CUDA_SUCCESS:
110
111
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        return None, error_str.value.decode()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
112

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

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

    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