source: orange-bioinformatics/_bioinformatics/obiKEGG/entry/fields.py @ 1734:91d14dd2cf0e

Revision 1734:91d14dd2cf0e, 5.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 14 months ago (diff)

obiKEGG code style fixes.

Line 
1"""
2Wrapper classes for db entry fields to support pythonic
3interface.
4
5"""
6
7
8class DBField(object):
9    """
10    Base DBGET entry field
11    """
12    __SLOTS__ = ["text"]
13
14    def __init__(self, text):
15        self.text = text
16
17    def _convert(self):
18        """
19        Convert the contents into python representation using builtin types.
20        """
21        return self.text.rstrip("\n")
22
23
24class DBSimpleField(DBField):
25    """
26    Simple field (with no subsections).
27    """
28    __SLOTS__ = ["text"]
29    # TITLE must be set in subclasses or object instances
30    TITLE = None
31
32    def __str__(self):
33        return self.format()
34
35    def format(self, section_indent=12, subsection_indent=0):
36        fmt = (" " * subsection_indent) + "%-" + \
37              str(section_indent - subsection_indent) + \
38              "s%s"
39        text = self._indent(self.text, section_indent)
40        text = fmt % (self.TITLE, text)
41        return text
42
43    def _indent(self, text, section_indent=12):
44        indent_str = "\n" + " " * section_indent
45        nl_count = text.count("\n")
46        return text.replace("\n", indent_str, nl_count - 1)
47
48
49class DBEntryField(DBSimpleField):
50    """
51    ENTRY field (all entries start with this field)
52    """
53    __SLOTS__ = ["text"]
54    TITLE = "ENTRY"
55
56
57class DBNameField(DBSimpleField):
58    __SLOTS__ = ["text"]
59    TITLE = "NAME"
60
61
62class DBDefinitionField(DBSimpleField):
63    __SLOTS__ = ["text"]
64    TITLE = "DEFINITION"
65
66
67class DBFieldWithSubsections(DBSimpleField):
68    """
69    A field with subsections (for instance REFERENCE in genome)
70    """
71    __SLOTS__ = ["text", "subsections"]
72    TITLE = None
73    SUBSECTIONS = None
74
75    def __init__(self, text, subsections=None):
76        self.text = text
77        self.subsections = subsections or []
78
79    def format(self, section_indent=12, subsection_indent=2):
80        text = DBSimpleField.format(self, section_indent, subsection_indent=0)
81        subsections = [sub.format(section_indent, subsection_indent)\
82                       for sub in self.subsections]
83        return "".join([text] + subsections)
84
85    def _convert(self):
86        my = DBSimpleField._convert(self)
87        subs = [(s.TITLE.lower(), s._convert()) \
88                for s in self.subsections]
89        return (my, subs)
90
91
92class DBTaxonomyField(DBFieldWithSubsections):
93    __SLOTS__ = ["text", "subsections"]
94    TITLE = "TAXONOMY"
95    SUBSECTIONS = ["LINEAGE"]
96
97    @property
98    def taxid(self):
99        return DBSimpleField._convert(self).split(":")[1]
100
101
102class DBDataSourceField(DBSimpleField):
103    __SLOTS__ = ["text"]
104    TITLE = "DATA_SOURCE"
105
106
107class DBReference(DBFieldWithSubsections):
108    __SLOTS__ = ["text", "subsections"]
109    TITLE = "REFERENCE"
110    SUBSECTIONS = ["AUTHORS", "TITLE", "JOURNAL"]
111
112    @property
113    def authors(self):
114        return self.subsections[0]
115
116    @property
117    def title(self):
118        return self.subsections[1]
119
120    @property
121    def journal(self):
122        return self.subsections[2]
123
124
125class DBDBLinks(DBSimpleField):
126    __SLOTS__ = ["text"]
127    TITLE = "DBLINKS"
128
129    @property
130    def links(self):
131        return [tuple(s.split(": ", 1)) \
132                for s in self.text.splitlines()]
133
134    def _convert(self):
135        # Some dblinks can span multiple lines but are always 'indented'
136        links = DBSimpleField._convert(self).replace("\n ", "").splitlines()
137        links = [tuple(link.split(": ", 1)) for link in links]
138        links = [(key, [v for v in values.split(" ") if v]) \
139                 for key, values in links]
140        return dict(links)
141
142
143class DBPathway(DBSimpleField):
144    __SLOTS__ = ["text"]
145    TITLE = "PATHWAY"
146
147    @property
148    def pathways(self):
149        return self._convert()
150
151    def _convert(self):
152        text = DBSimpleField._convert(self)
153        return [line.split(" ", 1)[0] for line in text.splitlines()]
154
155
156class DBAASeq(DBSimpleField):
157    __SLOTS__ = ["text"]
158    TITLE = "AASEQ"
159
160    @property
161    def sequence(self):
162        return self.split("\n", 1)[1].replace("\n", "")
163
164    @property
165    def sequence_lenght(self):
166        return int(self.text.split("\n", 1)[0])
167
168    def _convert(self):
169        text = DBSimpleField._convert(self)
170        count, seq = text.split("\n", 1)
171        return seq.replace("\n", "")
172
173
174class DBNTSeq(DBSimpleField):
175    __SLOTS__ = ["text"]
176    TITLE = "NTSEQ"
177
178    @property
179    def sequence(self):
180        return self.split("\n", 1)[1].replace("\n", "")
181
182    @property
183    def sequence_lenght(self):
184        return int(self.text.split("\n", 1)[0])
185
186    def _convert(self):
187        text = DBSimpleField._convert(self)
188        count, seq = text.split("\n", 1)
189        return seq.replace("\n", "")
190
191
192class DBPathwayMapField(DBSimpleField):
193    __SLOTS__ = ["text"]
194    TITLE = "PATHWAY_MAP"
195
196    def kgml_url(self):
197        return "http://www.genome.jp/kegg-bin/download?entry={0}&format=kgml".format(self.pathway_id)
198
199    @property
200    def pathway_id(self):
201        return self.text.split(" ", 1)[0]
202
203
204class DBGeneField(DBSimpleField):
205    __SLOTS__ = ["text"]
206    TITLE = "GENE"
207
208    def _convert(self):
209        text = DBSimpleField._convert(self)
210        lines = text.splitlines()
211        return [line.split(" ", 1)[0] for line in lines]
212
213
214class DBEnzymeField(DBSimpleField):
215    __SLOTS__ = ["text"]
216    TITLE = "ENZYME"
217
218    def _convert(self):
219        text = DBSimpleField._convert(self)
220        lines = text.splitlines()
221        return lines
222
223
224class DBCompoundField(DBSimpleField):
225    __SLOTS__ = ["text"]
226    TITLE = "COMPOUND"
227
228    def _convert(self):
229        text = DBSimpleField._convert(self)
230        lines = text.splitlines()
231        return [line.split(" ", 1)[0] for line in lines]
Note: See TracBrowser for help on using the repository browser.