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  _parsers={"AlignAce": _AlignAce_read, 
 35            "MEME": _MEME_read, 
 36            } 
 37   
38 -def _from_pfm(handle):
39 return Motif()._from_jaspar_pfm(handle)
40
41 -def _from_sites(handle):
42 return Motif()._from_jaspar_sites(handle)
43 44 _readers={"jaspar-pfm": _from_pfm, 45 "jaspar-sites": _from_sites 46 } 47 48 49
50 -def parse(handle, format):
51 """Parses an output file of motif finding programs. 52 53 Currently supported formats: 54 - AlignAce 55 - MEME 56 57 You can also use single-motif formats, although the Bio.Motif.read() 58 function is simpler to use in this situation. 59 - jaspar-pfm 60 - jaspar-sites 61 62 For example: 63 64 >>> from Bio import Motif 65 >>> for motif in Motif.parse(open("Motif/alignace.out"), "AlignAce"): 66 ... print(motif.consensus()) 67 TCTACGATTGAG 68 CTGCACCTAGCTACGAGTGAG 69 GTGCCCTAAGCATACTAGGCG 70 GCCACTAGCAGAGCAGGGGGC 71 CGACTCAGAGGTT 72 CCACGCTAAGAGAAGTGCCGGAG 73 GCACGTCCCTGAGCA 74 GTCCATCGCAAAGCGTGGGGC 75 GAGATCAGAGGGCCG 76 TGGACGCGGGG 77 GACCAGAGCCTCGCATGGGGG 78 AGCGCGCGTG 79 GCCGGTTGCTGTTCATTAGG 80 ACCGACGGCAGCTAAAAGGG 81 GACGCCGGGGAT 82 CGACTCGCGCTTACAAGG 83 """ 84 try: 85 parser=_parsers[format] 86 87 except KeyError: 88 try: #not a true parser, try reader formats 89 reader=_readers[format] 90 except: 91 raise ValueError("Wrong parser format") 92 else: #we have a proper reader 93 yield reader(handle) 94 else: # we have a proper reader 95 for m in parser(handle).motifs: 96 yield m
97
98 -def read(handle, format):
99 """Reads a motif from a handle using a specified file-format. 100 101 This supports the same formats as Bio.Motif.parse(), but 102 only for files containing exactly one record. For example, 103 reading a pfm file: 104 105 >>> from Bio import Motif 106 >>> motif = Motif.read(open("Motif/SRF.pfm"), "jaspar-pfm") 107 >>> motif.consensus() 108 Seq('GCCCATATATGG', IUPACUnambiguousDNA()) 109 110 Or a single-motif MEME file, 111 112 >>> from Bio import Motif 113 >>> motif = Motif.read(open("Motif/meme.out"), "MEME") 114 >>> motif.consensus() 115 Seq('CTCAATCGTA', IUPACUnambiguousDNA()) 116 117 If the handle contains no records, or more than one record, 118 an exception is raised: 119 120 >>> from Bio import Motif 121 >>> motif = Motif.read(open("Motif/alignace.out"), "AlignAce") 122 Traceback (most recent call last): 123 ... 124 ValueError: More than one motif found in handle 125 126 If however you want the first record from a file containing 127 multiple records this function would raise an exception (as 128 shown in the example above). Instead use: 129 130 >>> from Bio import Motif 131 >>> motif = next(Motif.parse(open("Motif/alignace.out"), "AlignAce")) 132 >>> motif.consensus() 133 Seq('TCTACGATTGAG', IUPACUnambiguousDNA()) 134 135 Use the Bio.Motif.parse(handle, format) function if you want 136 to read multiple records from the handle. 137 """ 138 iterator = parse(handle, format) 139 try: 140 first = next(iterator) 141 except StopIteration: 142 first = None 143 if first is None: 144 raise ValueError("No motifs found in handle") 145 try: 146 second = next(iterator) 147 except StopIteration: 148 second = None 149 if second is not None: 150 raise ValueError("More than one motif found in handle") 151 return first
152 153
154 -def _test():
155 """Run the Bio.Motif module's doctests. 156 157 This will try and locate the unit tests directory, and run the doctests 158 from there in order that the relative paths used in the examples work. 159 """ 160 import doctest 161 import os 162 if os.path.isdir(os.path.join("..", "..", "Tests")): 163 print("Runing doctests...") 164 cur_dir = os.path.abspath(os.curdir) 165 os.chdir(os.path.join("..", "..", "Tests")) 166 doctest.testmod() 167 os.chdir(cur_dir) 168 del cur_dir 169 print("Done")
170 171 if __name__ == "__main__": 172 #Run the doctests 173 _test() 174