def filterRow(self, dataDict): filterResult = True for key in self.filterDict: rule = self.filterDict[key] if not type(rule[1]) is TupleType and not type( rule[1]) is ListType: rule[1] = (rule[1], ) if rule[0] == 'is': if key in dataDict: if dataDict[key] not in rule[1]: filterResult = False elif rule[0] == 'is not': if key in dataDict: if dataDict[key] in rule[1]: filterResult = False else: raise brdfException("filterRow : unsupported rule %s" % rule[0]) return filterResult
myObject = ob() result = "" fetchdisplaylogger.info("initialising object") try: myObject.initFromDatabase(int(fieldDict['obid']), 'ob', connection) except: # try assuming we have an lsid try: myObject.initFromDatabase(fieldDict['obid'], 'ob', connection) # complete the initialisation of the object , as in general the # displayFunction will need some data fields from the complete object. # see also the *Pages.py module which has a complete set of the # types for which we support complete intitialisation except: raise brdfException("object not found : %s" % fieldDict['obid']) if myObject.databaseFields['tablename'] == 'microarrayobservation': myObject = microarrayObservation() myObject.username = fieldDict['REMOTE_USER'] myObject.initFromDatabase(int(fieldDict['obid']), connection) myObject.initMetadata(connection) myObject.discoverLinks(connection) myObject.initComments(connection) myObject.initHyperLinks(connection) myObject.initListMembership(connection) myObject.initDisplayFunctions(connection) else: raise brdfException( "fetchDisplay needs to be updated to be able to initialise %s" % myObject.databaseFields['tablename'])
def addBatchMember(self,connection, memberob = None, memberxref = None, membershiptype = "Batch Member", inclusioncomment = None, \ batchorder = None, addeddate = None, addedby = None, voptypeid = None, checkExisting = True) : # if necessary check if this member is already in the db - if it is do not duplicate doInsert = True detailsDict = { "xreflsid": "%s%s" % (self.databaseFields["xreflsid"], { True: '', False: ":%s" % memberxref }[memberxref == None]), "batchob": self.databaseFields["obid"], "memberob": memberob, "memberxref": memberxref, "membershiptype": membershiptype, "inclusioncomment": inclusioncomment, "batchorder": batchorder, "addeddate": addeddate, "addedby": addedby, "voptypeid": voptypeid } insertCursor = connection.cursor() if checkExisting: if memberob != None: sql = """ select memberob from batchNamedMembershipLink where batchob = %(batchob)s and memberob = %(memberob)s """ elif memberxref == None: sql = """ select memberob from batchNamedMembershipLink where batchob = %(batchob)s and memberxref = %(memberxref)s """ else: raise brdfException( "addBatchMember : must specify an internal or external member identifier" ) modulelogger.info("checking for batchmember using %s" % (sql % detailsDict)) insertCursor.execute(sql, detailsDict) insertCursor.fetchone() modulelogger.info("rowcount = %s" % insertCursor.rowcount) if insertCursor.rowcount > 0: doInsert = False if doInsert: # set the listmembership type based on known list types, if we are not given a type #if voptype == None: # if self.databaseFields['membershiptype'] == "something": # queryDict['voptypeid'] = some op sql = """ insert into batchNamedMembershipLink(xreflsid, batchob,memberob,memberxref,inclusioncomment,voptypeid, membershiptype, batchorder, addeddate, addedby) values(%(xreflsid)s,%(batchob)s,%(memberob)s,%(memberxref)s,%(inclusioncomment)s,%(voptypeid)s, %(membershiptype)s, %(batchorder)s, %(addeddate)s, %(addedby)s) """ modulelogger.info("executing %s" % (sql % detailsDict)) insertCursor.execute(sql, detailsDict) connection.commit() self.obState.update({ 'NEW': 0, 'DB_PENDING': 0, 'ERROR': 0, 'MESSAGE': "database insert OK" }) # add to in-memory copies self.databaseFields['membershipcount'] += 1 sql = """ update batchob set membershipcount = %(membershipcount)s where obid = %(obid)s """ insertCursor.execute(sql, self.databaseFields) connection.commit() insertCursor.close()
def createSamplingFunction(self, connection, biosubjectob, xreflsid, bioprotocolob = None, \ labBookReference = None, samplingComment = None, parentsampleob = None, checkExisting = False,voptypeid = None): """ method used to relate this sample to a subject and optionally a protocol """ # check we do not have two parents if biosubjectob != None and parentsampleob != None: raise brdfException( "error can't specify both parent subject and parent sample") # check we have a parent if biosubjectob == None and parentsampleob == None: raise brdfException( "error must specify a parent sample or subject") functionDetails = { 'biosampleob': self.databaseFields['obid'], 'biosubjectob': eval({ False: "biosubjectob.databaseFields['obid']", True: "None" }[biosubjectob == None]), 'bioprotocolob': eval({ False: "bioprotocolob.databaseFields['obid']", True: "None" }[bioprotocolob == None]), 'labBookReference': labBookReference, 'samplingComment': samplingComment, 'xreflsid': xreflsid, 'parentsampleob': eval({ False: "parentsampleob.databaseFields['obid']", True: "None" }[parentsampleob == None]), 'voptypeid': voptypeid } insertCursor = connection.cursor() doInsert = True if checkExisting: if parentsampleob != None: sql = """ select obid from biosamplingfunction where parentsample = %(parentsampleob)s and biosampleob = %(biosampleob)s """ elif biosubjectob != None: sql = """ select obid from biosamplingfunction where biosubjectob = %(biosubjectob)s and biosampleob = %(biosampleob)s """ modulelogger.info( "createsamplingfunction checking for existing link using %s" % sql % functionDetails) insertCursor.execute(sql, functionDetails) insertCursor.fetchone() if insertCursor.rowcount >= 1: doInsert = False if doInsert: sql = """ insert into biosamplingfunction(biosampleob,biosubjectob,xreflsid,bioprotocolob,labBookReference, samplingComment, parentsample,voptypeid) values(%(biosampleob)s,%(biosubjectob)s,%(xreflsid)s,%(bioprotocolob)s,%(labBookReference)s, %(samplingComment)s, %(parentsampleob)s, %(voptypeid)s) """ insertCursor.execute(sql, functionDetails) connection.commit() insertCursor.close() self.obState.update({ 'NEW': 0, 'ERROR': 0, 'DB_PENDING': 0, 'MESSAGE': "sampling function insert OK" })
fetchgenetictestinfologger.info("initiated with %s\n\n"%str(fieldDict)) #expected required fields are context, xreflsid and field # field can be one of : # # comments # variation # primers connection=databaseModule.getConnection() myObject = ob() result="" fetchgenetictestinfologger.info("initialising object") try: myObject.initFromDatabase(fieldDict['xreflsid'],'ob',connection) except: raise brdfException("object not found : %s" % fieldDict['xreflsid']) if myObject.databaseFields['tablename'] == 'genetictestfact': myObject = geneticTestFact() myObject.username=fieldDict['REMOTE_USER'] myObject.initFromDatabase(fieldDict['xreflsid'],connection) myObject.initMetadata(connection) myObject.discoverLinks(connection) myObject.initComments(connection) myObject.initHyperLinks(connection) myObject.initListMembership(connection) else: raise brdfException("fetchAnalysis needs to be updated to be able to initialise %s"%myObject.databaseFields['tablename']) # intialise the ob with the call back URL paths for this instance of the brdf # don't really need this stuff but may as well complete the job
def fillBuffer(self): # if at the start get ID line. If we get some lines preceeding the ID line, then # these are assumed to be unnamed sequence and we fill the buffer with an unnamed sequence print "in fill buffer : %s" % str(self.parserState) if self.parserState["PRE"] == 1: # read until get ID line self.bufferedid = "anonymous_sequence" if self.subcoords[0] != None: self.bufferedid += " (extract from original sequence, from %s to %s)" % self.subcoords record = self.infile.readline() print "debug %s" % str(record) self.linecount += 1 if record == "": self.parserState["EOF"] = 1 self.parserState["BUFFER_OCCUPIED"] = 0 #print "=1=>%s"%record #while re.search('^\s*\>',record) == None: # record = self.infile.readline() # print "=2=>%s"%record # self.linecount += 1 # #print "1%s"%record #if record == "": # self.parserState["EOF"] = 1 # self.parserState["BUFFER_OCCUPIED"] = 0 # return #else: # self.parserState["IDLINE"] = 1 # self.bufferedid = record.strip() + " (extract from original sequence, from %s to %s)"%self.subcoords # #record = self.infile.readline() #self.linecount += 1 while re.search('^\s*\>', record) == None: print "=3=>%s" % record if len(record.strip()) > 0: record = record.strip() #self.bufferedSeq["sequence"].write(record.strip()) self.basecount += len(record) if self.subcoords[0] != None: if not self.parserState["PAST_SUBSEQUENCE"]: print "comparing %s and %s\n" % (self.basecount, self.subcoords[0]) if self.basecount >= self.subcoords[0]: # if necessary chop off something from the start of the record sublength = min( len(record), 1 + self.basecount - self.subcoords[0]) record = record[max(0, len(record) - sublength):] print "=4=>%s" % record # if necessary chop off something from the end print "comparing %s and %s\n" % ( self.basecount, self.subcoords[1]) if self.basecount > self.subcoords[1]: overhang = self.basecount - self.subcoords[ 1] sublength = max(0, len(record) - overhang) record = record[0:sublength] print "=5=>%s" % record self.parserState["PAST_SUBSEQUENCE"] = 1 if self.compression == None: if self.parserState["PAST_SUBSEQUENCE"] < 2: if linecount % 1000 == 1: print "=4=>writing %s" % record.strip() self.bufferedSeq["sequence"].write(record.strip()) if self.parserState["PAST_SUBSEQUENCE"] == 1: self.parserState["PAST_SUBSEQUENCE"] = 2 elif self.compression == 1: crecord = re.sub('AAA', '1', record.strip()) crecord = re.sub('AAC', '2', crecord) crecord = re.sub('AAG', '3', crecord) crecord = re.sub('AAT', '4', crecord) crecord = re.sub('ACA', '5', crecord) crecord = re.sub('ACC', '6', crecord) crecord = re.sub('ACG', '7', crecord) crecord = re.sub('ACT', '8', crecord) crecord = re.sub('AGA', '9', crecord) crecord = re.sub('NNNNNNNNNN', '0', crecord) if not self.parserState["PAST_SUBSEQUENCE"]: self.bufferedSeq["sequence"].write(crecord) else: raise brdfException( "unhandled compression type requested") self.parserState["BUFFER_OCCUPIED"] = 1 self.bufferedSeq["id"] = bufferedid record = self.infile.readline() self.linecount += 1 if record == "" or record == None: self.parserState["EOF"] = 1 return # we must have obtained the next id line or reached the end of the file. If the buffer is still not occupied , fill it self.parserState["ERROR"] = 0 self.parserState["PRE"] = 0 if self.parserState["EOF"] == 0: self.parserState["IDLINE"] = 1 self.bufferedid = record.strip() if self.subcoords[0] != None: self.bufferedid += " (extract from original sequence, from %s to %s)" % self.subcoords if self.parserState["BUFFER_OCCUPIED"] == 0: self.fillBuffer() return elif self.parserState["IDLINE"] == 1: # read sequence until get next id line or the end self.bufferedSeq["id"] = re.sub( "^\>", "", re.split('\s+', self.bufferedid)[0]) self.bufferedSeq["description"] = string.join( re.split('\s+', self.bufferedid)[1:], ' ') self.bufferedSeq["sequence"] = StringIO() self.basecount = 0 self.parserState["PAST_SUBSEQUENCE"] = 0 record = self.infile.readline() print "debug:%s" % str(record) self.linecount += 1 if record == "" or record == None: self.parserState["EOF"] = 1 return while re.search('^\s*\>', record) == None: #print "=6=>%s"%record if len(record.strip()) > 0: record = record.strip() #self.bufferedSeq["sequence"].write(record.strip()) self.basecount += len(record) if self.subcoords[0] != None: if not self.parserState["PAST_SUBSEQUENCE"]: if self.basecount >= self.subcoords[0]: # if necessary chop off something from the start of the record sublength = min( len(record), 1 + self.basecount - self.subcoords[0]) record = record[max(0, len(record) - sublength):] # if necessary chop off something from the end if self.basecount > self.subcoords[1]: overhang = self.basecount - self.subcoords[ 1] sublength = max(0, len(record) - overhang) record = record[0:sublength] self.parserState["PAST_SUBSEQUENCE"] = 1 if self.compression == None: if self.parserState["PAST_SUBSEQUENCE"] < 2: #if self.linecount%1000 == 1: # print "=6=>writing %s"%record.strip() self.bufferedSeq["sequence"].write(record.strip()) if self.parserState["PAST_SUBSEQUENCE"] == 1: self.parserState["PAST_SUBSEQUENCE"] = 2 elif self.compression == 1: crecord = re.sub('AAA', '1', record.strip()) crecord = re.sub('AAC', '2', crecord) crecord = re.sub('AAG', '3', crecord) crecord = re.sub('AAT', '4', crecord) crecord = re.sub('ACA', '5', crecord) crecord = re.sub('ACC', '6', crecord) crecord = re.sub('ACG', '7', crecord) crecord = re.sub('ACT', '8', crecord) crecord = re.sub('AGA', '9', crecord) crecord = re.sub('NNNNNNNNNN', '0', crecord) if not self.parserState["PAST_SUBSEQUENCE"]: self.bufferedSeq["sequence"].write(crecord) else: raise brdfException( "unhandled compression type requested") self.parserState["BUFFER_OCCUPIED"] = 1 self.bufferedSeq["id"] = self.bufferedid record = self.infile.readline() self.linecount += 1 if self.linecount % 1000 == 1: print "linecount = %s " % self.linecount if record == "" or record == None: self.parserState["EOF"] = 1 return # we must have obtained the next id line or reached the end of the file. self.parserState["ERROR"] = 0 self.parserState["PRE"] = 0 if self.parserState["EOF"] == 0: self.parserState["IDLINE"] = 1 self.bufferedid = record.strip() if self.subcoords[0] != None: self.bufferedid += " (extract from original sequence, from %s to %s)" % self.subcoords if self.parserState["BUFFER_OCCUPIED"] == 0: self.fillBuffer() return else: raise brdfException("unhandled parser state in fasta parser")