models.py 6.8 KB
Newer Older
Sander Bollen's avatar
Sander Bollen committed
1
from builtins import (range, str)
Bollen's avatar
init  
Bollen committed
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

__author__ = 'ahbbollen'

from datetime import datetime


class Variant(object):
    """
    This class describes a variant returned by LOVD
    """

    miracle_id = ''
    datum = ''
    refseq_build = ''
    chromosome = ''
    genomic_id_ncbi = ''
    position_g_start = ''
    position_g_end = ''
    variant_on_genome = ''
    is_present_father = ''
    is_present_mother = ''
    gene_id = ''
    gene_name = ''
    transcript_id_ncbi = ''
    variant_on_transcript_dna = ''
    variant_on_transcript_rna = ''
    variant_on_transcript_protein = ''
    allele = ''
    variant_on_genome_origin = ''
    in_gene_panel = ''
    confirm_in_lab = True

    @classmethod
    def from_dict(cls, dictionary, comments):
        results = []
        for n in range(comments["n_line"]):
            x = Variant()

            x.miracle_id = comments['id_miracle']
            if "active_gene_panel" in comments:
                x.datum = comments['active_gene_panel']
            else:
                x.datum = str(datetime.utcnow())

            for k in dictionary.keys():
                try:
                    setattr(x, k, dictionary[k][n])
Sander Bollen's avatar
Sander Bollen committed
49
                except:  # noqa
Bollen's avatar
init  
Bollen committed
50
51
52
                    pass

            x.variant_on_genome = dictionary["VariantOnGenome/DNA"][n]
Sander Bollen's avatar
Sander Bollen committed
53
54
55
56
57
58
59
60
61
62
63
64
            x.variant_on_transcript_dna = dictionary[
                "VariantOnTranscript/DNA"
            ][n]
            x.variant_on_transcript_rna = dictionary[
                "VariantOnTranscript/RNA"
            ][n]
            x.variant_on_transcript_protein = dictionary[
                "VariantOnTranscript/Protein"
            ][n]
            x.variant_on_genome_origin = dictionary[
                "VariantOnGenome/Genetic_origin"
            ][n]
Bollen's avatar
init  
Bollen committed
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

            if x.confirm_in_lab == "0":
                x.confirm_in_lab = False
            elif x.confirm_in_lab == "1":
                x.confirm_in_lab = True

            results.append(x)

        return results


class Primer(object):
    """
    This class represents a Primer we produce
    """

    chromosome = ''
    position = ''
    left = ''
    right = ''
    left_pos = ''
    right_pos = ''
    left_len = ''
    right_len = ''
    left_gc = ''
    right_gc = ''
    fragment_sequence = ''
    contains_SNP = False
    contains_freq_SNP = False
    snp_freq = 0.0
    blathits_left = []
    blathits_right = []
    left_name = ''
    right_name = ''

Sander Bollen's avatar
Sander Bollen committed
100
101
102
103
    def __init__(self, **kwargs):
        for kw, kv in kwargs.items():
            setattr(self, kw, kv)

Bollen's avatar
init  
Bollen committed
104
    @classmethod
Sander Bollen's avatar
Sander Bollen committed
105
106
    def from_p3(cls, forward, reverse, fragment=None, chromosome=None,
                position=None):
Bollen's avatar
init  
Bollen committed
107
        # line looks like this:
Sander Bollen's avatar
Sander Bollen committed
108
        # 0 CAGCACTGCTTGAGGGGAA  0 19 0 57.89 59.926 0.00 0.00 36.40 1.074
Bollen's avatar
init  
Bollen committed
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
168
169
170
        f_contents = [x for x in forward.strip().split(" ") if x]
        r_contents = [x for x in reverse.strip().split(" ") if x]
        primer = cls()
        primer.chromosome = chromosome
        primer.position = position
        primer.left = f_contents[1]
        primer.right = r_contents[1]
        primer.left_pos = f_contents[2]
        primer.right_pos = r_contents[2]
        primer.left_len = f_contents[3]
        primer.right_len = r_contents[3]
        primer.left_gc = f_contents[5]
        primer.right_gc = r_contents[5]
        primer.fragment_sequence = fragment
        return primer


class Region(object):

    def __init__(self, *args, **kwargs):
        self.chr = kwargs['chromosome']
        self.acc_nr = kwargs['acc_nr']

        self.start = kwargs['start']
        self.stop = kwargs['stop']

        self.padding_left = kwargs['padding_left']
        self.padding_right = kwargs['padding_right']
        if "ref" in kwargs:
            self.ref = kwargs['ref']
        else:
            self.ref = 'NA'

        if "other" in kwargs:
            self.other_information = kwargs['other']
        else:
            self.other_information = "NA"

    def __len__(self):
        return int(self.stop) - int(self.start)

    def size(self, padded=False):
        if padded:
            return int(self.stop_w_padding) - int(self.start_w_padding)
        else:
            return self.__len__()

    @property
    def start_w_padding(self):
        return int(self.start) - int(self.padding_left)

    @property
    def stop_w_padding(self):
        return int(self.stop) + int(self.padding_right)

    @classmethod
    def from_dict(cls, dictionary):
        for k in dictionary.keys():
            setattr(cls, k, dictionary[k])

    @classmethod
    def from_variant(cls, variant, padding_l=0, padding_r=0):
Sander Bollen's avatar
Sander Bollen committed
171
172
173
174
        return cls(chromosome=variant.chromosome,
                   acc_nr=variant.genomic_id_ncbi,
                   start=variant.position_g_start,
                   stop=variant.position_g_end,
Bollen's avatar
init  
Bollen committed
175
176
177
178
179
180
181
182
183
184
185
186
                   padding_left=padding_l,
                   padding_right=padding_r)

    @classmethod
    def from_bed(cls, line, reference, padding_l=0, padding_r=0):
        contents = line.strip().split("\t")

        if len(contents) > 3:
            other = ".".join(contents[3:])
        else:
            other = "NA"

Sander Bollen's avatar
Sander Bollen committed
187
188
189
190
191
192
193
194
        return cls(chromosome=contents[0],
                   acc_nr='NA',
                   start=int(contents[1]),
                   stop=int(contents[2]),
                   padding_left=padding_l,
                   padding_right=padding_r,
                   ref=reference,
                   other=other)
Bollen's avatar
init  
Bollen committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

    @classmethod
    def cut(cls, region, max_size, padded=False):
        """
        Cut object to maximum size
        Will cut on RIGHT side
        :param region: Region to cut
        :param max_size: maximum size wished
        :param padded: whether to use padded size
        :return: (cut, remainder)
        """

        if region.size(padded=padded) <= max_size:
            return None

        if not padded:
            remainder_start = region.start + max_size + 1
            cut_stop = region.start + max_size
        else:
            remainder_start = region.start_w_padding + max_size + 1
            cut_stop = region.start_w_padding + max_size
            if cut_stop < region.stop:
                raise ValueError("Stop would be before target")

        remainder = cls(chromosome=region.chr, acc_nr=region.acc_nr,
                        start=remainder_start,
                        stop=region.stop,
                        padding_left=region.padding_left,
                        padding_right=region.padding_right,
                        ref=region.ref,
                        other=region.other_information)
        cut = cls(chromosome=region.chr, acc_nr=region.acc_nr,
                  start=region.start,
                  stop=cut_stop,
                  padding_left=region.padding_left,
                  padding_right=region.padding_right,
                  ref=region.ref,
                  other=region.other_information)

        return cut, remainder