Update README.md
Browse files
README.md
CHANGED
|
@@ -47,50 +47,74 @@ Here is an example of how to use PDeepPP to process protein sequences and obtain
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import torch
|
| 50 |
-
import
|
|
|
|
|
|
|
| 51 |
from transformers import AutoModel
|
| 52 |
-
from processing_pdeeppp import PDeepPPProcessor
|
| 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 |
model.eval()
|
| 92 |
-
outputs = model(
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
```
|
| 95 |
|
| 96 |
## Training and customization
|
|
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import torch
|
| 50 |
+
import esm
|
| 51 |
+
from DataProcessor_pdeeppp import PDeepPPProcessor
|
| 52 |
+
from Pretraining_pdeeppp import PretrainingPDeepPP
|
| 53 |
from transformers import AutoModel
|
|
|
|
| 54 |
|
| 55 |
+
# Global parameter settings
|
| 56 |
+
device = torch.device("cpu")
|
| 57 |
+
pad_char = "X" # Padding character
|
| 58 |
+
target_length = 33 # Target length for sequence padding
|
| 59 |
+
mode = "BPS" # Mode setting (only configured in example.py)
|
| 60 |
+
esm_ratio = 1 # Ratio for ESM embeddings
|
| 61 |
+
|
| 62 |
+
# Initialize the PDeepPPProcessor
|
| 63 |
+
processor = PDeepPPProcessor(pad_char=pad_char, target_length=target_length)
|
| 64 |
+
|
| 65 |
+
# Example protein sequences (test sequences)
|
| 66 |
+
protein_sequences = ["VELYP", "YPLDL", "ESHINQKWVCK"]
|
| 67 |
+
|
| 68 |
+
# Preprocess the sequences
|
| 69 |
+
inputs = processor(sequences=protein_sequences, mode=mode, return_tensors="pt") # Dynamic mode parameter
|
| 70 |
+
processed_sequences = inputs["raw_sequences"]
|
| 71 |
+
|
| 72 |
+
# Load the ESM model
|
| 73 |
+
esm_model, esm_alphabet = esm.pretrained.esm2_t33_650M_UR50D()
|
| 74 |
+
esm_model = esm_model.to(device)
|
| 75 |
+
esm_model.eval()
|
| 76 |
+
|
| 77 |
+
# Initialize the PretrainingPDeepPP module
|
| 78 |
+
pretrainer = PretrainingPDeepPP(
|
| 79 |
+
embedding_dim=1280,
|
| 80 |
+
target_length=target_length,
|
| 81 |
+
esm_ratio=esm_ratio,
|
| 82 |
+
device=device
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Extract the vocabulary and ensure the padding character 'X' is included
|
| 86 |
+
vocab = set("".join(protein_sequences))
|
| 87 |
+
vocab.add(pad_char) # Add the padding character
|
| 88 |
+
|
| 89 |
+
# Generate pretrained features using the PretrainingPDeepPP module
|
| 90 |
+
pretrained_features = pretrainer.create_embeddings(
|
| 91 |
+
processed_sequences, vocab, esm_model, esm_alphabet
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Ensure pretrained features are on the same device
|
| 95 |
+
inputs["input_embeds"] = pretrained_features.to(device)
|
| 96 |
+
|
| 97 |
+
# Load the PDeepPP model
|
| 98 |
+
model_name = "fondress/PDeepPP_ACE"
|
| 99 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True) # Directly load the model
|
| 100 |
+
|
| 101 |
+
# Perform prediction
|
| 102 |
model.eval()
|
| 103 |
+
outputs = model(input_embeds=inputs["input_embeds"]) # Use pretrained features as model input
|
| 104 |
+
logits = outputs["logits"]
|
| 105 |
+
|
| 106 |
+
# Compute probability distributions and generate predictions
|
| 107 |
+
softmax = torch.nn.Softmax(dim=-1) # Apply softmax on the last dimension
|
| 108 |
+
probabilities = softmax(logits)
|
| 109 |
+
predicted_labels = (probabilities >= 0.5).long()
|
| 110 |
+
|
| 111 |
+
# Print the prediction results for each sequence
|
| 112 |
+
print("\nPrediction Results:")
|
| 113 |
+
for i, seq in enumerate(processed_sequences):
|
| 114 |
+
print(f"Sequence: {seq}")
|
| 115 |
+
print(f"Probability: {probabilities[i].item():.4f}")
|
| 116 |
+
print(f"Predicted Label: {predicted_labels[i].item()}")
|
| 117 |
+
print("-" * 50)
|
| 118 |
```
|
| 119 |
|
| 120 |
## Training and customization
|