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