Package Bio :: Package Motif
[hide private]
[frames] | no frames]

Source Code for Package Bio.Motif

  1  # Copyright 2003-2009 by Bartek Wilczynski.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  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 _Motif import Motif 
 13  from Parsers.AlignAce import AlignAceParser, CompareAceParser 
 14  from Parsers.MEME import MEMEParser,MASTParser 
 15  from Thresholds import ScoreDistribution 
 16   
 17  _parsers={"AlignAce":AlignAceParser, 
 18            "MEME":MEMEParser 
 19            } 
 20   
21 -def _from_pfm(handle):
22 return Motif()._from_jaspar_pfm(handle)
23
24 -def _from_sites(handle):
25 return Motif()._from_jaspar_sites(handle)
26 27 _readers={"jaspar-pfm": _from_pfm, 28 "jaspar-sites": _from_sites 29 } 30 31 32
33 -def parse(handle,format):
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: #not a true parser, try reader formats 72 reader=_readers[format] 73 except: 74 raise ValueError("Wrong parser format") 75 else: #we have a proper reader 76 yield reader(handle) 77 else: # we have a proper reader 78 for m in parser().parse(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
137 -def _test():
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 #Run the doctests 156 _test() 157