Ejemplo n.º 1
0
    def search(self, params):
        limit, offset, sort = parse_pagination_params(params)

        query_string = params.get('q')
        formula = params.get('formula')
        cactus = params.get('cactus')
        if query_string is None and formula is None and cactus is None:
            raise RestException(
                'Either \'q\', \'formula\' or \'cactus\' is required.')

        if query_string is not None:
            try:
                mongo_query = query.to_mongo_query(query_string)
            except query.InvalidQuery:
                raise RestException('Invalid query', 400)

            fields = ['inchikey', 'smiles', 'properties', 'name']
            cursor = MoleculeModel().find(query=mongo_query,
                                          fields=fields,
                                          limit=limit,
                                          offset=offset,
                                          sort=sort)
            mols = [x for x in cursor]
            num_matches = cursor.collection.count_documents(mongo_query)

            return search_results_dict(mols, num_matches, limit, offset, sort)

        elif formula:
            # Search using formula
            return MoleculeModel().findmol(params)

        elif cactus:
            if getCurrentUser() is None:
                raise RestException('Must be logged in to search with cactus.')

            # Disable cert verification for now
            # TODO Ensure we have the right root certs so this just works.
            r = requests.get(
                'https://cactus.nci.nih.gov/chemical/structure/%s/file?format=sdf'
                % cactus,
                verify=False)

            if r.status_code == 404:
                return []
            else:
                r.raise_for_status()

            sdf_data = r.content.decode('utf8')
            provenance = 'cactus: ' + cactus
            mol = create_molecule(sdf_data,
                                  'sdf',
                                  getCurrentUser(),
                                  True,
                                  provenance=provenance)

            return search_results_dict([mol], 1, limit, offset, sort)
Ejemplo n.º 2
0
    def findmol(self, search=None):
        limit, offset, sort = parse_pagination_params(search)

        if search is None:
            search = {}

        query = {}
        if 'queryString' in search:
            # queryString takes precedence over all other search params
            query_string = search['queryString']
            try:
                query = mol_query.to_mongo_query(query_string)
            except mol_query.InvalidQuery:
                raise RestException('Invalid query', 400)
        elif search:
            # If the search dict is not empty, perform a search
            if 'name' in search:
                query['name'] = {
                    '$regex': '^' + search['name'],
                    '$options': 'i'
                }
            if 'inchi' in search:
                query['inchi'] = search['inchi']
            if 'inchikey' in search:
                query['inchikey'] = search['inchikey']
            if 'smiles' in search:
                # Make sure it is canonical before searching
                query['smiles'] = openbabel.to_smiles(search['smiles'], 'smi')
            if 'formula' in search:
                formula_regx = re.compile('^%s$' % search['formula'],
                                          re.IGNORECASE)
                query['properties.formula'] = formula_regx
            if 'creatorId' in search:
                query['creatorId'] = ObjectId(search['creatorId'])

        fields = ['inchikey', 'smiles', 'properties', 'name']

        cursor = self.find(query,
                           fields=fields,
                           limit=limit,
                           offset=offset,
                           sort=sort)

        num_matches = cursor.collection.count_documents(query)

        mols = [x for x in cursor]
        return search_results_dict(mols, num_matches, limit, offset, sort)
Ejemplo n.º 3
0
    def find_geometries(self, moleculeId, user, paging_params):

        limit, offset, sort = parse_pagination_params(paging_params)

        query = {'moleculeId': ObjectId(moleculeId)}

        fields = [
            'creatorId', 'moleculeId', 'provenanceId', 'provenanceType',
            'energy'
        ]

        cursor = self.findWithPermissions(query,
                                          user=user,
                                          fields=fields,
                                          limit=limit,
                                          offset=offset,
                                          sort=sort)

        num_matches = cursor.collection.count_documents(query)

        geometries = [x for x in cursor]
        return search_results_dict(geometries, num_matches, limit, offset,
                                   sort)