annotate weblogolib/__init__.py @ 0:c55bdc2fb9fa

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