File size: 9,479 Bytes
4d12519 | 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 |
import json
from pytorch_lightning import LightningDataModule
from torch.utils.data import DataLoader, ConcatDataset, Dataset
from data_provider.stage1_dm import SwissProtDataset, OntoProteinDataset
import pandas as pd
class Antibiotic_Resistance(Dataset):
def __init__(self, data_path, prompt='', return_prompt=False):
super(Antibiotic_Resistance, self).__init__()
self.data_path = data_path
self.user_prompt = prompt
self.return_prompt = return_prompt
self.data_list = self._load_and_preprocess(self.data_path)
self.text2id = self._build_text_vocab()
def _load_and_preprocess(self, data_path):
data_list = []
df = pd.read_csv(data_path)
for _, row in df.iterrows():
try:
prot_seq = str(row['aa_seq']).strip()
result = str(row['label']).strip()
text_seq = f"<answer>{result}</answer>\n"
prompt = f"""
【Task】Predict the antibiotic resistance class of the given protein.
【Background】Antibiotic resistance refers to the ability of bacteria or other microbes to resist the effects of antibiotics that were once effective against them. Each protein is associated with resistance to exactly one of 19 antibiotic classes.
【Prediction Goal】Based on the provided protein sequence, determine which single antibiotic class (from 1 to 19) this protein confers resistance to.
【Output Format】Return only one predicted resistance class (a number from 1 to 19), wrapped in <answer> </answer> tags.
Example: <answer>7</answer>
"""
if self.user_prompt:
prompt += self.user_prompt
# extra可以返回原始feather字符串,也可以返回feather_vals
# 或 feather_raw
data_list.append((prot_seq, text_seq, prompt))
except Exception as e:
print(f"警告: 跳过有问题的行: {row},原因: {e}")
return data_list
def _build_text_vocab(self):
text2id = {}
for _, text_seq, _ in self.data_list:
if text_seq not in text2id:
text2id[text_seq] = len(text2id)
return text2id
def shuffle(self):
random.shuffle(self.data_list)
return self
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
prot_seq, text_seq, prompt = self.data_list[index]
if self.return_prompt:
return prot_seq, prompt, text_seq,index
return prot_seq, text_seq, index
class Thermostability(Dataset):
def __init__(self, data_path, prompt='', return_prompt=False):
super(Thermostability, self).__init__()
self.data_path = data_path
self.user_prompt = prompt
self.return_prompt = return_prompt
self.data_list = self._load_and_preprocess(self.data_path)
self.text2id = self._build_text_vocab()
def _load_and_preprocess(self, data_path):
data_list = []
df = pd.read_csv(data_path)
for _, row in df.iterrows():
try:
#name = str(row['name']).strip()
prot_seq = str(row['aa_seq']).strip()
result = str(row['label']).strip()
text_seq = f"<answer>{result}</answer>\n"
prompt = f"""
【Task】Predict the thermostability value of the given protein.
【Background】Thermostability refers to the ability of a molecule to resist irreversible chemical or physical changes at high temperatures, such as decomposition or aggregation.
【Output Format】Provide the predicted thermostability as a numeric value (e.g., melting temperature in °C). Wrap your answer in <answer></answer> tags.
"""
if self.user_prompt:
prompt += self.user_prompt
# extra可以返回原始feather字符串,也可以返回feather_vals
# 或 feather_raw
data_list.append((prot_seq, text_seq, prompt))
except Exception as e:
print(f"警告: 跳过有问题的行: {row},原因: {e}")
return data_list
def _build_text_vocab(self):
text2id = {}
for _, text_seq, _ in self.data_list:
if text_seq not in text2id:
text2id[text_seq] = len(text2id)
return text2id
def shuffle(self):
random.shuffle(self.data_list)
return self
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
prot_seq, text_seq, prompt = self.data_list[index]
if self.return_prompt:
return prot_seq, prompt, text_seq,index
return prot_seq, text_seq, index
class Material(Dataset):
def __init__(self, data_path, prompt='', return_prompt=False):
super(Material, self).__init__()
self.data_path = data_path
self.user_prompt = prompt
self.return_prompt = return_prompt
self.data_list = self._load_and_preprocess(self.data_path)
self.text2id = self._build_text_vocab()
def _load_and_preprocess(self, data_path):
data_list = []
df = pd.read_csv(data_path)
for _, row in df.iterrows():
try:
#name = str(row['name']).strip()
prot_seq = str(row['aa_seq']).strip()
result = str(row['label']).strip()
text_seq = f"<answer>{result}</answer>\n"
prompt = f"""
【Task】Determine whether the given material can be successfully produced.
【Background】In materials science, certain chemical compounds or materials may or may not be synthesizable (i.e., producible) under realistic experimental conditions. This task requires classifying whether the input material composition and structure allow for successful production. This is a binary classification problem.
【Question】Can this material be successfully produced?
【Output Format】Respond with either "1" or "0", and wrap your answer in <answer></answer> tags.
"""
if self.user_prompt:
prompt += self.user_prompt
# extra可以返回原始feather字符串,也可以返回feather_vals
# 或 feather_raw
data_list.append((prot_seq, text_seq, prompt))
except Exception as e:
print(f"警告: 跳过有问题的行: {row},原因: {e}")
return data_list
def _build_text_vocab(self):
text2id = {}
for _, text_seq, _ in self.data_list:
if text_seq not in text2id:
text2id[text_seq] = len(text2id)
return text2id
def shuffle(self):
random.shuffle(self.data_list)
return self
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
prot_seq, text_seq, prompt = self.data_list[index]
if self.return_prompt:
return prot_seq, prompt, text_seq,index
return prot_seq, text_seq, index
class Clone(Dataset):
def __init__(self, data_path, prompt='', return_prompt=False):
super(Clone, self).__init__()
self.data_path = data_path
self.user_prompt = prompt
self.return_prompt = return_prompt
self.data_list = self._load_and_preprocess(self.data_path)
self.text2id = self._build_text_vocab()
def _load_and_preprocess(self, data_path):
data_list = []
df = pd.read_csv(data_path)
for _, row in df.iterrows():
try:
#name = str(row['name']).strip()
prot_seq = str(row['aa_seq']).strip()
result = str(row['label']).strip()
text_seq = f"<answer>{result}</answer>\n"
prompt = f"""
【Task】Determine whether the given protein sequence can be successfully cloned.
【Background】In molecular biology, cloning refers to the process of creating copies of a DNA or protein sequence. Some sequences can be challenging to clone due to their length, GC-content, secondary structures, or toxicity to the host. This task requires predicting whether the given protein sequence is likely to be successfully cloned. This is a binary classification problem.
【Question】Can this protein sequence be successfully cloned?
【Output Format】Respond with either "1" or "0", and wrap your answer in <answer></answer> tags.
"""
if self.user_prompt:
prompt += self.user_prompt
# extra可以返回原始feather字符串,也可以返回feather_vals
# 或 feather_raw
data_list.append((prot_seq, text_seq, prompt))
except Exception as e:
print(f"警告: 跳过有问题的行: {row},原因: {e}")
return data_list
def _build_text_vocab(self):
text2id = {}
for _, text_seq, _ in self.data_list:
if text_seq not in text2id:
text2id[text_seq] = len(text2id)
return text2id
def shuffle(self):
random.shuffle(self.data_list)
return self
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
prot_seq, text_seq, prompt = self.data_list[index]
if self.return_prompt:
return prot_seq, prompt, text_seq,index
return prot_seq, text_seq, index
|