| 1 |
# Copyright (c) 2003-2005 The Regents of the University of California. |
| 2 |
# Copyright (c) 2005 Gavin E. Crooks |
| 3 |
|
| 4 |
# This software is distributed under the MIT Open Source License. |
| 5 |
# <http://www.opensource.org/licenses/mit-license.html> |
| 6 |
# |
| 7 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
# copy of this software and associated documentation files (the "Software"), |
| 9 |
# to deal in the Software without restriction, including without limitation |
| 10 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
# Software is furnished to do so, subject to the following conditions: |
| 13 |
# |
| 14 |
# The above copyright notice and this permission notice shall be included |
| 15 |
# in all copies or substantial portions of the Software. |
| 16 |
# |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
# THE SOFTWARE. |
| 24 |
|
| 25 |
"""Popular color codings for nucleic and amino acids. |
| 26 |
|
| 27 |
Classes: |
| 28 |
ColorScheme -- A color scheme |
| 29 |
SymbolColor |
| 30 |
IndexColor |
| 31 |
RefSeqColor |
| 32 |
|
| 33 |
Generic |
| 34 |
monochrome |
| 35 |
|
| 36 |
Nucleotides |
| 37 |
nucleotide |
| 38 |
base pairing |
| 39 |
|
| 40 |
Amino Acid |
| 41 |
hydrophobicity |
| 42 |
chemistry |
| 43 |
charge |
| 44 |
taylor |
| 45 |
|
| 46 |
Status : Beta - Needs documentation. |
| 47 |
|
| 48 |
""" |
| 49 |
# Good online references include bioruby and the JalView alignment editor. |
| 50 |
# Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), |
| 51 |
# "The Jalview Java Alignment Editor," Bioinformatics, 12, 426-7 |
| 52 |
# http://www.jalview.org |
| 53 |
|
| 54 |
from typing import List, Optional, Sequence |
| 55 |
|
| 56 |
from . import seq |
| 57 |
from .color import Color |
| 58 |
from .seq import Alphabet |
| 59 |
|
| 60 |
|
| 61 |
# TODO: Make as abstract |
| 62 |
class ColorRule: |
| 63 |
""" |
| 64 |
Define an interface for coloring individual symbols based on their position |
| 65 |
and identity. Subclasses should reimplement the symbol_color() method to |
| 66 |
return a Color object based on the given parameters. |
| 67 |
""" |
| 68 |
|
| 69 |
def symbol_color(self, seq_index: int, symbol: str, rank: int) -> Optional[Color]: |
| 70 |
raise NotImplementedError # pragma: no cover |
| 71 |
|
| 72 |
|
| 73 |
class ColorScheme(ColorRule): |
| 74 |
""" |
| 75 |
Specify which color each symbol in a sequence logo should be. |
| 76 |
|
| 77 |
A color scheme is primarily a container of color rules. These rules would |
| 78 |
be along the lines of "hydrophobic residues are blue" or "indices 5-10 are |
| 79 |
red" or "the wildtype sequence is black". When a color is requested for a |
| 80 |
particular symbol, each rule is consulted in turn until one provides a |
| 81 |
color. If no rule provides a color, the given default color will be used. |
| 82 |
""" |
| 83 |
|
| 84 |
def __init__( |
| 85 |
self, |
| 86 |
rules: List[ColorRule] = [], |
| 87 |
title: str = "", |
| 88 |
description: str = "", |
| 89 |
default_color: str = "black", |
| 90 |
alphabet: Alphabet = seq.generic_alphabet, |
| 91 |
) -> None: |
| 92 |
self.rules = rules |
| 93 |
self.title = title |
| 94 |
self.description = description |
| 95 |
self.default_color = Color.from_string(default_color) |
| 96 |
self.alphabet = alphabet |
| 97 |
|
| 98 |
def symbol_color(self, seq_index: int, symbol: str, rank: int) -> Color: |
| 99 |
if symbol not in self.alphabet: |
| 100 |
raise KeyError("Colored symbol '%s' does not exist in alphabet." % symbol) |
| 101 |
|
| 102 |
for rule in self.rules: |
| 103 |
color = rule.symbol_color(seq_index, symbol, rank) |
| 104 |
if color is not None: |
| 105 |
return color |
| 106 |
|
| 107 |
return self.default_color |
| 108 |
|
| 109 |
|
| 110 |
class SymbolColor(ColorRule): |
| 111 |
""" |
| 112 |
Represent the given set of symbols (e.g. "DEHKR" for charged residues) with |
| 113 |
a single color. |
| 114 |
""" |
| 115 |
|
| 116 |
def __init__( |
| 117 |
self, symbols: str, color: str, description: Optional[str] = None |
| 118 |
) -> None: |
| 119 |
self.symbols = symbols |
| 120 |
self.color = Color.from_string(color) |
| 121 |
self.description = description |
| 122 |
|
| 123 |
def symbol_color(self, seq_index: int, symbol: str, rank: int) -> Optional[Color]: |
| 124 |
if symbol in self.symbols: |
| 125 |
return self.color |
| 126 |
return None |
| 127 |
|
| 128 |
|
| 129 |
class IndexColor(ColorRule): |
| 130 |
""" |
| 131 |
Represent the given set of indices (e.g. range(10) for the first ten |
| 132 |
residues) with a single color. |
| 133 |
""" |
| 134 |
|
| 135 |
def __init__( |
| 136 |
self, indices: Sequence[int], color: str, description: Optional[str] = None |
| 137 |
) -> None: |
| 138 |
self.indices = indices |
| 139 |
self.color = Color.from_string(color) |
| 140 |
self.description = description |
| 141 |
|
| 142 |
def symbol_color(self, seq_index: int, symbol: str, rank: int) -> Optional[Color]: |
| 143 |
if seq_index in self.indices: |
| 144 |
return self.color |
| 145 |
return None |
| 146 |
|
| 147 |
|
| 148 |
class RefSeqColor(ColorRule): |
| 149 |
""" |
| 150 |
Color the given reference sequence in its own color, so you can easily see |
| 151 |
which positions match that sequence and which don't. |
| 152 |
""" |
| 153 |
|
| 154 |
def __init__( |
| 155 |
self, ref_seq: str, color: str, description: Optional[str] = None |
| 156 |
) -> None: |
| 157 |
self.ref_seq = ref_seq |
| 158 |
self.color = Color.from_string(color) |
| 159 |
self.description = description |
| 160 |
|
| 161 |
def symbol_color(self, seq_index: int, symbol: str, rank: int) -> Optional[Color]: |
| 162 |
if symbol == self.ref_seq[seq_index]: |
| 163 |
return self.color |
| 164 |
return None |
| 165 |
|
| 166 |
|
| 167 |
monochrome = ColorScheme([]) # This list intentionally left blank |
| 168 |
|
| 169 |
# From makelogo |
| 170 |
nucleotide = ColorScheme( |
| 171 |
[ |
| 172 |
SymbolColor("G", "orange"), |
| 173 |
SymbolColor("TU", "red"), |
| 174 |
SymbolColor("C", "blue"), |
| 175 |
SymbolColor("A", "green"), |
| 176 |
], |
| 177 |
) |
| 178 |
|
| 179 |
base_pairing = ColorScheme( |
| 180 |
[ |
| 181 |
SymbolColor("TAU", "darkorange", "Weak (2 Watson-Crick hydrogen bonds)"), |
| 182 |
SymbolColor("GC", "blue", "Strong (3 Watson-Crick hydrogen bonds)"), |
| 183 |
], |
| 184 |
) |
| 185 |
|
| 186 |
# From Crooks2004c-Proteins-SeqStr.pdf |
| 187 |
hydrophobicity = ColorScheme( |
| 188 |
[ |
| 189 |
SymbolColor("RKDENQ", "blue", "hydrophilic"), |
| 190 |
SymbolColor("SGHTAP", "green", "neutral"), |
| 191 |
SymbolColor("YVMCLFIW", "black", "hydrophobic"), |
| 192 |
], |
| 193 |
alphabet=seq.unambiguous_protein_alphabet, |
| 194 |
) |
| 195 |
|
| 196 |
# from makelogo |
| 197 |
chemistry = ColorScheme( |
| 198 |
[ |
| 199 |
SymbolColor("GSTYC", "green", "polar"), |
| 200 |
SymbolColor("NQ", "purple", "neutral"), |
| 201 |
SymbolColor("KRH", "blue", "basic"), |
| 202 |
SymbolColor("DE", "red", "acidic"), |
| 203 |
SymbolColor("PAWFLIMV", "black", "hydrophobic"), |
| 204 |
], |
| 205 |
alphabet=seq.unambiguous_protein_alphabet, |
| 206 |
) |
| 207 |
|
| 208 |
charge = ColorScheme( |
| 209 |
[SymbolColor("KRH", "blue", "Positive"), SymbolColor("DE", "red", "Negative")], |
| 210 |
alphabet=seq.unambiguous_protein_alphabet, |
| 211 |
) |
| 212 |
|
| 213 |
taylor = ColorScheme( |
| 214 |
[ |
| 215 |
SymbolColor("A", "#CCFF00"), |
| 216 |
SymbolColor("C", "#FFFF00"), |
| 217 |
SymbolColor("D", "#FF0000"), |
| 218 |
SymbolColor("E", "#FF0066"), |
| 219 |
SymbolColor("F", "#00FF66"), |
| 220 |
SymbolColor("G", "#FF9900"), |
| 221 |
SymbolColor("H", "#0066FF"), |
| 222 |
SymbolColor("I", "#66FF00"), |
| 223 |
SymbolColor("K", "#6600FF"), |
| 224 |
SymbolColor("L", "#33FF00"), |
| 225 |
SymbolColor("M", "#00FF00"), |
| 226 |
SymbolColor("N", "#CC00FF"), |
| 227 |
SymbolColor("P", "#FFCC00"), |
| 228 |
SymbolColor("Q", "#FF00CC"), |
| 229 |
SymbolColor("R", "#0000FF"), |
| 230 |
SymbolColor("S", "#FF3300"), |
| 231 |
SymbolColor("T", "#FF6600"), |
| 232 |
SymbolColor("V", "#99FF00"), |
| 233 |
SymbolColor("W", "#00CCFF"), |
| 234 |
SymbolColor("Y", "#00FFCC"), |
| 235 |
], |
| 236 |
title="Taylor", |
| 237 |
description="W. Taylor, Protein Engineering, Vol 10 , 743-746 (1997)", |
| 238 |
alphabet=seq.unambiguous_protein_alphabet, |
| 239 |
) |