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 >>> with open("Motif/alignace.out") as handle: 66 ... for motif in Motif.parse(handle, "AlignAce"): 67 ... print(motif.consensus()) 68 ... 69 TCTACGATTGAG 70 CTGCACCTAGCTACGAGTGAG 71 GTGCCCTAAGCATACTAGGCG 72 GCCACTAGCAGAGCAGGGGGC 73 CGACTCAGAGGTT 74 CCACGCTAAGAGAAGTGCCGGAG 75 GCACGTCCCTGAGCA 76 GTCCATCGCAAAGCGTGGGGC 77 GAGATCAGAGGGCCG 78 TGGACGCGGGG 79 GACCAGAGCCTCGCATGGGGG 80 AGCGCGCGTG 81 GCCGGTTGCTGTTCATTAGG 82 ACCGACGGCAGCTAAAAGGG 83 GACGCCGGGGAT 84 CGACTCGCGCTTACAAGG 85 """ 86 try: 87 parser=_parsers[format] 88 89 except KeyError: 90 try: #not a true parser, try reader formats 91 reader=_readers[format] 92 except: 93 raise ValueError("Wrong parser format") 94 else: #we have a proper reader 95 yield reader(handle) 96 else: # we have a proper reader 97 for m in parser(handle).motifs: 98 yield m
99
100 -def read(handle, format):
101 """Reads a motif from a handle using a specified file-format. 102 103 This supports the same formats as Bio.Motif.parse(), but 104 only for files containing exactly one record. For example, 105 reading a pfm file: 106 107 >>> from Bio import Motif 108 >>> with open("Motif/SRF.pfm") as handle: 109 ... motif = Motif.read(handle, "jaspar-pfm") 110 ... 111 >>> motif.consensus() 112 Seq('GCCCATATATGG', IUPACUnambiguousDNA()) 113 114 Or a single-motif MEME file, 115 116 >>> from Bio import Motif 117 >>> with open("Motif/meme.out") as handle: 118 ... motif = Motif.read(handle, "MEME") 119 ... 120 >>> motif.consensus() 121 Seq('CTCAATCGTA', IUPACUnambiguousDNA()) 122 123 If the handle contains no records, or more than one record, 124 an exception is raised: 125 126 >>> from Bio import Motif 127 >>> with open("Motif/alignace.out") as handle: 128 ... motif = Motif.read(handle, "AlignAce") 129 ... 130 Traceback (most recent call last): 131 ... 132 ValueError: More than one motif found in handle 133 134 If however you want the first record from a file containing 135 multiple records this function would raise an exception (as 136 shown in the example above). Instead use: 137 138 >>> from Bio import Motif 139 >>> with open("Motif/alignace.out") as handle: 140 ... motif = next(Motif.parse(handle, "AlignAce")) 141 ... 142 >>> motif.consensus() 143 Seq('TCTACGATTGAG', IUPACUnambiguousDNA()) 144 145 Use the Bio.Motif.parse(handle, format) function if you want 146 to read multiple records from the handle. 147 """ 148 iterator = parse(handle, format) 149 try: 150 first = next(iterator) 151 except StopIteration: 152 first = None 153 if first is None: 154 raise ValueError("No motifs found in handle") 155 try: 156 second = next(iterator) 157 except StopIteration: 158 second = None 159 if second is not None: 160 raise ValueError("More than one motif found in handle") 161 return first
162 163 164 if __name__ == "__main__": 165 from Bio._utils import run_doctest 166 run_doctest(verbose=0) 167