def pushDrugBankNamePropInDB(self): cDB = DBrequest.DBrequest() i = 1 for PROP in LPROP: cDB.addElement("drugbank_prop", ["id", "property"], [i, PROP]) i = i + 1
def pushTablePropAllInDB(self): if not "pTableInAll" in self.__dict__: print("GENERATE TABLE FIRST") dtopush = toolbox.loadMatrixToDict(self.pTableInAll) cDB = DBrequest.DBrequest() cDB.verbose = 0 i = 0 i = 500000 lchem = list(dtopush.keys()) imax = len(lchem) while i < imax: #for chem in dtopush.keys(): chem = lchem[i] outChem = cDB.getRow("dsstox_prop", "db_id='%s'" % (chem)) if outChem == []: wprop = "{" + ",".join([ "\"%s\"" % (dtopush[chem][PROP].replace("'", "")) for PROP in LPROP ]) + "}" cDB.addElement("dsstox_prop", ["db_id", "prop_value"], [chem, wprop]) i = i + 1 return
def pushNeighbors(self): prneighbor = pathFolder.createFolder(self.prout + "Neighbors/") ptable3Dim = prneighbor + "Table_DIM1D2D-2_1.csv" ptableNDim = prneighbor + "Table_DIM1D2D-170_207.csv" if path.exists(ptable3Dim) and path.exists(ptableNDim): ddist3D = toolbox.loadMatrixToDict(ptable3Dim) for chem in ddist3D.keys(): ddist3D[chem] = ddist3D[chem]["Neighbors"].split(" ") ddistND = toolbox.loadMatrixToDict(ptableNDim) for chem in ddistND.keys(): ddistND[chem] = ddistND[chem]["Neighbors"].split(" ") cDB = DBrequest.DBrequest() cDB.verbose = 0 for chem in ddist3D.keys(): # print(chem) #out1D2D = cDB.getRow("%s_neighbors"%(self.nameMap), "inchikey='%s'" % (chem)) out1D2D = [] if out1D2D == []: w3D = "{" + ",".join( ["\"%s\"" % (neighbor) for neighbor in ddist3D[chem]]) + "}" wND = "{" + ",".join( ["\"%s\"" % (neighbor) for neighbor in ddistND[chem]]) + "}" cDB.addElement( "%s_neighbors" % (self.nameMap), ["inchikey", "neighbors_dim3", "neighbors_dimn"], [chem, w3D, wND])
def __init__(self, p_sdf, p_LD50, pr_out): self.p_sdf = p_sdf self.p_LD50 = p_LD50 self.pr_out = pathFolder.createFolder(pr_out + "TOX_DB/") self.cDB = DBrequest.DBrequest()
def __init__(self, name_update, pr_OPERA_preproc, pr_out): self.name_update = name_update self.pr_OPERA_preproc = pr_OPERA_preproc self.pr_out = pr_out self.loadOPERAFileInClass() self.cDB = DBrequest.DBrequest(verbose=0) self.pr_desc = pathFolder.createFolder(self.pr_out + "DESC/")
def pushDssToxNamePropInDB(self): cDB = DBrequest.DBrequest() cDB.verbose = 1 i = 1 for PROP in LPROP: cDB.addElement("dsstox_name_prop", ["id", "name"], [i, PROP]) i = i + 1
def uploadAC50InDB(TC, nametable): cDB = DBrequest.DBrequest() lassays = list(TC.dassays.keys()) for chem in TC.dchem.keys(): for assay in lassays: if assay in list(TC.dchem[chem].activeAssays.keys()): cDB.addElement( nametable, ["casn", "ac50", "assay"], [chem, TC.dchem[chem].activeAssays[assay], assay])
def Notes_Response(flag): res = DBrequest.dbNoteRes(flag) notes = [] for note_list in res: notes.append(Data_Model.Note(note_list[0], note_list[1], note_list[2])) ob = Data_Model.Notes(notes) a = json.dumps(ob, indent=3, cls=Data_Model.NotesEncoder, ensure_ascii=False) return a
def pushPropNameInDB(self, typeDesc): ldesc = Chemical.getLdesc(typeDesc) if typeDesc == "1D2D" or typeDesc == "1D" or typeDesc == "2D": DBname = "desc_1d2d_prop" elif typeDesc == "3D": DBname = "desc_3d_prop" cDB = DBrequest.DBrequest() i = 1 for desc in ldesc: cDB.addElement(DBname, ["id", "descriptor"], [i, desc]) i = i + 1
def pushChemInDB(self): if not "dchem" in self.__dict__: self.loadlistChem() if self.nameMap == "dsstox": cDB = DBrequest.DBrequest() cDB.verbose = 0 for chem in self.dchem.keys(): cDB.addElement("dsstox_chem", [ "db_id", "smiles_origin", "smiles_clean", "inchikey", "qsar_ready" ], [ chem, self.dchem[chem]["smiles_origin"], self.dchem[chem]["smiles_clean"], self.dchem[chem]["inchikey"], self.dchem[chem]["qsar_ready"] ]) else: cDB = DBrequest.DBrequest() ddd
def loadOPERADesc(pOPERA, table): ddesc = toolbox.loadMatrixToDict(pOPERA, sep = ",") LPROP = list(ddesc[list(ddesc.keys())[0]].keys()) cDB = DBrequest.DBrequest() cDB.verbose = 1 i = 1 for PROP in LPROP[1:]: cDB.addElement(table, ["id", "name"], [i, PROP]) i = i + 1 #loadOPERADesc("/home/borrela2/sandbox/VM/ChemMap2Site/temp/949289/OPERA/ZWRUINPWMLAQRD-UHFFFAOYSA-N.csv", "desc_opera_name")
def DataParser(data): deser = json.loads(data) # поле operation определяет операцию для действий с логином и/или паролем, т. е. там где указан type равный auth if str(type(deser)) == '<class \'dict\'>' and deser['type'] == 'auth': if deser['operation'] == 'authentication': Data_Authentication = Data_Model.Authentication( deser['login'], deser['password'], deser['operation']) result = DBrequest.dbAuth(Data_Authentication) return result elif deser['operation'] == 'registration': Data_Registration = Data_Model.Authentication( deser['login'], deser['password'], deser['operation']) return DBrequest.dbReg(Data_Registration) elif deser['operation'] == 'changepassword': #смена пароля Data_Change_Password = Data_Model.Authentication( deser['login'], deser['password'], deser['operation']) return DBrequest.dbChangePassword(Data_Change_Password) #type для заметок: добавление, удаление, проверка на существование elif deser['type'] == 'addNote': Data_Notes = Data_Model.Note(deser['name'], deser['data'], deser['secflag']) return DBrequest.dbNoteAdd(Data_Notes) elif deser['type'] == 'deleteNote': Data_Notes = Data_Model.Note(deser['name'], deser['data'], deser['secflag']) return DBrequest.dbNoteDelete(Data_Notes) elif deser[ 'type'] == 'checkexist': #Проверка на существование заметки с таким же названием Data_Notes = Data_Model.Note(deser['name'], deser['data'], deser['secflag']) return DBrequest.dbNoteExist(Data_Notes)
def updateTableProp(self, nameMap): cDB = DBrequest.DBrequest() self.verbose = 0 if not "dchem" in self.__dict__: self.loadlistChem() for chem in self.dchem: dsstox = chem.replace("\"", "") cmdSQL = "UPDATE dsstox_prop SET %s=true WHERE db_id='%s';" % ( self.nameMap, dsstox) cDB.updateTable(cmdSQL)
def computeCoords(self, corVal, distributionVal, insertDB=1): if not "p1D2D" in self.__dict__ and not "p3D" in self.__dict__: self.computeDesc(insertDB=0) err = 0 # create coords prmap = pathFolder.createFolder(self.prout + "map_" + str(corVal) + "-" + str(distributionVal) + "/") pcoordDim1Dim2 = prmap + "coord1D2D.csv" pcoordDim3D = prmap + "coord3D.csv" if path.exists(pcoordDim1Dim2) and path.exists(pcoordDim3D): self.pcoords1D2D = pcoordDim1Dim2 self.pcoords3D = pcoordDim3D elif not path.exists(pcoordDim1Dim2) or not path.exists(pcoordDim3D): runExternalSoft.RComputeMapFiles(self.p1D2D, self.p3D, prmap, corVal, distributionVal) elif not path.exists(pcoordDim1Dim2) or not path.exists(pcoordDim3D): print("ERROR file map") err = 1 self.pcoords1D2D = pcoordDim1Dim2 self.pcoords3D = pcoordDim3D if insertDB == 1 and err == 0: dcoord1D2D = toolbox.loadMatrixToDict(pcoordDim1Dim2, sep=",") dcoord3D = toolbox.loadMatrixToDict(pcoordDim3D, sep=",") cDB = DBrequest.DBrequest() cDB.verbose = 0 for chem in dcoord1D2D.keys(): #print(chem) #out1D2D = cDB.getRow("drugbank_coords", "inchikey='%s'" % (chem)) out1D2D = [] if out1D2D == []: nbdim1d2d = len(dcoord1D2D[chem].keys()) - 1 nbdim3d = len(dcoord3D[chem].keys()) - 1 w1D2D = "{" + ",".join([ "\"%s\"" % (dcoord1D2D[chem]["DIM" + str(i)]) for i in range(1, nbdim1d2d + 1) ]) + "}" w3D = "{" + ",".join([ "\"%s\"" % (dcoord3D[chem]["DIM3-" + str(i)]) for i in range(1, nbdim3d + 1) ]) + "}" cDB.addElement( "drugbank_coords", ["inchikey", "dim1d2d", "dim3d", "indrugbank"], [chem, w1D2D, w3D, "True"])
def pushPropInDB(self): if not "dchem" in self.__dict__: self.parseSDFDB() cDB = DBrequest.DBrequest() for chem in self.dchem.keys(): lprop = self.dchem[chem]["DB_prop"] wprop = "{" + ",".join([ "\"%s\"" % (prop.replace("\'", "").replace("\"", "")) for prop in lprop ]) + "}" cDB.addElement("drugbank_prop", ["drugbank_id", "prop_value"], [self.dchem[chem]["drugbank_id"], wprop]) return
def pushChemInDB(self): if not "dchem" in self.__dict__: self.parseSDFDB() cDB = DBrequest.DBrequest() for chem in self.dchem.keys(): cDB.addElement("drugbank_chem", [ "db_id", "smiles_origin", "smiles_clean", "inchikey", "qsar_ready" ], [ self.dchem[chem]["drugbank_id"], self.dchem[chem]["smiles_origin"], self.dchem[chem]["smiles_clean"], self.dchem[chem]["inchikey"], self.dchem[chem]["qsar_ready"] ])
def prepChemForWebsite(PTOX21CHEMSUM, PTOX21CHEM, prout, indb=0): dTox21ChemSum = toolbox.loadMatrixToDict(PTOX21CHEMSUM, sep=",") dTox21Chem = toolbox.loadMatrixToDict(PTOX21CHEM, sep='\t') #print(dTox21Chem) #print(dTox21ChemSum) cDB = DBrequest.DBrequest() dout = {} for chem in dTox21ChemSum: CAS = dTox21ChemSum[chem]["casn"] dtxid = dTox21ChemSum[chem]["dsstox_substance_id"] name = dTox21ChemSum[chem]["chnm"] try: SMILES = dTox21Chem[dtxid]["smiles_origin"] except: SMILES = 0 dout[CAS] = {} dout[CAS]["DTXID"] = dtxid dout[CAS]["name"] = name dout[CAS]["SMILES"] = SMILES pfilout = prout + "ChemSum" filout = open(pfilout, "w") filout.write("CAS\tDTXID\tName\tSMILES\n") for chem in dout.keys(): if dout[chem]["SMILES"] == 0: continue filout.write("%s\t%s\t%s\t%s\n" % (chem, dout[chem]["DTXID"], dout[chem]["name"], dout[chem]["SMILES"])) if indb == 1: cDB.addElement("bodymap_chemicals", ["casn", "dsstox_id", "name", "smiles"], [ chem, dout[chem]["DTXID"], dout[chem]["name"], dout[chem]["SMILES"] ]) filout.close() return dout
def pushPropInDB(self): tableDB = "dsstox_chem" print("Push prop in DB: ") if not "dchem" in self.__dict__: self.loadlistChem() cDB = DBrequest.DBrequest() cDB.verbose = 0 for chem in self.dchem.keys(): cmdSQL = "SELECT count(*) FROM %s WHERE inchikey = '%s';" % ( tableDB, self.dchem[chem]["inchikey"]) findInch = cDB.execCMD(cmdSQL)[0][0] if findInch != 0: cmdSQL = "UPDATE %s SET %s = TRUE WHERE inchikey = '%s'" % ( tableDB, self.nameMap, self.dchem[chem]["inchikey"]) cDB.updateTable(cmdSQL) print("Finish push prop in DB <-") return
def pushAssayMapInDB(self, nameTable): self.cDB = DBrequest.DBrequest() self.cDB.verbose = 0 for assay in self.dprepmap.keys(): if self.dprepmap[assay]["Type of body mapping"] == "none": continue if self.dprepmap[assay]["Type of body mapping"] == "tissue": if self.dprepmap[assay]["Tissue"] == "liver": organ = "Liver" system = "Digestive System" elif self.dprepmap[assay]["Tissue"] == "vascular": organ = "Vascular" system = "Cardiovascular System" elif self.dprepmap[assay]["Tissue"] == "lung": organ = "Lung" system = "Respiratory System" elif self.dprepmap[assay]["Tissue"] == "skin": organ = "Skin" system = "Integumentary System" elif self.dprepmap[assay]["Tissue"] == "thyroid gland": organ = "Thyroid gland" system = "Endocrine System" elif self.dprepmap[assay]["Tissue"] == "cervix": organ = "Uterus" system = "Urogenital System" else: print(self.dprepmap[assay]["Tissue"]) dddd self.cDB.addElement(nameTable, ["assay", "type_map", "organ", "system"], [assay, "tissue", organ, system]) elif self.dprepmap[assay]["Type of body mapping"] == "viability": self.cDB.addElement(nameTable, ["assay", "type_map"], [assay, "viability"]) elif self.dprepmap[assay]["Type of body mapping"] == "gene target": gene = self.dprepmap[assay]["Gene"] if gene == "NA": continue self.cDB.addElement(nameTable, ["assay", "type_map", "gene"], [assay, "gene", gene])
def pushDSSTOXNeighbors(self, prin): cDB = DBrequest.DBrequest() cDB.verbose = 0 lfile = listdir(prin) for fileNeighbor in lfile: try: dneighbor = toolbox.loadMatrixToDict(prin + fileNeighbor) except: remove(prin + fileNeighbor) continue inchkey = list(dneighbor.keys())[0] dneighbor[inchkey]["Neighbors"] = dneighbor[inchkey][ "Neighbors"].split(" ") w3D = "{" + ",".join([ "\"%s\"" % (neighbor) for neighbor in dneighbor[inchkey]["Neighbors"] ]) + "}" cDB.addElement("dsstox_neighbors", ["inchikey", "neighbors_dim3"], [inchkey, w3D]) return
def computeCoords(self, corVal, distributionVal, insertDB=1): if not "p1D2D" in self.__dict__ and not "p3D" in self.__dict__: self.computeDesc(insertDB=0, w=1) # create coords prmap = pathFolder.createFolder(self.prout + "map_" + str(corVal) + "-" + str(distributionVal) + "/") self.prmap = prmap pcoordDim1Dim2 = prmap + "coord1D2D.csv" pcoordDim3D = prmap + "coord3D.csv" if not path.exists(pcoordDim1Dim2) or not path.exists(pcoordDim3D): runExternalSoft.RComputeMapFiles(self.p1D2D, self.p3D, prmap, corVal, distributionVal) if not path.exists(pcoordDim1Dim2) or not path.exists(pcoordDim3D): print("ERROR file map") return else: self.pcoords1D2D = pcoordDim1Dim2 self.pcoords3D = pcoordDim3D if insertDB == 1: if self.nameMap == "dsstox": dcoord1D2D = toolbox.loadMatrixCoords(pcoordDim1Dim2, 10) dcoord3D = toolbox.loadMatrixCoords(pcoordDim3D, 10) else: dcoord1D2D = toolbox.loadMatrixToDict(pcoordDim1Dim2, ",") dcoord3D = toolbox.loadMatrixToDict(pcoordDim3D, ",") cDB = DBrequest.DBrequest() cDB.verbose = 0 lchem = list(dcoord1D2D.keys()) i = 0 imax = len(lchem) while i < imax: #out1D2D = cDB.getRow("%s_coords"%(self.nameMap), "inchikey='%s'" % (chem)) #if out1D2D == []: if self.nameMap == "dsstox": w1D2D = "{" + ",".join([ "\"%s\"" % (str(coord)) for coord in dcoord1D2D[lchem[i]] ]) + "}" w3D = "{" + ",".join([ "\"%s\"" % (str(coord)) for coord in dcoord3D[lchem[i]] ]) + "}" cDB.addElement("%s_coords" % (self.nameMap), ["inchikey", "dim1d2d", "dim3d", "in_db"], [lchem[i], w1D2D, w3D, "1"]) del dcoord1D2D[lchem[i]] del dcoord3D[lchem[i]] del lchem[i] imax = imax - 1 else: nbdim1d2d = len(dcoord1D2D[lchem[i]].keys()) - 1 nbdim3d = len(dcoord3D[lchem[i]].keys()) - 1 w1D2D = "{" + ",".join([ "\"%s\"" % (dcoord1D2D[lchem[i]]["DIM" + str(i)]) for i in range(1, nbdim1d2d + 1) ]) + "}" w3D = "{" + ",".join([ "\"%s\"" % (dcoord3D[lchem[i]]["DIM3-" + str(i)]) for i in range(1, nbdim3d + 1) ]) + "}" cDB.addElement("%s_coords" % (self.nameMap), ["inchikey", "dim1d2d", "dim3d", "in_db"], [lchem[i], w1D2D, w3D, "1"]) del dcoord1D2D[lchem[i]] del dcoord3D[lchem[i]] del lchem[i] imax = imax - 1
def __init__(self, pr_tripodFiles): self.pr_tripodFiles = pr_tripodFiles self.DB = DBrequest.DBrequest(verbose=0)
def uploadGeneExpInDB(dAssayCleaned, cNB, dorgan, prout, w=0): """ parms: - cAssayCleaned: class assays loaded from AssayMapping.py - cNB: class to load Nextbio data from nextbio.py - w: 1 if write expression by organ gene by gene and 0 no write - pr_out: path folder for w=1 """ # load cDB cDB = DBrequest.DBrequest() prgene = pathFolder.createFolder(prout + "geneExp/") for assay in list(dAssayCleaned.keys()): genes = dAssayCleaned[assay]["Gene"] print(genes) if genes != "NA" and genes != "" and dAssayCleaned[assay][ "Type of body mapping"] == "gene target": if search("\|", genes): print(genes) genes = genes.split("|") print(gene) else: genes = [genes] for gene in genes: pgene = prgene + gene + ".csv" if path.exists(pgene): pass if w == 1: filgene = open(pgene, "w") filgene.write("system\torgan\texp\tcontrol\n") dgene = cNB.loadGeneToBodyAtlas(gene) dw = {} for system in dgene.keys(): dw[system] = {} for organ in dgene[system].keys(): organw = dorgan[system][organ] if not organw in list(dw[system].keys()): dw[system][organw] = {} dw[system][organw]["exp"] = [] dw[system][organw]["control"] = [] dw[system][organw]["control"].append( float(dgene[system][organ]["control"])) dw[system][organw]["exp"].append( float(dgene[system][organ]["expression"])) for system in dw.keys(): for organw in dw[system].keys(): if w == 1: filgene.write("%s\t%s\t%s\t%s\n" % (system, organw, max(dw[system][organw]["exp"]), max(dw[system][organw]["control"]))) # add in DB cDB.addElement("bodymap_genemap", [ "gene", "control", "expresson", "system", "organ" ], [ assay["gene"], max(dw[system][organw]["control"]), max(dw[system][organw]["exp"]), system, organw ]) if w == 1: filgene.close()
def computeDesc(self, insertDB=1, update=0): pfilout1D2D = self.prout + "1D2D.csv" pfilout3D = self.prout + "3D.csv" if update == 0 and path.exists(pfilout3D) and path.exists( pfilout1D2D) and insertDB == 0: self.p1D2D = pfilout1D2D self.p3D = pfilout3D return if not "dchem" in self.__dict__: self.parseSDFDB() lchemID = list(self.dchem.keys()) shuffle(lchemID) ldesc1D2D = Chemical.getLdesc("1D2D") ldesc3D = Chemical.getLdesc("3D") filout1D2D = open(pfilout1D2D, "w") filout1D2D.write("inchikey\t" + "\t".join(ldesc1D2D) + "\n") filout3D = open(pfilout3D, "w") filout3D.write("inchikey\t" + "\t".join(ldesc3D) + "\n") if insertDB == 1: cDB = DBrequest.DBrequest() for chemID in lchemID: SMILESClean = self.dchem[chemID]["smiles_clean"] if SMILESClean == "NA": continue cChem = Chemical.Chemical(SMILESClean, self.prDesc) cChem.prepChem() #print(SMILESClean) if cChem.err == 0: # 2D descriptors cChem.computeAll2D(update=0) cChem.writeMatrix("2D") filout1D2D.write("%s\t%s\n" % (cChem.inchikey, "\t".join( [str(cChem.all2D[desc]) for desc in ldesc1D2D]))) # insert in DB if insertDB == 1: cDB.verbose = 0 out1D2D = cDB.getRow("desc_1d2d", "inchikey='%s'" % (cChem.inchikey)) if out1D2D == []: valDesc = [ cChem.all2D[desc1D2D] for desc1D2D in ldesc1D2D ] valDesc = [ '-9999' if desc == "NA" else desc for desc in valDesc ] w1D2D = "{" + ",".join( ["\"%s\"" % (desc) for desc in valDesc]) + "}" cDB.addElement("desc_1d2d", ["inchikey", "desc_value"], [cChem.inchikey, w1D2D]) # 3D descriptors cChem.set3DChemical() # control if 3D generated if cChem.err == 0: cChem.computeAll3D(update=0) cChem.writeMatrix("3D") # write master table filout3D.write("%s\t%s\n" % (cChem.inchikey, "\t".join( [str(cChem.all3D[desc]) for desc in ldesc3D]))) # put in table descriptors if insertDB == 1: out3D = cDB.getRow("desc_3d", "inchikey='%s'" % (cChem.inchikey)) if out3D == []: valDesc = [ cChem.all3D[desc3D] for desc3D in ldesc3D ] valDesc = [ '-9999' if desc == "NA" else desc for desc in valDesc ] w3D = "{" + ",".join( ["\"%s\"" % (desc) for desc in valDesc]) + "}" cDB.addElement("desc_3d", ["inchikey", "desc_value"], [cChem.inchikey, w3D]) filout1D2D.close() filout3D.close() self.p1D2D = pfilout1D2D self.p3D = pfilout3D
def computeDesc(self, insertDB=0, w=0): if not "dchem" in self.__dict__: self.loadlistChem() pfilout1D2D = self.prout + "1D2D.csv" pfilout3D = self.prout + "3D.csv" if path.exists(pfilout1D2D) and path.exists( pfilout3D) and w == 1 and insertDB == 0: self.p1D2D = pfilout1D2D self.p3D = pfilout3D return lchemID = list(self.dchem.keys()) # can be shuffle if len(lchemID) < 50000 and self.iend == 0: shuffle(lchemID) imax = len(lchemID) if self.iend == 0 or self.iend > imax: iend = imax else: iend = self.iend lchemID = lchemID[self.istart:iend] shuffle(lchemID) ldesc1D2D = Chemical.getLdesc("1D2D") ldesc3D = Chemical.getLdesc("3D") if insertDB == 1: cDB = DBrequest.DBrequest() if w == 1: filout1D2D = open(pfilout1D2D, "w") filout1D2D.write("inchikey\t" + "\t".join(ldesc1D2D) + "\n") filout3D = open(pfilout3D, "w") filout3D.write("inchikey\t" + "\t".join(ldesc3D) + "\n") i = 0 imax = len(lchemID) while i < imax: if i % 1000 == 0: print(i) SMILESClean = self.dchem[lchemID[i]]["smiles_clean"] if SMILESClean == "NA": i = i + 1 continue cChem = Chemical.Chemical(SMILESClean, self.prDesc) cChem.prepChem() # print(SMILESClean) if cChem.err == 0: # 2D descriptors cChem.computeAll2D(update=0) if cChem.err == 1: i = i + 1 continue cChem.writeMatrix("2D") if w == 1: filout1D2D.write("%s\t%s\n" % (cChem.inchikey, "\t".join( [str(cChem.all2D[desc]) for desc in ldesc1D2D]))) # insert in DB if insertDB == 1: cDB.verbose = 0 out1D2D = cDB.getRow("desc_1d2d", "inchikey='%s'" % (cChem.inchikey)) if out1D2D == []: valDesc = [ cChem.all2D[desc1D2D] for desc1D2D in ldesc1D2D ] valDesc = [ '-9999' if desc == "NA" else desc for desc in valDesc ] w1D2D = "{" + ",".join( ["\"%s\"" % (desc) for desc in valDesc]) + "}" cDB.addElement("desc_1d2d", ["inchikey", "desc_value"], [cChem.inchikey, w1D2D]) # 3D descriptors cChem.set3DChemical() # control if 3D generated if cChem.err == 0: cChem.computeAll3D(update=0) cChem.writeMatrix("3D") if cChem.err == 1: i = i + 1 continue # write master table if w == 1: filout3D.write("%s\t%s\n" % (cChem.inchikey, "\t".join( [str(cChem.all3D[desc]) for desc in ldesc3D]))) # put in table descriptors if insertDB == 1: out3D = cDB.getRow("desc_3d", "inchikey='%s'" % (cChem.inchikey)) if out3D == []: valDesc = [ cChem.all3D[desc3D] for desc3D in ldesc3D ] valDesc = [ '-9999' if desc == "NA" else desc for desc in valDesc ] w3D = "{" + ",".join( ["\"%s\"" % (desc) for desc in valDesc]) + "}" cDB.addElement("desc_3d", ["inchikey", "desc_value"], [cChem.inchikey, w3D]) i = i + 1 if w == 1: filout1D2D.close() filout3D.close() self.p1D2D = pfilout1D2D self.p3D = pfilout3D
def splitMap(self, nbsplit, dim, insertDB=0): if not "prmap" in self.__dict__: print("Generate the map files first") return else: prout = pathFolder.createFolder(self.prmap + "split_" + str(nbsplit) + "/") self.prmaps = prout if not "psplitMap" in self.__dict__: self.psplitMap = {} # generate only one file with chem and map if dim == 1: pfilout = prout + "mapx_split.csv" elif dim == 2: pfilout = prout + "mapy_split.csv" else: pfilout = prout + "mapz_split.csv" self.psplitMap[dim] = pfilout if path.exists(pfilout) and insertDB == 0: return elif not path.exists(pfilout): coord1D2D = self.prmap + "coord1D2D.csv" coord3D = self.prmap + "coord3D.csv" if dim == 1 or dim == 2: din = toolbox.loadMatrixCoords(coord1D2D, 2) else: din = toolbox.loadMatrixCoords(coord3D, 2) # max and min 1D2D maxDim = 0.0 minDim = 0.0 nbchem = len(list(din.keys())) nbchembymap = int(nbchem / nbsplit) # calibrate max and min print("== Initiate calibration ==") for chem in din.keys(): if dim == 1 or dim == 3: dimVal = din[chem][0] elif dim == 2: dimVal = din[chem][1] if dimVal > maxDim: maxDim = dimVal if dimVal < minDim: minDim = dimVal print("== End calibration ==") dmap = {} imap = 1 dmap[imap] = [] dimVal = minDim while dimVal < maxDim: dimVal = dimVal + 0.10 if len(dmap[imap]) > nbchembymap: imap = imap + 1 dmap[imap] = [] ichem = 0 lchem = list(din.keys()) nbchem = len(lchem) while ichem < nbchem: if dim == 1 or dim == 3: valtemp = din[lchem[ichem]][0] elif dim == 2: valtemp = din[lchem[ichem]][1] if valtemp < dimVal: dmap[imap].append(deepcopy(lchem[ichem])) del din[lchem[ichem]] del lchem[ichem] nbchem = nbchem - 1 continue else: ichem = ichem + 1 print("==== Write output ====") filout = open(pfilout, "w") filout.write("inchikey\tmap\n") for d in dmap.keys(): for chem in dmap[d]: filout.write("%s\t%s\n" % (chem, d)) filout.close() if insertDB == 1: cDB = DBrequest.DBrequest() #cDB.verbose = 1 dmap = toolbox.loadMatrixToDict(pfilout) tableIn = "dsstox_coords" if dim == 1: mapIn = "mapx" elif dim == 2: mapIn = "mapy" else: mapIn = "mapz" for chem in dmap.keys(): inch = chem.replace("\"", "") cmdSQL = "UPDATE %s SET %s=%s WHERE inchikey='%s';" % ( tableIn, mapIn, dmap[chem]["map"], inch) cDB.updateTable(cmdSQL)