File size: 10,550 Bytes
9ba7cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f5cc9
9ba7cd5
 
a8f5cc9
9ba7cd5
a8f5cc9
9ba7cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f5cc9
9ba7cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f5cc9
9ba7cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f5cc9
9ba7cd5
 
a8f5cc9
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#-*- coding:utf-8 -*-

# import sys, os, shutil, re, logging, subprocess, string, io, argparse, bisect, concurrent, gzip, zipfile, tarfile, json, pickle, time, datetime, random, math, copy, itertools, functools, collections, multiprocessing, threading, queue, signal, inspect, warnings, distutils.spawn
import sys
import os
import pickle
import re
import torch
import random
import gzip
from os.path import exists, join, getsize, isfile, isdir, abspath, basename
from typing import Dict, Union, Optional, List, Tuple, Mapping
import numpy as np
import pandas as pd
from tqdm.auto import trange, tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Dict, Union, Optional, List, Tuple, Mapping
import datasets

def get_md5(aa_str):
    """
    Calculate MD5 values for protein sequence
    """
    import hashlib
    assert isinstance(aa_str, str), aa_str

    aa_str = aa_str.upper()
    return hashlib.md5(aa_str.encode('utf-8')).hexdigest()

def load_fasta(seqFn, rem_tVersion=False, load_annotation=False, full_line_as_id=False):
    """
    seqFn               -- Fasta file or input handle (with readline implementation)
    rem_tVersion        -- Remove version information. ENST000000022311.2 => ENST000000022311
    load_annotation     -- Load sequence annotation
    full_line_as_id     -- Use the full head line (starts with >) as sequence ID. Can not be specified simutanouly with load_annotation

    Return:
        {tid1: seq1, ...} if load_annotation==False
        {tid1: seq1, ...},{tid1: annot1, ...} if load_annotation==True
    """
    if load_annotation and full_line_as_id:
        raise RuntimeError("Error: load_annotation and full_line_as_id can not be specified simutanouly")
    if rem_tVersion and full_line_as_id:
        raise RuntimeError("Error: rem_tVersion and full_line_as_id can not be specified simutanouly")

    fasta = {}
    annotation = {}
    cur_tid = ''
    cur_seq = ''
    
    if isinstance(seqFn, str):
        IN = open(seqFn)
    elif hasattr(seqFn, 'readline'):
        IN = seqFn
    else:
        raise RuntimeError(f"Expected seqFn: {type(seqFn)}")
    for line in IN:
        if line[0] == '>':
            if cur_tid != '':
                fasta[cur_tid] = re.sub(r"\s", "", cur_seq)
                cur_seq = ''
            data = line[1:-1].split(None, 1)
            cur_tid = line[1:-1] if full_line_as_id else data[0]
            annotation[cur_tid] = data[1] if len(data)==2 else ""
            if rem_tVersion and '.' in cur_tid: 
                cur_tid = ".".join(cur_tid.split(".")[:-1])
        elif cur_tid != '':
            cur_seq += line.rstrip()
    
    if isinstance(seqFn, str):
        IN.close()

    if cur_seq != '':
        fasta[cur_tid] = re.sub(r"\s", "", cur_seq)
    
    if load_annotation:
        return fasta, annotation
    else:
        return fasta

def load_msa_txt(file_or_stream, load_id=False, load_annot=False, sort=False):
    """
    Read msa txt file
    
    Parmeters
    --------------
    file_or_stream: file or stream to read (with read method)
    load_id: read identity and return
    
    Return
    --------------
    msa: list of msa sequences, the first sequence in msa is the query sequence
    id_arr: Identity of msa sequences
    annotations: Annotations of msa sequences
    """
    msa = []
    id_arr = []
    annotations = []
    
    if hasattr(file_or_stream, 'read'):
        lines = file_or_stream.read().strip().split('\n')
    elif file_or_stream.endswith('.gz'):
        with gzip.open(file_or_stream) as IN:
            lines = IN.read().decode().strip().split('\n')
    else:
        with open(file_or_stream) as IN:
            lines = IN.read().strip().split('\n')
        # lines = open(file_or_stream).read().strip().split('\n')
    
    for idx,line in enumerate(lines):
        data = line.strip().split()
        if idx == 0:
            assert len(data) == 1, f"Expect 1 element for the 1st line, but got {data} in {file_or_stream}"
            q_seq = data[0]
        else:
            if len(data) >= 2:
                id_arr.append( float(data[1]) )
            else:
                assert len(q_seq) == len(data[0])
                id_ = round(np.mean([ r1==r2 for r1,r2 in zip(q_seq, data[0]) ]), 3)
                id_arr.append(id_)
            msa.append( data[0] )
            if len(data) >= 3:
                annot = " ".join(data[2:])
                annotations.append( annot )
            else:
                annotations.append(None)
    
    id_arr = np.array(id_arr, dtype=np.float64)
    if sort:
        id_order = np.argsort(id_arr)[::-1]
        msa      = [ msa[i] for i in id_order ]
        id_arr   = id_arr[id_order]
        annotations = [ annotations[i] for i in id_order ]
    msa = [q_seq] + msa
    
    outputs = [ msa ]
    if load_id:
        outputs.append( id_arr )
    if load_annot:
        outputs.append( annotations )
    if len(outputs) == 1:
        return outputs[0]
    return outputs

# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """
"""

# You can copy an official description
_DESCRIPTION = """
Contact map prediction aims to determine whether two residues, $i$ and $j$, are in contact or not, based on their distance with a certain threshold ($<$8 Angstrom). This task is an important part of the early Alphafold version for structural prediction. 
"""

_HOMEPAGE = "https://huggingface.co/datasets/genbio-ai/contact_prediction_binary_rag"

_LICENSE = "Apache license 2.0"

class DownStreamConfig(datasets.BuilderConfig):
    """BuilderConfig for downstream taks dataset."""

    def __init__(self, *args, **kwargs):
        """BuilderConfig downstream tasks dataset.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(*args, name=f"downstream", **kwargs)

class DownStreamTasks(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.1.0")
    BUILDER_CONFIG_CLASS = DownStreamConfig
    BUILDER_CONFIGS = [ DownStreamConfig() ]
    DEFAULT_CONFIG_NAME = None

    def _info(self):
        features = datasets.Features(
            {
                "seq":         datasets.Value("string"),
                "label":       datasets.Array2D(shape=(None, 2), dtype='int32'),
                "msa":         datasets.Sequence(datasets.Value("string")),
                "str_emb":     datasets.Array2D(shape=(None, 384), dtype='float32'),
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(
        self, dl_manager: datasets.DownloadManager
    ) -> List[datasets.SplitGenerator]:
        train_parquet_file  = dl_manager.download(f"data/train-00000-of-00001.parquet")
        valid_parquet_file  = dl_manager.download(f"data/valid-00000-of-00001.parquet")
        test_parquet_file   = dl_manager.download(f"data/test-00000-of-00001.parquet")
        msa_path            = dl_manager.download_and_extract(f"msa.tar")
        # msa_path            = dl_manager.download(f"msa")
        str_file            = dl_manager.download(f"md5_to_str.fasta")
        codebook_file       = dl_manager.download(f"codebook.pt")

        assert os.path.exists(join(msa_path, 'msa'))
        msa_path = join(msa_path, 'msa')

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, 
                gen_kwargs={
                            "parquet_file": train_parquet_file,
                            "msa_path": msa_path, 
                            "str_file": str_file, 
                            "codebook_file": codebook_file
                            }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, 
                gen_kwargs={
                            "parquet_file": valid_parquet_file,
                            "msa_path": msa_path, 
                            "str_file": str_file, 
                            "codebook_file": codebook_file
                            }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, 
                gen_kwargs={
                            "parquet_file": test_parquet_file,
                            "msa_path": msa_path, 
                            "str_file": str_file, 
                            "codebook_file": codebook_file
                            }
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, parquet_file, msa_path, str_file, codebook_file):
        
        dataset = datasets.Dataset.from_parquet(parquet_file)
        md5_to_str = load_fasta(str_file)
        codebook = torch.load(codebook_file, 'cpu', weights_only=True).numpy()
        
        for key, item in enumerate(dataset):
            seq   = item['seq']
            label = item['label']
            md5_val = get_md5(seq)
            if md5_val not in md5_to_str or md5_to_str[md5_val] == "":
                str_emb  = np.zeros([len(seq), 384], dtype=np.float32)
            else:
                str_toks = np.array([ int(x) for x in md5_to_str[md5_val].split('-')])
                str_emb  = codebook[str_toks]
            
            msa = load_msa_txt(join(msa_path, md5_val+'.txt.gz'))
            assert len(msa[0]) == len(seq), f"Error: {len(msa[0])} != {len(seq)}"
            assert len(msa[0]) == str_emb.shape[0], f"Error: {len(msa[0])} != {str_emb.shape[0]}"
            # breakpoint()
            yield key, {
                "seq":     seq,
                "label":   label,
                "msa":     msa,
                "str_emb": str_emb
            }

    def _as_dataset(
        self,
        split: Optional[datasets.Split] = None,
        **kwargs
    ) -> datasets.Dataset:
        dataset = super()._as_dataset(split=split, **kwargs)
        dataset.set_format(
            type="numpy",
            columns=["label", "str_emb"],
            output_all_columns=True
        )
        return dataset