-
Notifications
You must be signed in to change notification settings - Fork 6
/
sgrna_target.py
168 lines (139 loc) · 3.2 KB
/
sgrna_target.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# Author: John Hawkins (jsh) [[email protected]]
import collections
import copy
import itertools
import logging
import os.path
import re
import subprocess
import sys
from Bio import SeqIO
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
class Error(Exception):
pass
class SampleError(Error):
pass
DNA_PAIRINGS = str.maketrans('atcgATCG', 'tagcTAGC')
def revcomp(x):
return x.translate(DNA_PAIRINGS)[::-1]
def bool_from_rev(x):
if type(x) is bool:
return x
if x == 'rev':
return True
elif x == 'fwd':
return False
else:
logging.fatal('Unrecognized bool signifier {x}'.format(**vars()))
def bool_from_sense(x):
if type(x) is bool:
return x
if x == 'sense':
return True
elif x == 'anti':
return False
else:
logging.fatal('Unrecognized bool signifier {x}'.format(**vars()))
def none_or_bool(x):
if x == 'None':
return None
if type(x) is bool:
return x
elif x == 'True':
return True
elif x == 'False':
return False
else:
logging.fatal('Unrecognized bool signifier {x}'.format(**vars()))
def none_or_int(x):
if x == 'None':
return None
if type(x) is int:
return x
else:
return int(x)
def none_or_str(x):
if x == 'None':
return None
if type(x) is str:
return x
else:
return str(x)
class sgrna_target(object):
def __init__(self, target, pam, chrom, start, end, reverse):
self.gene = None
self.offset = None
self.target = str(target)
self.pam = str(pam)
self.chrom = str(chrom)
self.start = int(start)
self.end = int(end)
self.reverse = bool_from_rev(reverse)
self.sense_strand = None
self.specificity = 0
@classmethod
def from_tsv(cls, tsv, sep='\t'):
"""Alternate factory constructor from serialized sgrna_target string.
Intended for reading back in the result of sgrna_target.__str__()
"""
(gene,
offset,
target,
pam,
chrom,
start,
end,
reverse,
sense_strand,
specificity) = tsv.strip().split(sep)
t = sgrna_target(target, pam, chrom, start, end, reverse)
t.gene = none_or_str(gene)
t.offset = none_or_int(offset)
t.sense_strand = bool_from_sense(sense_strand)
t.specificity = none_or_int(specificity)
return t
@classmethod
def header(cls, sep='\t'):
return sep.join([
'gene',
'offset',
'target',
'pam',
'chrom',
'start',
'end',
'repldir',
'transdir',
'specificity'])
def __str__(self, sep='\t'):
if self.reverse:
rev_str = 'rev'
else:
rev_str = 'fwd'
if self.sense_strand:
sst_str = 'sense'
else:
sst_str = 'anti'
return sep.join([str(x) for x in [
self.gene,
self.offset,
self.target,
self.pam,
self.chrom,
self.start,
self.end,
rev_str,
sst_str,
self.specificity]])
def id_str(self, sep=';'):
return sep.join([str(x) for x in [
self.target,
self.pam,
self.chrom,
self.start,
self.reverse]])
def sequence_with_pam(self):
"""DNA sequence with trailing PAM in place."""
return self.target + self.pam