forked from hackerfriendly/ACTG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atcg.py
69 lines (62 loc) · 1.44 KB
/
atcg.py
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
'''
atcg.py
Reverse complement plugin for Sublime Text 2
'''
import sublime, sublime_plugin
class BaseCommand(sublime_plugin.TextCommand):
def __init__(self, view):
self.view = view
def run(self, edit):
view = self.view
sels = view.sel()
if len(sels) > 1 or not sels[0].empty():
for sel in view.sel():
if not sel.empty():
s = view.substr(sel).strip()
s = self.convert(s)
view.replace(edit, sel, s)
else:
all = sublime.Region(0, view.size())
s = view.substr(all).strip()
s = self.convert(s)
view.replace(edit, all, s)
def convert(self, s):
complines = []
# Gracefully handle line endings
if '\n' in s:
postfix = '\n'
else:
postfix = ''
for line in s.split('\n'):
complines.append(''.join(self.process(line)))
return postfix.join(complines)
class ReverseComplementCommand(BaseCommand):
@staticmethod
def process(s):
return ComplementCommand.process(ReverseCommand.process(s))
class ReverseCommand(BaseCommand):
@staticmethod
def process(s):
return s[::-1]
class ComplementCommand(BaseCommand):
@staticmethod
def process(l):
flip = {
'A': 'T',
'C': 'G',
'G': 'C',
'T': 'A',
'N': 'N',
'a': 't',
'c': 'g',
'g': 'c',
't': 'a',
'n': 'n'
}
line_complement = []
for i in list(l):
if not i in flip:
sublime.error_message('Selection contains non-nucleotides.')
return False
line_complement.append(flip[i])
return line_complement