annotate weblogolib/__init__.py @ 15:981eb8c3a756 default tip

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