Skip to main content

Python3 implementation of mismatch string kernel

Project description

Mismatch string kernel

A simple Python3 implementation of the mismatch kernel described in the publication below:

%0 Journal Article
%T Mismatch string kernels for discriminative protein classification
%A Leslie, Christina S
%A Eskin, Eleazar
%A Cohen, Adiel
%A Weston, Jason
%A Noble, William Stafford
%J Bioinformatics
%V 20
%N 4
%P 467-476
%@ 1460-2059
%D 2004
%I Oxford University Press
%U https://doi.org/10.1093/bioinformatics/btg431

Usage

To understand the technicalities of what this kernel does please refer to the article above.

Initializing the kernel

First you have to define an alphabet from which the k-mers will be generated, the length k of the k-mers and m the maximum number of mismatches between mers, for example:

ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
k = 3
m = 1

Then you can create a MismatchKernel object with such parameters:

from mismatch_kernel import MismatchKernel

mk = MismatchKernel(ALPHABET, k, m)

Mapping a string to (k-m) feature space

You can use the vectorize(x) function to map a string x to the (k-m) feature space.

Note that the alphabet is in general case sensitive, so if your strings needs to be case sensitive (i.e. "string" != "StRiNg"), your alphabet should contain both uppercase and lowercase letters. Also this will much increase computational time because the k-mer feature space has dimension #(ALPHABET)^k; same thing goes if you need to distinguish punctuation, for example in the alphabet above the strings will be different based on the spaces they contain (i.e. "space" != "spa ce"). In general the strings you pass to this module functions will be normalized, i.e. every character not in the alphabet will be removed. For example if you call vectorize("String") after defining the above alphabet you are actually vectorizing "tring", so you should have called vectorize("String".lower()) instead.

The vectorize(x) function returns a tuple (x_norm, dok) where x_norm is the actual string that has been vectorized (i.e. x normalized) so you can check if that's what you actually wanted to vectorize, and dok is the vector in DOK (dictionary of keys) format (because the vectors are generally sparse), so it will be a dictionary like {2: 1, 3: 1, 14: 1, 17: 2, 30: 1, 41: 1, ...} meaning that the vector has non-zero values only at the position of the dictionary keys, i.e. [0, 0, 1, 1, 0, ..., 0, 1, 0, 0, 2, ...]. You can push x_norm in a dictionary along with the vector so you don't have to vectorize it again, this is what the get_kernel() function actually does.

Example

x_norm, vect = mk.mismatch_tree.vectorize("doc. Frankenstein".lower())
print("{} -> {}".format(x_norm, vect))
> doc frankenstein -> {10: 1, 13: 1, 37: 1, 64: 1, ...}

Calculating the kernel between two strings

You can use the get_kernel(x1, x2) function to get the kernel between x1 and x2, the kernel varies between 0 and 1, the more similar the two strings the greater it will be (1 if the strings are equal). The function will automatically normalize and vectorize the two strings to compute the kernel.

Example

ker = mk.get_kernel("doc. Frankenstein".lower(), "doc. Drunkenstein".lower())
print(ker)
> 0.7500011542039571

Using or supplying already calculated mismatch vectors and kernels

The get_kernel function will save in the MismatchKernel object the mismatch vectors of every string it vectorizes in the MISMATCH_VECTORS attribute, that is a dictionary that stores strings as keys and the corresponding vector as values (i.e. {'doc frankenstein': {10: 1, 13: 1, 37: 1, 64: 1, ...}, 'doc drunkenstein': {80: 1, 98: 1, 116: 1, 121: 1, ...}) so if you call next mk.get_kernel("doc drunkenstein", "doc nykterstein" it won't vectorize again "doc drunkenstein".

Likewise every calculated kernel will be stored in the KERNEL_MATRIX attribute, that is a dictionary that stores strings as keys and another dictionary with strings as keys and the kernel value between the two keys as values (i.e. {'doc frankenstein': {'doc drunkenstein': 0.7500011542039571, 'doc nykterstein': 0.5041614599291009}}). If you have to calculate the kernel for a batch of strings you can call get_kernel from the same MismatchKernel object so the strings for which the mismatch vector or the kernel have already been calculated won't be calculated again.

If you already have one or both of these dictionaries you can pass it to the MismatchKernel constructor:

mk = MismatchKernel(ALPHABET, k, m, vectors_dict, kernels_dict)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mismatch_string_kernel-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

mismatch_string_kernel-0.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file mismatch_string_kernel-0.0.1.tar.gz.

File metadata

  • Download URL: mismatch_string_kernel-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for mismatch_string_kernel-0.0.1.tar.gz
Algorithm Hash digest
SHA256 16e3aafdd015170156f4b0c8b9e23ee893874c5218364600eea18b1d89033a76
MD5 0f098bcdc45746df4e358dc78b0cf214
BLAKE2b-256 6e29c9812c06b6868694e70be68a1d7c25905e047b25d3d6b658fdbdff3ccd16

See more details on using hashes here.

File details

Details for the file mismatch_string_kernel-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: mismatch_string_kernel-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for mismatch_string_kernel-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6502d1fb40ea24320a7165ca1f73ca9639660075165ad1d405b5acefeab039a
MD5 8ce32540bf5e57d4496d7e10d89b375a
BLAKE2b-256 796de3127f60ee27248c1d1e12745b2abf23eca7889e09bb303afe7a93641235

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page