Example #1
0
 def delete_sample_meta(self, sample_id, sample_meta_id):
     response = requests.delete(
         default_config.get_elab_api_url() +
         'samples/{sample_id}/meta/{sample_meta_id}'.format(
             sample_id=sample_id, sample_meta_id=sample_meta_id),
         headers=self.headers)
     return response.status_code
Example #2
0
 def get_sample_type(self, name):
     '''
     Get sample type with specified name.
     '''
     response = requests.get(default_config.get_elab_api_url() +
                             'sampleTypes?name={name}'.format(name=name),
                             headers=self.headers)
     return response.json()
Example #3
0
 def get_sample_type_meta(self, sample_type_id):
     '''
     Get sample type meta with specified sample_type_id.
     '''
     response = requests.get(default_config.get_elab_api_url() +
                             'sampleTypes/{sample_type_id}/meta'.format(
                                 sample_type_id=sample_type_id),
                             headers=self.headers)
     return response.json()
Example #4
0
    def get_sample(self, sample_id):
        ''':returns: Sample with the specified sample id. '''

        response = requests.get(
            default_config.get_elab_api_url() +
            'samples/{sample_id}'.format(sample_id=sample_id),
            headers=self.headers)

        return response.json()
Example #5
0
    def get_sample_meta(self, sample_type_name, meta_key):
        ''' Get sample meta information for only the specified meta key in all samples with the specified sample type
        :param sample_type_name: sample type name
        :param meta_key: meta key (field name)

        :returns example:
            {'meta_key': 'BCCRC Jira Ticket',
             'meta_option_values': [],
             'meta_sample_data_type': 'TEXT',
             'meta_sample_type_id': 25463,
             'sample_type_name': 'Tissue'}
        '''

        response = requests.get(default_config.get_elab_api_url() +
                                'sampleTypes',
                                headers=self.headers)
        # get all sample types
        sample_types = response.json()['data']

        ret_type = None

        for type in sample_types:
            # filter by specified sample type name
            if type['name'] == sample_type_name:
                response = requests.get(default_config.get_elab_api_url() +
                                        'sampleTypes/' +
                                        str(type['sampleTypeID']) + '/meta',
                                        headers=self.headers)
                sampleTypeMeta = response.json()['data']

                for meta in sampleTypeMeta:
                    if meta['key'] == meta_key:

                        ret_type = {
                            'sample_type_name': type['name'],
                            'meta_key': meta['key'],
                            'meta_sample_data_type': meta['sampleDataType'],
                            'meta_sample_type_id': meta['sampleTypeID'],
                            'meta_option_values': meta['optionValues']
                        }

                        return ret_type

        return ret_type
Example #6
0
    def get_all_sample_sets(self):
        logger.info('getting all samples...')

        # get sample count
        response = requests.get(default_config.get_elab_api_url() +
                                'sampleSeries?$records=1',
                                headers=self.headers)
        total_records = response.json()['totalRecords']

        # get all sample meta meta data
        page = 0
        sample_sets = []
        while len(sample_sets) != total_records:
            response = requests.get(default_config.get_elab_api_url() +
                                    'sampleSeries?$page=' + str(page),
                                    headers=self.headers)
            sample_sets += response.json()['data']
            page += 1

        assert len(sample_sets) == total_records

        return sample_sets
Example #7
0
    def put_sample_meta(self, sample_id, key):
        '''
        This call will check if a meta field with the specified key already exists in the sample record with the
         specified sample_id. If it does exist, nothing is done.

        If a meta field with the specified key does not exist it, then a new meta field is created with an empty
         value.

        The key must exist in the sample type definition of the sample. This implies that the key needs to
         be added to the sample type definition for the sample before the key can be added to existing records
         through this function.

        The value for the key is always set to the empty string.

        :param sample_id sample id
        :param key field name
        :param sampleTypeMetaID sample type meta ID or field id defined in the sample type of sample with the
               specified sample id

        :returns updated sample record.

        :raise exception
             if key is not found in the sample type definition of sample specified by sample_id.
        '''
        # if key already exists in sample, do nothing
        if self.is_key_in_sample_meta(sample_id, key):
            return

        # get sample type id from sample with specified sample id
        sample = self.get_sample(sample_id)
        sample_type_id = sample['sampleType']['sampleTypeID']

        # check if key exists in sample type meta
        if self.is_key_in_sample_type_meta(sample_type_id, key) == False:
            raise Exception(
                'key ' + key +
                ' is not found in sample type definition for sample with sample id '
                + sample_id + '. See sample type definition -  \n' +
                json.dumps(self.get_sample_type_meta(sample_type_id), indent=1)
            )

        # put key with empty value field

        # data = '{ \
        #         "key": "'+key+'", \
        #         "sampleTypeMetaID": '+str(self.get_sample_type_meta_id(sample_type_id, key))+', \
        #         "sampleDataType": "'+self.get_sample_data_type(sample_type_id, key)+'" \
        #         }'

        data = {
            "key": key,
            "sampleTypeMetaID":
            self.get_sample_type_meta_id(sample_type_id, key),
            "sampleDataType": self.get_sample_data_type(sample_type_id, key)
        }

        print(data)

        response = requests.post(
            default_config.get_elab_api_url() +
            'samples/{sample_id}/meta'.format(sample_id=sample_id),
            headers=self.headers,
            data=data)
        logger.info('put missing key ' + key + ' for sample with ' +
                    sample_id + ' response: ' + str(response.text))

        response = requests.get(
            default_config.get_elab_api_url() +
            'samples/{sample_id}/meta'.format(sample_id=sample_id),
            headers=self.headers)
        return response.json()
Example #8
0
 def get_sample_meta(self, sample_id):
     response = requests.get(
         default_config.get_elab_api_url() +
         'samples/{sample_id}/meta'.format(sample_id=sample_id),
         headers=self.headers)
     return response.json()
Example #9
0
    def extract_scrna_table(self, sample_sets=None):
        '''
        Extract SCRNA table from elab.
        '''

        headers = {
            'Authorization': default_config.get_elab_api_token(),
            "Host": default_config.get_elab_host_url()
        }

        # get sample count
        response = requests.get(default_config.get_elab_api_url() +
                                'sampleSeries?$records=1',
                                headers=headers)
        total_records = response.json()['totalRecords']

        # get all sample meta meta data
        page = 0
        sample_sets = []
        while len(sample_sets) != total_records:
            response = requests.get(default_config.get_elab_api_url() +
                                    'sampleSeries?$page=' + str(page),
                                    headers=headers)
            sample_sets += response.json()['data']
            page += 1

        assert len(sample_sets) == total_records

        # filter by patient subset
        patient_subset = []

        pt_id_list = []
        for ii in range(1, 73):  # patients 1 to 72
            id = 'SPECTRUM-OV-0' + str("{:02d}".format(ii))
            pt_id_list.append(id)

        for sample in sample_sets:
            if sample['name'] in pt_id_list:
                patient_subset.append(sample)

        logger.info("attempting to get data for " + str(len(patient_subset)) +
                    " patients...")

        # get all sample meta data for patient subset
        #elab_sample_data = []

        elab_metadata = []

        for patient in patient_subset:
            sample_ids = patient["sampleIDs"]

            logger.info("getting data for patient " + patient['name'])

            for sample_id in sample_ids:
                response = requests.get(
                    default_config.get_elab_api_url() +
                    'samples/{sampleid}'.format(sampleid=sample_id),
                    headers=headers)

                # filter sample meta data by Tissue samples only
                if response.json()["sampleType"]["name"] == "Tissue":
                    response = requests.get(
                        default_config.get_elab_api_url() +
                        'samples/{sampleid}/meta'.format(sampleid=sample_id),
                        headers=headers)
                    all_elab_metadata = response.json()

                    # pull key value attributes only from elab API pull
                    data = {}

                    for meta in all_elab_metadata['data']:
                        if 'value' not in meta.keys():
                            data[meta['key']] = meta['files'][0]['name']
                        else:
                            data[meta['key']] = meta['value']

                    elab_metadata.append(data)

        # break  # just collect 1 since it takes time to collect all

        with open("elab_metadata", 'w') as outfile:
            jstr = json.dumps(elab_metadata,
                              sort_keys=True,
                              indent=2,
                              separators=(',', ': '))
            outfile.write(jstr)