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  """Tools for sequence motif analysis (DEPRECATED, see Bio.motifs instead). 
  6   
  7  This module (Bio.Motif) has been deprecated and will be removed in a 
  8  future release of release of Biopython. Please use the new module 
  9  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  from __future__ import print_function 
 17   
 18  import warnings 
 19  from Bio import BiopythonDeprecationWarning 
 20  warnings.warn("The module Bio.Motif has been deprecated and will be " 
 21                "removed in a future release of Biopython. Instead " 
 22                "please use the new module Bio.motifs instead. Please " 
 23                "be aware that though the functionality of Bio.Motif " 
 24                "is retained (and extended) in Bio.motifs, usage may " 
 25                "be different.", 
 26                BiopythonDeprecationWarning) 
 27   
 28   
 29  from Bio.Motif._Motif import Motif 
 30  from Bio.Motif.Parsers.AlignAce import read as _AlignAce_read 
 31  from Bio.Motif.Parsers.MEME import read as _MEME_read 
 32  from Bio.Motif.Thresholds import ScoreDistribution 
 33   
 34  __docformat__ = "restructuredtext en" 
 35   
 36  _parsers={"AlignAce": _AlignAce_read, 
 37            "MEME": _MEME_read, 
 38            } 
 39   
 40   
41 -def _from_pfm(handle):
42 return Motif()._from_jaspar_pfm(handle)
43 44
45 -def _from_sites(handle):
46 return Motif()._from_jaspar_sites(handle)
47 48 _readers={"jaspar-pfm": _from_pfm, 49 "jaspar-sites": _from_sites 50 } 51 52
53 -def parse(handle, format):
54 """Parses an output file of motif finding programs. 55 56 Currently supported formats: 57 58 - AlignAce 59 - MEME 60 61 You can also use single-motif formats, although the Bio.Motif.read() 62 function is simpler to use in this situation. 63 64 - jaspar-pfm 65 - jaspar-sites 66 67 For example: 68 69 >>> from Bio import Motif 70 >>> with open("Motif/alignace.out") as handle: 71 ... for motif in Motif.parse(handle, "AlignAce"): 72 ... print(motif.consensus()) 73 ... 74 TCTACGATTGAG 75 CTGCACCTAGCTACGAGTGAG 76 GTGCCCTAAGCATACTAGGCG 77 GCCACTAGCAGAGCAGGGGGC 78 CGACTCAGAGGTT 79 CCACGCTAAGAGAAGTGCCGGAG 80 GCACGTCCCTGAGCA 81 GTCCATCGCAAAGCGTGGGGC 82 GAGATCAGAGGGCCG 83 TGGACGCGGGG 84 GACCAGAGCCTCGCATGGGGG 85 AGCGCGCGTG 86 GCCGGTTGCTGTTCATTAGG 87 ACCGACGGCAGCTAAAAGGG 88 GACGCCGGGGAT 89 CGACTCGCGCTTACAAGG 90 """ 91 try: 92 parser=_parsers[format] 93 94 except KeyError: 95 try: # not a true parser, try reader formats 96 reader=_readers[format] 97 except: 98 raise ValueError("Wrong parser format") 99 else: # we have a proper reader 100 yield reader(handle) 101 else: # we have a proper reader 102 for m in parser(handle).motifs: 103 yield m
104 105
106 -def read(handle, format):
107 """Reads a motif from a handle using a specified file-format. 108 109 This supports the same formats as Bio.Motif.parse(), but 110 only for files containing exactly one record. For example, 111 reading a pfm file: 112 113 >>> from Bio import Motif 114 >>> with open("Motif/SRF.pfm") as handle: 115 ... motif = Motif.read(handle, "jaspar-pfm") 116 ... 117 >>> motif.consensus() 118 Seq('GCCCATATATGG', IUPACUnambiguousDNA()) 119 120 Or a single-motif MEME file, 121 122 >>> from Bio import Motif 123 >>> with open("Motif/meme.out") as handle: 124 ... motif = Motif.read(handle, "MEME") 125 ... 126 >>> motif.consensus() 127 Seq('CTCAATCGTA', IUPACUnambiguousDNA()) 128 129 If the handle contains no records, or more than one record, 130 an exception is raised: 131 132 >>> from Bio import Motif 133 >>> with open("Motif/alignace.out") as handle: 134 ... motif = Motif.read(handle, "AlignAce") 135 ... 136 Traceback (most recent call last): 137 ... 138 ValueError: More than one motif found in handle 139 140 If however you want the first record from a file containing 141 multiple records this function would raise an exception (as 142 shown in the example above). Instead use: 143 144 >>> from Bio import Motif 145 >>> with open("Motif/alignace.out") as handle: 146 ... motif = next(Motif.parse(handle, "AlignAce")) 147 ... 148 >>> motif.consensus() 149 Seq('TCTACGATTGAG', IUPACUnambiguousDNA()) 150 151 Use the Bio.Motif.parse(handle, format) function if you want 152 to read multiple records from the handle. 153 """ 154 iterator = parse(handle, format) 155 try: 156 first = next(iterator) 157 except StopIteration: 158 first = None 159 if first is None: 160 raise ValueError("No motifs found in handle") 161 try: 162 second = next(iterator) 163 except StopIteration: 164 second = None 165 if second is not None: 166 raise ValueError("More than one motif found in handle") 167 return first
168 169 170 if __name__ == "__main__": 171 from Bio._utils import run_doctest 172 run_doctest(verbose=0) 173