def insertMeasurementGenePanel()

in clns-eTarget_ingest/genomicdata_json.py [0:0]


    def insertMeasurementGenePanel(self, specimen_id, baseline, source_txt):
        # insert new measuremnt gene panel with empty set of data and gene panel name of 'foundationmedicine'
        try:
            tumourFractionScore= None    
            tumourFractionUnit = None
            mean_exon_depth = None
            percentExons100x = None
            if 'sample' in self.genomicalDataAll:
                sample = self.genomicalDataAll['sample']
                if 'tumourFractionScore' in sample:
                    if 'value' in sample['tumourFractionScore']:
                        tumourFractionScore=sample['tumourFractionScore']['value']
                        if not isinstance(tumourFractionScore, (float, int)):
                            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: tumourFractionScore requires a value field')
                            self.log.logMessage(self.filename + ' value in tumourFractionScore must be number')
                            self.errorflag=True
                            raise GenomicDataException("value in tumourFractionScore must be a number")
                        tumourFractionScore=str(tumourFractionScore)
                    else:
                        self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: tumourFractionScore requires a value')
                        self.log.logMessage(self.filename + ' tumourFractionScore requires a value')
                        self.errorflag=True
                        raise GenomicDataException("missing value in tumourFractionScore")
                    if 'unit' in sample['tumourFractionScore']:
                        tumourFractionUnit=sample['tumourFractionScore']['unit']
            
                    if tumourFractionUnit is not None:
                        tumourFractionUnit=tumourFractionUnit.strip()    
            
                if 'meanExonDepth' in sample:
                    mean_exon_depth = str(sample['meanExonDepth'])
                    print('median exon depth ' + str(mean_exon_depth) )
                
                if 'percentExons100x' in sample:
                    percentExons100x = str(sample['percentExons100x'])
                    print('percentExons100x ' + percentExons100x)
                 
                 
            mistatus=None
            minscore=None
            tmbscore=None
            tmbstatus=None
            tmbunit=None
            
            if 'bioMarkers' in self.genomicalDataAll:   
                biomarkers = self.genomicalDataAll['bioMarkers']
                if not isinstance(biomarkers, list):
                    self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: "bioMarkers" must be a list')
                    self.log.logMessage(self.filename + ' biomarkers must be of type list')
                    self.errorflag=True
                    raise GenomicDataException("value in bioMarker must be a list")
                for biomarker in biomarkers:
                    if not 'type' in biomarker:
                        self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: bioMarkers requires a type field')
                        self.log.logMessage(self.filename + ' bioMarkers requires a type field')
                        self.errorflag=True
                        raise GenomicDataException("bioMarkers requires a type field")
                    if biomarker['type'].lower()=='microsatellite-instability':
                        if not 'status' in biomarker:
                            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: microsatellite-instability requires a status field')
                            self.log.logMessage(self.filename + ' microsatellite-instability requires a status field')
                            self.errorflag=True
                            raise GenomicDataException("microsatellite-instability requires a status field")
                        mistatus=biomarker['status']
                        if len(mistatus.strip())==0:
                            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: status field needs a value in microsatellite-instability')
                            self.log.logMessage(self.filename + ' status field needs a value in microsatellite-instability')
                            self.errorflag=True
                            raise GenomicDataException("status field needs a value in microsatellite-instability")
                        if 'score' in biomarker:
                            minscore=biomarker['score']
                        print(mistatus)
                    elif biomarker['type'].lower()=='tumour mutation burden':
                        if not 'status' in biomarker:
                            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: tumour mutation burden requires a status field')
                            self.log.logMessage(self.filename + ' tumour mutation burden requires a status field')
                            self.errorflag=True
                            raise GenomicDataException("tumour mutation burden requires a status field")
                        tmbstatus=biomarker['status']
                        if len(tmbstatus.strip())==0:
                            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: status field needs a value in tumour mutation burden')
                            self.log.logMessage(self.filename + ' status field needs a value in tumour mutation burden')
                            self.errorflag=True
                            raise GenomicDataException("status field needs a value in tumour mutation burden")
                        if 'score' in biomarker:
                            if not 'value' in biomarker['score']:
                                self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: tumour mutation burden requires a score value')
                                self.log.logMessage(self.filename + ' tumour mutation burden requires a score value')
                                self.errorflag=True
                                raise GenomicDataException("tumour mutation burden requires a score value")
                            tmbscore=str(biomarker['score']['value'])
                            if 'unit' in biomarker['score']:
                                tmbunit=biomarker['score']['unit']
                    else:
                        self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: bioMarker type not supported')
                        self.log.logMessage(self.filename + ' bioMarker type not supported')
                        self.errorflag=True
                        raise GenomicDataException("bioMarker type not supported")
        
            select_gp_concept_id = "SELECT data_source_concept_id from CONCEPT_DATA_SOURCES where short_code = '"+source_txt+"'"
            cursor = self.conn.cursor()
            cursor.execute(select_gp_concept_id)
            row = cursor.fetchone()
            cursor.close()
            if row is not None:
                data_source_concept_id=row[0]
                
            else:
                self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: data source not registered')
                self.log.logMessage(self.filename + ' data source not registered ' + source_txt)
                self.errorflag=True
                raise GenomicDataException("data source not found")

            addFMsql = "INSERT INTO MEASUREMENT_GENE_PANEL (specimen_id, data_source_concept_id,average_read_depth, ngs_run, baseline_number, microsatellite_instability_status, tmb_score, tmb_status, tmb_unit, mean_exon_depth, percent_exons_100x, tumour_fraction_score, tumour_fraction_unit) VALUES(%s, %s, 'n/a', 'FM', %s, %s, %s, %s, %s, %s, %s, %s, %s)"
            cursor = self.conn.cursor()
            cursor.execute(addFMsql, (specimen_id, data_source_concept_id, baseline, mistatus, tmbscore, tmbstatus, tmbunit, mean_exon_depth, percentExons100x, tumourFractionScore, tumourFractionUnit))
            measurement_gene_panel_id = cursor.lastrowid
            print('measurement gene panel id ' + str(measurement_gene_panel_id))
            return measurement_gene_panel_id
        except Exception as e:
            self.log.logMessage('cannot get all biomarker values')
            self.log.logMessage(str(e))
            self.errorflag=True
            print(e)
            raise e