Package: pandas-charm
pandas-charm is a small Python package for getting character
matrices (alignments) into and out ofpandas.
Use this library to make pandas interoperable withBioPythonandDendroPy.Convert between the following objects:BioPython MultipleSeqAlignment <-> pandas DataFrameDendroPy CharacterMatrix <-> pandas DataFrame“Sequence dictionary” <-> pandas DataFrameThe code has been tested with Python 2.7, 3.5 and 3.6.Source repository:https://github.com/jmenglund/pandas-charmTable of contentsInstallationRunning the testsUsageDendroPy CharacterMatrix to pandas DataFramepandas DataFrame to Dendropy CharacterMatrixBioPython MultipleSeqAlignment to pandas DataFramepandas DataFrame to BioPython MultipleSeqAlignment“Sequence dictionary” to pandas DataFramepandas DataFrame to “sequence dictionary”The nameLicenseCitingAuthorInstallationFor most users, the easiest way is probably to install the latest version
hosted onPyPI:$ pip install pandas-charmThe project is hosted athttps://github.com/jmenglund/pandas-charmand
can also be installed using git:$ git clone https://github.com/jmenglund/pandas-charm.git
$ cd pandas-charm
$ python setup.py installYou 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 systemcondaor the packagevirtualenv.Running the testsTesting is carried out withpytest:$ pytest -v test_pandascharm.pyTest coverage can be calculated withCoverage.pyusing the following commands:$ coverage run -m pytest
$ coverage report -m pandascharm.pyThe code follow style conventions inPEP8, which can be checked
withpycodestyle:$ pycodestyle pandascharm.py test_pandascharm.py setup.pyUsageThe 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 dendropyDendroPy CharacterMatrix to pandas DataFrame>>>importpandasaspd>>>importpandascharmaspc>>>importdendropy>>>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)>>>dft1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  ABy 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  Apandas DataFrame to Dendropy CharacterMatrix>>>importpandasaspd>>>importpandascharmaspc>>>importdendropy>>>df=pd.DataFrame({...'t1':['T','C','C','A','A'],...'t2':['T','G','C','A','A'],...'t3':['T','G','-','A','A']})>>>dft1 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-AABioPython MultipleSeqAlignment to pandas DataFrame>>>fromioimportStringIO>>>importpandasaspd>>>importpandascharmaspc>>>fromBioimportAlignIO>>>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)>>>dft1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  Apandas DataFrame to BioPython MultipleSeqAlignment>>>importpandasaspd>>>importpandascharmaspc>>>importBio>>>df=pd.DataFrame({...'t1':['T','C','C','A','A'],...'t2':['T','G','C','A','A'],...'t3':['T','G','-','A','A']})>>>dft1 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“Sequence dictionary” to pandas DataFrame>>>importpandasaspd>>>importpandascharmaspc>>>d={...'t1':'TCCAA',...'t2':'TGCAA',...'t3':'TG-AA'...}>>>df=pc.from_sequence_dict(d)>>>dft1 t2 t3
0  T  T  T
1  C  G  G
2  C  C  -
3  A  A  A
4  A  A  Apandas DataFrame to “sequence dictionary”>>>importpandasaspd>>>importpandascharmaspc>>>df=pd.DataFrame({...'t1':['T','C','C','A','A'],...'t2':['T','G','C','A','A'],...'t3':['T','G','-','A','A']})>>>pc.to_sequence_dict(df){'t1': 'TCCAA', 't2': 'TGCAA', 't3': 'TG-AA'}The namepandas-charm got its name from the pandas library plus an acronym for
CHARacter Matrix.Licensepandas-charm is distributed under theMIT license.CitingIf 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:Choose your preferred citation style in the “Cite as” section on the Zenodo
page.AuthorMarkus Englund,orcid.org/0000-0003-1688-7112