Package: seqlogo
seqlogoPython port of Bioconductor'sseqLogoserved byWebLogoOverviewIn the field of bioinformatics, a common task is to look for sequence motifs at
different sites along the genome or within a protein sequence. One aspect of this
analysis involves creating a variant of a Position Matrix (PM): Position Frequency Matrix (PFM),
Position Probability Matrix (PPM), and Position Weight Matrix (PWM). The formal format for
a PWM file can be foundhere.SpecificationA PM file can be just a plain text, whitespace delimited matrix, such that the number of columns
matches the number of letters in your desired alphabet and the number of rows is the number of positions
in your sequence. Any comment lines that start with#will be skipped.Note:TRANSFAC matrixandMEME Motifformats are not directly supported.Whereis the probability that atposition,letter is seen.This is often generated in a frequentist fashion. If a pipeline
tallies all observed letters at each position, this is called a Position Frequency Matrix (PFM).The PFM can be converted to a PPM in a straight-forward manner, creating a matrix
that for any given position and letter, the probability of that letter at that position
is reported.A PWM is the PPM converted into log-likelihood. Pseudocounts can be applied to prevent
probabilities of 0 from turing into -inf in the conversion process. Lastly, each position's
log-likelihood is corrected for some background probability for every given letter in the
selected alphabet.Featuresseqlogocan use any PM as entry points for analysis (from a file or in array formats)
and, subsequently, plot the sequence logos.seqlogowas written to support BIOINF 529 :Bioinformatics Concepts and Algorithms
at the University of Michigan in the Department of Computational Medicine & Bioinformatics.seqlogoattempts to blend the user-friendly api of Bioconductor'sseqLogoand the rendering power of theWebLogoPython API.seqlogosupports the following alphabets:Alphabet nameAlphabet Letters"DNA""ACGT""reduced DNA""ACGTN-""ambig DNA""ACGTRYSWKMBDHVN-""RNA""ACGU""reduced RNA""ACGUN-""ambig RNA""ACGURYSWKMBDHVN-""AA""ACDEFGHIKLMNPQRSTVWY""reduced AA""ACDEFGHIKLMNPQRSTVWYX*-""ambig AA""ACDEFGHIKLMNOPQRSTUVWYBJZX*-"(Boldedalphabet names are the most commonly used)seqlogocan also render sequence logos in a number of formats:"svg"(default)"eps""pdf""jpeg""png"All plots can be rendered in 4 different sizes:"small": 3.54" wide"medium": 5" wide"large": 7.25" wide"xlarge": 10.25" wideNote: all sizes taken fromthispublication
guide from Science Magazine.Recommended settings:For best results, implementseqlogowithin a IPython/Jupyter environment (for inline plotting purposes).Initially written for Python 3.7, but has shown to work in versions 3.5+ (Python 2.7 is not supported)SetupMinimal Requirements:numpypandasweblogoNote: it is strongly encouraged thatjupyteris installed as well.condaenvironment:To produce the ideal virtual environment that will runseqlogoon aconda-based
build, clone the repo or download the environment.yml within the repo. Then run the following
command:$condaenvcreate-fenvironment.ymlInstallationTo install usingpip: (recommended)$pipinstallseqlogoTo install usingconda$condainstall-cbiocondaseqlogoOr install from GitHub directly$pipinstallgit+https://github.com/betteridiot/seqlogo.git#egg=seqlogoQuickstartImportingimportnumpyasnpimportpandasaspdimportseqlogoGenerate some PM data (without frequency data)For many demonstrations that speak to PWMs, they are often started with PPM data.
Many packages preclude sequence logo generation from this entry point. However,seqlogocan handle it just fine. One point to make though is that if no count
data is provided,seqlogojust generates the PFM data by multiplying the
probabilities by 100. This isonlyforweblogolibcompatability.# Setting seed for demonstration purposes>>>np.random.seed(42)# Making a fake PPM>>>random_ppm=np.random.dirichlet(np.ones(4),size=6)>>>ppm=seqlogo.Ppm(random_ppm)>>>ppmACGT00.0821970.5272520.2306410.15991110.0703750.0703630.0248260.83443520.1619620.2169720.0036650.61740130.7356380.0982900.0826380.08343440.1798980.3689310.2804630.17070850.4985100.0791380.1820040.240349Generate some frequency data and convert to PWMSometimes the user has frequency data instead of PWM. To construct aPwminstance
that automatically computes Information Content and PWM values, the user can use
theseqlogo.pfm2pwm()function.# Setting seed for demonstration purposes>>>np.random.seed(42)# Making some fake Position Frequency Data (PFM)>>>pfm=pd.DataFrame(np.random.randint(0,36,size=(8,4)))# Convert to Position Weight Matrix (PWM)>>>pwm=seqlogo.pfm2pwm(pfm)>>>pwmACGT00.698830-0.301170-1.3011700.21340410.2630340.552541-0.584962-0.58496220.1485230.7542440.148523-3.37503930.182864-4.2094530.3141090.6485284-4.0000000.3219281.000000-0.5405685-0.222392-0.0297470.0857300.14017860.6974370.597902-2.209453-0.62449170.736966-0.5849620.502500-2.000000seqlogo.CompletePmdemoHere is a quickstart guide on how to leverage the power ofseqlogo.CompletePm# Setting seed for demonstration purposes>>>np.random.seed(42)# Making a fake PWM>>>random_ppm=np.random.dirichlet(np.ones(4),size=6)>>>cpm=seqlogo.CompletePM(ppm=random_ppm)# Pfm was imputed>>>print(cpm.pfm)ACGT085223151772832162106137398841736281754971824# Shows the how the PPM data was formatted>>>print(cpm.ppm)ACGT00.0821970.5272520.2306410.15991110.0703750.0703630.0248260.83443520.1619620.2169720.0036650.61740130.7356380.0982900.0826380.08343440.1798980.3689310.2804630.17070850.4985100.0791380.1820040.240349# Computing the PWM using default background and pseudocounts>>>print(cpm.pwm)ACGT0-1.6047731.076564-0.116281-0.6446621-1.828788-1.829031-3.3319831.7388712-0.626276-0.204418-6.0918621.30427931.557068-1.346815-1.597049-1.5832234-0.4747490.5614230.165882-0.55039650.995695-1.659494-0.457960-0.056800# See the consensus sequence>>>print(cpm.consensus)CTTACA# See the Information Content>>>print(cpm.ic)00.30580611.11085620.63714930.74898940.07428650.268034dtype:float64Plot the sequence logo with information content scaling# Setting seed for demonstration purposes>>>np.random.seed(42)# Making a fake PWM>>>random_ppm=np.random.dirichlet(np.ones(4),size=6)>>>ppm=seqlogo.Ppm(random_ppm)>>>seqlogo.seqlogo(ppm,ic_scale=False,format='svg',size='medium')The above code will produce:Plot the sequence logo with no information content scaling# Setting seed for demonstration purposes>>>np.random.seed(42)# Making a fake PWM>>>random_ppm=np.random.dirichlet(np.ones(4),size=6)>>>ppm=seqlogo.Ppm(random_ppm)>>>seqlogo.seqlogo(ppm,ic_scale=False,format='svg',size='medium')The above code will produce:Documentationseqlogoexposes 5 classes to the user for handling PM data:seqlogo.Pm: the base class for all other specialized PM subclassesseqlogo.Pfm: The class used for handling PFM dataseqlogo.Ppm: The class used for handling PPM dataseqlogo.Pwm: The class used for handling PWM dataseqlogo.CompletePm: This final class will take any/all of the other PM subclass data
and compute any of the other missing data. That is, if the user only provides aseqlogo.Pfmand passes it toseqlogo.CompletePm, it will solve for the PPM, PWM, consensus sequence, and
information content.Additionally,seqlogoalso provides 6 methods for converting PM structures:seqlogo.pfm2ppm: converts a PFM to a PPMseqlogo.pfm2pwm: converts a PFM to a PWMseqlogo.ppm2pfm: converts a PPM to a PFMseqlogo.ppm2pwm: converts a PPM to a PWMseqlogo.pwm2pfm: converts a PWM to a PFMseqlogo.pwm2ppm: converts a PWM to a PPMThe signatures for each item above are as follows:Classesseqlogo.CompletePm(pfm=None,ppm=None,pwm=None,background=None,pseudocount=None,alphabet_type='DNA',alphabet=None,default_pm='ppm'):"""Creates the CompletePm instance. If the user does not define any `pm_filename_or_array`,it will be initialized to empty. Will generate all other attributes as soonas a `pm_filename_or_array` is supplied.Args:pfm (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPFM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)ppm (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPPM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)pwm (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPWM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)background (constant or Collection): Offsets used to calculate background letter probabilities (defaults: Ifusing an Nucleic Acid alphabet: 0.25; if using an Aminio Acid alphabet: Robinson-Robinson Frequencies)pseudocount (constant): Some constant to offset PPM conversion to PWM to prevent -/+ inf. (defaults to 1e-10)alphabet_type (str): Desired alphabet to use. Order matters (default: 'DNA')"DNA" := "ACGT""reduced DNA" := "ACGTN-""ambig DNA" := "ACGTRYSWKMBDHVN-""RNA" := "ACGU""reduced RNA" := "ACGUN-""ambig RNA" := "ACGURYSWKMBDHVN-""AA" : = "ACDEFGHIKLMNPQRSTVWY""reduced AA" := "ACDEFGHIKLMNPQRSTVWYX*-""ambig AA" := "ACDEFGHIKLMNOPQRSTUVWYBJZX*-""custom" := None(default: 'DNA')alphabet (str): if 'custom' is selected or a specialize alphabet is desired, this accepts a string (default: None)default_pm (str): which of the 3 pm's do you want to call '*home*'? (default: 'ppm')"""seqlogo.Pm(pm_filename_or_array=None,pm_type='ppm',alphabet_type='DNA',alphabet=None,background=None,pseudocount=None):"""Initializes the PmCreates the Pm instance. If the user does not define `pm_filename_or_array`,it will be initialized to empty. Will generate all other attributes as soonas a `pm_filename_or_array` is supplied.Args:pm_filename_or_array (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)alphabet_type (str): Desired alphabet to use. Order matters (default: 'DNA')"DNA" := "ACGT""reduced DNA" := "ACGTN-""ambig DNA" := "ACGTRYSWKMBDHVN-""RNA" := "ACGU""reduced RNA" := "ACGUN-""ambig RNA" := "ACGURYSWKMBDHVN-""AA" : = "ACDEFGHIKLMNPQRSTVWY""reduced AA" := "ACDEFGHIKLMNPQRSTVWYX*-""ambig AA" := "ACDEFGHIKLMNOPQRSTUVWYBJZX*-""custom" := None(default: 'DNA')alphabet (str): if 'custom' is selected or a specialize alphabet is desired, this accepts a string (default: None)background (constant or Collection): Offsets used to calculate background letter probabilities (defaults: Ifusing an Nucleic Acid alphabet: 0.25; if using an Aminio Acid alphabet: Robinson-Robinson Frequencies)pseudocount (constant): Some constant to offset PPM conversion to PWM to prevent -/+ inf. (default: 1e-10)"""seqlogo.Pfm(pfm_filename_or_array=None,pm_type='pfm',alphabet_type='DNA',alphabet=None,background=None,pseudocount=None):"""Initializes the PfmCreates the Pfm instance. If the user does not define `pfm_filename_or_array`,it will be initialized to empty. Will generate all other attributes as soonas a `pfm_filename_or_array` is supplied.Args:pfm_filename_or_array (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPFM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)alphabet_type (str): Desired alphabet to use. Order matters (default: 'DNA')"DNA" := "ACGT""reduced DNA" := "ACGTN-""ambig DNA" := "ACGTRYSWKMBDHVN-""RNA" := "ACGU""reduced RNA" := "ACGUN-""ambig RNA" := "ACGURYSWKMBDHVN-""AA" : = "ACDEFGHIKLMNPQRSTVWY""reduced AA" := "ACDEFGHIKLMNPQRSTVWYX*-""ambig AA" := "ACDEFGHIKLMNOPQRSTUVWYBJZX*-""custom" := None(default: 'DNA')alphabet (str): if 'custom' is selected or a specialize alphabet is desired, this accepts a string (default: None)background (constant or Collection): Offsets used to calculate background letter probabilities (defaults: Ifusing an Nucleic Acid alphabet: 0.25; if using an Aminio Acid alphabet: Robinson-Robinson Frequencies)pseudocount (constant): Some constant to offset PPM conversion to PWM to prevent -/+ inf. (default: 1e-10)"""seqlogo.Ppm(ppm_filename_or_array=None,pm_type='ppm',alphabet_type='DNA',alphabet=None,background=None,pseudocount=None):"""Initializes the PpmCreates the Ppm instance. If the user does not define `ppm_filename_or_array`,it will be initialized to empty. Will generate all other attributes as soonas a `ppm_filename_or_array` is supplied.Args:ppm_filename_or_array (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPPM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)alphabet_type (str): Desired alphabet to use. Order matters (default: 'DNA')"DNA" := "ACGT""reduced DNA" := "ACGTN-""ambig DNA" := "ACGTRYSWKMBDHVN-""RNA" := "ACGU""reduced RNA" := "ACGUN-""ambig RNA" := "ACGURYSWKMBDHVN-""AA" : = "ACDEFGHIKLMNPQRSTVWY""reduced AA" := "ACDEFGHIKLMNPQRSTVWYX*-""ambig AA" := "ACDEFGHIKLMNOPQRSTUVWYBJZX*-""custom" := None(default: 'DNA')alphabet (str): if 'custom' is selected or a specialize alphabet is desired, this accepts a string (default: None)background (constant or Collection): Offsets used to calculate background letter probabilities (defaults: Ifusing an Nucleic Acid alphabet: 0.25; if using an Aminio Acid alphabet: Robinson-Robinson Frequencies)pseudocount (constant): Some constant to offset PPM conversion to PWM to prevent -/+ inf. (default: 1e-10)"""seqlogo.Pwm(pwm_filename_or_array=None,pm_type='pwm',alphabet_type='DNA',alphabet=None,background=None,pseudocount=None):"""Initializes the PwmCreates the Pwm instance. If the user does not define `pwm_filename_or_array`,it will be initialized to empty. Will generate all other attributes as soonas a `pwm_filename_or_array` is supplied.Args:pwm_filename_or_array (str or `numpy.ndarray` or `pandas.DataFrame` or Pm): The user suppliedPWM. If it is a filename, the file will be openedand parsed. If it is an `numpy.ndarray` or `pandas.DataFrame`,it will just be assigned. (default: None, skips '#' comment lines)alphabet_type (str): Desired alphabet to use. Order matters (default: 'DNA')"DNA" := "ACGT""reduced DNA" := "ACGTN-""ambig DNA" := "ACGTRYSWKMBDHVN-""RNA" := "ACGU""reduced RNA" := "ACGUN-""ambig RNA" := "ACGURYSWKMBDHVN-""AA" : = "ACDEFGHIKLMNPQRSTVWY""reduced AA" := "ACDEFGHIKLMNPQRSTVWYX*-""ambig AA" := "ACDEFGHIKLMNOPQRSTUVWYBJZX*-""custom" := None(default: 'DNA')alphabet (str): if 'custom' is selected or a specialize alphabet is desired, this accepts a string (default: None)background (constant or Collection): Offsets used to calculate background letter probabilities (defaults: Ifusing an Nucleic Acid alphabet: 0.25; if using an Aminio Acid alphabet: Robinson-Robinson Frequencies)pseudocount (constant): Some constant to offset PPM conversion to PWM to prevent -/+ inf. (default: 1e-10)"""Conversion Methodsseqlogo.pfm2ppm(pfm):"""Converts a Pfm to a ppm arrayArgs:pfm (Pfm): a fully initialized PfmReturns:(np.array): converted values"""seqlogo.pfm2pwm(pfm,background=None,pseudocount=None):"""Converts a Pfm to a pwm arrayArgs:pfm (Pfm): a fully initialized Pfmbackground: accounts for relative weights from background. Must be a constant or same number of columns as Pwm (default: None)pseudocount (const): The number used to offset log-likelihood conversion from probabilites (default: None -> 1e-10)Returns:(np.array): converted values"""seqlogo.ppm2pfm(ppm):"""Converts a Ppm to a pfm arrayArgs:ppm (Ppm): a fully initialized PpmReturns:(np.array): converted values"""seqlogo.ppm2pwm(ppm,background=None,pseudocount=None):"""Converts a Ppm to a pwm arrayArgs:ppm (Ppm): a fully initialized Ppmbackground: accounts for relative weights from background. Must be a constant or same number of columns as Pwm (default: None)pseudocount (const): The number used to offset log-likelihood conversion from probabilites (default: None -> 1e-10)Returns:(np.array): converted valuesRaises:ValueError: if the pseudocount isn't a constant or the same length as sequence"""seqlogo.pwm2pfm(pwm,background=None,pseudocount=None):"""Converts a Pwm to a pfm arrayArgs:pwm (Pwm): a fully initialized Pwmbackground: accounts for relative weights from background. Must be a constant or same number of columns as Pwm (default: None)pseudocount (const): The number used to offset log-likelihood conversion from probabilites (default: None -> 1e-10)Returns:(np.array): converted values"""seqlogo.pwm2ppm(pwm,background=None,pseudocount=None):"""Converts a Pwm to a ppm arrayArgs:pwm (Pwm): a fully initialized Pwmbackground: accounts for relative weights from background. Must be a constant or same number of columns as Pwm (default: None)pseudocount (const): The number used to offset log-likelihood conversion from probabilites (default: None -> 1e-10)Returns:(np.array): converted valuesRaises:ValueError: if the pseudocount isn't a constant or the same length as sequence"""ContributingPlease see our contribution guidelineshereAcknowledgmentsBembom O (2018). seqlogo: Sequence logos for DNA sequence alignments. R package version 1.48.0.Crooks GE, Hon G, Chandonia JM, Brenner SE WebLogo: A sequence logo generator,
Genome Research, 14:1188-1190, (2004).