forked from biopython/biopython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mast.py
164 lines (140 loc) · 4.8 KB
/
mast.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
# Copyright 2008 by Bartek Wilczynski.
# Adapted from Bio.MEME.Parser by Jason A. Hackney. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
from __future__ import print_function
from Bio.Alphabet import IUPAC
from Bio.motifs import meme
class Record(list):
"""The class for holding the results from a MAST run.
A mast.Record holds data about matches between motifs and sequences.
The motifs held by the Record are objects of the class meme.Motif.
The mast.Record class inherits from list, so you can access individual
motifs in the record by their index. Alternatively, you can find a motif
by its name:
>>> from Bio import motifs
>>> with open("mast.output.txt") as f:
... record = motifs.parse(f, 'MAST')
>>> motif = record[0]
>>> print(motif.name)
1
>>> motif = record['1']
>>> print(motif.name)
1
"""
def __init__(self):
self.sequences = []
self.version = ""
self.database = ""
self.diagrams = {}
self.alphabet = None
def __getitem__(self, key):
if isinstance(key, str):
for motif in self:
if motif.name==key:
return motif
else:
return list.__getitem__(self, key)
def read(handle):
"""read(handle)"""
record = Record()
__read_version(record, handle)
__read_database_and_motifs(record, handle)
__read_section_i(record, handle)
__read_section_ii(record, handle)
__read_section_iii(record, handle)
return record
# Everything below is private
def __read_version(record, handle):
for line in handle:
if "MAST version" in line:
break
else:
raise ValueError("Improper input file. Does not begin with a line with 'MAST version'")
record.version = line.strip().split()[2]
def __read_database_and_motifs(record, handle):
for line in handle:
if line.startswith('DATABASE AND MOTIFS'):
break
line = next(handle)
if not line.startswith('****'):
raise ValueError("Line does not start with '****':\n%s" % line)
line = next(handle)
if not 'DATABASE' in line:
raise ValueError("Line does not contain 'DATABASE':\n%s" % line)
words = line.strip().split()
record.database = words[1]
if words[2] == '(nucleotide)':
record.alphabet = IUPAC.unambiguous_dna
elif words[2] == '(peptide)':
record.alphabet = IUPAC.protein
for line in handle:
if 'MOTIF WIDTH' in line:
break
line = next(handle)
if not '----' in line:
raise ValueError("Line does not contain '----':\n%s" % line)
for line in handle:
if not line.strip():
break
words = line.strip().split()
motif = meme.Motif(record.alphabet)
motif.name = words[0]
motif.length = int(words[1])
# words[2] contains the best possible match
record.append(motif)
def __read_section_i(record, handle):
for line in handle:
if line.startswith('SECTION I:'):
break
for line in handle:
if line.startswith('SEQUENCE NAME'):
break
line = next(handle)
if not line.startswith('---'):
raise ValueError("Line does not start with '---':\n%s" % line)
for line in handle:
if not line.strip():
break
else:
sequence, description_evalue_length = line.split(None, 1)
record.sequences.append(sequence)
line = next(handle)
if not line.startswith('****'):
raise ValueError("Line does not start with '****':\n%s" % line)
def __read_section_ii(record, handle):
for line in handle:
if line.startswith('SECTION II:'):
break
for line in handle:
if line.startswith('SEQUENCE NAME'):
break
line = next(handle)
if not line.startswith('---'):
raise ValueError("Line does not start with '---':\n%s" % line)
for line in handle:
if not line.strip():
break
elif line.startswith(" "):
diagram = line.strip()
record.diagrams[sequence] += diagram
else:
sequence, pvalue, diagram = line.split()
record.diagrams[sequence] = diagram
line = next(handle)
if not line.startswith('****'):
raise ValueError("Line does not start with '****':\n%s" % line)
def __read_section_iii(record, handle):
for line in handle:
if line.startswith('SECTION III:'):
break
for line in handle:
if line.startswith('****'):
break
for line in handle:
if line.startswith('*****'):
break
for line in handle:
if line.strip():
break