1
2
3
4
5 """Tools for sequence motif analysis (OBSOLETE, see Bio.motifs instead).
6
7 This module (Bio.Motif) is now obsolete, and will be deprecated and
8 removed in a future release of release of Biopython. Please use the
9 new module Bio.motifs instead.
10
11 This contains the core Motif class containing various I/O methods as
12 well as methods for motif comparisons and motif searching in sequences.
13 It also inlcudes functionality for parsing AlignACE and MEME programs.
14 """
15
16 import warnings
17 warnings.warn("The module Bio.Motif is now obsolete, and will be"
18 "deprecated and removed in a future release of"
19 "release of Biopython. As a replacement for Bio.Motif,"
20 "please use the new module Bio.motifs instead. Please"
21 "be aware that though the functionality of Bio.Motif"
22 "is retained (and extended) in Bio.motifs, usage may"
23 "be different.",
24 PendingDeprecationWarning)
25
26
27 from Bio.Motif._Motif import Motif
28 from Bio.Motif.Parsers.AlignAce import read as _AlignAce_read
29 from Bio.Motif.Parsers.MEME import read as _MEME_read
30 from Bio.Motif.Thresholds import ScoreDistribution
31
32 _parsers={"AlignAce" : _AlignAce_read,
33 "MEME" : _MEME_read,
34 }
35
38
41
42 _readers={"jaspar-pfm": _from_pfm,
43 "jaspar-sites": _from_sites
44 }
45
46
47
49 """Parses an output file of motif finding programs.
50
51 Currently supported formats:
52 - AlignAce
53 - MEME
54
55 You can also use single-motif formats, although the Bio.Motif.read()
56 function is simpler to use in this situation.
57 - jaspar-pfm
58 - jaspar-sites
59
60 For example:
61
62 >>> from Bio import Motif
63 >>> for motif in Motif.parse(open("Motif/alignace.out"),"AlignAce"):
64 ... print motif.consensus()
65 TCTACGATTGAG
66 CTGCACCTAGCTACGAGTGAG
67 GTGCCCTAAGCATACTAGGCG
68 GCCACTAGCAGAGCAGGGGGC
69 CGACTCAGAGGTT
70 CCACGCTAAGAGAAGTGCCGGAG
71 GCACGTCCCTGAGCA
72 GTCCATCGCAAAGCGTGGGGC
73 GAGATCAGAGGGCCG
74 TGGACGCGGGG
75 GACCAGAGCCTCGCATGGGGG
76 AGCGCGCGTG
77 GCCGGTTGCTGTTCATTAGG
78 ACCGACGGCAGCTAAAAGGG
79 GACGCCGGGGAT
80 CGACTCGCGCTTACAAGG
81 """
82 try:
83 parser=_parsers[format]
84
85 except KeyError:
86 try:
87 reader=_readers[format]
88 except:
89 raise ValueError("Wrong parser format")
90 else:
91 yield reader(handle)
92 else:
93 for m in parser(handle).motifs:
94 yield m
95
96 -def read(handle,format):
97 """Reads a motif from a handle using a specified file-format.
98
99 This supports the same formats as Bio.Motif.parse(), but
100 only for files containing exactly one record. For example,
101 reading a pfm file:
102
103 >>> from Bio import Motif
104 >>> motif = Motif.read(open("Motif/SRF.pfm"),"jaspar-pfm")
105 >>> motif.consensus()
106 Seq('GCCCATATATGG', IUPACUnambiguousDNA())
107
108 Or a single-motif MEME file,
109
110 >>> from Bio import Motif
111 >>> motif = Motif.read(open("Motif/meme.out"),"MEME")
112 >>> motif.consensus()
113 Seq('CTCAATCGTA', IUPACUnambiguousDNA())
114
115 If the handle contains no records, or more than one record,
116 an exception is raised:
117
118 >>> from Bio import Motif
119 >>> motif = Motif.read(open("Motif/alignace.out"),"AlignAce")
120 Traceback (most recent call last):
121 ...
122 ValueError: More than one motif found in handle
123
124 If however you want the first record from a file containing
125 multiple records this function would raise an exception (as
126 shown in the example above). Instead use:
127
128 >>> from Bio import Motif
129 >>> motif = Motif.parse(open("Motif/alignace.out"),"AlignAce").next()
130 >>> motif.consensus()
131 Seq('TCTACGATTGAG', IUPACUnambiguousDNA())
132
133 Use the Bio.Motif.parse(handle, format) function if you want
134 to read multiple records from the handle.
135 """
136 iterator = parse(handle, format)
137 try:
138 first = iterator.next()
139 except StopIteration:
140 first = None
141 if first is None:
142 raise ValueError("No motifs found in handle")
143 try:
144 second = iterator.next()
145 except StopIteration:
146 second = None
147 if second is not None:
148 raise ValueError("More than one motif found in handle")
149 return first
150
151
153 """Run the Bio.Motif module's doctests.
154
155 This will try and locate the unit tests directory, and run the doctests
156 from there in order that the relative paths used in the examples work.
157 """
158 import doctest
159 import os
160 if os.path.isdir(os.path.join("..","..","Tests")):
161 print "Runing doctests..."
162 cur_dir = os.path.abspath(os.curdir)
163 os.chdir(os.path.join("..","..","Tests"))
164 doctest.testmod()
165 os.chdir(cur_dir)
166 del cur_dir
167 print "Done"
168
169 if __name__ == "__main__":
170
171 _test()
172