annotate weblogolib/__init__.py @ 7:8d676bbd1f2d

Uploaded
author davidmurphy
date Mon, 16 Jan 2012 07:03:36 -0500
parents 4d47ab2b7bcc
children 5149eb3a89c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1 #!/usr/bin/env python
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
3 # -------------------------------- WebLogo --------------------------------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
4
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
5 # Copyright (c) 2003-2004 The Regents of the University of California.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
6 # Copyright (c) 2005 Gavin E. Crooks
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
7 # Copyright (c) 2006, The Regents of the University of California, through print
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
8 # Lawrence Berkeley National Laboratory (subject to receipt of any required
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
9 # approvals from the U.S. Dept. of Energy). All rights reserved.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
10
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
11 # This software is distributed under the new BSD Open Source License.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
12 # <http://www.opensource.org/licenses/bsd-license.html>
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
13 #
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
14 # Redistribution and use in source and binary forms, with or without
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
15 # modification, are permitted provided that the following conditions are met:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
16 #
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
17 # (1) Redistributions of source code must retain the above copyright notice,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
18 # this list of conditions and the following disclaimer.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
19 #
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
20 # (2) Redistributions in binary form must reproduce the above copyright
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
21 # notice, this list of conditions and the following disclaimer in the
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
22 # documentation and or other materials provided with the distribution.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
23 #
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
24 # (3) Neither the name of the University of California, Lawrence Berkeley
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
25 # National Laboratory, U.S. Dept. of Energy nor the names of its contributors
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
26 # may be used to endorse or promote products derived from this software
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
27 # without specific prior written permission.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
28 #
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
29 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
30 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
31 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
32 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
33 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
34 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
35 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
36 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
37 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
38 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
39 # POSSIBILITY OF SUCH DAMAGE.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
40
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
41 # Replicates README.txt
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
42
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
43 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
44 WebLogo (http://code.google.com/p/weblogo/) is a tool for creating sequence
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
45 logos from biological sequence alignments. It can be run on the command line,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
46 as a standalone webserver, as a CGI webapp, or as a python library.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
47
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
48 The main WebLogo webserver is located at http://bespoke.lbl.gov/weblogo/
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
49
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
50
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
51 Codonlogo is based on Weblogo.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
52
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
53 Please consult the manual for installation instructions and more information:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
54 (Also located in the weblogolib/htdocs subdirectory.)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
55
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
56 For help on the command line interface run
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
57 ./codonlogo --help
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
58
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
59 To build a simple logo run
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
60 ./codonlogo < cap.fa > logo0.eps
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
61
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
62 To run as a standalone webserver at localhost:8080
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
63 ./codonlogo --server
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
64
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
65 To create a logo in python code:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
66 >>> from weblogolib import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
67 >>> fin = open('cap.fa')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
68 >>> seqs = read_seq_data(fin)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
69 >>> data = LogoData.from_seqs(seqs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
70 >>> options = LogoOptions()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
71 >>> options.title = "A Logo Title"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
72 >>> format = LogoFormat(data, options)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
73 >>> fout = open('cap.eps', 'w')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
74 >>> eps_formatter( data, format, fout)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
75
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
76
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
77 -- Distribution and Modification --
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
78 This package is distributed under the new BSD Open Source License.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
79 Please see the LICENSE.txt file for details on copyright and licensing.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
80 The WebLogo source code can be downloaded from http://code.google.com/p/weblogo/
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
81 WebLogo requires Python 2.3, 2.4 or 2.5, the corebio python toolkit for computational
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
82 biology (http://code.google.com/p/corebio), and the python array package
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
83 'numpy' (http://www.scipy.org/Download)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
84
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
85 # -------------------------------- CodonLogo --------------------------------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
86
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
87
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
88 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
89
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
90 from math import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
91 import random
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
92 from itertools import izip, count
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
93 import sys
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
94 import copy
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
95 import os
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
96 from itertools import product
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
97 from datetime import datetime
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
98 from StringIO import StringIO
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
99
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
100
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
101
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
102 from corebio.data import rna_letters, dna_letters, amino_acid_letters
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
103 import random
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
104
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
105 # python2.3 compatability
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
106 from corebio._future import Template
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
107 from corebio._future.subprocess import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
108 from corebio._future import resource_string, resource_filename
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
109
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
110
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
111 from math import log, sqrt
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
112
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
113 # Avoid 'from numpy import *' since numpy has lots of names defined
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
114 from numpy import array, asarray, float64, ones, zeros, int32,all,any, shape
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
115 import numpy as na
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
116
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
117 from corebio.utils.deoptparse import DeOptionParser
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
118 from optparse import OptionGroup
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
119
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
120 from color import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
121 from colorscheme import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
122 from corebio.seq import Alphabet, Seq, SeqList
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
123 from corebio import seq_io
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
124 from corebio.utils import isfloat, find_command, ArgumentError
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
125 from corebio.moremath import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
126 from corebio.data import amino_acid_composition
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
127 from corebio.seq import unambiguous_rna_alphabet, unambiguous_dna_alphabet, unambiguous_protein_alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
128
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
129 codon_alphabetU=['AAA', 'AAC', 'AAG', 'AAU', 'ACA', 'ACC', 'ACG', 'ACU', 'AGA', 'AGC', 'AGG', 'AGU', 'AUA', 'AUC', 'AUG', 'AUU', 'CAA', 'CAC', 'CAG', 'CAU', 'CCA', 'CCC', 'CCG', 'CCU', 'CGA', 'CGC', 'CGG', 'CGU', 'CUA', 'CUC', 'CUG', 'CUU', 'GAA', 'GAC', 'GAG', 'GAU', 'GCA', 'GCC', 'GCG', 'GCU', 'GGA', 'GGC', 'GGG', 'GGU', 'GUA', 'GUC', 'GUG', 'GUU', 'UAA', 'UAC', 'UAG', 'UAU', 'UCA', 'UCC', 'UCG', 'UCU', 'UGA', 'UGC', 'UGG', 'UGU', 'UUA', 'UUC', 'UUG', 'UUU']
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
130 codon_alphabetT=['AAA', 'AAC', 'AAG', 'AAT', 'ACA', 'ACC', 'ACG', 'ACT', 'AGA', 'AGC', 'AGG', 'AGT', 'ATA', 'ATC', 'ATG', 'ATT', 'CAA', 'CAC', 'CAG', 'CAT', 'CCA', 'CCC', 'CCG', 'CCT', 'CGA', 'CGC', 'CGG', 'CGT', 'CTA', 'CTC', 'CTG', 'CTT', 'GAA', 'GAC', 'GAG', 'GAT', 'GCA', 'GCC', 'GCG', 'GCT', 'GGA', 'GGC', 'GGG', 'GGT', 'GTA', 'GTC', 'GTG', 'GTT', 'TAA', 'TAC', 'TAG', 'TAT', 'TCA', 'TCC', 'TCG', 'TCT', 'TGA', 'TGC', 'TGG', 'TGT', 'TTA', 'TTC', 'TTG', 'TTT']
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
131
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
132 altype="codonsT"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
133 offset=0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
134 isreversed=False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
135 col=[]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
136
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
137
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
138
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
139 __all__ = ['LogoSize',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
140 'LogoOptions',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
141 'description',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
142 '__version__',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
143 'LogoFormat',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
144 'LogoData',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
145 'Dirichlet',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
146 'GhostscriptAPI',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
147 'std_color_schemes',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
148 'default_color_schemes',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
149 'classic',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
150 'std_units',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
151 'std_sizes',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
152 'std_alphabets',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
153 'std_percentCG',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
154 'pdf_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
155 'jpeg_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
156 'png_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
157 'png_print_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
158 'txt_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
159 'eps_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
160 'formatters',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
161 'default_formatter',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
162 'base_distribution',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
163 'equiprobable_distribution',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
164 'read_seq_data',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
165 'which_alphabet',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
166 'color',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
167 'colorscheme',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
168 ]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
169
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
170 description = "Create sequence logos from biological sequence alignments."
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
171
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
172 __version__ = "1.0"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
173
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
174 # These keywords are subsituted by subversion.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
175 # The date and revision will only tell the truth after a branch or tag,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
176 # since different files in trunk will have been changed at different times
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
177 release_date ="$Date: 2011-09-17 16:30:00 -0700 (Tue, 14 Oct 2008) $".split()[1]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
178 release_build = "$Revision: 53 $".split()[1]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
179 release_description = "CodonLogo %s (%s)" % (__version__, release_date)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
180
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
181
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
182
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
183 def cgi(htdocs_directory) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
184 import weblogolib._cgi
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
185 weblogolib._cgi.main(htdocs_directory)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
186
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
187 class GhostscriptAPI(object) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
188 """Interface to the command line program Ghostscript ('gs')"""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
189
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
190 formats = ('png', 'pdf', 'jpeg')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
191
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
192 def __init__(self, path=None) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
193 try:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
194 command = find_command('gs', path=path)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
195 except EnvironmentError:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
196 try:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
197 command = find_command('gswin32c.exe', path=path)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
198 except EnvironmentError:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
199 raise EnvironmentError("Could not find Ghostscript on path."
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
200 " There should be either a gs executable or a gswin32c.exe on your system's path")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
201
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
202 self.command = command
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
203
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
204 def version(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
205 args = [self.command, '--version']
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
206 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
207 p = Popen(args, stdout=PIPE)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
208 (out,err) = p.communicate()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
209 except OSError :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
210 raise RuntimeError("Cannot communicate with ghostscript.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
211 return out.strip()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
212
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
213 def convert(self, format, fin, fout, width, height, resolution=300) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
214 device_map = { 'png':'png16m', 'pdf':'pdfwrite', 'jpeg':'jpeg'}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
215
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
216 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
217 device = device_map[format]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
218 except KeyError:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
219 raise ValueError("Unsupported format.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
220
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
221 args = [self.command,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
222 "-sDEVICE=%s" % device,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
223 "-dPDFSETTINGS=/printer",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
224 #"-q", # Quite: Do not dump messages to stdout.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
225 "-sstdout=%stderr", # Redirect messages and errors to stderr
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
226 "-sOutputFile=-", # Stdout
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
227 "-dDEVICEWIDTHPOINTS=%s" % str(width),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
228 "-dDEVICEHEIGHTPOINTS=%s" % str(height),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
229 "-dSAFER", # For added security
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
230 "-dNOPAUSE",]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
231
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
232 if device != 'pdf' :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
233 args.append("-r%s" % str(resolution) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
234 if resolution < 300 : # Antialias if resolution is Less than 300 DPI
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
235 args.append("-dGraphicsAlphaBits=4")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
236 args.append("-dTextAlphaBits=4")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
237 args.append("-dAlignToPixels=0")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
238
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
239 args.append("-") # Read from stdin. Must be last argument.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
240
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
241 error_msg = "Unrecoverable error : Ghostscript conversion failed " \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
242 "(Invalid postscript?). %s" % " ".join(args)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
243
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
244 source = fin.read()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
245
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
246 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
247 p = Popen(args, stdin=PIPE, stdout = PIPE, stderr= PIPE)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
248 (out,err) = p.communicate(source)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
249 except OSError :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
250 raise RuntimeError(error_msg)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
251
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
252 if p.returncode != 0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
253 error_msg += '\nReturn code: %i\n' % p.returncode
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
254 if err is not None : error_msg += err
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
255 raise RuntimeError(error_msg)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
256
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
257 print >>fout, out
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
258 # end class Ghostscript
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
259
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
260
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
261 aa_composition = [ amino_acid_composition[_k] for _k in
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
262 unambiguous_protein_alphabet]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
263
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
264
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
265
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
266 # ------ DATA ------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
267
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
268 classic = ColorScheme([
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
269 ColorGroup("G", "orange" ),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
270 ColorGroup("TU", "red"),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
271 ColorGroup("C", "blue"),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
272 ColorGroup("A", "green")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
273 ] )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
274
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
275 std_color_schemes = {"auto": None, # Depends on sequence type
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
276 "monochrome": monochrome,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
277 "base pairing": base_pairing,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
278 "classic": classic,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
279 "hydrophobicity" : hydrophobicity,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
280 "chemistry" : chemistry,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
281 "charge" : charge,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
282 }#
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
283
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
284 default_color_schemes = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
285 unambiguous_protein_alphabet: hydrophobicity,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
286 unambiguous_rna_alphabet: base_pairing,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
287 unambiguous_dna_alphabet: base_pairing
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
288 #codon_alphabet:codonsU
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
289 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
290
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
291
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
292 std_units = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
293 "bits" : 1./log(2),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
294 "nats" : 1.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
295 "digits" : 1./log(10),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
296 "kT" : 1.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
297 "kJ/mol" : 8.314472 *298.15 /1000.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
298 "kcal/mol": 1.987 *298.15 /1000.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
299 "probability" : None,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
300 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
301
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
302 class LogoSize(object) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
303 def __init__(self, stack_width, stack_height) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
304 self.stack_width = stack_width
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
305 self.stack_height = stack_height
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
306
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
307 def __repr__(self):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
308 return stdrepr(self)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
309
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
310 # The base stack width is set equal to 9pt Courier.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
311 # (Courier has a width equal to 3/5 of the point size.)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
312 # Check that can get 80 characters in journal page @small
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
313 # 40 chacaters in a journal column
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
314 std_sizes = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
315 "small" : LogoSize( stack_width = 10, stack_height = 10*1*5),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
316 "medium" : LogoSize( stack_width = 10*2, stack_height = 10*2*5),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
317 "large" : LogoSize( stack_width = 10*3, stack_height = 10*3*5),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
318 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
319
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
320
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
321 std_alphabets = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
322 'protein': unambiguous_protein_alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
323 'rna': unambiguous_rna_alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
324 'dna': unambiguous_dna_alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
325 'codonsU':codon_alphabetU,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
326 'codonsT':codon_alphabetT
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
327 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
328
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
329 std_percentCG = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
330 'H. sapiens' : 40.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
331 'E. coli' : 50.5,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
332 'S. cerevisiae' : 38.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
333 'C. elegans' : 36.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
334 'D. melanogaster': 43.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
335 'M. musculus' : 42.,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
336 'T. thermophilus' : 69.4,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
337 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
338
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
339 # Thermus thermophilus: Henne A, Bruggemann H, Raasch C, Wiezer A, Hartsch T,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
340 # Liesegang H, Johann A, Lienard T, Gohl O, Martinez-Arias R, Jacobi C,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
341 # Starkuviene V, Schlenczeck S, Dencker S, Huber R, Klenk HP, Kramer W,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
342 # Merkl R, Gottschalk G, Fritz HJ: The genome sequence of the extreme
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
343 # thermophile Thermus thermophilus.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
344 # Nat Biotechnol 2004, 22:547-53
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
345
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
346 def stdrepr(obj) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
347 attr = vars(obj).items()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
348
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
349
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
350 attr.sort()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
351 args = []
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
352 for a in attr :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
353 if a[0][0]=='_' : continue
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
354 args.append( '%s=%s' % ( a[0], repr(a[1])) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
355 args = ',\n'.join(args).replace('\n', '\n ')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
356 return '%s(\n %s\n)' % (obj.__class__.__name__, args)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
357
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
358
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
359 class LogoOptions(object) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
360 """ A container for all logo formating options. Not all of these
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
361 are directly accessible through the CLI or web interfaces.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
362
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
363 To display LogoOption defaults:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
364 >>> from weblogolib import *
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
365 >>> LogoOptions()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
366
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
367
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
368 Attributes:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
369 o alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
370 o creator_text -- Embedded as comment in figures.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
371 o logo_title
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
372 o logo_label
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
373 o stacks_per_line
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
374 o unit_name
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
375 o show_yaxis
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
376 o yaxis_label -- Default depends on other settings.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
377 o yaxis_tic_interval
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
378 o yaxis_minor_tic_ratio
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
379 o yaxis_scale
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
380 o show_xaxis
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
381 o xaxis_label
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
382 o xaxis_tic_interval
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
383 o rotate_numbers
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
384 o number_interval
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
385 o show_ends
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
386 o show_fineprint
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
387 o fineprint
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
388 o show_boxes
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
389 o shrink_fraction
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
390 o show_errorbars
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
391 o errorbar_fraction
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
392 o errorbar_width_fraction
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
393 o errorbar_gray
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
394 o resolution -- Dots per inch
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
395 o default_color
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
396 o color_scheme
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
397 o debug
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
398 o logo_margin
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
399 o stroke_width
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
400 o tic_length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
401 o size
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
402 o stack_margin
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
403 o pad_right
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
404 o small_fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
405 o fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
406 o title_fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
407 o number_fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
408 o text_font
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
409 o logo_font
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
410 o title_font
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
411 o first_index
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
412 o logo_start
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
413 o logo_end
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
414 o scale_width
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
415 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
416
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
417 def __init__(self, **kwargs) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
418 """ Create a new LogoOptions instance.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
419
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
420 >>> L = LogoOptions(logo_title = "Some Title String")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
421 >>> L.show_yaxis = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
422 >>> repr(L)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
423 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
424
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
425 self.creator_text = release_description,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
426 self.alphabet = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
427
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
428 self.logo_title = ""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
429 self.logo_label = ""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
430 self.stacks_per_line = 20
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
431
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
432 self.unit_name = "bits"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
433
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
434 self.show_yaxis = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
435 # yaxis_lable default depends on other settings. See LogoFormat
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
436 self.yaxis_label = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
437 self.yaxis_tic_interval = 1.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
438 self.yaxis_minor_tic_ratio = 5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
439 self.yaxis_scale = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
440
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
441 self.show_xaxis = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
442 self.xaxis_label = ""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
443 self.xaxis_tic_interval =1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
444 self.rotate_numbers = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
445 self.number_interval = 5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
446 self.show_ends = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
447
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
448 self.show_fineprint = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
449 self.fineprint = "CodonLogo "+__version__
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
450
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
451 self.show_boxes = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
452 self.shrink_fraction = 0.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
453
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
454 self.show_errorbars = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
455 self.altype = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
456
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
457 self.errorbar_fraction = 0.90
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
458 self.errorbar_width_fraction = 0.25
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
459 self.errorbar_gray = 0.50
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
460
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
461 self.resolution = 96. # Dots per inch
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
462
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
463 self.default_color = Color.by_name("black")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
464 self.color_scheme = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
465 #self.show_color_key = False # NOT yet implemented
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
466
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
467 self.debug = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
468
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
469 self.logo_margin = 2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
470 self.stroke_width = 0.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
471 self.tic_length = 5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
472
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
473 self.size = std_sizes["medium"]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
474
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
475 self.stack_margin = 0.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
476 self.pad_right = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
477
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
478 self.small_fontsize = 6
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
479 self.fontsize = 10
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
480 self.title_fontsize = 12
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
481 self.number_fontsize = 8
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
482
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
483 self.text_font = "ArialMT"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
484 self.logo_font = "Arial-BoldMT"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
485 self.title_font = "ArialMT"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
486
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
487 self.first_index = 1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
488 self.logo_start = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
489 self.logo_end=None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
490
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
491 # Scale width of characters proportional to gaps
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
492 self.scale_width = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
493
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
494 from corebio.utils import update
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
495 update(self, **kwargs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
496
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
497 def __repr__(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
498 attr = vars(self).items()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
499 attr.sort()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
500 args = []
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
501 for a in attr :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
502 if a[0][0]=='_' : continue
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
503 args.append( '%s=%s' % ( a[0], repr(a[1])) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
504 args = ',\n'.join(args).replace('\n', '\n ')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
505 return '%s(\n %s\n)' % (self.__class__.__name__, args)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
506 # End class LogoOptions
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
507
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
508
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
509
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
510
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
511 class LogoFormat(LogoOptions) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
512 """ Specifies the format of the logo. Requires a LogoData and LogoOptions
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
513 objects.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
514
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
515 >>> data = LogoData.from_seqs(seqs )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
516 >>> options = LogoOptions()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
517 >>> options.title = "A Logo Title"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
518 >>> format = LogoFormat(data, options)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
519 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
520 # TODO: Raise ArgumentErrors instead of ValueError and document
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
521 def __init__(self, data, options= None) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
522
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
523 LogoOptions.__init__(self)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
524 #global offset
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
525 if options is not None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
526 self.__dict__.update(options.__dict__)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
527
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
528 #offset=options.frame
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
529
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
530 self.alphabet = data.alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
531 self.seqlen = data.length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
532 self.altype = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
533 self.show_title = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
534 self.show_xaxis_label = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
535 self.yaxis_minor_tic_interval = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
536 self.lines_per_logo = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
537 self.char_width = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
538 self.line_margin_left = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
539 self.line_margin_right = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
540 self.line_margin_bottom = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
541 self.line_margin_top = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
542 self.title_height = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
543 self.xaxis_label_height = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
544 self.line_height = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
545 self.line_width = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
546 self.logo_height = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
547 self.logo_width = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
548 self.creation_date = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
549 self.end_type = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
550
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
551 if self.stacks_per_line< 1 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
552 raise ArgumentError("Stacks per line should be greater than zero.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
553 "stacks_per_line" )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
554
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
555 if self.size.stack_height<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
556 raise ArgumentError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
557 "Stack height must be greater than zero.", "stack_height")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
558 if (self.small_fontsize <= 0 or self.fontsize <=0 or
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
559 self.title_fontsize<=0 ):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
560 raise ValueError("Font sizes must be positive.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
561
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
562 if self.errorbar_fraction<0.0 or self.errorbar_fraction>1.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
563 raise ValueError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
564 "The visible fraction of the error bar must be between zero and one.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
565
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
566 if self.yaxis_tic_interval<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
567 raise ArgumentError( "The yaxis tic interval cannot be negative.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
568 'yaxis_tic_interval')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
569
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
570 if self.size.stack_width <= 0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
571 raise ValueError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
572 "The width of a stack should be a positive number.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
573
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
574 if self.yaxis_minor_tic_interval and \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
575 self.yaxis_minor_tic_interval<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
576 raise ValueError("Distances cannot be negative.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
577
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
578 if self.xaxis_tic_interval<=0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
579 raise ValueError("Tic interval must be greater than zero.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
580
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
581 if self.number_interval<=0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
582 raise ValueError("Invalid interval between numbers.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
583
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
584 if self.shrink_fraction<0.0 or self.shrink_fraction>1.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
585 raise ValueError("Invalid shrink fraction.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
586
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
587 if self.stack_margin<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
588 raise ValueError("Invalid stack margin." )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
589
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
590 if self.logo_margin<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
591 raise ValueError("Invalid logo margin." )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
592
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
593 if self.stroke_width<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
594 raise ValueError("Invalid stroke width.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
595
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
596 if self.tic_length<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
597 raise ValueError("Invalid tic length.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
598
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
599 # FIXME: More validation
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
600
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
601 # Inclusive upper and lower bounds
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
602 # FIXME: Validate here. Move from eps_formatter
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
603 if self.logo_start is None: self.logo_start = self.first_index
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
604
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
605 if self.logo_end is None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
606 self.logo_end = self.seqlen + self.first_index -1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
607
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
608 self.total_stacks = self.logo_end - self.logo_start +1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
609
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
610 if self.logo_start - self.first_index <0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
611 raise ArgumentError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
612 "Logo range extends before start of available sequence.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
613 'logo_range')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
614
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
615 if self.logo_end - self.first_index >= self.seqlen :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
616 raise ArgumentError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
617 "Logo range extends beyond end of available sequence.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
618 'logo_range')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
619
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
620 if self.logo_title : self.show_title = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
621 if not self.fineprint : self.show_fineprint = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
622 if self.xaxis_label : self.show_xaxis_label = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
623
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
624 if self.yaxis_label is None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
625 self.yaxis_label = self.unit_name
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
626
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
627 if self.yaxis_label :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
628 self.show_yaxis_label = True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
629 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
630 self.show_yaxis_label = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
631 self.show_ends = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
632
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
633 if not self.yaxis_scale :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
634 conversion_factor = std_units[self.unit_name]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
635 if conversion_factor :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
636 self.yaxis_scale=log(len(self.alphabet))*conversion_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
637 #self.yaxis_scale=max(data.entropy)*conversion_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
638 #marker# this is where I handle the max height. needs revision.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
639 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
640 self.yaxis_scale=1.0 # probability units
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
641
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
642 if self.yaxis_scale<=0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
643 raise ValueError(('yaxis_scale', "Invalid yaxis scale"))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
644 if self.yaxis_tic_interval >= self.yaxis_scale:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
645 self.yaxis_tic_interval /= 2.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
646
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
647 self.yaxis_minor_tic_interval \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
648 = float(self.yaxis_tic_interval)/self.yaxis_minor_tic_ratio
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
649
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
650 if self.color_scheme is None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
651 #if self.alphabet in default_color_schemes :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
652 #self.color_scheme = default_color_schemes[self.alphabet]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
653 #else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
654 self.color_scheme = codonsT
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
655 #else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
656 #for color, symbols, desc in options.colors:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
657 #try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
658 #self.color_scheme.append( ColorGroup(symbols, color, desc) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
659 #print >>sys.stderr, color_scheme.groups[2]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
660 #except ValueError :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
661 #raise ValueError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
662 #"error: option --color: invalid value: '%s'" % color )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
663
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
664
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
665 self.lines_per_logo = 1+ ( (self.total_stacks-1) / self.stacks_per_line)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
666
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
667 if self.lines_per_logo==1 and not self.pad_right:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
668 self.stacks_per_line = min(self.stacks_per_line, self.total_stacks)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
669
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
670 self.char_width = self.size.stack_width - 2* self.stack_margin
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
671
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
672
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
673 if self.show_yaxis :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
674 self.line_margin_left = self.fontsize * 3.0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
675 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
676 self.line_margin_left = 0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
677
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
678 if self.show_ends :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
679 self.line_margin_right = self.fontsize *1.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
680 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
681 self.line_margin_right = self.fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
682
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
683 if self.show_xaxis :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
684 if self.rotate_numbers :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
685 self.line_margin_bottom = self.number_fontsize *2.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
686 else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
687 self.line_margin_bottom = self.number_fontsize *1.5
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
688 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
689 self.line_margin_bottom = 4
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
690
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
691 self.line_margin_top = 4
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
692
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
693 if self.show_title :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
694 self.title_height = self.title_fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
695 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
696 self.title_height = 0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
697
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
698 self.xaxis_label_height =0.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
699 if self.show_xaxis_label :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
700 self.xaxis_label_height += self.fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
701 if self.show_fineprint :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
702 self.xaxis_label_height += self.small_fontsize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
703
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
704 self.line_height = (self.size.stack_height + self.line_margin_top +
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
705 self.line_margin_bottom )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
706 self.line_width = (self.size.stack_width*self.stacks_per_line +
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
707 self.line_margin_left + self.line_margin_right )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
708
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
709 self.logo_height = int(2*self.logo_margin + self.title_height \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
710 + self.xaxis_label_height + self.line_height*self.lines_per_logo)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
711 self.logo_width = int(2*self.logo_margin + self.line_width )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
712
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
713
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
714 self.creation_date = datetime.now().isoformat(' ')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
715
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
716 end_type = '-'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
717 end_types = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
718 unambiguous_protein_alphabet: 'p',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
719 unambiguous_rna_alphabet: '-',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
720 unambiguous_dna_alphabet: 'd'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
721 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
722 if self.show_ends and self.alphabet in end_types:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
723 end_type = end_types[self.alphabet]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
724 self.end_type = end_type
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
725 # End __init__
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
726 # End class LogoFormat
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
727
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
728
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
729
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
730 # ------ Logo Formaters ------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
731 # Each formatter is a function f(LogoData, LogoFormat, output file).
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
732 # that draws a represntation of the logo into the given file.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
733 # The main graphical formatter is eps_formatter. A mapping 'formatters'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
734 # containing all available formatters is located after the formatter
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
735 # definitions.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
736
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
737 def pdf_formatter(data, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
738 """ Generate a logo in PDF format."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
739
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
740 feps = StringIO()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
741 eps_formatter(data, format, feps)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
742 feps.seek(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
743
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
744 gs = GhostscriptAPI()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
745 gs.convert('pdf', feps, fout, format.logo_width, format.logo_height)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
746
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
747
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
748 def _bitmap_formatter(data, format, fout, device) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
749 feps = StringIO()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
750 eps_formatter(data, format, feps)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
751 feps.seek(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
752
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
753 gs = GhostscriptAPI()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
754 gs.convert(device, feps, fout,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
755 format.logo_width, format.logo_height, format.resolution)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
756
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
757
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
758 def jpeg_formatter(data, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
759 """ Generate a logo in JPEG format."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
760 _bitmap_formatter(data, format, fout, device="jpeg")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
761
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
762
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
763 def png_formatter(data, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
764 """ Generate a logo in PNG format."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
765
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
766 _bitmap_formatter(data, format, fout, device="png")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
767
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
768
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
769 def png_print_formatter(data, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
770 """ Generate a logo in PNG format with print quality (600 DPI) resolution."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
771 format.resolution = 600
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
772 _bitmap_formatter(data, format, fout, device="png")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
773
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
774
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
775 def txt_formatter( logodata, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
776 """ Create a text representation of the logo data.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
777 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
778 print >>fout, str(logodata)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
779
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
780
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
781
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
782
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
783 def eps_formatter( logodata, format, fout) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
784 """ Generate a logo in Encapsulated Postscript (EPS)"""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
785
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
786 subsitutions = {}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
787 from_format =[
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
788 "creation_date", "logo_width", "logo_height",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
789 "lines_per_logo", "line_width", "line_height",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
790 "line_margin_right","line_margin_left", "line_margin_bottom",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
791 "line_margin_top", "title_height", "xaxis_label_height",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
792 "creator_text", "logo_title", "logo_margin",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
793 "stroke_width", "tic_length",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
794 "stacks_per_line", "stack_margin",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
795 "yaxis_label", "yaxis_tic_interval", "yaxis_minor_tic_interval",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
796 "xaxis_label", "xaxis_tic_interval", "number_interval",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
797 "fineprint", "shrink_fraction", "errorbar_fraction",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
798 "errorbar_width_fraction",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
799 "errorbar_gray", "small_fontsize", "fontsize",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
800 "title_fontsize", "number_fontsize", "text_font",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
801 "logo_font", "title_font",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
802 "logo_label", "yaxis_scale", "end_type",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
803 "debug", "show_title", "show_xaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
804 "show_xaxis_label", "show_yaxis", "show_yaxis_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
805 "show_boxes", "show_errorbars", "show_fineprint",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
806 "rotate_numbers", "show_ends", "altype",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
807
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
808 ]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
809
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
810 for s in from_format :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
811 subsitutions[s] = getattr(format,s)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
812
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
813
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
814 from_format_size = ["stack_height", "stack_width"]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
815 for s in from_format_size :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
816 subsitutions[s] = getattr(format.size,s)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
817
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
818 subsitutions["shrink"] = str(format.show_boxes).lower()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
819
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
820
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
821 # --------- COLORS --------------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
822 def format_color(color):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
823 return " ".join( ("[",str(color.red) , str(color.green),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
824 str(color.blue), "]"))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
825
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
826 subsitutions["default_color"] = format_color(format.default_color)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
827 global col
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
828 colors = []
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
829
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
830 if altype=="codonsT" or altype=="codonsU":
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
831 for group in col:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
832 cf = format_color(group.color)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
833 colors.append( " ("+group.symbols+") " + cf )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
834 for group in format.color_scheme.groups :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
835 cf = format_color(group.color)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
836
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
837 colors.append( " ("+group.symbols+") " + cf )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
838 #print >>sys.stderr,opts.colors
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
839 #print >>sys.stderr,logodata.options
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
840 #print >>sys.stderr, group.symbols
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
841 #print >>sys.stderr, cf
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
842
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
843
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
844
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
845 else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
846 for group in format.color_scheme.groups :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
847 cf = format_color(group.color)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
848 for s in group.symbols :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
849 colors.append( " ("+s+") " + cf )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
850
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
851 subsitutions["color_dict"] = "\n".join(colors)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
852 data = []
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
853
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
854 # Unit conversion. 'None' for probability units
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
855 conv_factor = std_units[format.unit_name]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
856
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
857 data.append("StartLine")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
858
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
859
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
860 seq_from = format.logo_start- format.first_index
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
861 seq_to = format.logo_end - format.first_index +1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
862
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
863 # seq_index : zero based index into sequence data
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
864 # logo_index : User visible coordinate, first_index based
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
865 # stack_index : zero based index of visible stacks
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
866 for seq_index in range(seq_from, seq_to) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
867 logo_index = seq_index + format.first_index
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
868 stack_index = seq_index - seq_from
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
869
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
870 if stack_index!=0 and (stack_index % format.stacks_per_line) ==0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
871 data.append("")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
872 data.append("EndLine")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
873 data.append("StartLine")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
874 data.append("")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
875
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
876
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
877 if isreversed :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
878 data.append("(%d) StartStack" % logo_index)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
879 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
880 data.append("(%d) StartStack" % logo_index)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
881
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
882
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
883
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
884 if conv_factor:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
885 stack_height = logodata.entropy[seq_index] * std_units[format.unit_name]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
886 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
887 stack_height = 1.0 # Probability
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
888
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
889 # if logodata.entropy_interval is not None and conv_factor:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
890 # Draw Error bars
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
891 # low, high = logodata.entropy_interval[seq_index]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
892 # center = logodata.entropy[seq_index]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
893
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
894
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
895 # down = (center - low) * conv_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
896 # up = (high - center) * conv_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
897 # data.append(" %f %f %f DrawErrorbarFirst" % (down, up, stack_height) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
898
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
899 s = zip(logodata.counts[seq_index], logodata.alphabet)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
900 def mycmp( c1, c2 ) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
901 # Sort by frequency. If equal frequency then reverse alphabetic
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
902 if c1[0] == c2[0] : return cmp(c2[1], c1[1])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
903 return cmp(c1[0], c2[0])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
904
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
905 s.sort(mycmp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
906
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
907 C = float(sum(logodata.counts[seq_index]))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
908 if C > 0.0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
909 fraction_width = 1.0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
910 if format.scale_width :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
911 fraction_width = logodata.weight[seq_index]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
912 for c in s:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
913 data.append(" %f %f (%s) ShowSymbol" % (fraction_width, c[0]*stack_height/C, c[1]) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
914
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
915 # Draw error bar on top of logo. Replaced by DrawErrorbarFirst above.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
916 if logodata.entropy_interval is not None and conv_factor:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
917 low, high = logodata.entropy_interval[seq_index]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
918 center = logodata.entropy[seq_index]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
919
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
920 down = (center - low) * conv_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
921 up = (high - center) * conv_factor
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
922 data.append(" %f %f DrawErrorbar" % (down, up) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
923
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
924 data.append("EndStack")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
925 data.append("")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
926
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
927 data.append("EndLine")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
928 subsitutions["logo_data"] = "\n".join(data)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
929
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
930
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
931 # Create and output logo
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
932 template = resource_string( __name__, 'template.eps', __file__)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
933 logo = Template(template).substitute(subsitutions)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
934 print >>fout, logo
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
935
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
936
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
937 # map between output format names and logo
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
938 formatters = {
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
939 'eps': eps_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
940 'pdf': pdf_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
941 'png': png_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
942 'png_print' : png_print_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
943 'jpeg' : jpeg_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
944 'txt' : txt_formatter,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
945 }
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
946
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
947 default_formatter = eps_formatter
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
948
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
949
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
950
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
951
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
952
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
953 def parse_prior(composition, alphabet, weight=None) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
954 """ Parse a description of the expected monomer distribution of a sequence.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
955
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
956 Valid compositions:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
957
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
958 - None or 'none' : No composition sepecified
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
959 - 'auto' or 'automatic': Use the typical average distribution
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
960 for proteins and an equiprobable distribution for
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
961 everything else.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
962 - 'equiprobable' : All monomers have the same probability.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
963 - a percentage, e.g. '45%' or a fraction '0.45':
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
964 The fraction of CG bases for nucleotide alphabets
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
965 - a species name, e.g. 'E. coli', 'H. sapiens' :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
966 Use the average CG percentage for the specie's
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
967 genome.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
968 - An explicit distribution, e.g. {'A':10, 'C':40, 'G':40, 'T':10}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
969 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
970
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
971
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
972 if composition is None: return None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
973 comp = composition.strip()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
974
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
975 if comp.lower() == 'none': return None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
976
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
977 if weight is None and alphabet is not None: weight = float(len(alphabet))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
978 if comp.lower() == 'equiprobable' :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
979 prior = weight * equiprobable_distribution(len(alphabet))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
980
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
981
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
982 elif comp.lower() == 'auto' or comp.lower() == 'automatic':
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
983 if alphabet == unambiguous_protein_alphabet :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
984 prior = weight * asarray(aa_composition, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
985 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
986 prior = weight * equiprobable_distribution(len(alphabet))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
987 elif comp in std_percentCG :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
988 prior = weight * base_distribution(std_percentCG[comp])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
989
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
990 elif comp[-1] == '%' :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
991 prior = weight * base_distribution( float(comp[:-1]))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
992
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
993 elif isfloat(comp) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
994 prior = weight * base_distribution( float(comp)*100. )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
995
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
996 elif composition[0] == '{' and composition[-1] == '}' :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
997 explicit = composition[1: -1]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
998 explicit = explicit.replace(',',' ').replace("'", ' ').replace('"',' ').replace(':', ' ').split()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
999
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1000 if len(explicit) != len(alphabet)*2 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1001 #print explicit
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1002 raise ValueError("Explicit prior does not match length of alphabet")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1003 prior = - ones(len(alphabet), float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1004 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1005 for r in range(len(explicit)/2) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1006 letter = explicit[r*2]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1007 index = alphabet.index(letter)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1008 value = float(explicit[r*2 +1])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1009 prior[index] = value
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1010 except ValueError :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1011 raise ValueError("Cannot parse explicit composition")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1012
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1013 if any(prior==-1.) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1014 raise ValueError("Explicit prior does not match alphabet")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1015 prior/= sum(prior)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1016 prior *= weight
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1017
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1018
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1019 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1020 raise ValueError("Unknown or malformed composition: %s"%composition)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1021 if len(prior) != len(alphabet) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1022 raise ValueError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1023 "The sequence alphabet and composition are incompatible.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1024
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1025 return prior
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1026
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1027
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1028 def base_distribution(percentCG) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1029 A = (1. - (percentCG/100.))/2.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1030 C = (percentCG/100.)/2.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1031 G = (percentCG/100.)/2.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1032 T = (1. - (percentCG/100))/2.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1033 return asarray((A,C,G,T), float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1034
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1035 def equiprobable_distribution( length) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1036 return ones( (length), float64) /length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1037
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1038
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1039
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1040
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1041 def read_seq_data(fin, input_parser=seq_io.read,alphabet=None, ignore_lower_case=False, max_file_size=0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1042 # TODO: Document this method and enviroment variable
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1043 max_file_size =int(os.environ.get("WEBLOGO_MAX_FILE_SIZE", max_file_size))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1044
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1045 # If max_file_size is set, or if fin==stdin (which is non-seekable), we
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1046 # read the data and replace fin with a StringIO object.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1047
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1048 if(max_file_size>0) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1049 data = fin.read(max_file_size)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1050
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1051 more_data = fin.read(2)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1052 if more_data != "" :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1053 raise IOError("File exceeds maximum allowed size: %d bytes" % max_file_size)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1054
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1055 fin = StringIO(data)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1056 elif fin == sys.stdin:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1057 fin = StringIO(fin.read())
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1058
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1059 seqs = input_parser(fin)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1060
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1061 if seqs is None or len(seqs) ==0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1062 raise ValueError("Please provide a multiple sequence alignment")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1063
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1064 if ignore_lower_case :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1065 # Case is significant. Do not count lower case letters.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1066 for i,s in enumerate(seqs) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1067 seqs[i] = s.mask()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1068
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1069 global altype
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1070 if(altype=="codonsT" or altype=="codonsU"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1071 if 'T' in seqs[0] or 't' in seqs[0]:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1072 altype="codonsT"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1073 if 'U' in seqs[0] or 'u' in seqs[0]:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1074 altype="codonsU"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1075 global offset
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1076 global isreversed
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1077 seq2=[""]*len(seqs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1078 seq2 = []
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1079 for i in xrange(len(seqs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1080 seq2.append([])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1081 if(offset%6>2):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1082 for x in range(0,len(seqs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1083 backs=seqs[x][::-1]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1084 for y in range(0,len(backs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1085 seq2[x].append(str(backs[y]))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1086
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1087 if(altype=="codonsU"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1088 for x in range(0,len(seq2)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1089 for y in range(0,len(seq2[x])):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1090 if(cmp(seq2[x][y],'G')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1091 seq2[x][y]="C"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1092 elif(cmp(seq2[x][y],'A')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1093 seq2[x][y]='U'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1094 elif(cmp(seq2[x][y],'U')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1095 seq2[x][y]='A'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1096 elif(cmp(seq2[x][y],'C')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1097 seq2[x][y]='G'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1098 if(altype=="codonsT"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1099 for x in range(0,len(seq2)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1100 for y in range(0,len(seq2[x])):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1101 if(cmp(seq2[x][y],'G')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1102 seq2[x][y]='C'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1103 elif(cmp(seq2[x][y],'A')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1104 seq2[x][y]='T'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1105 elif(cmp(seq2[x][y],'T')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1106 seq2[x][y]='A'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1107 elif(cmp(seq2[x][y],'C')==0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1108 seq2[x][y]='G'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1109 offset=offset%3
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1110 isreversed=True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1111 for x in range(0,len(seqs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1112 seqs[x]=Seq("".join(seq2[x]))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1113
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1114
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1115 # Add alphabet to seqs.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1116 if alphabet :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1117 seqs.alphabet = alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1118 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1119 seqs.alphabet = which_alphabet(seqs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1120
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1121 return seqs
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1122
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1123
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1124
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1125 #TODO: Move to seq_io?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1126 # Would have to check that no symbol outside of full alphabet?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1127 def which_alphabet(seqs) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1128 """ Returns the most appropriate unambiguous protien, rna or dna alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1129 for a Seq or SeqList.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1130 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1131 alphabets = (unambiguous_protein_alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1132 unambiguous_rna_alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1133 unambiguous_dna_alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1134 )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1135 # Heuristic
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1136 # Count occurances of each letter. Downweight longer alphabet.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1137 #for x in seqs:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1138
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1139
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1140 if( altype=="codonsU"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1141 return codon_alphabetU
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1142 if( altype=="codonsT"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1143 return codon_alphabetT
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1144 else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1145 score = [1.0*asarray(seqs.tally(a)).sum()/sqrt(len(a)) for a in alphabets]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1146 #print score
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1147 best = argmax(score) # Ties go to last in list.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1148 a = alphabets[best]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1149 return a
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1150
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1151
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1152
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1153 class LogoData(object) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1154 """The data needed to generate a sequence logo.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1155
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1156 - alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1157 - length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1158 - counts -- An array of character counts
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1159 - entropy -- The relative entropy of each column
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1160 - entropy_interval -- entropy confidence interval
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1161 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1162
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1163 def __init__(self, length=None, alphabet = None, counts =None,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1164 entropy =None, entropy_interval = None, weight=None) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1165 """Creates a new LogoData object"""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1166 self.length = length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1167 self.alphabet = alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1168 self.counts = counts
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1169 self.entropy = entropy
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1170 self.entropy_interval = entropy_interval
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1171 self.weight = weight
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1172
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1173
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1174
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1175 #@classmethod
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1176 def from_counts(cls, alphabet, counts, prior= None):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1177
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1178 """Build a logodata object from counts."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1179 seq_length, A = counts.shape
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1180
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1181 if prior is not None: prior = array(prior, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1182 if prior is None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1183 R = log(A)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1184 ent = zeros( seq_length, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1185 entropy_interval = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1186 for i in range (0, seq_length) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1187 C = sum(counts[i])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1188 #FIXME: fixup corebio.moremath.entropy()?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1189 if C == 0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1190 ent[i] = 0.0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1191 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1192 ent[i] = R - entropy(counts[i])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1193 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1194 ent = zeros( seq_length, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1195 entropy_interval = zeros( (seq_length,2) , float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1196
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1197 R = log(A)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1198
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1199 for i in range (0, seq_length) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1200 alpha = array(counts[i] , float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1201 alpha += prior
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1202
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1203 posterior = Dirichlet(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1204 ent[i] = posterior.mean_relative_entropy(prior/sum(prior))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1205 entropy_interval[i][0], entropy_interval[i][1] = \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1206 posterior.interval_relative_entropy(prior/sum(prior), 0.95)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1207 weight = array( na.sum(counts,axis=1) , float)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1208
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1209 weight /= max(weight)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1210
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1211 return cls(seq_length, alphabet, counts, ent, entropy_interval, weight)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1212 from_counts = classmethod(from_counts)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1213
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1214
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1215 #@classmethod
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1216 def from_seqs(cls, seqs, prior= None):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1217
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1218
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1219 alphabet=seqs.alphabet
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1220
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1221 #get the offset and if it's greater than 2 swap bases to get the reverse compliment and reverse.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1222 for x in range(0,len(seqs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1223 seqs[x]=seqs[x].upper()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1224 counter=0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1225
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1226
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1227 """Build a 2D array from a SeqList, a list of sequences."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1228 # --- VALIDATE DATA ---
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1229 # check that there is at least one sequence of length 1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1230 if len(seqs)==0 or len(seqs[0]) ==0:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1231 raise ValueError("No sequence data found.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1232 sys.exit(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1233 # Check sequence lengths
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1234 seq_length = len(seqs[0])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1235 for i,s in enumerate(seqs) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1236 #print i,s, len(s)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1237 if seq_length != len(s) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1238 raise ArgumentError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1239 "Sequence number %d differs in length from the previous sequences" % (i+1) ,'sequences')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1240 sys.exit(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1241
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1242 if(altype=="codonsT" or altype=="codonsU"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1243 x = [[0]*64 for x in xrange(seq_length/3)]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1244 counter=offset
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1245
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1246 while counter+offset<seq_length:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1247 for i in range(0,len(seqs)):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1248 if len(str(seqs[i][(counter):(counter+3)]))==3 and len(seqs[i][(counter):(counter+3)].strip("GATUC"))==0 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1249 if(str(seqs[i][(counter):(counter+3)]) in alphabet):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1250 x[counter/3][ (alphabet.index(str(seqs[i][(counter):(counter+3)]))) ]+=1
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1251 counter=counter+3
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1252 counts=asarray(x)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1253 else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1254 counts = asarray(seqs.tally())
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1255
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1256 return cls.from_counts(alphabet, counts, prior)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1257 from_seqs = classmethod(from_seqs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1258
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1259
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1260
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1261 def __str__(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1262 out = StringIO()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1263 print >>out, '## LogoData'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1264 print >>out, '# First column is position number, couting from zero'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1265 print >>out, '# Subsequent columns are raw symbol counts'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1266 print >>out, '# Entropy is mean entropy measured in nats.'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1267 print >>out, '# Low and High are the 95% confidence limits.'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1268 print >>out, '# Weight is the fraction of non-gap symbols in the column.'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1269 print >>out, '#\t'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1270 print >>out, '#\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1271 for a in self.alphabet :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1272 print >>out, a, '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1273 print >>out, 'Entropy\tLow\tHigh\tWeight'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1274
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1275 for i in range(self.length) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1276 print >>out, i, '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1277 for c in self.counts[i] : print >>out, c, '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1278 print >>out, self.entropy[i], '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1279 if self.entropy_interval is not None:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1280 print >>out, self.entropy_interval[i][0], '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1281 print >>out, self.entropy_interval[i][1], '\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1282 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1283 print >>out, '\t','\t',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1284 if self.weight is not None :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1285 print >>out, self.weight[i],
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1286 print >>out, ''
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1287 print >>out, '# End LogoData'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1288
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1289 return out.getvalue()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1290
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1291 # ====================== Main: Parse Command line =============================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1292 def main():
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1293 """CodonLogo command line interface """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1294
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1295 # ------ Parse Command line ------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1296 parser = _build_option_parser()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1297 (opts, args) = parser.parse_args(sys.argv[1:])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1298 if args : parser.error("Unparsable arguments: %s " % args)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1299
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1300 if opts.serve:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1301 httpd_serve_forever(opts.port) # Never returns?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1302 sys.exit(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1303
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1304
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1305 # ------ Create Logo ------
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1306 try:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1307 data = _build_logodata(opts)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1308
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1309
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1310 format = _build_logoformat(data, opts)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1311
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1312
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1313 formatter = opts.formatter
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1314 formatter(data, format, opts.fout)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1315
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1316 except ValueError, err :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1317 print >>sys.stderr, 'Error:', err
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1318 sys.exit(2)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1319 except KeyboardInterrupt, err:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1320 sys.exit(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1321 # End main()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1322
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1323
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1324 def httpd_serve_forever(port=8080) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1325 """ Start a webserver on a local port."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1326 import BaseHTTPServer
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1327 import CGIHTTPServer
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1328
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1329 class __HTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1330 def is_cgi(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1331 if self.path == "/create.cgi":
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1332 self.cgi_info = '', 'create.cgi'
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1333 return True
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1334 return False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1335
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1336 # Add current directory to PYTHONPATH. This is
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1337 # so that we can run the standalone server
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1338 # without having to run the install script.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1339 pythonpath = os.getenv("PYTHONPATH", '')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1340 pythonpath += ":" + os.path.abspath(sys.path[0]).split()[0]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1341 os.environ["PYTHONPATH"] = pythonpath
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1342
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1343 htdocs = resource_filename(__name__, 'htdocs', __file__)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1344 os.chdir(htdocs)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1345
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1346 HandlerClass = __HTTPRequestHandler
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1347 ServerClass = BaseHTTPServer.HTTPServer
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1348 httpd = ServerClass(('', port), HandlerClass)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1349 print "Serving HTTP on localhost:%d ..." % port
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1350
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1351 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1352 httpd.serve_forever()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1353 except KeyboardInterrupt:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1354 sys.exit(0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1355 # end httpd_serve_forever()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1356
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1357 def read_priors(finp, alphabet ,max_file_size=0):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1358
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1359 max_file_size =int(os.environ.get("WEBLOGO_MAX_FILE_SIZE", max_file_size))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1360 if(max_file_size>0) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1361 data = finp.read(max_file_size)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1362 more_data = finp.read(2)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1363 if more_data != "" :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1364 raise IOError("File exceeds maximum allowed size: %d bytes" % max_file_size)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1365 finp = StringIO(data)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1366 priordict={}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1367 while 1:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1368 line = finp.readline()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1369 if not line:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1370 break
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1371 line = line.split()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1372 priordict[line[0]]=line[1]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1373 return priordict
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1374
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1375 def _build_logodata(options) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1376 global offset
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1377 offset=options.frame
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1378 options.alphabet = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1379 options.ignore_lower_case = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1380 #options.default_color = Color.by_name("black")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1381 options.color_scheme=None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1382 #options.colors=[]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1383 options.show_ends=False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1384 seqs = read_seq_data(options.fin,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1385 options.input_parser.read,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1386 alphabet=options.alphabet,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1387 ignore_lower_case = options.ignore_lower_case)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1388 if(options.priorfile!=None):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1389 if(altype=="CodonsT"):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1390 options.composition= str(read_priors(options.priorfile,codon_alphabetT))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1391 options.alphabet = codon_alphabetT
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1392 else:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1393 options.composition= str(read_priors(options.priorfile,codon_alphabetU))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1394 options.alphabet = codon_alphabetU
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1395
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1396 prior = parse_prior( options.composition,seqs.alphabet, options.weight)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1397 data = LogoData.from_seqs(seqs, prior)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1398 return data
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1399
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1400
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1401 def _build_logoformat( logodata, opts) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1402 """ Extract and process relevant option values and return a
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1403 LogoFormat object."""
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1404
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1405 args = {}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1406 direct_from_opts = [
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1407 "stacks_per_line",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1408 "logo_title",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1409 "yaxis_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1410 "show_xaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1411 "show_yaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1412 "xaxis_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1413 "show_ends",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1414 "fineprint",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1415 "show_errorbars",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1416 "show_boxes",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1417 "yaxis_tic_interval",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1418 "resolution",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1419 "alphabet",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1420 "debug",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1421 "show_ends",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1422 "default_color",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1423 #"show_color_key",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1424 "color_scheme",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1425 "unit_name",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1426 "logo_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1427 "yaxis_scale",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1428 "first_index",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1429 "logo_start",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1430 "logo_end",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1431 "scale_width",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1432 "frame",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1433 ]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1434
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1435 for k in direct_from_opts:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1436 args[k] = opts.__dict__[k]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1437 logo_size = copy.copy(opts.__dict__['logo_size'])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1438 size_from_opts = ["stack_width", "stack_height"]
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1439 for k in size_from_opts :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1440 length = getattr(opts, k)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1441 if length : setattr( logo_size, k, length )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1442 args["size"] = logo_size
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1443
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1444 global col
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1445
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1446 if opts.colors:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1447 color_scheme = ColorScheme()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1448 for color, symbols, desc in opts.colors:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1449 try :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1450 #c = Color.from_string(color)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1451 color_scheme.groups.append( ColorGroup(symbols, color, desc) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1452 #print >> sys.stderr, color
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1453
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1454 col.append( ColorGroup(symbols, color, desc) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1455
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1456 except ValueError :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1457 raise ValueError(
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1458 "error: option --color: invalid value: '%s'" % color )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1459 if(altype!="codonsU" and altype!="codonsT") :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1460 args["color_scheme"] = color_scheme
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1461
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1462 #cf = colorscheme.format_color(col[0])
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1463 #col.append( " ("+group.symbols+") " + cf )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1464
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1465 logooptions = LogoOptions()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1466 for a, v in args.iteritems() :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1467 setattr(logooptions,a,v)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1468
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1469
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1470
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1471 theformat = LogoFormat(logodata, logooptions )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1472
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1473 return theformat
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1474
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1475
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1476
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1477
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1478
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1479
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1480 # ========================== OPTIONS ==========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1481 def _build_option_parser() :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1482 defaults = LogoOptions()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1483 parser = DeOptionParser(usage="%prog [options] < sequence_data.fa > sequence_logo.eps",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1484 description = description,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1485 version = __version__ ,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1486 add_verbose_options = False
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1487 )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1488
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1489 io_grp = OptionGroup(parser, "Input/Output Options",)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1490 data_grp = OptionGroup(parser, "Logo Data Options",)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1491 format_grp = OptionGroup(parser, "Logo Format Options",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1492 "These options control the format and display of the logo.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1493 color_grp = OptionGroup(parser, "Color Options",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1494 "Colors can be specified using CSS2 syntax. e.g. 'red', '#FF0000', etc.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1495 advanced_grp = OptionGroup(parser, "Advanced Format Options",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1496 "These options provide fine control over the display of the logo. ")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1497 server_grp = OptionGroup(parser, "CodonLogo Server",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1498 "Run a standalone webserver on a local port.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1499
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1500
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1501 parser.add_option_group(io_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1502 parser.add_option_group(data_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1503 parser.add_option_group(format_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1504 parser.add_option_group(color_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1505 parser.add_option_group(advanced_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1506 parser.add_option_group(server_grp)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1507
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1508 # ========================== IO OPTIONS ==========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1509
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1510
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1511
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1512 io_grp.add_option( "-f", "--fin",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1513 dest="fin",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1514 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1515 type="file_in",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1516 default=sys.stdin,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1517 help="Sequence input file (default: stdin)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1518 metavar="FILENAME")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1519
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1520 io_grp.add_option( "-R", "--prior",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1521 dest="priorfile",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1522 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1523 type="file_in",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1524 help="A file with 64 codons and their prior probabilities, one per line, each codon followed by a space and it's probability.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1525 metavar="FILENAME")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1526
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1527 io_grp.add_option("", "--fin-format",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1528 dest="input_parser",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1529 action="store", type ="dict",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1530 default = seq_io,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1531 choices = seq_io.format_names(),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1532 help="Multiple sequence alignment format: (%s)" %
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1533 ', '.join([ f.names[0] for f in seq_io.formats]),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1534 metavar="FORMAT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1535
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1536 io_grp.add_option("-o", "--fout", dest="fout",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1537 type="file_out",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1538 default=sys.stdout,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1539 help="Output file (default: stdout)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1540 metavar="FILENAME")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1541
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1542
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1543
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1544 io_grp.add_option( "-F", "--format",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1545 dest="formatter",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1546 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1547 type="dict",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1548 choices = formatters,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1549 metavar= "FORMAT",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1550 help="Format of output: eps (default), png, png_print, pdf, jpeg, txt",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1551 default = default_formatter)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1552
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1553
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1554 # ========================== Data OPTIONS ==========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1555
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1556 data_grp.add_option("-m", "--frame",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1557 dest="frame",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1558 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1559 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1560 default=0,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1561 help="Offset of the reading frame you wish to look in (default: 0) 3-6 indicate the reverse complement in which case codonlogo will read from the last symbol in the sequences backwards and replace each base with it's complement.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1562 metavar="NUMBER")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1563
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1564 #data_grp.add_option("-T", "--type",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1565 #dest="altype",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1566 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1567 #type="boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1568 #default=True,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1569 #help="Generate a codon logo rather than a sequence logo (default: True)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1570 #metavar="YES/NO")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1571
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1572
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1573
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1574 #data_grp.add_option( "-A", "--sequence-type",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1575 #dest="alphabet",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1576 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1577 #type="dict",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1578 #choices = std_alphabets,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1579 #help="The type of sequence data: 'protein', 'rna' or 'dna'.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1580 #metavar="TYPE")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1581
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1582 #data_grp.add_option( "-a", "--alphabet",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1583 #dest="alphabet",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1584 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1585 #help="The set of symbols to count, e.g. 'AGTC'. "
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1586 #"All characters not in the alphabet are ignored. "
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1587 #"If neither the alphabet nor sequence-type are specified then codonlogo will examine the input data and make an educated guess. "
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1588 #"See also --sequence-type, --ignore-lower-case" )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1589
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1590 # FIXME Add test?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1591 #data_grp.add_option( "", "--ignore-lower-case",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1592 #dest="ignore_lower_case",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1593 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1594 #type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1595 #default=False,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1596 #metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1597 #help="Disregard lower case letters and only count upper case letters in sequences? (Default: No)"
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1598 #)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1599
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1600 data_grp.add_option( "-U", "--units",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1601 dest="unit_name",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1602 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1603 choices = std_units.keys(),
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1604 type="choice",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1605 default = defaults.unit_name,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1606 help="A unit of entropy ('bits' (default), 'nats', 'digits'), or a unit of free energy ('kT', 'kJ/mol', 'kcal/mol'), or 'probability' for probabilities",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1607 metavar = "NUMBER")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1608
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1609
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1610 data_grp.add_option( "", "--composition",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1611 dest="composition",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1612 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1613 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1614 default = "auto",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1615 help="The expected composition of the sequences: 'auto' (default), 'equiprobable', 'none' (Do not perform any compositional adjustment), ",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1616 metavar="COMP.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1617
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1618 data_grp.add_option( "", "--weight",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1619 dest="weight",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1620 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1621 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1622 default = None,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1623 help="The weight of prior data. Default: total pseudocounts equal to the number of monomer types.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1624 metavar="NUMBER")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1625
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1626 data_grp.add_option( "-i", "--first-index",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1627 dest="first_index",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1628 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1629 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1630 default = 1,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1631 help="Index of first position in sequence data (default: 1)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1632 metavar="INDEX")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1633
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1634 data_grp.add_option( "-l", "--lower",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1635 dest="logo_start",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1636 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1637 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1638 help="Lower bound of sequence to display",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1639 metavar="INDEX")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1640
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1641 data_grp.add_option( "-u", "--upper",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1642 dest="logo_end",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1643 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1644 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1645 help="Upper bound of sequence to display",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1646 metavar="INDEX")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1647
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1648 # ========================== FORMAT OPTIONS ==========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1649
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1650 format_grp.add_option( "-s", "--size",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1651 dest="logo_size",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1652 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1653 type ="dict",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1654 choices = std_sizes,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1655 metavar = "LOGOSIZE",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1656 default = defaults.size,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1657 help="Specify a standard logo size (small, medium (default), large)" )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1658
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1659
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1660
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1661 format_grp.add_option( "-n", "--stacks-per-line",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1662 dest="stacks_per_line",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1663 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1664 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1665 help="Maximum number of logo stacks per logo line. (default: %default)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1666 default = defaults.stacks_per_line,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1667 metavar="COUNT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1668
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1669 format_grp.add_option( "-t", "--title",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1670 dest="logo_title",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1671 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1672 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1673 help="Logo title text.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1674 default = defaults.logo_title,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1675 metavar="TEXT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1676
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1677 format_grp.add_option( "", "--label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1678 dest="logo_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1679 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1680 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1681 help="A figure label, e.g. '2a'",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1682 default = defaults.logo_label,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1683 metavar="TEXT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1684
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1685 format_grp.add_option( "-X", "--show-xaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1686 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1687 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1688 default= defaults.show_xaxis,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1689 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1690 help="Display sequence numbers along x-axis? (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1691
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1692 format_grp.add_option( "-x", "--xlabel",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1693 dest="xaxis_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1694 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1695 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1696 default = defaults.xaxis_label,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1697 help="X-axis label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1698 metavar="TEXT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1699
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1700 format_grp.add_option( "-S", "--yaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1701 dest="yaxis_scale",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1702 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1703 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1704 help="Height of yaxis in units. (Default: Maximum value with uninformative prior.)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1705 metavar = "UNIT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1706
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1707 format_grp.add_option( "-Y", "--show-yaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1708 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1709 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1710 dest = "show_yaxis",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1711 default= defaults.show_yaxis,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1712 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1713 help="Display entropy scale along y-axis? (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1714
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1715 format_grp.add_option( "-y", "--ylabel",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1716 dest="yaxis_label",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1717 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1718 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1719 help="Y-axis label (default depends on plot type and units)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1720 metavar="TEXT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1721
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1722 #format_grp.add_option( "-E", "--show-ends",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1723 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1724 #type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1725 #default= defaults.show_ends,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1726 #metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1727 #help="Label the ends of the sequence? (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1728
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1729 format_grp.add_option( "-P", "--fineprint",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1730 dest="fineprint",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1731 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1732 type="string",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1733 default= defaults.fineprint,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1734 help="The fine print (default: Codonlogo version)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1735 metavar="TEXT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1736
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1737 format_grp.add_option( "", "--ticmarks",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1738 dest="yaxis_tic_interval",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1739 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1740 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1741 default= defaults.yaxis_tic_interval,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1742 help="Distance between ticmarks (default: %default)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1743 metavar = "NUMBER")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1744
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1745
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1746 format_grp.add_option( "", "--errorbars",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1747 dest = "show_errorbars",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1748 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1749 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1750 default= defaults.show_errorbars,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1751 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1752 help="Display error bars? (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1753
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1754
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1755
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1756 # ========================== Color OPTIONS ==========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1757 # TODO: Future Feature
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1758 # color_grp.add_option( "-K", "--color-key",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1759 # dest= "show_color_key",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1760 # action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1761 # type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1762 # default= defaults.show_color_key,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1763 # metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1764 # help="Display a color key (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1765
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1766
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1767 #color_scheme_choices = std_color_schemes.keys()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1768 #color_scheme_choices.sort()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1769 #color_grp.add_option( "-c", "--color-scheme",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1770 #dest="color_scheme",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1771 #action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1772 #type ="dict",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1773 #choices = std_color_schemes,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1774 #metavar = "SCHEME",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1775 #default = None, # Auto
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1776 #help="Specify a standard color scheme (%s)" % \
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1777 #", ".join(color_scheme_choices) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1778
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1779 color_grp.add_option( "-C", "--color",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1780 dest="colors",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1781 action="append",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1782 metavar="COLOR SYMBOLS DESCRIPTION ",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1783 nargs = 3,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1784 default=[],
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1785 help="Specify symbol colors, e.g. --color black AG 'Purine' --color red TC 'Pyrimidine' ")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1786
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1787 color_grp.add_option( "", "--default-color",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1788 dest="default_color",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1789 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1790 metavar="COLOR",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1791 default= defaults.default_color,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1792 help="Symbol color if not otherwise specified.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1793
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1794 # ========================== Advanced options =========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1795
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1796 advanced_grp.add_option( "-W", "--stack-width",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1797 dest="stack_width",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1798 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1799 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1800 default= None,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1801 help="Width of a logo stack (default: %s)"% defaults.size.stack_width,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1802 metavar="POINTS" )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1803
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1804 advanced_grp.add_option( "-H", "--stack-height",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1805 dest="stack_height",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1806 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1807 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1808 default= None,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1809 help="Height of a logo stack (default: %s)"%defaults.size.stack_height,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1810 metavar="POINTS" )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1811
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1812 advanced_grp.add_option( "", "--box",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1813 dest="show_boxes",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1814 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1815 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1816 default=False,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1817 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1818 help="Draw boxes around symbols? (default: no)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1819
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1820 advanced_grp.add_option( "", "--resolution",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1821 dest="resolution",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1822 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1823 type="float",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1824 default=96,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1825 help="Bitmap resolution in dots per inch (DPI). (default: 96 DPI, except png_print, 600 DPI) Low resolution bitmaps (DPI<300) are antialiased.",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1826 metavar="DPI")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1827
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1828 advanced_grp.add_option( "", "--scale-width",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1829 dest="scale_width",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1830 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1831 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1832 default= True,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1833 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1834 help="Scale the visible stack width by the fraction of symbols in the column? (i.e. columns with many gaps of unknowns are narrow.) (default: yes)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1835
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1836 advanced_grp.add_option( "", "--debug",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1837 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1838 type = "boolean",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1839 default= defaults.debug,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1840 metavar = "YES/NO",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1841 help="Output additional diagnostic information. (default: %default)")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1842
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1843
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1844 # ========================== Server options =========================
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1845 server_grp.add_option( "", "--serve",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1846 dest="serve",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1847 action="store_true",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1848 default= False,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1849 help="Start a standalone CodonLogo server for creating sequence logos.")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1850
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1851 server_grp.add_option( "", "--port",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1852 dest="port",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1853 action="store",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1854 type="int",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1855 default= 8080,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1856 help="Listen to this local port. (Default: %default)",
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1857 metavar="PORT")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1858
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1859 return parser
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1860
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1861 # END _build_option_parser
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1862
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1863
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1864 ##############################################################
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1865
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1866 class Dirichlet(object) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1867 """The Dirichlet probability distribution. The Dirichlet is a continuous
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1868 multivariate probability distribution across non-negative unit length
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1869 vectors. In other words, the Dirichlet is a probability distribution of
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1870 probability distributions. It is conjugate to the multinomial
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1871 distribution and is widely used in Bayesian statistics.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1872
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1873 The Dirichlet probability distribution of order K-1 is
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1874
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1875 p(theta_1,...,theta_K) d theta_1 ... d theta_K =
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1876 (1/Z) prod_i=1,K theta_i^{alpha_i - 1} delta(1 -sum_i=1,K theta_i)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1877
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1878 The normalization factor Z can be expressed in terms of gamma functions:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1879
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1880 Z = {prod_i=1,K Gamma(alpha_i)} / {Gamma( sum_i=1,K alpha_i)}
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1881
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1882 The K constants, alpha_1,...,alpha_K, must be positive. The K parameters,
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1883 theta_1,...,theta_K are nonnegative and sum to 1.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1884
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1885 Status:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1886 Alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1887 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1888 __slots__ = 'alpha', '_total', '_mean',
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1889
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1890
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1891
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1892
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1893 def __init__(self, alpha) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1894 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1895 Args:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1896 - alpha -- The parameters of the Dirichlet prior distribution.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1897 A vector of non-negative real numbers.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1898 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1899 # TODO: Check that alphas are positive
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1900 #TODO : what if alpha's not one dimensional?
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1901 self.alpha = asarray(alpha, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1902
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1903 self._total = sum(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1904 self._mean = None
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1905
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1906
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1907 def sample(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1908 """Return a randomly generated probability vector.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1909
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1910 Random samples are generated by sampling K values from gamma
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1911 distributions with parameters a=\alpha_i, b=1, and renormalizing.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1912
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1913 Ref:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1914 A.M. Law, W.D. Kelton, Simulation Modeling and Analysis (1991).
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1915 Authors:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1916 Gavin E. Crooks <gec@compbio.berkeley.edu> (2002)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1917 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1918 alpha = self.alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1919 K = len(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1920 theta = zeros( (K,), float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1921
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1922 for k in range(K):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1923 theta[k] = random.gammavariate(alpha[k], 1.0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1924 theta /= sum(theta)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1925
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1926 return theta
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1927
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1928 def mean(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1929 if self._mean ==None:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1930 self._mean = self.alpha / self._total
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1931 return self._mean
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1932
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1933 def covariance(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1934 alpha = self.alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1935 A = sum(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1936 #A2 = A * A
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1937 K = len(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1938 cv = zeros( (K,K), float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1939
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1940 for i in range(K) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1941 cv[i,i] = alpha[i] * (1. - alpha[i]/A) / (A * (A+1.) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1942
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1943 for i in range(K) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1944 for j in range(i+1,K) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1945 v = - alpha[i] * alpha[j] / (A * A * (A+1.) )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1946 cv[i,j] = v
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1947 cv[j,i] = v
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1948 return cv
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1949
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1950 def mean_x(self, x) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1951 x = asarray(x, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1952 if shape(x) != shape(self.alpha) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1953 raise ValueError("Argument must be same dimension as Dirichlet")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1954 return sum( x * self.mean())
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1955
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1956 def variance_x(self, x) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1957 x = asarray(x, float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1958 if shape(x) != shape(self.alpha) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1959 raise ValueError("Argument must be same dimension as Dirichlet")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1960
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1961 cv = self.covariance()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1962 var = na.dot(na.dot(na.transpose( x), cv), x)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1963 return var
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1964
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1965
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1966 def mean_entropy(self) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1967 """Calculate the average entropy of probabilities sampled
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1968 from this Dirichlet distribution.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1969
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1970 Returns:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1971 The average entropy.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1972
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1973 Ref:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1974 Wolpert & Wolf, PRE 53:6841-6854 (1996) Theorem 7
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1975 (Warning: this paper contains typos.)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1976 Status:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1977 Alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1978 Authors:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1979 GEC 2005
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1980
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1981 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1982 # TODO: Optimize
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1983 alpha = self.alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1984 A = float(sum(alpha))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1985 ent = 0.0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1986 for a in alpha:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1987 if a>0 : ent += - 1.0 * a * digamma( 1.0+a) # FIXME: Check
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1988 ent /= A
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1989 ent += digamma(A+1.0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1990 return ent
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1991
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1992
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1993
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1994 def variance_entropy(self):
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1995 """Calculate the variance of the Dirichlet entropy.
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1996
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1997 Ref:
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1998 Wolpert & Wolf, PRE 53:6841-6854 (1996) Theorem 8
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
1999 (Warning: this paper contains typos.)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2000 """
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2001 alpha = self.alpha
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2002 A = float(sum(alpha))
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2003 A2 = A * (A+1)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2004 L = len(alpha)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2005
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2006 dg1 = zeros( (L) , float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2007 dg2 = zeros( (L) , float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2008 tg2 = zeros( (L) , float64)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2009
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2010 for i in range(L) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2011 dg1[i] = digamma(alpha[i] + 1.0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2012 dg2[i] = digamma(alpha[i] + 2.0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2013 tg2[i] = trigamma(alpha[i] + 2.0)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2014
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2015 dg_Ap2 = digamma( A+2. )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2016 tg_Ap2 = trigamma( A+2. )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2017
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2018 mean = self.mean_entropy()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2019 var = 0.0
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2020
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2021 for i in range(L) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2022 for j in range(L) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2023 if i != j :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2024 var += (
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2025 ( dg1[i] - dg_Ap2 ) * (dg1[j] - dg_Ap2 ) - tg_Ap2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2026 ) * (alpha[i] * alpha[j] ) / A2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2027 else :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2028 var += (
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2029 ( dg2[i] - dg_Ap2 ) **2 + ( tg2[i] - tg_Ap2 )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2030 ) * ( alpha[i] * (alpha[i]+1.) ) / A2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2031
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2032 var -= mean**2
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2033 return var
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2034
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2035
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2036
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2037 def mean_relative_entropy(self, pvec) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2038 ln_p = na.log(pvec)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2039 return - self.mean_x(ln_p) - self.mean_entropy()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2040
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2041
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2042 def variance_relative_entropy(self, pvec) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2043 ln_p = na.log(pvec)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2044 return self.variance_x(ln_p) + self.variance_entropy()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2045
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2046
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2047 def interval_relative_entropy(self, pvec, frac) :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2048 mean = self.mean_relative_entropy(pvec)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2049 variance = self.variance_relative_entropy(pvec)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2050 # If the variance is small, use the standard 95%
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2051 # confidence interval: mean +/- 1.96 * sd
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2052 if variance< 0.1 :
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2053 sd = sqrt(variance)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2054 return max(0.0, mean - sd*1.96), mean + sd*1.96
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2055 sd = sqrt(variance)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2056 return max(0.0, mean - sd*1.96), mean + sd*1.96
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2057
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2058 g = gamma.from_mean_variance(mean, variance)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2059 low_limit = g.inverse_cdf( (1.-frac)/2.)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2060 high_limit = g.inverse_cdf( 1. - (1.-frac)/2. )
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2061
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2062 return low_limit, high_limit
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2063
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2064
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2065 # Standard python voodoo for CLI
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2066 if __name__ == "__main__":
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2067 ## Code Profiling. Uncomment these lines
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2068 #import hotshot, hotshot.stats
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2069 #prof = hotshot.Profile("stones.prof")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2070 #prof.runcall(main)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2071 #prof.close()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2072 #stats = hotshot.stats.load("stones.prof")
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2073 #stats.strip_dirs()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2074 #stats.sort_stats('cumulative', 'calls')
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2075 #stats.print_stats(40)
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2076 #sys.exit()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2077
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2078 main()
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2079
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2080
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2081
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2082
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2083
4d47ab2b7bcc Uploaded
davidmurphy
parents:
diff changeset
2084