def test_GO_relation_5(): bp = GO.GO_category({ 'id': ['GO:0008150'], 'name': ['biological_process'], 'def': [''] }) mp = GO.GO_category({ 'id': ['GO:0008152'], 'name': ['metabolic process'], 'def': [''] }) cp = GO.GO_category({ 'id': ['GO:0009987'], 'name': ['cellular process'], 'def': [''] }) m = GO.GO_category({ 'id': ['GO:0032259'], 'name': ['methylation'], 'def': [''] }) is_a = GO.GO_relation({ 'id': ['is_a'], 'name': ['is_a'], 'is_transitive': [True] }) is_a.add_pair(mp, bp) is_a.add_pair(cp, bp) is_a.add_pair(m, mp) is_a.add_pair(bp, m) # Introduce a cycle for testing. is_a_copy = is_a.copy() assert is_a == is_a_copy
def run(self): self.connect() self.login() time.sleep(1) line = '' while not self._terminate: for char in self._sock.recv(1024): if char not in '\r\n': line += char continue if char == '\r': continue if char == '\n': if self._irc_debug: print '[-s2c-] %s'%line prefix = '' command = '' arguments = '' if line[0] == ':': line = line[1:] i = line.find(' ') prefix = GO.unicod(line[0:i]) line = line[i+1:] i = line.find(' ') command = 'on_%s'%line[0:i] arguments = GO.unicod(line[i+1:]) if command in dir(self): Thread(target=getattr(self, command), args=(prefix, arguments)).start() line = ''
def test_GO_relation_1(): relation = GO.GO_relation({'id': ['rel'], 'name': ['relation']}) assert relation.id == 'rel' assert relation.name == 'relation' assert relation.is_transitive == False assert relation.others == {} assert str(relation) == '<rel>'
def test_GO_relation_4(): bp = GO.GO_category({ 'id': ['GO:0008150'], 'name': ['biological_process'], 'def': [''] }) mp = GO.GO_category({ 'id': ['GO:0008152'], 'name': ['metabolic process'], 'def': [''] }) cp = GO.GO_category({ 'id': ['GO:0009987'], 'name': ['cellular process'], 'def': [''] }) m = GO.GO_category({ 'id': ['GO:0032259'], 'name': ['methylation'], 'def': [''] }) is_a = GO.GO_relation({ 'id': ['is_a'], 'name': ['is_a'], 'is_transitive': [True] }) is_a.add_pair(mp, bp) is_a.add_pair(cp, bp) is_a.add_pair(m, mp) is_a.add_pair(m, bp) # Because the relation is transitive. assert (cp, bp) in is_a assert (m, bp) in is_a assert (m, mp) in is_a assert (mp, bp) in is_a assert not (bp, cp) in is_a assert not (bp, m) in is_a assert not (bp, mp) in is_a assert not (cp, m) in is_a assert not (cp, mp) in is_a assert not (m, cp) in is_a assert not (mp, cp) in is_a assert not (mp, m) in is_a assert is_a[bp] == set() assert is_a[cp] == {bp} assert is_a[m] == {bp, mp} assert is_a[mp] == {bp} assert sorted(is_a) == [(mp, bp), (cp, bp), (m, bp), (m, mp)]
def putcmd(self, cmd): if self._connected: cmd = GO.utf8(cmd) self._mutex.acquire() if self._irc_debug: print '[-c2s-] %s'%cmd self._sock.sendall(cmd) self._sock.sendall('\r\n') self._mutex.release()
def test_GO_category_1(): category = GO.GO_category({ 'id': ['42'], 'name': ['fortytwo'], 'def': ['answer'] }) assert category.id == '42' assert category.name == 'fortytwo' assert category.defination == 'answer' assert category.others == {} assert str(category) == '42 (fortytwo)'
def test_GO_1(): go = GO.GO('go.obo') assert 'is_a' in go.relations assert 'ends_during' in go.relations assert 'happens_during' in go.relations assert 'has_part' in go.relations assert 'negatively_regulates' in go.relations assert 'never_in_taxon' in go.relations assert 'occurs_in' in go.relations assert 'part_of' in go.relations assert 'positively_regulates' in go.relations assert 'regulates' in go.relations assert 'starts_during' in go.relations
def seekGOTerms(keywords, output_file = None): ''' keywords: A list of words to search for. ''' ret = {} # Key: GO ID, Value: Term for gid in go.go_terms.keys(): gx = GO.GOTerm() gx = go.getGOTermOf(gid) desc = gx.getGOName() for k in keywords: k = k.upper() if desc.upper().find(k) >= 0: ret[gid] = desc print gid, desc break f=None if output_file is not None: f=open(output_file, 'w') for gid in ret.keys(): s = '\t'+gid+'\t# '+ret[gid] if f is not None: f.write(s+'\n') else: print s if f is not None: f.close()
#keywords = ['transcription', 'translation', 'transcribed' ] #keywords = ['cell cycle', 'division' ] #keywords = [ 'cytoskeleton' ] #keywords = [ 'metabolism' ] #keywords = ['Protein folding', 'chaperone', 'heat shock' ] #keywords = ['proteolysis', 'proteasom', 'ubiquitin' ] #keywords = [ 'splicing', 'spliceosom' ] #keywords = [ 'transport', 'localiz' ] #keywords = [ 'oxidoreduct' ] #output_file = 'r:/tmp.txt' if len(sys.argv) == 4: global go go_file = sys.argv[1] keyword = sys.argv[2].replace('"', '') keywords = [keyword] output_file = sys.argv[3] go = GO.GO(obo_file = go_file) seekGOTerms(keywords, output_file) print 'DONE' else: displayHelp()
def history(self, cmd): (ok, date) = self._check_for_date(''.join(cmd.arguments())) if ok: exists = False for chan_message in GO.storage.get_channel_log(cmd.channel().name(), date): cmd.reply(u'%s [%s] %s: %s'%(cmd.channel().name(), chan_message[0].strftime('%H:%M:%S'), GO.unicod(chan_message[1]), GO.unicod(chan_message[2]))) exists = True if not exists: cmd.reply(u'За данную дату сообщения на канале %s отсутствуют'%cmd.channel().name()) else: cmd.reply_error(date)
def wiki(self, cmd): (ok, kw) = self._check_for_string('%20'.join(cmd.arguments())) if ok: downloaded = urllib2.urlopen('http://ru.wikipedia.org/w/api.php?format=xml&action=opensearch&search=%s'%GO.utf8(kw)) dom = parseString(downloaded.read()) downloaded.close() root = dom.getElementsByTagName('SearchSuggestion')[0].getElementsByTagName('Section')[0] cnt = root.getElementsByTagName('Item').length if cnt == 0: cmd.reply(u'Нет такого в википедии') elif cnt > 1: for a in root.getElementsByTagName('Item'): if a.getElementsByTagName('Text')[0].firstChild.data.strip() == u' '.join(cmd.arguments()): cmd.reply(u'%s (%s)'%(a.getElementsByTagName('Description')[0].firstChild.data.strip(), a.getElementsByTagName('Url')[0].firstChild.data.strip())) cmd.reply(u'Варианты: %s'%u', '.join([a.getElementsByTagName('Text')[0].firstChild.data.strip() for a in root.getElementsByTagName('Item')])) elif cnt == 1: i = root.getElementsByTagName('Item')[0] cmd.reply(u'%s (%s)'%(i.getElementsByTagName('Description')[0].firstChild.data.strip(), i.getElementsByTagName('Url')[0].firstChild.data.strip())) else: cmd.reply_error(kw)
def sync_forums_data(self): #self.download_part_of_all() GO.bot.send_channel_log(u'RSDN. Синхронизация. Запуск.') msgcount = { True: 0, False: 0 } mdrcount = { True: 0, False: 0 } ratcount = { True: 0, False: 0 } sync_iteration = 0 while True: msgcnt = 0 ratcnt = 0 mdrcnt = 0 sync_iteration += 1 GO.bot.send_channel_log(u'RSDN. Синхронизация. Итерация: %d.'%sync_iteration) GO.bot.send_channel_log(u'RSDN. Синхронизация. Будет загружено отсутствующих сообщений: %d'%(self.max_broken_per_sync_iteration if len(self._missed_rsdn_messages_mids) > self.max_broken_per_sync_iteration else len(self._missed_rsdn_messages_mids))) newData = self.sync_new_data() GO.bot.send_channel_log(u'RSDN. Синхронизация. Сообщения загружены. В итерацию не попало %d отсутствующих сообщений'%len(self._missed_rsdn_messages_mids)) if newData == None: break if len(newData['newMessages']): GO.bot.send_channel_log(u'RSDN. Синхронизация. Обработка сообщений.') for message in newData['newMessages'][0]: msgcount[GO.storage.update_rsdn_messages(message)] += 1 fid = message['forumId'] if fid and self.date_is_today(message['messageDate']): forum_name = self.forums[fid]['sname'] text = u'`%s`. Автор: %s'%( message['subject'], message['userNick'], ) urls = u' | '.join([ u'#%s'%forum_name, self.get_forum_url_by_id(fid), self.get_message_url_by_id(message['messageId']), self.get_member_url_by_id(message['userId']) ]) GO.bot.send_rsdn_notification(u'В форуме `%s` новое сообщение: %s'%( self.forums[fid]['name'], text )) GO.bot.send_rsdn_notification(urls) if message['parentId'] == 0: GO.bot.send_channel_notification(forum_name, u'Новый топик %s'%text) GO.bot.send_channel_notification(forum_name, urls) else: parent_msg = GO.storage.get_rsdn_message(message['parentId']) if parent_msg != None: GO.bot.send_user_notification(parent_msg[2], u'В форуме `%s` ответ на сообщение `%s`: %s'%( self.forums[fid]['name'], GO.unicod(parent_msg[4]), text )) GO.bot.send_user_notification(parent_msg[2], urls) #self.mineMissedInMessages(newData['newMessages'][0]) if len(newData['newRating']): GO.bot.send_channel_log(u'RSDN. Синхронизация. Обработка рейтинга.') for rating in newData['newRating'][0]: ratcount[GO.storage.update_rsdn_rating(rating)] += 1 if self.date_is_today(rating['rateDate']): target_msg = GO.storage.get_rsdn_message(rating['messageId']) if target_msg == None: self.get_rsdn_topic(rating['messageId']) target_msg = GO.storage.get_rsdn_message(rating['messageId']) from_user = GO.storage.get_rsdn_member(rating['userId']) if from_user == None: self.get_rsdn_member(rating['userId']) from_user = GO.storage.get_rsdn_member(rating['userId']) rate = rating['rate'] r = '' if rate > 0: r = u'%d'%(rating['userRating']*rate) elif rate == 0: r = u'-1' elif rate == -2: r = u':)' elif rate == -4: r = u'+1' text = u'Оценка %s сообщению `%s` от пользователя %s'%( r, GO.unicod(target_msg[4]) if target_msg != None else u'--нет-в-бд--', GO.unicod(from_user[1]) if target_msg != None else u'--нет-в-бд--' ) urls = u' | '.join([ self.get_message_url_by_id(rating['messageId']), self.get_member_url_by_id(rating['userId']) ]) GO.bot.send_rsdn_notification(text) GO.bot.send_rsdn_notification(urls) if target_msg != None: GO.bot.send_user_notification(target_msg[2], u'В форуме `%s` ответ на сообщение `%s`: %s'%( self.forums[fid]['name'], GO.unicod(target_msg[4]), text )) GO.bot.send_user_notification(target_msg[2], urls) #self.mineMissedInMessages(newData['newRating'][0]) if len(newData['newModerate']): GO.bot.send_channel_log(u'RSDN. Синхронизация. Обработка модерирования.') for moderate in newData['newModerate'][0]: mdrcount[GO.storage.update_rsdn_moderate(moderate)] += 1 #self.mineMissedInMessages(newData['newModerate'][0]) GO.bot.send_channel_log(u'RSDN. Синхронизация. Получение списка отсутствующих сообщений.') for mid in GO.storage.get_broken_messages(list(self.forums.keys())): if mid not in self._missed_rsdn_messages_mids: self._missed_rsdn_messages_mids.append(mid) #print len(newData['newRating']), len(newData['newModerate']), len(newData['newMessages']), len(self._missed_rsdn_messages_mids) if len(newData['newRating']) == 0 and len(newData['newModerate']) == 0 and len(newData['newMessages']) == 0 and len(self._missed_rsdn_messages_mids) < self.min_broken_per_sync_iteration: break GO.bot.send_channel_log(u'RSDN. Синхронизация. Собщения: принято %d, из них новых %d, обновлено %d.'%(msgcount[True]+msgcount[False], msgcount[False], msgcount[True])) GO.bot.send_channel_log(u'RSDN. Синхронизация. Оценки: принято %d, из них новых %d, обновлено %d.'%(ratcount[True]+ratcount[False], ratcount[False], ratcount[True])) GO.bot.send_channel_log(u'RSDN. Синхронизация. Модерирование: принято %d, из них новых %d, обновлено %d.'%(mdrcount[True]+mdrcount[False], mdrcount[False], mdrcount[True])) newUsers = self.sync_new_users() if newUsers != None: usrcount = { True: 0, False: 0 } for newUsersBit in newUsers: for user in newUsersBit['users'][0]: usrcount[GO.storage.update_rsdn_members(user)] += 1 GO.bot.send_rsdn_notification(u'Новый пользователь `%s`, `%s`, `%s`: %s'%( user['userName'], user['userNick'], user['realName'], self.get_member_url_by_id(user['userId']) )) GO.bot.send_channel_log(u'RSDN. Синхронизация. Новые пользователи: принято %d, из них новых %d, обновлено %d.'%(usrcount[True]+usrcount[False], usrcount[False], usrcount[True])) if len(self._missed_rsdn_members_uids): users = self.load_members_by_ids(self._missed_rsdn_members_uids) self._missed_rsdn_members_uids = [] if users: usrcount = { True: 0, False: 0 } for user in users['users'][0]: usrcount[GO.storage.update_rsdn_members(user)] += 1 GO.bot.send_channel_log(u'RSDN. Синхронизация. Пропущенные пользователи: принято %d, из них новых %d, обновлено %d.'%(usrcount[True]+usrcount[False], usrcount[False], usrcount[True])) GO.bot.send_channel_log(u'RSDN. Синхронизация. Закончено.')
def test__pop_single_value_2(): with pytest.raises(KeyError): GO._pop_single_value('42', {})
def test__pop_single_value_1(): attributes = {'42': ['answer'], 'foo': ['bar']} answer = GO._pop_single_value('42', attributes) assert answer == 'answer' assert attributes == {'foo': ['bar']}
def test_GO_2(): go = GO.GO('go.obo') assert len((go.relations['is_a'], [go.categories['GO:0000022']])) == 2
def test__pop_single_value_5(): with pytest.raises(TypeError): GO._pop_single_value('42', None)
def test__pop_single_value_4(): with pytest.raises(ValueError): GO._pop_single_value('42', {'42': []})