def get_compound_infos(compound_id): baseUrl = 'http://rest.kegg.jp/get/' + compound_id req = urllib2.Request(baseUrl) response = urllib2.urlopen(req) infolines = response.read().split('\n') current_info_type = '' compound_info = dict() for line in infolines: infos = line.split(' ') if not line.startswith(' ') and not line.startswith('/'): current_info_type = infos[0] infos = infos[1:] for info_item in infos: if info_item.endswith('\n'): info_item = info_item[:-1] if info_item == 'Compound' and current_info_type == 'ENTRY': continue try: compound_info[current_info_type] += info_item except: compound_info[current_info_type] = info_item try: names = compound_info['NAME'].split(';') compound_info['NAME'] = names[0] compound_info['NICKNAME'] = '' for i in range(1, len(names)): compound_info['NICKNAME'] += names[i] + '_' new_compound = compound(compound_id=compound_info['ENTRY']) new_compound.name = compound_info['NAME'] new_compound.nicknames = compound_info['NICKNAME'] new_compound.formula = compound_info['FORMULA'] new_compound.exact_mass = compound_info['EXACT_MASS'] new_compound.mol_mass = compound_info['MOL_WEIGHT'] try: new_compound.save() return new_compound except: traceback.print_exc() print '%s can not be saved' % compound_info['NAME'] return None except: pass return None
def saveCompoundDataToDB(compound_file): current_info_type = '' compound_info = dict() for line in compound_file.readlines(): if line.endswith('\n'): line = line[:-1] infos = line.split(' ') if not line.startswith(' ') and not line.startswith('/'): current_info_type = infos[0] infos = infos[1:] for info_item in infos: if info_item.endswith('\n'): info_item = info_item[:-1] if info_item == 'Compound' and current_info_type == 'ENTRY': continue try: compound_info[current_info_type] += info_item except: compound_info[current_info_type] = info_item #gene nick names try: names = compound_info['NAME'].split(';') compound_info['NAME'] = names[0] compound_info['NICKNAME'] = '' for i in range(1, len(names)): compound_info['NICKNAME'] += names[i] + '_' new_compound = compound(compound_id=compound_info['ENTRY']) new_compound.name = compound_info['NAME'] new_compound.nicknames = compound_info['NICKNAME'] new_compound.formula = compound_info['FORMULA'] new_compound.exact_mass = compound_info['EXACT_MASS'] new_compound.mol_mass = compound_info['MOL_WEIGHT'] try: new_compound.save() except: traceback.print_exc() print '%s can not be saved' % compound_info['NAME'] except: pass