Skip to main content

A small Python library for getting character matrices (alignments) into and out of pandas

Project description

Build status Coverage status PyPI status License DOI

pandas-charm is a small Python package for getting character matrices (alignments) into and out of pandas. Use this library to make pandas interoperable with BioPython and DendroPy.

Convert between the following objects:

  • BioPython MultipleSeqAlignment <-> pandas DataFrame

  • DendroPy CharacterMatrix <-> pandas DataFrame

  • Python dictionary <-> pandas DataFrame

The code has been tested with Python 2.7, 3.5 and 3.6.

Source repository: https://github.com/jmenglund/pandas-charm


Installation

For most users, the easiest way is probably to install the latest version hosted on PyPI:

$ pip install pandas-charm

The project is hosted at https://github.com/jmenglund/pandas-charm and can also be installed using git:

$ git clone https://github.com/jmenglund/pandas-charm.git
$ cd pandas-charm
$ python setup.py install

You may consider installing pandas-charm and its required Python packages within a virtual environment in order to avoid cluttering your system’s Python path. See for example the environment management system conda or the package virtualenv.

Running the tests

Testing is carried out with pytest:

$ pytest -v test_pandascharm.py

Test coverage can be calculated with Coverage.py using the following commands:

$ coverage run -m pytest
$ coverage report -m pandascharm.py

The code follow style conventions in PEP8, which can be checked with pycodestyle:

$ pycodestyle pandascharm.py test_pandascharm.py setup.py

Usage

The following examples show how to use pandas-charm. The examples are written with Python 3 code, but pandas-charm should work also with Python 2.7+. You need to install BioPython and/or DendroPy manually before you start:

$ pip install biopython
$ pip install dendropy

DendroPy CharacterMatrix to pandas DataFrame

>>> import pandas as pd
>>> import pandascharm as pc
>>> import dendropy
>>> dna_string = '3 5\nt1  TCCAA\nt2  TGCAA\nt3  TG-AA\n'
>>> print(dna_string)
3 5
t1  TCCAA
t2  TGCAA
t3  TG-AA

>>> matrix = dendropy.DnaCharacterMatrix.get(
...     data=dna_string, schema='phylip')
>>> df = pc.from_charmatrix(matrix)
>>> df
  t1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  A

By default, characters are stored as rows and sequences as columns in the DataFrame. If you want rows to hold sequences, just transpose the matrix in pandas:

>>> df.transpose()
    0  1  2  3  4
t1  T  C  C  A  A
t2  T  G  C  A  A
t3  T  G  -  A  A

pandas DataFrame to Dendropy CharacterMatrix

>>> import pandas as pd
>>> import pandascharm as pc
>>> import dendropy
>>> df = pd.DataFrame({
...     't1': ['T', 'C', 'C', 'A', 'A'],
...     't2': ['T', 'G', 'C', 'A', 'A'],
...     't3': ['T', 'G', '-', 'A', 'A']})
>>> df
  t1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  A

>>> matrix = pc.to_charmatrix(df, data_type='dna')
>>> print(matrix.as_string('phylip'))
3 5
t1  TCCAA
t2  TGCAA
t3  TG-AA

BioPython MultipleSeqAlignment to pandas DataFrame

>>> from io import StringIO
>>> import pandas as pd
>>> import pandascharm as pc
>>> from Bio import AlignIO
>>> dna_string = '3 5\nt1  TCCAA\nt2  TGCAA\nt3  TG-AA\n'
>>> f = StringIO(dna_string)  # make the string a file-like object
>>> alignment = AlignIO.read(f, 'phylip-relaxed')
>>> print(alignment)
SingleLetterAlphabet() alignment with 3 rows and 5 columns
TCCAA t1
TGCAA t2
TG-AA t3
>>> df = pc.from_bioalignment(alignment)
>>> df
  t1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  A

pandas DataFrame to BioPython MultipleSeqAlignment

>>> import pandas as pd
>>> import pandascharm as pc
>>> import Bio
>>> df = pd.DataFrame({
...     't1': ['T', 'C', 'C', 'A', 'A'],
...     't2': ['T', 'G', 'C', 'A', 'A'],
...     't3': ['T', 'G', '-', 'A', 'A']})
>>> df
  t1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  A

>>> alignment = pc.to_bioalignment(df, alphabet='generic_dna')
>>> print(alignment)
SingleLetterAlphabet() alignment with 3 rows and 5 columns
TCCAA t1
TGCAA t2
TG-AA t3

Python dictionary to pandas DataFrame

>>> import pandas as pd
>>> import pandascharm as pc
>>> d = {
...     't1': 'TCCAA',
...     't2': 'TGCAA',
...     't3': 'TG-AA'
... }
>>> df = pc.from_dict(d)
>>> df
  t1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  A

pandas DataFrame to Python dictionary

>>> import pandas as pd
>>> import pandascharm as pc
>>> df = pd.DataFrame({
...     't1': ['T', 'C', 'C', 'A', 'A'],
...     't2': ['T', 'G', 'C', 'A', 'A'],
...     't3': ['T', 'G', '-', 'A', 'A']})
>>> pc.to_dict(df)
{'t1': 'TCCAA', 't2': 'TGCAA', 't3': 'TG-AA'}

The name

pandas-charm got its name from the pandas library plus an acronym for CHARacter Matrix.

License

pandas-charm is distributed under the MIT license.

Citing

If you use results produced with this package in a scientific publication, please just mention the package name in the text and cite the Zenodo DOI of this project:

DOI

Choose your preferred citation style in the “Cite as” section on the Zenodo page.

Author

Markus Englund, orcid.org/0000-0003-1688-7112

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

pandas-charm-0.2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

pandas_charm-0.2.0-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pandas-charm-0.2.0.tar.gz.

File metadata

  • Download URL: pandas-charm-0.2.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for pandas-charm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9d28082adb3d94197e8644f1beb86121b0d5504523fb15ff01a4e9e1f502c235
MD5 bb12866de0ca3abea3787f777df012b2
BLAKE2b-256 da50fcd96018838369e5f9774a4ee7cc62a15c70f0adb2a25544e3f39b0a5d2e

See more details on using hashes here.

File details

Details for the file pandas_charm-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pandas_charm-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for pandas_charm-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 38403e2fc6844916fc84bfe671c2f075d97bff84a0b2a25ee68fd02084c39e0d
MD5 67f4287b45402f240cefa5c937883c6e
BLAKE2b-256 d72037a55f19c42e71e328da38b1e671009b90fe3c800b26f8fe4c6b56e7278f

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page