File: cmlhandler.py

package info (click to toggle)
chemical-structures 2.2.dfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,808 kB
  • sloc: xml: 7,224; javascript: 1,194; python: 993; sh: 51; makefile: 42
file content (125 lines) | stat: -rwxr-xr-x 3,569 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""CML file handler for Python.

This module provides an xml handler for CML file.

Exported classes:
    CMLHandler - CML file handler.
""" 

import sys
import xml.sax.handler

class CMLHandler(xml.sax.handler.ContentHandler):
    """Class for receiving logical CML content events.
  
    It supports CML entities as defined by the 2.0 version. For more details,
    see http://www.xml-cml.org/

    The order of event in this class mirrors the order of the information in
    the document.
    """
    def __init__(self):
        """Creates an instance of the CMLHandler class.

        Set the object attributes with default values.
        """
        self.id = "" 
        self.formula = ""
        self.inchi = "InChI="
        self.smiles = ""
        self.name = ""
        self.inName = False
        self.weight = ""
        self.inWeight = False
        self.monoisotopic_weight = ""
        self.inMonoisotopicWeight = False
        self.mp = ""
        self.mpSet = False
        self.inMp = False
        self.bp = "" 
        self.inBp = False
        self.bpSet = False

    def startElement(self, name, attributes):
        """Signals the start of an element.

        The function set a variable depending on the element and the
        attributes.

        Parameters:
            name - contains the raw CML name of the element type as a string.
            attributes - contains an instance of the Attributes class.
        """
        if name == "molecule":
            self.id = attributes["id"]

        if name == "formula":
            self.formula = attributes["concise"]

        if name == "identifier":
            self.inIdentifier = True
            if attributes["convention"] == "iupac:inchi":
                self.inchi += attributes["value"]

        if name == "name":
            self.inName = True

        if name == "scalar":
            if attributes["dictRef"] == "cml:molwt":
                self.inWeight = True
            elif attributes["dictRef"] == "cml:monoisotopicwt":
                self.inMonoisotopicWeight = True
            elif attributes["dictRef"] == "cml:mp":
                self.inMp = True
            elif attributes["dictRef"] == "cml:bp":
                self.inBp = True

    def characters(self, data):
        """Receives notification of character data.
        
        The parser will call this method to report each chunk of character
        data.

        Parameters:
            data - contains the chunk of character data.
        """
        if self.inName:
            self.name += data

        if self.inWeight:
            self.weight += data

        if self.inMonoisotopicWeight:
            self.monoisotopic_weight += data

        if self.inMp:
            self.mp += data

        if self.inBp:
            self.bp += data

    def endElement(self,name):
        """Signals the end of an element.

        Parameters:
            name - contains the name of the element type.
        """
        if name == "name":
            self.inName = False

        if name == "scalar":
            if self.inWeight:
                self.inWeight = False
            elif self.inMonoisotopicWeight:
                self.inMonoisotopicWeight = False
            elif self.inMp:
                self.inMp = False
                self.mp = self.mp.strip()
                if self.mp:
                    self.mpSet = True
            elif self.inBp:
                self.inBp = False
                self.bp = self.bp.strip()
                if self.bp:
                    self.bpSet = True