File size: 2,574 Bytes
d53784b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import json

def find_mutations(reference, sample):
    mutations = []

    i, j = 0, 0
    pos = 1  # 1-based position for biology

    while i < len(reference) and j < len(sample):
        if reference[i] == sample[j]:
            i += 1
            j += 1
            pos += 1
        else:
            # Substitution
            if i + 1 < len(reference) and j + 1 < len(sample) and reference[i+1] == sample[j+1]:
                mutations.append({
                    "type": "substitution",
                    "position": pos,
                    "reference": reference[i],
                    "observed": sample[j]
                })
                i += 1
                j += 1
                pos += 1
            # Insertion
            elif j + 1 < len(sample) and reference[i] == sample[j+1]:
                mutations.append({
                    "type": "insertion",
                    "position": pos,
                    "inserted": sample[j]
                })
                j += 1
                pos += 1
            # Deletion
            elif i + 1 < len(reference) and reference[i+1] == sample[j]:
                mutations.append({
                    "type": "deletion",
                    "position": pos,
                    "deleted": reference[i]
                })
                i += 1
                pos += 1
            else:
                mutations.append({
                    "type": "substitution",
                    "position": pos,
                    "reference": reference[i],
                    "observed": sample[j]
                })
                i += 1
                j += 1
                pos += 1

    while j < len(sample):
        mutations.append({
            "type": "insertion",
            "position": pos,
            "inserted": sample[j]
        })
        j += 1
        pos += 1

    while i < len(reference):
        mutations.append({
            "type": "deletion",
            "position": pos,
            "deleted": reference[i]
        })
        i += 1
        pos += 1

    return mutations


if __name__ == "__main__":
    data = json.loads(sys.stdin.read())

    reference = data["reference"].upper()
    sample = data["sample"].upper()

    result = find_mutations(reference, sample)

    message = "No mutation found. Sequence matches reference."
    if len(result) > 0:
        message = f"Mutation found! Total mutations detected: {len(result)}"

    print(json.dumps({
        "message": message,
        "total_mutations": len(result),
        "mutations": result
    }))