2.5. Python API

The following tutorial illustrates how to use the BpForms Python API. An interactive version of this tutorial is also available in the whole-cell modeling sandox.

2.5.1. Importing BpForms

Run this command to import BpForms.:

import bpforms

2.5.2. Creating biopolymer forms

Use the BpForms notation and the bpforms.BpForm.from_str method to create an instance of bpforms.BpForm that represents a form of a biopolymer.:

dna_form = bpforms.DnaForm().from_str('''ACG[
    id: "dI"
    | structure: InChI=1S
        /C10H12N4O4
        /c15-2-6-5(16)1-7(18-6)14-4-13-8-9(14)11-3-12-10(8)17
        /h3-7,15-16H,1-2H2,(H,11,12,17)
        /t5-,6+,7+
        /m0
        /s1
    | base-monomer: "A"
    ]AC'''.replace('\n', '').replace(' ', ''))

2.5.3. Getting and setting monomers

Individual monomers and slices of monomers can be get and set similar to lists.:

dna_form[0]
    => <bpforms.core.Monomer at 0x7fb365341240>

dna_form[1] = bpforms.dna_alphabet.monomers.A

dna_form[1:3]
    => [<bpforms.core.Monomer at 0x7fb365341240>, <bpforms.core.Monomer at 0x7fb365330cf8>]

dna_form[1:3] = bpforms.DnaForm().from_str('TA')

2.5.4. Getting and setting the base of a monomer

Optionally, BpForms can track the monomers that are generated from a monomer (e.g. m2A is generated from A). This can be get and set using the bpforms.Monomer.base_monomers attribute. This attribute is a set of bpforms.Monomer.:

di_monomer = dna_form[3]
di_monomer.base_monomers
    => set(<bpforms.core.Monomer at 0x7fb365341240>)
di_monomer.base_monomers.add(bpforms.Monomer())

2.5.5. Protonation and tautomerization

Calculate the major protation and tautomerization state of each monomer in the biopolymer form.:

dna_form.protonate(8., major_tautomer=True)

2.5.6. Calculation of physical properties

Use these commands to calculate the length, formula, molecular weight, and charge of the biopolymer form.:

len(dna_form)
    => 6

dna_form.get_formula()
    => AttrDefault(<class 'float'>, False, {'C': 59.0, 'N': 24.0, 'O': 37.0, 'P': 5.0, 'H': 66.0})

dna_form.get_mol_wt()
    => 1858.17680999

dna_form.get_charge()
    => -7

2.5.7. Generating FASTA sequences for BpForms

The to_fasta method generates FASTA representations of BpForms. Where annotated, this method uses the base_monomers attribute to represent modified monomers using the code for their root (e.g. m2A is represented as “A”). Monomers that don’t have their base annotated are represented as “N”.:

dna_form.to_fasta()
    => ACGAAC

2.5.8. Determine if two biopolymers describe the same structure

Use the following command to determine if two instances of BpForm describe the same biopolymer.:

dna_form_1 = bpforms.DnaForm().from_str('ACGT')
dna_form_2 = bpforms.DnaForm().from_str('ACGT')
dna_form_3 = bpforms.DnaForm().from_str('GCTC')

dna_form_1.is_equal(dna_form_2)
    => True

dna_form_1.is_equal(dna_form_3)
    => False