Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +26 -0
- mutation_analyzer.py +93 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from mutation_analyzer import find_mutations
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
class DNAInput(BaseModel):
|
| 8 |
+
reference: str
|
| 9 |
+
sample: str
|
| 10 |
+
|
| 11 |
+
@app.post("/analyze")
|
| 12 |
+
def analyze_dna(data: DNAInput):
|
| 13 |
+
reference = data.reference.upper()
|
| 14 |
+
sample = data.sample.upper()
|
| 15 |
+
|
| 16 |
+
mutations = find_mutations(reference, sample)
|
| 17 |
+
|
| 18 |
+
message = "No mutation found. Sequence matches reference."
|
| 19 |
+
if len(mutations) > 0:
|
| 20 |
+
message = f"Mutation found! Total mutations detected: {len(mutations)}"
|
| 21 |
+
|
| 22 |
+
return {
|
| 23 |
+
"message": message,
|
| 24 |
+
"total_mutations": len(mutations),
|
| 25 |
+
"mutations": mutations
|
| 26 |
+
}
|
mutation_analyzer.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def find_mutations(reference, sample):
|
| 5 |
+
mutations = []
|
| 6 |
+
|
| 7 |
+
i, j = 0, 0
|
| 8 |
+
pos = 1 # 1-based position for biology
|
| 9 |
+
|
| 10 |
+
while i < len(reference) and j < len(sample):
|
| 11 |
+
if reference[i] == sample[j]:
|
| 12 |
+
i += 1
|
| 13 |
+
j += 1
|
| 14 |
+
pos += 1
|
| 15 |
+
else:
|
| 16 |
+
# Substitution
|
| 17 |
+
if i + 1 < len(reference) and j + 1 < len(sample) and reference[i+1] == sample[j+1]:
|
| 18 |
+
mutations.append({
|
| 19 |
+
"type": "substitution",
|
| 20 |
+
"position": pos,
|
| 21 |
+
"reference": reference[i],
|
| 22 |
+
"observed": sample[j]
|
| 23 |
+
})
|
| 24 |
+
i += 1
|
| 25 |
+
j += 1
|
| 26 |
+
pos += 1
|
| 27 |
+
# Insertion
|
| 28 |
+
elif j + 1 < len(sample) and reference[i] == sample[j+1]:
|
| 29 |
+
mutations.append({
|
| 30 |
+
"type": "insertion",
|
| 31 |
+
"position": pos,
|
| 32 |
+
"inserted": sample[j]
|
| 33 |
+
})
|
| 34 |
+
j += 1
|
| 35 |
+
pos += 1
|
| 36 |
+
# Deletion
|
| 37 |
+
elif i + 1 < len(reference) and reference[i+1] == sample[j]:
|
| 38 |
+
mutations.append({
|
| 39 |
+
"type": "deletion",
|
| 40 |
+
"position": pos,
|
| 41 |
+
"deleted": reference[i]
|
| 42 |
+
})
|
| 43 |
+
i += 1
|
| 44 |
+
pos += 1
|
| 45 |
+
else:
|
| 46 |
+
mutations.append({
|
| 47 |
+
"type": "substitution",
|
| 48 |
+
"position": pos,
|
| 49 |
+
"reference": reference[i],
|
| 50 |
+
"observed": sample[j]
|
| 51 |
+
})
|
| 52 |
+
i += 1
|
| 53 |
+
j += 1
|
| 54 |
+
pos += 1
|
| 55 |
+
|
| 56 |
+
while j < len(sample):
|
| 57 |
+
mutations.append({
|
| 58 |
+
"type": "insertion",
|
| 59 |
+
"position": pos,
|
| 60 |
+
"inserted": sample[j]
|
| 61 |
+
})
|
| 62 |
+
j += 1
|
| 63 |
+
pos += 1
|
| 64 |
+
|
| 65 |
+
while i < len(reference):
|
| 66 |
+
mutations.append({
|
| 67 |
+
"type": "deletion",
|
| 68 |
+
"position": pos,
|
| 69 |
+
"deleted": reference[i]
|
| 70 |
+
})
|
| 71 |
+
i += 1
|
| 72 |
+
pos += 1
|
| 73 |
+
|
| 74 |
+
return mutations
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
data = json.loads(sys.stdin.read())
|
| 79 |
+
|
| 80 |
+
reference = data["reference"].upper()
|
| 81 |
+
sample = data["sample"].upper()
|
| 82 |
+
|
| 83 |
+
result = find_mutations(reference, sample)
|
| 84 |
+
|
| 85 |
+
message = "No mutation found. Sequence matches reference."
|
| 86 |
+
if len(result) > 0:
|
| 87 |
+
message = f"Mutation found! Total mutations detected: {len(result)}"
|
| 88 |
+
|
| 89 |
+
print(json.dumps({
|
| 90 |
+
"message": message,
|
| 91 |
+
"total_mutations": len(result),
|
| 92 |
+
"mutations": result
|
| 93 |
+
}))
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|