comp_seq_sim

class comp_seq_sim(seq1=None, seq2=None, df_seq=None)[source]

Bases:

Compute pairwise similarity between two or more sequences ([pro], requires aaanalysis[pro]).

The sequence similarity score between two sequences is the alignment score expressed as a percentage of the length of the longest sequence (range [0, 100]). The alignment score is obtained using the Bio.Align.PairwiseAligner from BioPython with default settings.

Added in version 1.0.0.

Parameters:
  • seq1 (str, optional) – First sequence to align.

  • seq2 (str, optional) – Second sequence to align.

  • df_seq (pd.DataFrame, shape (n_samples, n_seq_info), optional) – DataFrame containing an entry column with unique protein identifiers and a sequence column with full protein sequences.

Returns:

  • seq_sim (float) – Sequence similarity score as a percentage in [0, 100], returned when seq1 and seq2 are provided (pairwise comparison of two strings).

  • df_pw_sim (pd.DataFrame, shape (n_samples, n_samples)) – Pairwise similarity matrix with entry values as both index and columns, returned when df_seq is provided.

See also

  • Bio.Align.PairwiseAligner for details on the similarity computation.

Warning

  • This function requires biopython, which is automatically installed via pip install aaanalysis[pro].

Examples

You can compute the pairwise sequence similarity between two sequences using the features comput_seq_sim() function:

import aaanalysis as aa
aa.options["verbose"] = False

seq1 = "GTGGHWWW"
seq2 = "GTGGHFALSE"

# Compute similarity between two sequences
seq_sim = aa.comp_seq_sim(seq1=seq1, seq2=seq2)
print(seq_sim)
30.0

To compute the similarity between all pairs of sequences from a DataFrame, you can use the df_seq parameter:

import pandas as pd

# Pairwise similarity across all sequences in a df_seq (needs 'entry' + 'sequence' columns)
df_seq = pd.DataFrame({
    "entry": ["P1", "P2", "P3"],
    "sequence": ["GTGGHWWW", "GTGGHFALSE", "GTGGHWWA"],
})
df_pw_sim = aa.comp_seq_sim(df_seq=df_seq)
aa.display_df(df=df_pw_sim, show_shape=True)
DataFrame shape: (3, 3)
  P1 P2 P3
P1 100.000000 30.000000 87.500000
P2 30.000000 100.000000 30.000000
P3 87.500000 30.000000 100.000000