Skip to content

Support for additional motif formats #909

Closed

Description

@ghuls

Below is some code I wrote more than a year ago and is not updated against a recent version of BioPython. @mdehoon and @barwil : The patches probably some rework anyway for merging, but before bringing them up to date and splitting them in separate pull requests, I want to make sure that they make a chance to be upstreamed.

This code adds support for:

diff --git a/Bio/motifs/__init__.py b/Bio/motifs/__init__.py
index 0710dd8..15a5552 100644
--- a/Bio/motifs/__init__.py
+++ b/Bio/motifs/__init__.py
@@ -34,10 +34,11 @@ def parse(handle, format):

         - AlignAce:      AlignAce output file format
         - consensus:     motif consensus file
+        - ClusterBuster: Cluster Buster position frequency matrix format
         - MEME:          MEME output file motif
         - MAST:          MAST output file motif
         - TRANSFAC:      TRANSFAC database file format
-        - pfm:           JASPAR-style position-frequency matrix
+        - pfm:           JASPAR-style position frequency matrix
         - jaspar:        JASPAR-style multiple PFM format
         - sites:         JASPAR-style sites file

@@ -74,6 +75,10 @@ def parse(handle, format):
         from Bio.motifs import alignace
         record = alignace.read(handle)
         return record
+    elif format == "clusterbuster":
+        from Bio.motifs import clusterbuster
+        record = clusterbuster.read(handle)
+        return record
     elif format == "consensus":
         from Bio.motifs import consensus
         record = consensus.read(handle)
@@ -86,10 +91,18 @@ def parse(handle, format):
         from Bio.motifs import mast
         record = mast.read(handle)
         return record
+    elif format in ('pfm_four_columns', 'pfm_four_rows'):
+        from Bio.motifs import pfm
+        record = pfm.read(handle, format)
+        return record
     elif format == "transfac":
         from Bio.motifs import transfac
         record = transfac.read(handle)
         return record
+    elif format == "xms":
+        from Bio.motifs import xms
+        record = xms.read(handle)
+        return record
     elif format in ('pfm', 'sites', 'jaspar'):
         from Bio.motifs import jaspar
         record = jaspar.read(handle, format)
@@ -498,15 +511,20 @@ class Motif(object):
             f.write(im)

     def format(self, format):
-        """Returns a string representation of the Motif in a given format
+        """Returns a string representation of the Motif in a given format.

-        Currently supported fromats:
+        Currently supported formats:
+         - clusterbuster: Cluster Buster position frequency matrix format
          - pfm : JASPAR single Position Frequency Matrix
          - jaspar : JASPAR multiple Position Frequency Matrix
          - transfac : TRANSFAC like files
         """

-        if format in ('pfm', 'jaspar'):
+        if format == "clusterbuster":
+            from Bio.motifs import clusterbuster
+            motifs = [self]
+            return clusterbuster.write(motifs)
+        elif format in ('pfm', 'jaspar'):
             from Bio.motifs import jaspar
             motifs = [self]
             return jaspar.write(motifs, format)
@@ -519,16 +537,20 @@ class Motif(object):


 def write(motifs, format):
-    """Returns a string representation of motifs in a given format
+    """Returns a string representation of motifs in a given format.

     Currently supported formats (case is ignored):
+     - clusterbuster: Cluster Buster position frequency matrix format
      - pfm : JASPAR simple single Position Frequency Matrix
      - jaspar : JASPAR multiple PFM format
      - transfac : TRANSFAC like files
     """

     format = format.lower()
-    if format in ("pfm", "jaspar"):
+    if format == "clusterbuster":
+        from Bio.motifs import clusterbuster
+        return clusterbuster.write(motifs)
+    elif format in ("pfm", "jaspar"):
         from Bio.motifs import jaspar
         return jaspar.write(motifs, format)
     elif format == "transfac":
diff --git a/Bio/motifs/jaspar/__init__.py b/Bio/motifs/jaspar/__init__.py
index bca4e3e..4ff83b8 100644
--- a/Bio/motifs/jaspar/__init__.py
+++ b/Bio/motifs/jaspar/__init__.py
@@ -276,8 +276,8 @@ def _read_jaspar(handle):

         if head_match:
             identifier = head_match.group(1)
-            if head_match.group(2):
-                name = head_match.group(2)
+            if head_match.group(3):
+                name = head_match.group(3)
             else:
                 name = identifier
         elif row_match_long:
diff --git a/Bio/motifs/matrix.py b/Bio/motifs/matrix.py
index 8df895a..ac6dae2 100644
--- a/Bio/motifs/matrix.py
+++ b/Bio/motifs/matrix.py
@@ -278,6 +278,14 @@ class FrequencyPositionMatrix(GenericPositionMatrix):
         # Actual normalization is done in the PositionWeightMatrix initializer
         return PositionWeightMatrix(self.alphabet, counts)

+    def scale(self, scaling_factor=1):
+        """Create and return a position-weight matrix by scaling the counts matrix."""
+        counts = {}
+        for letter in self.alphabet.letters:
+            counts[letter] = [count * scaling_factor for count in self[letter]]
+
+        return GenericPositionMatrix(self.alphabet, counts)
+

 class PositionWeightMatrix(GenericPositionMatrix):

diff --git a/Tests/test_motifs.py b/Tests/test_motifs.py
index ea17c8f..7aba107 100644
--- a/Tests/test_motifs.py
+++ b/Tests/test_motifs.py
@@ -16,6 +16,7 @@ from Bio.Seq import Seq
 class MotifTestsBasic(unittest.TestCase):
     def setUp(self):
         self.CONSENSUSin = open("motifs/consensus.txt")
+        self.CLUSTERBUSTERin = open("motifs/clusterbuster.pfm")
         self.PFMin = open("motifs/SRF.pfm")
         self.SITESin = open("motifs/Arnt.sites")
         self.TFout = "motifs/tf.out"
@@ -27,6 +28,7 @@ class MotifTestsBasic(unittest.TestCase):

     def tearDown(self):
         self.CONSENSUSin.close()
+        self.CLUSTERBUSTERin.close()
         self.PFMin.close()
         self.SITESin.close()
         if os.path.exists(self.TFout):
@@ -408,6 +410,21 @@ class MotifTestsBasic(unittest.TestCase):
         self.assertEqual(record[1].degenerate_consensus, "AAAAAAAAACCCCCCCCCC")
         self.assertEqual(record[2].degenerate_consensus, "GCGAAGGAAGCAGCGCGTGTG")

+    def test_clusterbuster_parsing_and_output(self):
+        """Test if Bio.motifs can parse and output Cluster Buster PFM files.
+        """
+        record = motifs.parse(self.CLUSTERBUSTERin, "clusterbuster")
+        self.assertEqual(len(record), 3)
+        self.assertEqual(record[0].name, "MA0004.1")
+        self.assertEqual(record[1].name, "MA0006.1")
+        self.assertEqual(record[2].name, "MA0008.1")
+        self.assertEqual(record[0].degenerate_consensus, "CACGTG")
+        self.assertEqual(record[1].degenerate_consensus, "YGCGTG")
+        self.assertEqual(record[2].degenerate_consensus, "CAATTATT")
+
+        self.CLUSTERBUSTERin.seek(0)
+        self.assertEqual(motifs.write(record, "clusterbuster"), self.CLUSTERBUSTERin.read())
+
     def test_pfm_parsing(self):
         """Test if Bio.motifs can parse JASPAR-style pfm files.
         """

Bio/motifs/clusterbuster.py

# Copyright 2015 by Gert Hulselmans.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Parse Cluster Buster position frequency matrix files."""

from Bio import motifs
from Bio.Alphabet import IUPAC


__docformat__ = "restructuredtext en"


class Record(list):
    """A Bio.motifs.clusterbuster.Record stores the information in a Cluster Buster matrix table.
    The record inherits from a list containing the individual motifs.
    """

    def __str__(self):
        return "\n".join(str(motif) for motif in self)


def read(handle):
    """Read motifs in Cluster Buster position frequency matrix format from a file handle.

    Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html
    """

    motif_nbr = 0
    record = Record()

    for line in handle:
        line = line.strip()
        if line:
            if line.startswith('>'):
                if motif_nbr != 0:
                    motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                    motif.name = motif_name
                    record.append(motif)

                motif_name = line[1:].strip()
                nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []}
                motif_nbr += 1
            else:
                if line.startswith('#'):
                    continue

                matrix_columns = line.split('\t')

                if len(matrix_columns) == 4:
                    [nucleotide_counts[nucleotide].append(float(nucleotide_count))
                     for nucleotide, nucleotide_count in zip(['A', 'C', 'G', 'T'], matrix_columns)]

    motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
    motif.name = motif_name
    record.append(motif)

    return record


def write(motifs):
    """Return the representation of motifs in Cluster Buster position frequency matrix format."""

    lines = []
    for m in motifs:
        line = '>{0}\n'.format(m.name)
        lines.append(line)
        for ACGT_counts in zip(m.counts['A'], m.counts['C'], m.counts['G'], m.counts['T']):
            lines.append('{0:0.0f}\t{1:0.0f}\t{2:0.0f}\t{3:0.0f}\n'.format(*ACGT_counts))

    # Finished; glue the lines together.
    text = ''.join(lines)

    return text

Bio/motifs/pfm.py

# Copyright 2015 by Gert Hulselmans.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Parse various position frequency matrix format files."""

import re

from Bio import motifs
from Bio.Alphabet import IUPAC


__docformat__ = "restructuredtext en"


class Record(list):
    """A Bio.motifs.pfm.Record stores the information in a position frequency matrix table.
    The record inherits from a list containing the individual motifs.
    """

    def __str__(self):
        return "\n".join(str(motif) for motif in self)


def read(handle, pfm_format):
    """Read motif(s) from a file in various position frequency matrix formats.

    Return the record of PFM(s).
    Call the appropriate routine based on the format passed.
    """
    pfm_format = pfm_format.lower()
    if pfm_format == "pfm_four_columns":
        record = _read_pfm_four_columns(handle)
        return record
    elif pfm_format == "pfm_four_rows":
        record = _read_pfm_four_rows(handle)
        return record
    else:
        raise ValueError("Unknown Position Frequency matrix format '%s'" % pfm_format)


def _read_pfm_four_columns(handle):
    """Read motifs in Cluster Buster position frequency matrix format from a file handle.

    Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html

    #cisbp
    Pos A   C   G   T
    1   0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846
    2   0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846
    3   0.971153846153846   0.00961538461538462 0.00961538461538462 0.00961538461538462
    4   0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846
    5   0.00961538461538462 0.971153846153846   0.00961538461538462 0.00961538461538462
    6   0.971153846153846   0.00961538461538462 0.00961538461538462 0.00961538461538462
    7   0.00961538461538462 0.971153846153846   0.00961538461538462 0.00961538461538462
    8   0.00961538461538462 0.00961538461538462 0.00961538461538462 0.971153846153846

    #c2h2 zfs
    Gene    ENSG00000197372
    Pos A   C   G   T
    1   0.341303    0.132427    0.117054    0.409215
    2   0.283785    0.077066    0.364552    0.274597
    3   0.491055    0.078208    0.310520    0.120217
    4   0.492621    0.076117    0.131007    0.300256
    5   0.250645    0.361464    0.176504    0.211387
    6   0.276694    0.498070    0.197793    0.027444
    7   0.056317    0.014631    0.926202    0.002850
    8   0.004470    0.007769    0.983797    0.003964
    9   0.936213    0.058787    0.002387    0.002613
    10  0.004352    0.004030    0.002418    0.989200
    11  0.013277    0.008165    0.001991    0.976567
    12  0.968132    0.002263    0.002868    0.026737
    13  0.397623    0.052017    0.350783    0.199577
    14  0.000000    0.000000    1.000000    0.000000
    15  1.000000    0.000000    0.000000    0.000000
    16  0.000000    0.000000    1.000000    0.000000
    17  0.000000    0.000000    1.000000    0.000000
    18  1.000000    0.000000    0.000000    0.000000
    19  0.000000    1.000000    0.000000    0.000000
    20  1.000000    0.000000    0.000000    0.000000

    #c2h2 zfs
    Gene    FBgn0000210
    Motif   M1734_0.90
    Pos A   C   G   T
    1   0.25    0.0833333   0.0833333   0.583333
    2   0.75    0.166667    0.0833333   0
    3   0.833333    0   0   0.166667
    4   1   0   0   0
    5   0   0.833333    0.0833333   0.0833333
    6   0.333333    0   0   0.666667
    7   0.833333    0   0   0.166667
    8   0.5 0   0.333333    0.166667
    9   0.5 0.0833333   0.166667    0.25
    10  0.333333    0.25    0.166667    0.25
    11  0.166667    0.25    0.416667    0.166667

    # flyfactorsurvey (cluster buster)
    >AbdA_Cell_FBgn0000014
    1   3   0   14
    0   0   0   18
    16  0   0   2
    18  0   0   0
    1   0   0   17
    0   0   6   12
    15  1   2   0

    # homer
    >ATGACTCATC AP-1(bZIP)/ThioMac-PU.1-ChIP-Seq(GSE21512)/Homer    6.049537    -1.782996e+03   0   9805.3,5781.0,3085.1,2715.0,0.00e+00
    0.419   0.275   0.277   0.028
    0.001   0.001   0.001   0.997
    0.010   0.002   0.965   0.023
    0.984   0.003   0.001   0.012
    0.062   0.579   0.305   0.054
    0.026   0.001   0.001   0.972
    0.043   0.943   0.001   0.012
    0.980   0.005   0.001   0.014
    0.050   0.172   0.307   0.471
    0.149   0.444   0.211   0.195

    # hocomoco
    > AHR_si
    40.51343240527031  18.259112547756697  56.41253757072521  38.77363485291994
    10.877470982533044  11.870876719950774  34.66312982331297  96.54723985087516
    21.7165707818416  43.883079837598544  20.706746561638717  67.6523201955933
    2.5465132509466635  1.3171620263517245  145.8637051322628  4.231336967110781
    0.0  150.35847450464382  1.4927836298652875  2.1074592421627525
    3.441039751299748  0.7902972158110341  149.37613720253387  0.3512432070271259
    0.0  3.441039751299748  0.7024864140542533  149.81519121131782
    0.0  0.0  153.95871737667187  0.0
    43.07922333291745  66.87558226865211  16.159862546986584  27.844049228115868

    # neph
    UW.Motif.0001   atgactca
    0.772949    0.089579    0.098612    0.038860
    0.026652    0.004653    0.025056    0.943639
    0.017663    0.023344    0.918728    0.040264
    0.919596    0.025414    0.029759    0.025231
    0.060312    0.772259    0.104968    0.062462
    0.037406    0.020643    0.006667    0.935284
    0.047316    0.899024    0.026928    0.026732
    0.948639    0.019497    0.005737    0.026128

    # tiffin
    T   A   G   C
    30  0   28  40
    0   0   0   99
    0   55  14  29
    0   99  0   0
    20  78  0   0
    0   52  7   39
    19  46  11  22
    0   60  38  0
    0   33  0   66
    73  0   25  0
    99  0   0   0
    """
    record = Record()

    motif_name = None
    motif_nbr = 0
    motif_nbr_added = 0

    default_nucleotide_order = ['A', 'C', 'G', 'T']
    nucleotide_order = default_nucleotide_order
    #nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []}

    for line in handle:
        line = line.strip()

        if line:
            columns = line.split()
            nbr_columns = len(columns)

            if line.startswith('#'):
                # Skip comment lines.
                continue
            elif line.startswith('>'):
                # Parse ">AbdA_Cell_FBgn0000014" and "> AHR_si" like lines and put the part after ">" as motif name.
                if motif_nbr != 0 and motif_nbr_added != motif_nbr:
                    # Add the previous motif to the record.
                    motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                    motif.name = motif_name
                    record.append(motif)
                    motif_nbr_added = motif_nbr

                # Reinitialize variables for the new motif.
                motif_name = line[1:].strip()
                nucleotide_order = default_nucleotide_order
            elif columns[0] == 'Gene':
                # Parse "Gene   ENSG00000197372" like lines and put the gene name as motif name.
                if motif_nbr != 0 and motif_nbr_added != motif_nbr:
                    # Add the previous motif to the record.
                    motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                    motif.name = motif_name
                    record.append(motif)
                    motif_nbr_added = motif_nbr

                # Reinitialize variables for the new motif.
                motif_name = columns[1]
                nucleotide_order = default_nucleotide_order
            elif columns[0] == 'Motif':
                # Parse "Motif  M1734_0.90" like lines.
                if motif_nbr != 0 and motif_nbr_added != motif_nbr:
                    # Add the previous motif to the record.
                    motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                    motif.name = motif_name
                    record.append(motif)
                    motif_nbr_added = motif_nbr

                # Reinitialize variables for the new motif.
                motif_name = columns[1]
                nucleotide_order = default_nucleotide_order
            elif columns[0] == 'Pos':
                # Parse "Pos    A   C   G   T" like lines and change nucleotide order if necessary.
                if nbr_columns == 5:
                    # If the previous line was not a "Gene  ENSG00000197372" like line, a new motif starts here.
                    if motif_nbr != 0 and motif_nbr_added != motif_nbr:
                        # Add the previous motif to the record.
                        motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                        motif.name = motif_name
                        record.append(motif)
                        motif_nbr_added = motif_nbr

                    nucleotide_order = default_nucleotide_order

                    if set(columns[1:]) == set(default_nucleotide_order):
                        nucleotide_order = columns[1:]
            elif columns[0] in default_nucleotide_order:
                # Parse "A  C   G   T" like lines and change nucleotide order if necessary.
                if nbr_columns == 4:
                    nucleotide_order = default_nucleotide_order
                    if set(columns) == set(default_nucleotide_order):
                        nucleotide_order = columns
            else:
                # Parse matrix columns lines and use the correct nucleotide order.
                if nbr_columns == 4:
                    matrix_columns = columns
                elif nbr_columns == 5:
                    matrix_columns = columns[1:]
                else:
                    continue

                if motif_nbr == motif_nbr_added:
                    # A new motif matrix starts here, so reinitialize variables for the new motif.
                    nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []}
                    motif_nbr += 1

                [nucleotide_counts[nucleotide].append(float(nucleotide_count))
                 for nucleotide, nucleotide_count in zip(nucleotide_order, matrix_columns)]
        else:
            # Empty lines can be separators between motifs.
            if motif_nbr != 0 and motif_nbr_added != motif_nbr:
                # Add the previous motif to the record.
                motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
                motif.name = motif_name
                record.append(motif)
                motif_nbr_added = motif_nbr

            # Reinitialize variables for the new motif.
            motif_name = None
            nucleotide_order = default_nucleotide_order
            #nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []}

    if motif_nbr != 0 and motif_nbr_added != motif_nbr:
        motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
        motif.name = motif_name
        record.append(motif)

    return record


def _read_pfm_four_rows(handle):
    """Read motifs in position frequency matrix format from a file handle.

    Cluster Buster motif format: http://zlab.bu.edu/cluster-buster/help/cis-format.html

    #hdpi
    A   0   5   6   5   1   0
    C   1   1   0   0   0   4
    G   5   0   0   0   3   0
    T   0   0   0   1   2   2

    # yetfasco
    A   0.5 0.0 0.0 0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.5 0.0 0.0833333334583333
    T   0.0 0.0 0.0 0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.0 0.0 0.0833333334583333
    G   0.0 1.0 0.0 0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.0 1.0 0.249999999875
    C   0.5 0.0 1.0 0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.25    0.5 0.0 0.583333333208333

    #flyfactorsurvey ZFP finger
    A |     92    106    231    135      0      1    780     28      0    700    739     94     60    127    130
    C |    138     82    129     81    774      1      3      1      0      6     17     49    193    122    148
    G |    270    398     54    164      7    659      1    750    755     65      1     41    202    234    205
    T |    290    204    375    411      9    127      6     11     36     20     31    605    335    307    308

    # scertf pcm
    A | 9 1 1 97 1 94
    T | 80 1 97 1 1 2
    C | 9 97 1 1 1 2
    G | 2 1 1 1 97 2

    # scertf pfm
    A | 0.090 0.010 0.010 0.970 0.010 0.940
    C | 0.090 0.970 0.010 0.010 0.010 0.020
    G | 0.020 0.010 0.010 0.010 0.970 0.020
    T | 0.800 0.010 0.970 0.010 0.010 0.020

    #idmmpmm
    > abd-A
    0.218451749734889 0.0230646871686108 0.656680805938494 0.898197242841994 0.040694591728526 0.132953340402969 0.74907211028632 0.628313891834571
    0.0896076352067868 0.317338282078473 0.321580063626723 0.0461293743372216 0.0502386002120891 0.040694591728526 0.0284994697773065 0.0339342523860021
    0.455991516436904 0.0691940615058324 0.0108695652173913 0.0217391304347826 0.0284994697773065 0.0284994697773065 0.016304347826087 0.160127253446448
    0.235949098621421 0.590402969247084 0.0108695652173913 0.0339342523860021 0.880567338282079 0.797852598091198 0.206124072110286 0.17762460233298

    # JASPAR
        >MA0001.1 AGL3
        A  [ 0  3 79 40 66 48 65 11 65  0 ]
        C  [94 75  4  3  1  2  5  2  3  3 ]
        G  [ 1  0  3  4  1  0  5  3 28 88 ]
        T  [ 2 19 11 50 29 47 22 81  1  6 ]

    or::

        >MA0001.1 AGL3
        0  3 79 40 66 48 65 11 65  0
        94 75  4  3  1  2  5  2  3  3
        1  0  3  4  1  0  5  3 28 88
        2 19 11 50 29 47 22 81  1  6
    """

    record = Record()

    name_pattern = re.compile(r"^>\s*(.+)\s*")
    row_pattern_with_nucleotide_letter = re.compile(r"\s*([ACGT])\s*[[]*[|]*\s*([0-9.\s]+)\s*[]]*\s*")
    row_pattern_without_nucleotide_letter = re.compile(r"\s*([0-9.\s]+)\s*")

    motif_name = None
    nucleotide_counts = {}
    row_count = 0
    nucleotides = ['A', 'C', 'G', 'T']

    for line in handle:
        line = line.strip()

        name_match = name_pattern.match(line)
        row_match_with_nucleotide_letter = row_pattern_with_nucleotide_letter.match(line)
        row_match_without_nucleotide_letter = row_pattern_without_nucleotide_letter.match(line)

        if name_match:
            motif_name = name_match.group(1)
        elif row_match_with_nucleotide_letter:
            (nucleotide, counts_str) = row_match_with_nucleotide_letter.group(1, 2)
            current_nucleotide_counts = counts_str.split()
            nucleotide_counts[nucleotide] = [float(current_nucleotide_count) for current_nucleotide_count in current_nucleotide_counts]
            row_count += 1
            if row_count == 4:
                motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)

                if motif_name:
                    motif.name = motif_name

                record.append(motif)

                motif_name = None
                nucleotide_counts = {}
                row_count = 0
        elif row_match_without_nucleotide_letter:
            current_nucleotide_counts = row_match_without_nucleotide_letter.group(1).split()
            nucleotide_counts[nucleotides[row_count]] = [float(current_nucleotide_count) for current_nucleotide_count in current_nucleotide_counts]
            row_count += 1
            if row_count == 4:
                motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)

                if motif_name:
                    motif.name = motif_name

                record.append(motif)

                motif_name = None
                nucleotide_counts = {}
                row_count = 0

    return record


def _read_jaspar(handle):
    """Read motifs from a JASPAR formatted file (PRIVATE).

    Format is one or more records of the form, e.g.::

        >MA0001.1 AGL3
        A  [ 0  3 79 40 66 48 65 11 65  0 ]
        C  [94 75  4  3  1  2  5  2  3  3 ]
        G  [ 1  0  3  4  1  0  5  3 28 88 ]
        T  [ 2 19 11 50 29 47 22 81  1  6 ]

    or::

        >MA0001.1 AGL3
        0  3 79 40 66 48 65 11 65  0
        94 75  4  3  1  2  5  2  3  3
        1  0  3  4  1  0  5  3 28 88
        2 19 11 50 29 47 22 81  1  6

    """

    counts = {}

    record = Record()

    head_pat = re.compile(r"^>\s*(\S+)(\s+(\S+))?")
    row_pat_long = re.compile(r"\s*([ACGT])\s*\[\s*([0-9.\s]*)\s*\]")
    row_pat_long2 = re.compile(r"\s*([ACGT])\s*\|\s*(.*)\s*")
    row_pat_short = re.compile(r"\s*(.+)\s*")

    identifier = None
    name = None
    row_count = 0
    nucleotides = ['A', 'C', 'G', 'T']
    for line in handle:
        line = line.strip()

        head_match = head_pat.match(line)
        row_match_long = row_pat_long.match(line)
        row_match_short = row_pat_short.match(line)

        if head_match:
            identifier = head_match.group(1)
            if head_match.group(3):
                name = head_match.group(3)
            else:
                name = identifier
        elif row_match_long:
            (letter, counts_str) = row_match_long.group(1, 2)
            words = counts_str.split()
            counts[letter] = [float(x) for x in words]
            row_count += 1
            if row_count == 4:
                record.append(motifs.Motif(identifier, name, alphabet=IUPAC.unambiguous_dna,
                                    counts=counts))
                identifier = None
                name = None
                counts = {}
                row_count = 0
        elif row_match_short:
            words = row_match_short.group(1).split()
            counts[nucleotides[row_count]] = [float(x) for x in words]
            row_count += 1
            if row_count == 4:
                record.append(motifs.Motif(identifier, name, alphabet=IUPAC.unambiguous_dna,
                                    counts=counts))
                identifier = None
                name = None
                counts = {}
                row_count = 0

    return record


def _read_pfm(handle):
    """Read the motif from a JASPAR .pfm file (PRIVATE)."""
    counts = {}

    letters = "ACGT"
    for letter, line in zip(letters, handle):
        words = line.split()
        # if there is a letter in the beginning, ignore it
        if words[0] == letter:
            words = words[1:]
        counts[letter] = [float(x) for x in words]

    motif = motifs.Motif(matrix_id=None, name=None, alphabet=IUPAC.unambiguous_dna, counts=counts)
    motif.mask = "*" * motif.length
    record = Record()
    record.append(motif)

    return record



def write(motifs):
    """Return the representation of motifs in Cluster Buster position frequency matrix format."""

    lines = []
    for m in motifs:
        line = '>{0}\n'.format(m.name)
        lines.append(line)
        for ACGT_counts in zip(m.counts['A'], m.counts['C'], m.counts['G'], m.counts['T']):
            lines.append('{0:0.0f}\t{1:0.0f}\t{2:0.0f}\t{3:0.0f}\n'.format(*ACGT_counts))

    # Finished; glue the lines together.
    text = ''.join(lines)

    return text

Bio/motifs/xms.py

# Copyright 2015 by Gert Hulselmans.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Parse XMS motif files."""

from Bio import motifs
from Bio.Alphabet import IUPAC


from xml.dom import minidom, Node
import re


__docformat__ = "restructuredtext en"


class XMSScanner:
    def __init__(self, doc):
        self.record = Record()
        for child in doc.getElementsByTagName('motif'):
            if child.nodeType == Node.ELEMENT_NODE:
                self.handle_motif(child)

    def handle_motif(self, node):
        motif_name = self.get_text(node.getElementsByTagName("name"))
        nucleotide_counts = {'A': [], 'C': [], 'G': [], 'T': []}

        for column in node.getElementsByTagName("column"):
            [nucleotide_counts[nucleotide].append(float(nucleotide_count))
             for nucleotide, nucleotide_count in zip(['A', 'C', 'G', 'T'], self.get_acgt(column))]

        motif = motifs.Motif(alphabet=IUPAC.unambiguous_dna, counts=nucleotide_counts)
        motif.name = motif_name

        self.record.append(motif)

    def get_property_value(self, node, key_name):
        for cur_property in node.getElementsByTagName('prop'):
            right_property = False
            cur_value = None
            for child in cur_property.childNodes:
                if child.nodeType != Node.ELEMENT_NODE:
                    continue
                if child.tagName == 'key' and self.get_text([child]) == key_name:
                    right_property = True
                if child.tagName == 'value':
                    cur_value = self.get_text([child])
            if right_property:
                return cur_value
        return None

    def get_acgt(self, node):
        a, c, g, t = 0.0, 0.0, 0.0, 0.0
        for weight in node.getElementsByTagName('weight'):
            if weight.getAttribute("symbol") == "adenine":
                a = float(self.get_text([weight]))
            elif weight.getAttribute("symbol") == "cytosine":
                c = float(self.get_text([weight]))
            elif weight.getAttribute("symbol") == "guanine":
                g = float(self.get_text([weight]))
            elif weight.getAttribute("symbol") == "thymine":
                t = float(self.get_text([weight]))
        return a, c, g, t

    def get_text(self, nodelist):
        retlist = []
        for node in nodelist:
            if node.nodeType == Node.TEXT_NODE:
                retlist.append(node.wholeText)
            elif node.hasChildNodes:
                retlist.append(self.get_text(node.childNodes))

        return re.sub('\s+', ' ', ''.join(retlist))


class Record(list):
    """A Bio.motifs.xms.Record stores the information in a XMS matrix table.
    The record inherits from a list containing the individual motifs.
    """

    def __str__(self):
        return "\n".join(str(motif) for motif in self)


def read(handle):
    """Read motifs in XMS matrix format from a file handle.

    XMS is an XML format for describing regulatory motifs and PSSMs.
    This format was defined by Thomas Down, and used in the NestedMICA and MotifExplorer programs.
    """

    xms_doc = minidom.parse(handle)
    record = XMSScanner(xms_doc).record

    return record

Tests/motifs/clusterbuster.pfm

>MA0004.1
4   16  0   0
19  0   1   0
0   20  0   0
0   0   20  0
0   0   0   20
0   0   20  0
>MA0006.1
3   8   2   11
0   0   23  1
0   23  0   1
0   0   23  1
0   0   0   24
0   0   24  0
>MA0008.1
3   13  4   5
21  1   0   3
25  0   0   0
0   0   0   25
0   5   0   20
24  0   1   0
1   0   0   24
0   0   2   23

Tests/motifs/clusterbuster.pfm.out

>MA0004.1
4   16  0   0
19  0   1   0
0   20  0   0
0   0   20  0
0   0   0   20
0   0   20  0
>MA0006.1
3   8   2   11
0   0   23  1
0   23  0   1
0   0   23  1
0   0   0   24
0   0   24  0
>MA0008.1
3   13  4   5
21  1   0   3
25  0   0   0
0   0   0   25
0   5   0   20
24  0   1   0
1   0   0   24
0   0   2   23

Activity

  1. mdehoon commented on Aug 8, 2016

    @mdehoon
    Contributor

    I have no objections.

  2. peterjc commented on Aug 8, 2016

    @peterjc
    Member

    Thanks @mdehoon.

    @ghuls it sounds like this is worth turning into a pull request with the example files as unit tests.

    Note we're transitioning to dual licensing Biopython as per #898, so we'll need you to explicitly agree using both the BSD 3-Clause License and the current Biopython License Agreement for this contribution. Thanks!

  3. olgabot commented on May 29, 2017

    @olgabot

    Has this been added? I see there's a comment to parse HOMER motifs but it's not officially documented. I've written a simple parser but would prefer to contribute/use something centralized like BioPython

  4. peterjc commented on May 29, 2017

    @peterjc
    Member

    This has not been merged - but see #898 (comment) where @ghuls did agree to dual licensing.

    As the Bio.motifs module owner Michiel seems happy, so if @ghuls or you (@olgabot) would like to turn this into a pull request, that would be great.

    Separately a pull request to add HOMER motif support sounds great.

  5. j-andrews7 commented on Oct 24, 2017

    @j-andrews7
    Contributor

    I am planning to turn this into pull requests unless @ghuls would rather take care of it. I think it will alleviate many of the issues that have plagued the motifs module for some time. I will also try to add support for the minimal MEME format per #232.

  6. peterjc commented on Oct 24, 2017

    @peterjc
    Member

    @j-andrews7 Please go ahead and turn this into a pull request - as long as @ghuls is clearly credited that should be fine. Thanks.

    (Ideally commit the initial files with him as the author, but if you don't know how to do that in git, that's not essential)

  7. j-andrews7 commented on Oct 24, 2017

    @j-andrews7
    Contributor

    @peterjc I will look at it tonight and see what needs to be done to ensure it works. I will be sure to attribute @ghuls as appropriate.

    I'll see if I can figure out how to commit with him as the author and try to commit any changes I make personally under my own name.

  8. peterjc commented on Oct 25, 2017

    @peterjc
    Member

    I've emailed you about how to commit under a different name.

  9. j-andrews7 commented on Nov 14, 2017

    @j-andrews7
    Contributor

    @peterjc and @ghuls, I've just created the pull request that should add this code. I didn't write tests for all of the different four column and four row formats but those I tested manually worked fine. I committed the bulk of the code as @ghuls, so be sure to credit him appropriately. Most of my additions were just tests, flake8 compliance, and fixing a few edge cases.

  10. ghuls commented on Nov 14, 2017

    @ghuls
    ContributorAuthor

    @j-andrews7 Thanks for working on this.

  11. peterjc commented on May 16, 2019

    @peterjc
    Member

    Finally applied via #1453 - thank you everyone, sorry this took so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions