-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathtf_parser.py
89 lines (69 loc) · 3.94 KB
/
tf_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
TF parser for ProteinNet Records.
"""
__author__ = "Mohammed AlQuraishi"
__copyright__ = "Copyright 2018, Harvard Medical School"
__license__ = "MIT"
import tensorflow as tf
NUM_AAS = 20
NUM_DIMENSIONS = 3
def masking_matrix(mask, name=None):
""" Constructs a masking matrix to zero out pairwise distances due to missing residues or padding.
Args:
mask: 0/1 vector indicating whether a position should be masked (0) or not (1)
Returns:
A square matrix with all 1s except for rows and cols whose corresponding indices in mask are set to 0.
[MAX_SEQ_LENGTH, MAX_SEQ_LENGTH]
"""
with tf.name_scope(name, 'masking_matrix', [mask]) as scope:
mask = tf.convert_to_tensor(mask, name='mask')
mask = tf.expand_dims(mask, 0)
base = tf.ones([tf.size(mask), tf.size(mask)])
matrix_mask = base * mask * tf.transpose(mask)
return matrix_mask
def read_protein(filename_queue, max_length, num_evo_entries=21, name=None):
""" Reads and parses a ProteinNet TF Record.
Primary sequences are mapped onto 20-dimensional one-hot vectors.
Evolutionary sequences are mapped onto num_evo_entries-dimensional real-valued vectors.
Secondary structures are mapped onto ints indicating one of 8 class labels.
Tertiary coordinates are flattened so that there are 3 times as many coordinates as
residues.
Evolutionary, secondary, and tertiary entries are optional.
Args:
filename_queue: TF queue for reading files
max_length: Maximum length of sequence (number of residues) [MAX_LENGTH]. Not a
TF tensor and is thus a fixed value.
Returns:
id: string identifier of record
one_hot_primary: AA sequence as one-hot vectors
evolutionary: PSSM sequence as vectors
secondary: DSSP sequence as int class labels
tertiary: 3D coordinates of structure
matrix_mask: Masking matrix to zero out pairwise distances in the masked regions
pri_length: Length of amino acid sequence
keep: True if primary length is less than or equal to max_length
"""
with tf.name_scope(name, 'read_protein', []) as scope:
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
context, features = tf.parse_single_sequence_example(serialized_example,
context_features={'id': tf.FixedLenFeature((1,), tf.string)},
sequence_features={
'primary': tf.FixedLenSequenceFeature((1,), tf.int64),
'evolutionary': tf.FixedLenSequenceFeature((num_evo_entries,), tf.float32, allow_missing=True),
'secondary': tf.FixedLenSequenceFeature((1,), tf.int64, allow_missing=True),
'tertiary': tf.FixedLenSequenceFeature((NUM_DIMENSIONS,), tf.float32, allow_missing=True),
'mask': tf.FixedLenSequenceFeature((1,), tf.float32, allow_missing=True)})
id_ = context['id'][0]
primary = tf.to_int32(features['primary'][:, 0])
evolutionary = features['evolutionary']
secondary = tf.to_int32(features['secondary'][:, 0])
tertiary = features['tertiary']
mask = features['mask'][:, 0]
pri_length = tf.size(primary)
keep = pri_length <= max_length
one_hot_primary = tf.one_hot(primary, NUM_AAS)
# Generate tertiary masking matrix--if mask is missing then assume all residues are present
mask = tf.cond(tf.not_equal(tf.size(mask), 0), lambda: mask, lambda: tf.ones([pri_length]))
ter_mask = masking_matrix(mask, name='ter_mask')
return id_, one_hot_primary, evolutionary, secondary, tertiary, ter_mask, pri_length, keep