-- parsing and conversion to text
select mol_to_xqmol('Cc1n[nH]c(F)c1'::mol);
                                                                                                                                                                                                                                                                  mol_to_xqmol                                                                                                                                                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     "xqm_type": "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMGQCgAAAADBAdAKAAAAAMDB2A4AAAAAwEDBkAoAAAAAwQJACAAAAABBkBoAAAAAwMBCwABAAECaAwCA2gMAwRoDAQFAAQGaAwGAWgMQgEAAAAFAQIDBAYXBAAAAAAAAAAW",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "Cc1cc(F)[nH]n1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMGQCgAAAADBAdAOAAAAAMBAwdgKAAAAAMDBkAoAAAAAwQJACAAAAABBkBoAAAAAwMBCwABAAECaAwCA2gMAwRoDAQFAAQGaAwGAWgMQgEAAAAFAQIDBAYXBAAAAAAAAAAW",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "Cc1cc(F)n[nH]1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMGQCgAAAADBAcQKAAAAAACGQ0AAABBdG9tQXRvbWljTnVtIRoHAAAAAAAAABwAKwcQKAAAAAACGQ0AAABBdG9tQXRvbWljTnVtIRoHAAAAAAAAABwAKwZAKAAAAAMECQAgAAAAAQZAaAAAAAMDAQsAAQABAhgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKwIDaAwDBBgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKwQFAAQGGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArBgEYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACtCAQAAAAUBAgMEBhcEAAAAAAAAABY=",+
         "smarts": "[#6]-[#6]1-,=,:[#7]:[#7]-,=,:[#6](-[#9])-,=,:[#6]-,=,:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         "2",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
         "3"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         "1",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
         "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
         "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
         "6"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
 
(1 row)

select mol_to_xqmol('COC |LN:1:1.3|'::mol);
                                                                      mol_to_xqmol                                                                      
--------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                     +
     "xqm_type": "2",                                                                                                                                  +
     "mols": [                                                                                                                                         +
         {                                                                                                                                             +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAADAAAAAgAAAIABBgBoAAAAAAEDCAAoAAAAAAIGAGgAAAAAAQMLAAEAAQIAFwQAAAAAAAAAFg==",                            +
             "smiles": "COC"                                                                                                                           +
         },                                                                                                                                            +
         {                                                                                                                                             +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAEAAAAAwAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAgYAaAAAAAABAwsAAQABAgACAwAXBAAAAAAAAAAW",                +
             "smiles": "COOC"                                                                                                                          +
         },                                                                                                                                            +
         {                                                                                                                                             +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAFAAAABAAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAggAKAAAAAACBgBoAAAAAAEDCwABAAECAAIDAAMEABcEAAAAAAAAABY=",+
             "smiles": "COOOC"                                                                                                                         +
         }                                                                                                                                             +
     ]                                                                                                                                                 +
 }                                                                                                                                                     +
 
(1 row)

select mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
                                                                                                                                                                                                                                                                                mol_to_xqmol                                                                                                                                                                                                                                                                                
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     "xqm_type": "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     "num_entries": "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     "tautomerQueries": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
             "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBoAAAAAAEDCAAoAAAAAAIGQCgAAAAABAdAKAAAAAMDBkBoAAAAAwMBB0A4AAAAAwEDBkBoAAAAAwMBCwABAAECAAIDSAwEAkgMAwVIDAYESAwFBkgMQgEAAAAFAgMFBgQXBAAAAAAAAAAW",                                                                                                                                                                                                                                                                                                                                                    +
                     "smiles": "COc1cc[nH]n1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
                 },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0A4AAAAAwEDBkBoAAAAAwMBB2AoAAAAAwMGQGgAAAADAwELAAEAAQIgAgNoDAQCaAwDBWgMBgRoDAUGaAxCAQAAAAUCAwUGBBcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                                    +
                     "smiles": "COc1ccn[nH]1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
                 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
                 "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBoAAAAAAEDCAAoAAAAAAIGQCgAAAAABAcQKAAAAAACGQ0AAABBdG9tQXRvbWljTnVtIRoHAAAAAAAAABwAKwZAaAAAAAMDAQcQKAAAAAACGQ0AAABBdG9tQXRvbWljTnVtIRoHAAAAAAAAABwAKwZAaAAAAAMDAQsAAQABAgACAxgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKwQCGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArAwVIDAYEGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArBQYYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsXBAAAAAAAAAAW",                                +
                 "smarts": "[#6]-[#8]-[#6]1-,=,:[#7]:[#7]-,=,:[#6]-,=,:[#6]-,=,:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "5"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "2",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "6"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
         },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
             "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAIAAAACAAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAgZAKAAAAAAEB0AoAAAAAwMGQGgAAAADAwEHQDgAAAADAQMGQGgAAAADAwELAAEAAQIAAgMAAwRIDAUDSAwEBkgMBwVIDAYHSAxCAQAAAAUDBAYHBRcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                    +
                     "smiles": "COOc1cc[nH]n1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
                 },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAIAAAACAAAAIABBgBgAAAAAQMIACAAAAACCAAoAAAAAwIGQCgAAAADBAdAOAAAAAMBAwZAaAAAAAMDAQdgKAAAAAMDBkBoAAAAAwMBCwABAAECAAIDIAMEaAwFA2gMBAZoDAcFaAwGB2gMQgEAAAAFAwQGBwUXBAAAAAAAAAAW",                                                                                                                                                                                                                                                                                                                                        +
                     "smiles": "COOc1ccn[nH]1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
                 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
                 "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAIAAAACAAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAgZAKAAAAAAEBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBkBoAAAAAwMBBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBkBoAAAAAwMBCwABAAECAAIDAAMEGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArBQMYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsEBkgMBwUYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsGBxgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKxcEAAAAAAAAABY=",                +
                 "smarts": "[#6]-[#8]-[#8]-[#6]1-,=,:[#7]:[#7]-,=,:[#6]-,=,:[#6]-,=,:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
             },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "6"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "6",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "7"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
         },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
             "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAJAAAACQAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAggAKAAAAAACBkAoAAAAAAQHQCgAAAADAwZAaAAAAAMDAQdAOAAAAAMBAwZAaAAAAAMDAQsAAQABAgACAwADBAAEBUgMBgRIDAUHSAwIBkgMBwhIDEIBAAAABQQFBwgGFwQAAAAAAAAAFg==",                                                                                                                                                                                                                                                                                                                    +
                     "smiles": "COOOc1cc[nH]n1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
                 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
                     "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAJAAAACQAAAIABBgBgAAAAAQMIACAAAAACCAAgAAAAAggAKAAAAAMCBkAoAAAAAwQHQDgAAAADAQMGQGgAAAADAwEHYCgAAAADAwZAaAAAAAMDAQsAAQABAgACAwADBCAEBWgMBgRoDAUHaAwIBmgMBwhoDEIBAAAABQQFBwgGFwQAAAAAAAAAFg==",                                                                                                                                                                                                                                                                                                                        +
                     "smiles": "COOOc1ccn[nH]1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
                 "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAJAAAACQAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAggAKAAAAAACBkAoAAAAAAQHECgAAAAAAhkNAAAAQXRvbUF0b21pY051bSEaBwAAAAAAAAAcACsGQGgAAAADAwEHECgAAAAAAhkNAAAAQXRvbUF0b21pY051bSEaBwAAAAAAAAAcACsGQGgAAAADAwELAAEAAQIAAgMAAwQABAUYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsGBBgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKwUHSAwIBhgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAKwcIGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArFwQAAAAAAAAAFg==",+
                 "smarts": "[#6]-[#8]-[#8]-[#8]-[#6]1-,=,:[#7]:[#7]-,=,:[#6]-,=,:[#6]-,=,:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
             },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "7"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
             "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
                 "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "7",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
                 "8"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
             ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
 
(1 row)

select mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol, false);
                                                                                                                                                                                                                                                                    mol_to_xqmol                                                                                                                                                                                                                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     "xqm_type": "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0AoAAAAAwMHQDgAAAADAQMGQGgAAAADAwEGQGgAAAADAwELAAEAAQIgAgNoDAMEaAwEBWgMBQZoDAYCaAxCAQAAAAUCAwQFBhcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "COc1cc[nH]n1 |LN:1:1.3|"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
         },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0A4AAAAAwEDB2AoAAAAAwMGQGgAAAADAwEGQGgAAAADAwELAAEAAQIgAgNoDAMEaAwEBWgMBQZoDAYCaAxCAQAAAAUCAwQFBhcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "COc1ccn[nH]1 |LN:1:1.3|"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBkBoAAAAAwMBBkBoAAAAAwMBCwABAAECIAIDGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArAwRoDAQFGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArBQYYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsGAhgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAK0IBAAAABQIDBAUGFwQAAAAAAAAAFg==",+
         "smarts": "[#6]-[#8]-[#6]1-,=,:[#7]:[#7]-,=,:[#6]-,=,:[#6]-,=,:1 |LN:1:1.3|"                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
         "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "4"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
         "2",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "6"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
 
(1 row)

select mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol, true, false);
                                                                                                          mol_to_xqmol                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                             +
     "xqm_type": "2",                                                                                                                                                                                                          +
     "mols": [                                                                                                                                                                                                                 +
         {                                                                                                                                                                                                                     +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBoAAAAAAEDCAAoAAAAAAIGQCgAAAAABAdAKAAAAAMDBkBoAAAAAwMBB0A4AAAAAwEDBkBoAAAAAwMBCwABAAECAAIDSAwEAkgMAwVIDAYESAwFBkgMFwQAAAAAAAAAFg==",                            +
             "smiles": "COc1cc[nH]n1"                                                                                                                                                                                          +
         },                                                                                                                                                                                                                    +
         {                                                                                                                                                                                                                     +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAIAAAACAAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAgZAKAAAAAAEB0AoAAAAAwMGQGgAAAADAwEHQDgAAAADAQMGQGgAAAADAwELAAEAAQIAAgMAAwRIDAUDSAwEBkgMBwVIDAYHSAwXBAAAAAAAAAAW",                +
             "smiles": "COOc1cc[nH]n1"                                                                                                                                                                                         +
         },                                                                                                                                                                                                                    +
         {                                                                                                                                                                                                                     +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAJAAAACQAAAIABBgBoAAAAAAEDCAAoAAAAAAIIACgAAAAAAggAKAAAAAACBkAoAAAAAAQHQCgAAAADAwZAaAAAAAMDAQdAOAAAAAMBAwZAaAAAAAMDAQsAAQABAgACAwADBAAEBUgMBgRIDAUHSAwIBkgMBwhIDBcEAAAAAAAAABY=",+
             "smiles": "COOOc1cc[nH]n1"                                                                                                                                                                                        +
         }                                                                                                                                                                                                                     +
     ]                                                                                                                                                                                                                         +
 }                                                                                                                                                                                                                             +
 
(1 row)

select mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol, false, false);
                                                                                                mol_to_xqmol                                                                                                
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                         +
     "xqm_type": "1",                                                                                                                                                                                      +
     "mol": {                                                                                                                                                                                              +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0AoAAAAAwMHQDgAAAADAQMGQGgAAAADAwEGQGgAAAADAwELAAEAAQIgAgNoDAMEaAwEBWgMBQZoDAYCaAxCAQAAAAUCAwQFBhcEAAAAAAAAABY=",+
         "smiles": "COc1cc[nH]n1"                                                                                                                                                                          +
     }                                                                                                                                                                                                     +
 }                                                                                                                                                                                                         +
 
(1 row)

select mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol, false, false, true);
                                                                                                                                                                                                                                                                                                                                                                        mol_to_xqmol                                                                                                                                                                                                                                                                                                                                                                        
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     "xqm_type": "1",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     "mol": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZQKAAAAAMEGQcAAABBdG9tQW5kHhwCDQAAAEF0b21BdG9taWNOdW0hGgYAAAAAAAAAHAASAAAAQXRvbUV4cGxpY2l0RGVncmVlIRoDAAAAAAAAABwAKwdQKAAAAAMDGQcAAABBdG9tQW5kHhwCDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAASAAAAQXRvbUV4cGxpY2l0RGVncmVlIRoCAAAAAAAAABwAKwdQOAAAAAMBAxkHAAAAQXRvbUFuZB4cAg0AAABBdG9tQXRvbWljTnVtIRoHAAAAAAAAABwAEgAAAEF0b21FeHBsaWNpdERlZ3JlZSEaAgAAAAAAAAAcACsGUGgAAAADAwEZBwAAAEF0b21BbmQeHAINAAAAQXRvbUF0b21pY051bSEaBgAAAAAAAAAcABIAAABBdG9tRXhwbGljaXREZWdyZWUhGgIAAAAAAAAAHAArBlBoAAAAAwMBGQcAAABBdG9tQW5kHhwCDQAAAEF0b21BdG9taWNOdW0hGgYAAAAAAAAAHAASAAAAQXRvbUV4cGxpY2l0RGVncmVlIRoCAAAAAAAAABwAKwsAAQABAiACA2gMAwRoDAQFaAwFBmgMBgJoDEIBAAAABQIDBAUGFwQAAAAAAAAAFg==",+
         "smarts": "[#6]-[#8]-[#6&D3]1:[#7&D2]:[#7&D2]:[#6&D2]:[#6&D2]:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
 
(1 row)

-- substructure searching
select 'Cc1[nH]nc(F)c1'::mol @> mol_to_xqmol('Cc1n[nH]c(F)c1'::mol);
 ?column? 
----------
 t
(1 row)

select substruct('Cc1[nH]nc(F)c1'::mol,mol_to_xqmol('Cc1n[nH]c(F)c1'::mol));
 substruct 
-----------
 t
(1 row)

select substruct('Cc1[nH]nc(F)c1'::mol,mol_to_xqmol('Cc1n[nH]c(F)n1'::mol));
 substruct 
-----------
 f
(1 row)

select substruct('COC'::mol,'COC |LN:1:1.3|'::mol);
 substruct 
-----------
 t
(1 row)

select substruct('COC'::mol,mol_to_xqmol('COC |LN:1:1.3|'::mol));
 substruct 
-----------
 t
(1 row)

select substruct('COOC'::mol,'COC |LN:1:1.3|'::mol);
 substruct 
-----------
 f
(1 row)

select substruct('COOC'::mol,mol_to_xqmol('COC |LN:1:1.3|'::mol));
 substruct 
-----------
 t
(1 row)

select substruct('COOOC'::mol,mol_to_xqmol('COC |LN:1:1.3|'::mol));
 substruct 
-----------
 t
(1 row)

select substruct('COOOOC'::mol,mol_to_xqmol('COC |LN:1:1.3|'::mol));
 substruct 
-----------
 f
(1 row)

select 'COc1[nH]ncc1'@>mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
 ?column? 
----------
 t
(1 row)

select 'COc1[n][nH]cc1'@>mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
 ?column? 
----------
 t
(1 row)

select 'COOc1[nH]ncc1'@>mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
 ?column? 
----------
 t
(1 row)

select 'COOOc1[nH]ncc1'@>mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
 ?column? 
----------
 t
(1 row)

select 'COOOOc1[nH]ncc1'@>mol_to_xqmol('COC1=NNC=C1 |LN:1:1.3|'::mol);
 ?column? 
----------
 f
(1 row)

-- edge cases and error handling
select mol_to_xqmol('C1'::mol);
ERROR:  could not create molecule from SMILES 'C1'
LINE 1: select mol_to_xqmol('C1'::mol);
                            ^
select mol_to_xqmol('c1cc1'::mol);
ERROR:  MolToXQMol: Can't kekulize mol.  Unkekulized atoms: 0 1 2
select mol_to_xqmol('COC1=NNC=C1'::mol);
                                                                                                                                                                                                                                                                    mol_to_xqmol                                                                                                                                                                                                                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     "xqm_type": "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     "tautomers": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0AoAAAAAwMHQDgAAAADAQMGQGgAAAADAwEGQGgAAAADAwELAAEAAQIgAgNoDAMEaAwEBWgMBQZoDAYCaAxCAQAAAAUCAwQFBhcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "COc1cc[nH]n1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
         },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
         {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
             "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEB0A4AAAAAwEDB2AoAAAAAwMGQGgAAAADAwEGQGgAAAADAwELAAEAAQIgAgNoDAMEaAwEBWgMBQZoDAYCaAxCAQAAAAUCAwQFBhcEAAAAAAAAABY=",                                                                                                                                                                                                                                                                                                                                    +
             "smiles": "COc1ccn[nH]1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "template": {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAHAAAABwAAAIABBgBgAAAAAQMIACgAAAADAgZAKAAAAAMEBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBxAoAAAAAAIZDQAAAEF0b21BdG9taWNOdW0hGgcAAAAAAAAAHAArBkBoAAAAAwMBBkBoAAAAAwMBCwABAAECIAIDGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArAwRoDAQFGAAZHAAAAFNpbmdsZU9yRG91YmxlT3JBcm9tYXRpY0JvbmQhGgEAAAAAAAAAHAArBQYYABkcAAAAU2luZ2xlT3JEb3VibGVPckFyb21hdGljQm9uZCEaAQAAAAAAAAAcACsGAhgAGRwAAABTaW5nbGVPckRvdWJsZU9yQXJvbWF0aWNCb25kIRoBAAAAAAAAABwAK0IBAAAABQIDBAUGFwQAAAAAAAAAFg==",+
         "smarts": "[#6]-[#8]-[#6]1-,=,:[#7]:[#7]-,=,:[#6]-,=,:[#6]-,=,:1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "modifiedAtoms": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
         "3",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "4"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     ],                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     "modifiedBonds": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
         "2",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "4",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "5",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
         "6"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
 }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
 
(1 row)

select 'CCOC1=NNC=C1'::mol@>mol_to_xqmol('COC1=NNC=C1'::mol);
 ?column? 
----------
 t
(1 row)

select mol_to_xqmol('CCCC'::mol);
                                                              mol_to_xqmol                                                              
----------------------------------------------------------------------------------------------------------------------------------------
 {                                                                                                                                     +
     "xqm_type": "1",                                                                                                                  +
     "mol": {                                                                                                                          +
         "pkl": "776t3gAAAAAQAAAAAgAAAAAAAAAEAAAAAwAAAIABBgBgAAAAAQMGAGAAAAACAgYAYAAAAAICBgBgAAAAAQMLAAEAAQIAAgMAQgAAAAAXBAAAAAAAAAAW",+
         "smiles": "CCCC"                                                                                                              +
     }                                                                                                                                 +
 }                                                                                                                                     +
 
(1 row)

select 'CCCCOC'::mol@>mol_to_xqmol('CCCC'::mol);
 ?column? 
----------
 t
(1 row)

-- interaction with the generic groups
select 'COC1=NNC(CC)=C1'::mol @>> mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}');
 ?column? 
----------
 f
(1 row)

select 'COC1=NNC(C=C)=C1'::mol @>> mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}');
 ?column? 
----------
 t
(1 row)

select 'COC1=NNC(CC)=C1'::mol @>> mol_to_xqmol(mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}'));
 ?column? 
----------
 f
(1 row)

select 'COC1=NNC(C=C)=C1'::mol @>> mol_to_xqmol(mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}'));
 ?column? 
----------
 t
(1 row)

select 'COOC1=NNC(CC)=C1'::mol @>> mol_to_xqmol(mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}'));
 ?column? 
----------
 f
(1 row)

select 'COOC1=NNC(C=C)=C1'::mol @>> mol_to_xqmol(mol_adjust_query_properties('COC1=NNC(*)=C1 |$;;;;;;AEL_p;$,LN:1:1.3|'::mol,
  '{"makeDummiesQueries":true, "setGenericQueryFromProperties": true}'));
 ?column? 
----------
 t
(1 row)

-- problems we've encountered
select 'CCOC(=O)c1cc2n(n1)C(C(=O)O)Nc1cc(Cl)ccc1-2'::mol @> mol_to_xqmol('COCc1n[nH]c(C)c1 |LN:1:1.3|'::mol);
 ?column? 
----------
 t
(1 row)

-- link nodes and variable attachment points
select 'Cc1ccnc2nc(CN)[nH]c12'::mol@>>mol_to_xqmol(mol_from_ctab('qry 
  Mrv2305 09052314502D          

  0  0  0     0  0            999 V3000
M  V30 BEGIN CTAB
M  V30 COUNTS 13 13 0 0 0
M  V30 BEGIN ATOM
M  V30 1 N -4.75 1.9567 0 0
M  V30 2 C -6.0837 1.1867 0 0
M  V30 3 C -6.0837 -0.3534 0 0
M  V30 4 C -4.75 -1.1234 0 0
M  V30 5 C -3.4163 -0.3534 0 0
M  V30 6 C -3.4163 1.1867 0 0
M  V30 7 N -1.9692 1.7134 0 0
M  V30 8 N -1.8822 -0.7768 0 0
M  V30 9 C -1.0211 0.4999 0 0
M  V30 10 C 0.5179 0.5536 0 0
M  V30 11 N 1.2409 1.9133 0 0
M  V30 12 * -5.6391 -0.0967 0 0
M  V30 13 C -5.6391 -2.4067 0 0
M  V30 END ATOM
M  V30 BEGIN BOND
M  V30 1 1 1 2
M  V30 2 2 2 3
M  V30 3 1 3 4
M  V30 4 2 4 5
M  V30 5 1 5 6
M  V30 6 2 1 6
M  V30 7 1 8 9
M  V30 8 1 7 6
M  V30 9 1 5 8
M  V30 10 2 7 9
M  V30 11 1 9 10
M  V30 12 1 10 11
M  V30 13 1 12 13 ENDPTS=(3 4 3 2) ATTACH=ANY
M  V30 END BOND
M  V30 END CTAB
M  END'));
 ?column? 
----------
 t
(1 row)

select 'c1ccnc2nc(CN)[nH]c12'::mol@>>mol_to_xqmol(mol_from_ctab('qry 
  Mrv2305 09052314502D          

  0  0  0     0  0            999 V3000
M  V30 BEGIN CTAB
M  V30 COUNTS 13 13 0 0 0
M  V30 BEGIN ATOM
M  V30 1 N -4.75 1.9567 0 0
M  V30 2 C -6.0837 1.1867 0 0
M  V30 3 C -6.0837 -0.3534 0 0
M  V30 4 C -4.75 -1.1234 0 0
M  V30 5 C -3.4163 -0.3534 0 0
M  V30 6 C -3.4163 1.1867 0 0
M  V30 7 N -1.9692 1.7134 0 0
M  V30 8 N -1.8822 -0.7768 0 0
M  V30 9 C -1.0211 0.4999 0 0
M  V30 10 C 0.5179 0.5536 0 0
M  V30 11 N 1.2409 1.9133 0 0
M  V30 12 * -5.6391 -0.0967 0 0
M  V30 13 C -5.6391 -2.4067 0 0
M  V30 END ATOM
M  V30 BEGIN BOND
M  V30 1 1 1 2
M  V30 2 2 2 3
M  V30 3 1 3 4
M  V30 4 2 4 5
M  V30 5 1 5 6
M  V30 6 2 1 6
M  V30 7 1 8 9
M  V30 8 1 7 6
M  V30 9 1 5 8
M  V30 10 2 7 9
M  V30 11 1 9 10
M  V30 12 1 10 11
M  V30 13 1 12 13 ENDPTS=(3 4 3 2) ATTACH=ANY
M  V30 END BOND
M  V30 END CTAB
M  END'));
 ?column? 
----------
 f
(1 row)

