def parseRearrangement()

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


    def parseRearrangement(self, alteration,  measrumentgenepanelid, tar_id, updateSelection, prev_gene_panel_id):            
        #parse the sections with rearrangement and add to db
        print('parseRearrangement')       
        description = None
        inframe = None
        pos1 = None
        pos2 = None
        status = None
        supportingreadpairs = None
        type = None
        allelefraction = None
        rtype=None
        
        if not 'gene1' in alteration:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: gene1 field is required in rearrangements')
            self.log.logMessage(self.filename + ' gene1 field is required in rearrangements')
            self.errorflag=True
            raise GenomicDataException("gene1 field is required in rearrangements")  
        if len(alteration['gene1'].strip())==0:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: gene1 field needs a value in rearrangements')
            self.log.logMessage(self.filename + ' gene1 field needs a value in rearrangements')
            self.errorflag=True
            raise GenomicDataException("gene1 field needs a value in rearrangements")
        gene1_id = self.getGene(alteration['gene1'])
        
        if not 'gene2' in alteration:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: gene2 field is required in rearrangements')
            self.log.logMessage(self.filename + ' gene2 field is required in rearrangements')
            self.errorflag=True
            raise GenomicDataException("gene2 field is required in rearrangements")  
        if len(alteration['gene2'].strip())==0:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: gene2 field needs a value in rearrangements')
            self.log.logMessage(self.filename + ' gene2 field needs a value in rearrangements')
            self.errorflag=True
            raise GenomicDataException("gene2 field needs a value in rearrangements")
        gene2_id = self.getGene(alteration['gene2'])    
        
        if not 'rearrangementType' in alteration:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: rearrangementType field is required in rearrangements')
            self.log.logMessage(self.filename + ' rearrangementType field is required in rearrangements')
            self.errorflag=True
            raise GenomicDataException("rearrangementType field is required in rearrangements")  
        rtype = alteration['rearrangementType'] 
        if len(rtype.strip())==0:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: rearrangementType field needs a value in rearrangements')
            self.log.logMessage(self.filename + ' rearrangementType field needs a value in rearrangements')
            self.errorflag=True
            raise GenomicDataException("rearrangementType field needs a value in rearrangements")
        
        if not 'position1' in alteration:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: position1 field is required in rearrangements')
            self.log.logMessage(self.filename + ' position1 field is required in rearrangements')
            self.errorflag=True
            raise GenomicDataException("position1 field is required in rearrangements")  
        pos1 = alteration['position1'] 
        if len(pos1.strip())==0:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: position1 field needs a value in rearrangements')
            self.log.logMessage(self.filename + ' position1 field needs a value in rearrangements')
            self.errorflag=True
            raise GenomicDataException("position1 field needs a value in rearrangements")
        if not 'position2' in alteration:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: position2 field is required in rearrangements')
            self.log.logMessage(self.filename + ' position2 field is required in rearrangements')
            self.errorflag=True
            raise GenomicDataException("position2 field is required in rearrangements")  
        pos2 = alteration['position2']
        if len(pos2.strip())==0:
            self.log.systemStatusUpdate(self.filename, 'Genomic', self.log.timestamp(), 'Error: position2 field needs a value in rearrangements')
            self.log.logMessage(self.filename + ' position2 field needs a value in rearrangements')
            self.errorflag=True
            raise GenomicDataException("position2 field needs a value in rearrangements")
        
        if 'status' in alteration:
            status=alteration['status']
            
        if 'variantAlleleFrequency' in alteration:
            allelefraction=alteration['variantAlleleFrequency']
            
        if 'inFrame' in alteration:
            inframe = alteration['inFrame']
            
        if 'supportingReadPairs' in alteration:
            supportingreadpairs = alteration['supportingReadPairs']
        
        if 'description' in alteration:
            description =  alteration['description']
            
        #insert into measurement_gene_variant
        addRearrangement = "INSERT INTO MEASUREMENT_GENE_VARIANT (measurement_gene_panel_id, rearr_description, rearr_in_frame, rearr_gene_2, \
        rearr_pos1, rearr_pos2, status, rearr_number_of_reads, rearr_gene_1, variant_type, variant_allele_frequency, rearr_type) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        cursor = self.conn.cursor()
        cursor.execute(addRearrangement, (str(measrumentgenepanelid), description, inframe, str(gene2_id), pos1, pos2, status, supportingreadpairs, str(gene1_id),'rearrangement', allelefraction, rtype))
        rowid=cursor.lastrowid
        if updateSelection:
            #check whether was ticked before
            findPreviouseSelectionQuery="select type from SELECTED_GENE_VARIANT FULL OUTER JOIN MEASUREMENT_GENE_VARIANT on \
            MEASUREMENT_GENE_VARIANT.measurement_gene_variant_id=SELECTED_GENE_VARIANT.measurement_gene_variant_id \
            FULL OUTER JOIN MEASUREMENT_GENE_PANEL on MEASUREMENT_GENE_PANEL.measurement_gene_panel_id=MEASUREMENT_GENE_VARIANT.measurement_gene_panel_id \
            where person_id=(SELECT person_id FROM PERSON WHERE person.target_id = '"+tar_id+"') and \
            MEASUREMENT_GENE_PANEL.measurement_gene_panel_id=" +str(prev_gene_panel_id)+ " and  \
            rearr_gene_1="+str(gene1_id)+" and rearr_description = '"+str(description)+"' and rearr_in_frame ='"+str(inframe)+"' and  rearr_gene_2="+str(gene2_id)+" \
            and rearr_pos1='"+str(pos1)+"' and rearr_pos2='"+str(pos2)+"' and status='"+str(status)+"' and rearr_number_of_reads="+str(supportingreadpairs)+";"
#                 print(findPreviouseSelectionQuery)
            findPreviousSelectionCursor = self.conn.cursor()
            findPreviousSelectionCursor.execute(findPreviouseSelectionQuery)
            row=findPreviousSelectionCursor.fetchone()
#                 print(row)
            findPreviousSelectionCursor.close()
            if row is not None:
                #was previously selected
                insertSelectionQuery="insert into SELECTED_GENE_VARIANT (person_id, measurement_gene_variant_id, type) values((SELECT person_id FROM PERSON WHERE person.target_id = '"+tar_id+"'),"+str(rowid)+",'"+row[0]+"');"
#                     print("---------\n",insertSelectionQuery)
                insertSelectionCursor = self.conn.cursor()
                insertSelectionCursor.execute(insertSelectionQuery)
                insertSelectionCursor.close()