-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilSeq.py
More file actions
111 lines (73 loc) · 2.27 KB
/
Copy pathutilSeq.py
File metadata and controls
111 lines (73 loc) · 2.27 KB
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
"""A set of common functions related to sequence analysis.
Functions:
seqRev(s) --- reverse a DNA/RNA sequence.
seqComp(s) --- complement a DNA/RNA sequence.
seqRC(s) --- reverse complement a DNA/RNA sequence.
isWC(b1, b2) --- check if the two bases form a canonical watson-crick pair.
matchUp(top, bottom) --- match the top strand to the bottom.
"""
import re
def isWC(b1, b2):
"""Check if the two bases form a canonical watson-crick pair.
Parameters:
b1 : str --- a DNA/RNA base
b2 : str --- a DNA/RNA base
Returns:
A boolean. True when they form a pair; False when not.
"""
pair=''.join(sorted((b1+b2).upper()))
return True if pair=='CG' or pair=="AT" else False
def matchUp(top, bottom):
"""Match the top strand to the bottom.
Generate a string for the match, where
a match is denoted by '|' and mismatch by 'x'
Parameters:
top : str --- top strand in 5'->3' orientation
bottom : str --- bottom strand in 3'->5' orientation
Returns:
Three lines of text as shown in the following example.
5' CGCAGT 3'
|||x||
3' GCGACA 5'
"""
match=['|' if isWC(top[i], bottom[i]) else 'x'
for i in range(len(top))]
# turn into strings
top=''.join(top)
match=''.join(match)
bottom=''.join(bottom)
return f"5'{top}3'\n {match} \n3'{bottom}5'"
def seqRev(s):
"""Reverse a DNA/RNA sequence.
Parameters:
s : str --- input sequence.
Note:
The sequences are case sensitive.
Returns:
A string reversed.
"""
s_t="".join(reversed(s))
return s_t
def seqComp(s):
"""Complement a DNA/RNA sequence.
Parameters:
s : str --- input sequence.
Note:
The sequences are case sensitive.
Returns:
A string complementary to the input.
"""
be="ACGTUacgtu"
af="TGCAAtgcaa"
s_t=s.translate(str.maketrans(be, af))
return s_t
def seqRC(s):
"""Reverse complement a DNA/RNA sequence.
Parameters:
s : str --- input sequence.
Note:
The sequences are case sensitive.
Returns:
A string reverse complementary to the input.
"""
return seqComp(seqRev(s))