Spaces:
Running
Running
Update SPM/mcp_output/mcp_plugin/mcp_service.py
Browse files
SPM/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -14,7 +14,7 @@ mcp = FastMCP("sequence_pattern_matching_service")
|
|
| 14 |
@mcp.tool(name="sequence_pattern_matching", description="Perform sequence pattern matching for target protein alignment")
|
| 15 |
def sequence_pattern_matching(input_sequence: str, target_sequence: str) -> dict:
|
| 16 |
"""
|
| 17 |
-
Perform sequence pattern matching for target protein alignment.
|
| 18 |
|
| 19 |
Parameters:
|
| 20 |
input_sequence (str): The input sequence to be matched.
|
|
@@ -28,37 +28,101 @@ def sequence_pattern_matching(input_sequence: str, target_sequence: str) -> dict
|
|
| 28 |
|
| 29 |
import numpy as np
|
| 30 |
|
| 31 |
-
# Convert sequences to volume arrays
|
| 32 |
query_seq_volume = np.array([volume[i] for i in input_sequence])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
best_score = score
|
| 45 |
-
best_position = i
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
result = {
|
| 49 |
"input_sequence": input_sequence,
|
| 50 |
-
"
|
| 51 |
"best_score": float(best_score),
|
| 52 |
"best_position": int(best_position),
|
| 53 |
-
"matched_region":
|
| 54 |
"query_length": query_len,
|
| 55 |
-
"target_length": len(target_sequence)
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
return {"success": True, "result": result, "error": None}
|
| 59 |
except Exception as e:
|
| 60 |
return {"success": False, "result": None, "error": str(e)}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Create the application
|
| 63 |
def create_app() -> FastMCP:
|
| 64 |
"""
|
|
|
|
| 14 |
@mcp.tool(name="sequence_pattern_matching", description="Perform sequence pattern matching for target protein alignment")
|
| 15 |
def sequence_pattern_matching(input_sequence: str, target_sequence: str) -> dict:
|
| 16 |
"""
|
| 17 |
+
Perform sequence pattern matching for target protein alignment using the real SPM algorithm.
|
| 18 |
|
| 19 |
Parameters:
|
| 20 |
input_sequence (str): The input sequence to be matched.
|
|
|
|
| 28 |
|
| 29 |
import numpy as np
|
| 30 |
|
| 31 |
+
# Convert sequences to volume arrays using the real volume dictionary from SPM
|
| 32 |
query_seq_volume = np.array([volume[i] for i in input_sequence])
|
| 33 |
|
| 34 |
+
# Use the real volumeScoring function from SPM
|
| 35 |
+
# Create a mock uniprot_info for the target sequence
|
| 36 |
+
uniprot_info = f">target_sequence|length_{len(target_sequence)}"
|
| 37 |
|
| 38 |
+
# Call the real volumeScoring function
|
| 39 |
+
result_data = volumeScoring(query_seq_volume, uniprot_info, target_sequence)
|
| 40 |
|
| 41 |
+
# Extract results
|
| 42 |
+
best_score = result_data[1] # score
|
| 43 |
+
best_position = result_data[2] # position (0-indexed)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Get the matched region
|
| 46 |
+
query_len = len(input_sequence)
|
| 47 |
+
matched_region = target_sequence[best_position:best_position + query_len]
|
| 48 |
+
|
| 49 |
+
# Create comprehensive result
|
| 50 |
result = {
|
| 51 |
"input_sequence": input_sequence,
|
| 52 |
+
"target_sequence_preview": target_sequence[:100] + "..." if len(target_sequence) > 100 else target_sequence,
|
| 53 |
"best_score": float(best_score),
|
| 54 |
"best_position": int(best_position),
|
| 55 |
+
"matched_region": matched_region,
|
| 56 |
"query_length": query_len,
|
| 57 |
+
"target_length": len(target_sequence),
|
| 58 |
+
"uniprot_info": result_data[0],
|
| 59 |
+
"volume_difference": float(best_score),
|
| 60 |
+
"algorithm": "SPM Volume-based Pattern Matching"
|
| 61 |
}
|
| 62 |
|
| 63 |
return {"success": True, "result": result, "error": None}
|
| 64 |
except Exception as e:
|
| 65 |
return {"success": False, "result": None, "error": str(e)}
|
| 66 |
|
| 67 |
+
@mcp.tool(name="spm_database_search", description="Search query sequence against a FASTA database using SPM algorithm")
|
| 68 |
+
def spm_database_search(query_sequence: str, database_sequences: list) -> dict:
|
| 69 |
+
"""
|
| 70 |
+
Search query sequence against multiple database sequences using SPM algorithm.
|
| 71 |
+
|
| 72 |
+
Parameters:
|
| 73 |
+
query_sequence (str): The query sequence to search for.
|
| 74 |
+
database_sequences (list): List of dictionaries with 'name' and 'sequence' keys.
|
| 75 |
+
|
| 76 |
+
Returns:
|
| 77 |
+
dict: A dictionary containing success, result, or error fields.
|
| 78 |
+
"""
|
| 79 |
+
try:
|
| 80 |
+
from scripts.SequencePatternMatching import volumeScoring, volume
|
| 81 |
+
|
| 82 |
+
import numpy as np
|
| 83 |
+
|
| 84 |
+
# Convert query sequence to volume array
|
| 85 |
+
query_seq_volume = np.array([volume[i] for i in query_sequence])
|
| 86 |
+
|
| 87 |
+
# Perform search against all database sequences
|
| 88 |
+
results = []
|
| 89 |
+
|
| 90 |
+
for db_entry in database_sequences:
|
| 91 |
+
name = db_entry.get('name', 'unknown')
|
| 92 |
+
sequence = db_entry.get('sequence', '')
|
| 93 |
+
|
| 94 |
+
if not sequence:
|
| 95 |
+
continue
|
| 96 |
+
|
| 97 |
+
# Use the real volumeScoring function
|
| 98 |
+
result_data = volumeScoring(query_seq_volume, name, sequence)
|
| 99 |
+
|
| 100 |
+
results.append({
|
| 101 |
+
'name': result_data[0],
|
| 102 |
+
'score': float(result_data[1]),
|
| 103 |
+
'position': int(result_data[2]),
|
| 104 |
+
'matched_region': sequence[result_data[2]:result_data[2] + len(query_sequence)],
|
| 105 |
+
'sequence_length': len(sequence)
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
# Sort results by score (lower is better)
|
| 109 |
+
results.sort(key=lambda x: x['score'])
|
| 110 |
+
|
| 111 |
+
return {
|
| 112 |
+
"success": True,
|
| 113 |
+
"result": {
|
| 114 |
+
"query_sequence": query_sequence,
|
| 115 |
+
"total_searched": len(database_sequences),
|
| 116 |
+
"total_matches": len(results),
|
| 117 |
+
"top_matches": results[:10], # Top 10 matches
|
| 118 |
+
"best_match": results[0] if results else None
|
| 119 |
+
},
|
| 120 |
+
"error": None
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 125 |
+
|
| 126 |
# Create the application
|
| 127 |
def create_app() -> FastMCP:
|
| 128 |
"""
|