Alignment-based sequence methods
This notebook introduces the alignment-based sequence methods (operationalized by the Optimal Matching (OM) algorithm), which was originally developed for matching protein and DNA sequences in biology and used extensively for analyzing strings in computer science and recently widely applied to explore the neighborhood change.
It generally works by finding the minimum cost for aligning one sequence to match another using a combination of operations including substitution, insertion, deletion and transposition. The cost of each operation can be parameterized diferently and may be theory-driven or data-driven. The minimum cost is considered as the distance between the two sequences.
The sequence
module in giddy
provides a suite of alignment-based sequence methods.
Author: Wei Kang weikang9009@gmail.com
import numpy as np
import pandas as pd
import libpysal
import mapclassify as mc
f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv"))
pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])
q5 = np.array([mc.Quantiles(y,k=5).yb for y in pci]).transpose()
q5
q5.shape
Import Sequence
class from giddy.sequence
:
from giddy.sequence import Sequence
seq_hamming = Sequence(q5, dist_type="hamming")
seq_hamming
seq_hamming.seq_dis_mat #pairwise sequence distance matrix
seq_interval = Sequence(q5, dist_type="interval")
seq_interval
seq_interval.seq_dis_mat
seq_arbitrary = Sequence(q5, dist_type="arbitrary")
seq_arbitrary
seq_arbitrary.seq_dis_mat
seq_markov = Sequence(q5, dist_type="markov")
seq_markov
seq_markov.seq_dis_mat
"tran"
Biemann, T. (2011). A Transition-Oriented Approach to Optimal Matching. Sociological Methodology, 41(1), 195–221. https://doi.org/10.1111/j.1467-9531.2011.01235.x
seq_tran = Sequence(q5, dist_type="tran")
seq_tran
seq_tran.seq_dis_mat
seq_tran.seq_dis_mat