data_utils.py 3.02 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright 2021 AlQuraishi Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
16
from scipy.spatial.transform import Rotation
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
17
18
19
20


def random_template_feats(n_templ, n, batch_size=None):
    b = []
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
21
    if batch_size is not None:
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
22
23
24
25
26
27
        b.append(batch_size)
    batch = {
        "template_mask": np.random.randint(0, 2, (*b, n_templ)),
        "template_pseudo_beta_mask": np.random.randint(0, 2, (*b, n_templ, n)),
        "template_pseudo_beta": np.random.rand(*b, n_templ, n, 3),
        "template_aatype": np.random.randint(0, 22, (*b, n_templ, n)),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
28
        "template_all_atom_mask": np.random.randint(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
29
30
            0, 2, (*b, n_templ, n, 37)
        ),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
31
32
33
34
35
36
37
38
        "template_all_atom_positions": 
            np.random.rand(*b, n_templ, n, 37, 3) * 10,
        "template_torsion_angles_sin_cos": 
            np.random.rand(*b, n_templ, n, 7, 2),
        "template_alt_torsion_angles_sin_cos": 
            np.random.rand(*b, n_templ, n, 7, 2),
        "template_torsion_angles_mask": 
            np.random.rand(*b, n_templ, n, 7),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
39
    }
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
40
    batch = {k: v.astype(np.float32) for k, v in batch.items()}
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
41
42
43
    batch["template_aatype"] = batch["template_aatype"].astype(np.int64)
    return batch

44

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
45
46
def random_extra_msa_feats(n_extra, n, batch_size=None):
    b = []
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
47
    if batch_size is not None:
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
48
49
        b.append(batch_size)
    batch = {
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
50
51
52
53
54
55
56
57
58
59
60
61
        "extra_msa": np.random.randint(0, 22, (*b, n_extra, n)).astype(
            np.int64
        ),
        "extra_has_deletion": np.random.randint(0, 2, (*b, n_extra, n)).astype(
            np.float32
        ),
        "extra_deletion_value": np.random.rand(*b, n_extra, n).astype(
            np.float32
        ),
        "extra_msa_mask": np.random.randint(0, 2, (*b, n_extra, n)).astype(
            np.float32
        ),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
62
63
    }
    return batch
64
65


66
def random_affines_vector(dim):
67
68
69
70
    prod_dim = 1
    for d in dim:
        prod_dim *= d

71
    affines = np.zeros((prod_dim, 7)).astype(np.float32)
72
73
74

    for i in range(prod_dim):
        affines[i, :4] = Rotation.random(random_state=42).as_quat()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
75
76
77
        affines[i, 4:] = np.random.rand(
            3,
        ).astype(np.float32)
78
79
80
81

    return affines.reshape(*dim, 7)


82
def random_affines_4x4(dim):
83
84
85
86
    prod_dim = 1
    for d in dim:
        prod_dim *= d

87
    affines = np.zeros((prod_dim, 4, 4)).astype(np.float32)
88
89
90

    for i in range(prod_dim):
        affines[i, :3, :3] = Rotation.random(random_state=42).as_matrix()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
91
92
93
        affines[i, :3, 3] = np.random.rand(
            3,
        ).astype(np.float32)
94
95
96
97

    affines[:, 3, 3] = 1

    return affines.reshape(*dim, 4, 4)