Exemplo n.º 1
0
    def post(self):
        """
        Convert a molecule to another file format
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            # Checks to make sure we have everything we need in payload
            self.__validate_schema(payload)
            # Read the molecule
            mol = read_molecule_from_string(
                payload['molecule']['value'],
                payload['molecule']['input']['format'],
                payload['molecule']['input']['gz']
                if 'gz' in payload['molecule']['input'] else False,
                payload['molecule']['input']['reparse']
                if 'reparse' in payload['molecule']['input'] else False)

            # Prepare the molecule for writing
            ofs = oemolostream()
            if payload['molecule']['output']['format'] == "smiles":
                ofs_format = OEFormat_SMI
            else:
                ofs_format = OEGetFileType(
                    to_utf8(payload['molecule']['output']['format']))
            if ofs_format == OEFormat_UNDEFINED:
                raise Exception("Unknown output file type: " +
                                payload['molecule']['output']['format'])
            ofs.SetFormat(ofs_format)
            ofs.openstring()
            OEWriteMolecule(ofs, mol)

            # Get molecule output stream
            if 'gz' in payload['molecule']['output'] and payload['molecule'][
                    'output']['gz']:
                output = compress_string(ofs.GetString().decode('utf-8'))
            else:
                output = ofs.GetString().decode('utf-8')

            return Response(json.dumps({
                'molecule': {
                    'value':
                    output,
                    'format':
                    payload['molecule']['output']['format'],
                    'gz':
                    payload['molecule']['output']['gz']
                    if 'gz' in payload['molecule']['output'] else False
                }
            }),
                            status=200,
                            mimetype='application/json')

        except Exception as ex:
            return Response(json.dumps({"error": str(ex)}),
                            status=400,
                            mimetype='application/json')
Exemplo n.º 2
0
 def test_get_b64_smiles(self):
     # Compress and encode
     url_compressed = quote(compress_string('c1ccccc1'))
     response = self.app.get(
         '/v1/depict/structure/smiles?val={0}&debug=true&gz=true'.format(
             url_compressed))
     self.assertEqual("200 OK", response.status)
 def test_get_b64_pdb(self):
     with open(LIGAND_FILE, 'r') as f:
         ligand = f.read()
     # Compress and encode
     url_compressed = quote(compress_string(ligand))
     response = self.app.get('/v1/depict/structure/pdb?val={0}&debug=true&gz=true'.format(url_compressed))
     self.assertEqual("200 OK", response.status)
Exemplo n.º 4
0
 def test_get_b64_pdb(self):
     with open(LIGAND_FILE, 'r') as f:
         ligand = f.read()
     # Compress and encode
     url_compressed = quote(compress_string(ligand))
     response = self.app.get(
         '/v1/depict/structure/pdb?val={0}&debug=true&gz=true'.format(
             url_compressed))
     self.assertEqual("200 OK", response.status)
Exemplo n.º 5
0
    def post(self):
        """
        Convert a molecule to another file format
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            # Checks to make sure we have everything we need in payload
            self.__validate_schema(payload)
            # Read the molecule
            mol = read_molecule_from_string(
                payload['molecule']['value'],
                payload['molecule']['input']['format'],
                payload['molecule']['input']['gz'] if 'gz' in payload['molecule']['input'] else False,
                payload['molecule']['input']['reparse'] if 'reparse' in payload['molecule']['input'] else False
            )

            # Prepare the molecule for writing
            ofs = oemolostream()
            if payload['molecule']['output']['format'] == "smiles":
                ofs_format = OEFormat_SMI
            else:
                ofs_format = OEGetFileType(to_utf8(payload['molecule']['output']['format']))
            if ofs_format == OEFormat_UNDEFINED:
                raise Exception("Unknown output file type: " + payload['molecule']['output']['format'])
            ofs.SetFormat(ofs_format)
            ofs.openstring()
            OEWriteMolecule(ofs, mol)

            # Get molecule output stream
            if 'gz' in payload['molecule']['output'] and payload['molecule']['output']['gz']:
                output = compress_string(ofs.GetString().decode('utf-8'))
            else:
                output = ofs.GetString().decode('utf-8')

            return Response(json.dumps(
                {
                    'molecule': {
                        'value': output,
                        'format': payload['molecule']['output']['format'],
                        'gz': payload['molecule']['output']['gz'] if 'gz' in payload['molecule']['output'] else False
                    }
                }
            ), status=200, mimetype='application/json')

        except Exception as ex:
            return Response(json.dumps({"error": str(ex)}), status=400, mimetype='application/json')
Exemplo n.º 6
0
    def test_pdb_gz_to_sdf_gz(self):
        """
        Test PDB to SDF molecule conversion with gzipped and base64 encoded molecule in both input and output
        """
        # First perform the conversion using the toolktis here
        reference = OEGraphMol()
        ifs = oemolistream(LIGAND_FILE)
        OEReadMolecule(ifs, reference)

        # Read the ligand as a string to POST
        with open(LIGAND_FILE, 'r') as f:
            target = f.read()

        # Perform the conversion to SDF
        response = self.app.post(
            '/v1/convert/molecule',
            data=json.dumps(
                {
                    "molecule": {
                        "value": compress_string(target),
                        "input": {"format": "pdb", "gz": True},
                        "output": {"format": "sdf", "gz": True}
                    }
                }
            ),
            headers={"content-type": "application/json"}
        )
        # Test response status
        self.assertEqual("200 OK", response.status)

        # Test the output schema
        payload = json.loads(response.data.decode('utf-8'))
        self.assertIn('molecule', payload)
        self.assertIn('value', payload['molecule'])
        self.assertIn('gz', payload['molecule'])

        # Test the validity of the output molecule
        mol = OEGraphMol()
        ifs = oemolistream()
        ifs.SetFormat(OEFormat_SDF)
        self.assertTrue(ifs.openstring(inflate_string(payload['molecule']['value'])))
        self.assertTrue(ifs.IsValid())
        self.assertTrue(OEReadMolecule(ifs, mol))
        self.assertTrue(mol.IsValid())

        # Test that the two molecule strings are equal
        self.assertEqual(OECreateCanSmiString(reference), OECreateCanSmiString(mol))
 def test_interaction_b64_protein(self):
     """
     Test providing a gzipped and then base64 encoded receptor
     """
     # Read the protein and ligand
     with open(LIGAND_FILE, 'r') as f:
         ligand = f.read()
     with open(RECEPTOR_FILE, 'r') as f:
         receptor = f.read()
     # POST in JSON
     response = self.app.post(
         '/v1/depict/interaction?format=png&debug=true',
         data=json.dumps(
             {
                 "ligand": {"value": ligand, "format": "pdb"},
                 "receptor": {"value": compress_string(receptor), "format": "pdb", "gz": True}
             }
         ),
         headers={"content-type": "application/json"}
     )
     self.assertEqual("200 OK", response.status)
 def test_get_b64_smiles(self):
     # Compress and encode
     url_compressed = quote(compress_string('c1ccccc1'))
     response = self.app.get('/v1/depict/structure/smiles?val={0}&debug=true&gz=true'.format(url_compressed))
     self.assertEqual("200 OK", response.status)