<jupyter_start><jupyter_text>Logos from alignment Logomaker provides functionality to convert multiple sequence alignments to valid dataframes that can subsequentlybe drawn as logos. We begin by importing useful python packages.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.ion()

import logomaker as lm<jupyter_output><empty_output><jupyter_text>Consider the following multiple sequence alignment (in fasta format) representing CRP binding siteson the E-Coli genome (from [1](crpsitesfa)).<jupyter_code>with lm.open_example_datafile('crp_sites.fa', print_description=False) as f:
    raw_seqs = f.readlines()
raw_seqs[10:20]<jupyter_output><empty_output><jupyter_text>Here we have used the Logomaker method [open_example_datafile](https://logomaker.readthedocs.io/en/latest/examples_src.htmlopen-example-datafile) to read the sequences from a fasta file. We can clean up these raw sequences by running:<jupyter_code>seqs = [seq.strip() for seq in raw_seqs if ('#' not in seq) and ('>') not in seq]
# preview sequences
print('There are %d sequences, all of length %d'%(len(seqs), len(seqs[0])))
seqs[:5]<jupyter_output>There are 358 sequences, all of length 26<jupyter_text>Once in this form, we can use Logomaker's [Alignment_to_Matrix](https://logomaker.readthedocs.io/en/latest/matrix.htmlalignment-to-matrix) method to generate a counts dataframe, which can be input to Logomaker<jupyter_code>counts_mat = lm.alignment_to_matrix(seqs)
counts_mat.head()<jupyter_output><empty_output><jupyter_text>Entering the counts matrix into The Logo class draws a counts logo:<jupyter_code>lm.Logo(counts_mat)<jupyter_output><empty_output><jupyter_text>Multiple sequence alignment with gaps Logomaker maker is able to load alignments with gaps in them. Here, we load the WW domain protein alignment ([2](WWdomain))<jupyter_code># load ww alignment
with lm.open_example_datafile('ww_sequences.fa', print_description=False) as f:
    lines = f.readlines()
    
# preview loaded file
print(''.join(lines[:20]))<jupyter_output># 
# WW domain alignment in FASTA format.
# From PFAM, RP15 (4025)
# http://pfam.xfam.org/family/PF00397#tabview=tab3
# 
# References
# 
# Finn RD et al. (2014) Pfam: the protein families database.
# Nucl Acids Res. 42(Database issue):D222–30.
# 
>B7G9D5/250-280
LPPQW..TEA.VDVDT...GKFYFVHVET.......KETRWERP
>B7G9D5/328-356
--PGW..TAT.VDPAS...GRTYYYHAAT.......GETRWEPP
>B7G9D5/387-417
LPSGW..VEQ.TDPSS...GRPYYYHNAS.......NLTQWERP
>B7G9D5/503-533
LPAGW..VAA.NDPSS...GRTYYYHAES.......GVTSWNPP
>B7G9D5/626-656
LPNGW..QEL.VDPSS...GSTYYYNEVN.......GTTSWDRP<jupyter_text>As before, we clean up the sequences as follows:<jupyter_code># extract ww domain sequences
seqs = [seq.strip().upper() for seq in lines if ('#' not in seq) and ('>') not in seq]

# preview sequences
# Preview sequences
print('There are %d sequences, all of length %d'%(len(seqs), len(seqs[0])))
seqs[:10]<jupyter_output>There are 4025 sequences, all of length 44<jupyter_text>We load the alignment as a count matrix, again by using [Alignment_to_Matrix](https://logomaker.readthedocs.io/en/latest/matrix.htmlalignment-to-matrix). Note the use of the keyword argument `characters_to_ignore`: Logomaker can be told to ignore certain characters in sequences.<jupyter_code># create counts matrix
ww_counts_df = lm.alignment_to_matrix(sequences=seqs, to_type='counts', characters_to_ignore='.-X')

# preview counts dataframe
ww_counts_df.head()<jupyter_output><empty_output><jupyter_text>The full counts logo looks like:<jupyter_code># show full ww counts
lm.Logo(ww_counts_df)<jupyter_output><empty_output><jupyter_text>We can create a cropped logo by running the snippet below:<jupyter_code># filter base on counts
num_seqs = ww_counts_df.sum(axis=1)
pos_to_keep = num_seqs > len(seqs)/2
ww_counts_df = ww_counts_df[pos_to_keep]
ww_counts_df.reset_index(drop=True, inplace=True)<jupyter_output><empty_output><jupyter_text>The cropped logo now looks like:<jupyter_code># show cropped ww counts logo
lm.Logo(ww_counts_df)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11423679060665362, 'fraction_numerical': 0.02299412915851272, 'mean_word_length': 6.275800711743773, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 50, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '93841', 'license': 'MIT', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1452, 'n_words': 364, 'path': 'logomaker/tutorials/2_logos_from_alignment.ipynb', 'repo_name': 'BlingArida/logomaker', 'size': '4088'}