def test_complete_studies(self): ec = Client() id = 28795402 ncts = [ 'NCT00031265', 'NCT02199847', 'NCT00902980', 'NCT01266824', 'NCT03418909' ] article = ec.efetch(db='pubmed', id=id) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') for n in ncts[:3]: crud.review_trial(id, n, False, 'included', 'testuser_1', 1) for n in ncts[3:]: crud.review_trial(id, n, False, 'relevant', 'testuser_1', 1) crud.complete_studies(id, True) metadata = crud.review_medtadata_db(id) self.assertEqual(metadata['included_complete'], True) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] in ncts[:3]: self.assertEqual(trials[i]['verified'], True) self.assertEqual(trials[i]['relationship'], 'included') if t['nct_id'] in ncts[3:]: self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'relevant') crud.complete_studies(id, False) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] in ncts[:3]: self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'included')
def refresh_trials(json): """ reload & render HTML for trials @param json: JSON object specifying review_id, which 'type' of trials to refresh, and whether to generate a new plot """ id = json['review_id'] type = json['type'] plot_bool = json['plot'] locked = crud.review_lock_status(id) sort = json['sort'] if 'sort' in json else 'total_votes' trials = crud.get_review_trials_fast( id, order=sort, usr=current_user if current_user.is_authenticated else None) relevant = [ trial['nct_id'] for trial in trials['reg_trials'] if trial['relationship'] == 'relevant' ] verified = [ trial['nct_id'] for trial in trials['reg_trials'] if trial['relationship'] == 'included' ] if (type == 'rel' and relevant) or (type == 'incl' and verified): emit('page_content', { 'section': 'rel_trials' if type == 'rel' else 'incl_trials', 'sort': sort, 'data': render_template( 'rel_trials.html' if type == 'rel' else 'incl_trials.html', reg_trials=trials['reg_trials'], locked=locked) }, room=request.sid) if plot_bool: formatted = utils.trials_to_plotdata(trials['reg_trials']) socketio.emit('page_content', { 'section': 'plot', 'data': formatted, 'page': 'reviewdetail', 'review_id': id }, room=request.sid) elif type == 'incl' and not verified['ids']: emit('page_content', { 'section': 'incl_trials', 'sort': sort, 'data': render_template('incl_trials.html', reg_trials=[], locked=False) }, room=request.sid)
def test_get_review_trials_fast(self): ec = Client() id = 28795402 ncts = [ 'NCT00031265', 'NCT02199847', 'NCT00902980', 'NCT01266824', 'NCT03418909' ] article = ec.efetch(db='pubmed', id=id) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') for n in ncts: crud.review_trial(id, n, False, 'included', 'testuser_1', 1) trials = crud.get_review_trials_fast(id)['reg_trials'] retrieved_ncts = [t['nct_id'] for t in trials] for n in ncts: self.assertTrue(n in retrieved_ncts)
def get_plot(): """ generate new random TSNE plot for homepage """ ids = crud.get_locked() test_id = random.choice(ids) review_data = crud.review_medtadata_db(test_id) trials = crud.get_review_trials_fast( test_id, usr=current_user if current_user.is_authenticated else None) return json.dumps({ 'success': True, 'data': { 'section': 'plot', 'data': plot.get_tsne_data(trials['reg_trials']), 'page': 'home', 'review_id': test_id, 'title': review_data['title'] } }), 200, { 'ContentType': 'application/json' }
def test_vote(self): ec = Client() id = 28934560 nct_id = 'NCT00678431' article = ec.efetch(db='pubmed', id=id) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') crud.review_trial(id, nct_id, False, 'relevant', 'testuser_2', 2) link_id = crud.get_link_id(nct_id, id) crud.vote(link_id, 'up', 1) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_id: self.assertEqual(trials[i]['nct_id'], nct_id) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_2', 'testuser_1'}) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'relevant')
def search(json): """ conduct a search @param json: JSON object specifying serch keywords """ id = json['review_id'] emit('search_update', {'msg': 'Searching...'}, room=request.sid) eventlet.sleep(0) if not id: emit('page_content', { 'section': 'no_results', 'data': render_template('noresults.html', id=id) }, room=request.sid) return conn = dblib.create_con(VERBOSE=True) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) # try to retrieve review with matching PMID if id is int review = '' found = True if (utils.RepresentsInt(id)): review = crud.review_medtadata_db(id) # try to retrieve review with matching DOI if id is DOI elif utils.is_doi(id): cur.execute("SELECT * FROM systematic_reviews WHERE doi = %s;", (id, )) review = cur.fetchone() conn.close() # if not int or DOI, return no results page else: conn.close() emit('search_update', {'msg': 'Searching for keyword matches in our database'}, room=request.sid) search_result = request_data.advanced_search(id) if not search_result: emit('page_content', { 'section': 'no_results', 'data': render_template('noresults.html', id=id) }, room=request.sid) return emit('page_content', { 'section': 'search_results', 'data': render_template( 'searchresult.html', reviews=search_result, searchterm=id) }, room=request.sid) return # if there is no match in our DB if review is None: found = False if not current_user.is_authenticated: conn.close() emit('page_content', { 'section': 'no_results', 'data': render_template('noresults.html', id=id) }, room=request.sid) return emit('search_update', { 'msg': 'Not found in local database. Searching PubMed for article' }, room=request.sid) eventlet.sleep(0) if utils.is_doi(id): # try to retrieve PMID if DOI convert = crud.convert_id(id, 'pmid') if convert: id = convert # return no result if no results else: emit('search_update', {'msg': 'Not found in Pubmed :('}, room=request.sid) emit('page_content', { 'section': 'no_results', 'data': render_template('noresults.html', id=id) }, room=request.sid) return # try to retrieve the review from pubmed ec = Client(api_key=eutils_key) article = ec.efetch(db='pubmed', id=id) found_review = None for art in article: if art and str(art.pmid) == id: found_review = art break if found_review: result = found_review.pmid if not result: flash( 'Unable to retrieve metadata for this article. Please try again later' ) abort(404) emit('search_update', {'msg': 'Found article on PubMed. Downloading metadata...'}, room=request.sid) eventlet.sleep(0) crud.pubmedarticle_to_db(found_review, 'systematic_reviews') review = crud.review_medtadata_db(id) emit('page_content', { 'data': render_template('review_data.html', review=review), 'section': 'review_data' }, room=request.sid) eventlet.sleep(0) emit('search_update', {'msg': 'Saved metadata... triggering bots'}, room=request.sid) bot.docsim.delay(id, sess_id=request.sid) eventlet.sleep(0) if 'cochrane' in review['source'].lower() and 'doi' in review: cb_bb = bot.cochrane_ongoing_excluded.si(review['doi'], id, sess_id=request.sid) cb_bb.link(bot.basicbot2.si(review_id=id, sess_id=request.sid)) chord( (bot.cochranebot.s(review['doi'], id, sess_id=request.sid), bot.check_citations.s(id, sess_id=request.sid)), cb_bb).delay() else: chord((bot.check_citations.s(id, sess_id=request.sid)), bot.basicbot2.si(review_id=id, sess_id=request.sid)).delay() else: print 'no result' emit('page_content', { 'section': 'no_results', 'data': render_template('noresults.html', id=id) }, room=request.sid) return # if there IS a match in our DB if found: print 'emitting found review' eventlet.sleep(0) emit('search_update', {'msg': 'Found review in our database! Retrieving data..'}, room=request.sid) eventlet.sleep(0) print 'emitting review content' emit('page_content', { 'data': render_template('review_data.html', review=review, starred=crud.is_starred(review['review_id'], current_user.db_id) if current_user.is_authenticated else False), 'section': 'review_data', 'related_reviews': render_template('related_reviews.html', related_reviews=crud.related_reviews( review['review_id'])) }, room=request.sid) eventlet.sleep(0) trials = crud.get_review_trials_fast( review[0], usr=current_user if current_user.is_authenticated else None) relevant = [ trial['nct_id'] for trial in trials['reg_trials'] if trial['relationship'] == 'relevant' ] verified = [ trial['nct_id'] for trial in trials['reg_trials'] if trial['relationship'] == 'included' ] emit('search_update', {'msg': 'Generating cool plots...'}, room=request.sid) eventlet.sleep(0) formatted = utils.trials_to_plotdata(trials['reg_trials']) socketio.emit('page_content', { 'section': 'plot', 'data': formatted, 'page': 'reviewdetail', 'review_id': review[0] }, room=request.sid) emit('page_content', { 'section': 'rel_trials', 'data': render_template('rel_trials.html', reg_trials=trials['reg_trials'], locked=review['included_complete']) }, room=request.sid) eventlet.sleep(0) if verified: emit('page_content', { 'section': 'incl_trials', 'data': render_template('incl_trials.html', reg_trials=trials['reg_trials'], locked=review['included_complete']) }, room=request.sid) eventlet.sleep(0) else: emit('page_content', { 'section': 'incl_trials', 'data': render_template( 'incl_trials.html', reg_trials=[], locked=False) }, room=request.sid)
def test_review_trial(self): ec = Client() id = 28616955 nct_ids = ['NCT00195624', 'NCT00200889', 'NCT00207688'] article = ec.efetch(db='pubmed', id=id) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') self.assertEqual(len(crud.get_review_trials_fast(id)['reg_trials']), 0) # trial is inserted with correct values crud.review_trial(id, nct_ids[0], False, 'relevant', 'testuser_1', 1) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[0]: self.assertEqual(trials[i]['nct_id'], nct_ids[0]) self.assertEqual(trials[i]['upvotes'], 1) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['voters'], 'testuser_1') self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'relevant') # when the trial is added again by another user, it should recieve an upvote crud.review_trial(id, nct_ids[0], False, 'relevant', 'testuser_2', 2) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[0]: self.assertEqual(trials[i]['nct_id'], nct_ids[0]) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'}) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'relevant') # adding an existing trial from the relevant column as included will move it crud.review_trial(id, nct_ids[0], False, 'included', 'testuser_2', 2) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[0]: self.assertEqual(trials[i]['nct_id'], nct_ids[0]) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'}) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'included') # test included trial crud.review_trial(id, nct_ids[1], False, 'included', 'testuser_2', 2) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[1]: self.assertEqual(trials[i]['nct_id'], nct_ids[1]) self.assertEqual(trials[i]['upvotes'], 1) self.assertEqual(trials[i]['voters'], 'testuser_2') self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'included') # trying to insert a relevant trial when it's already included will give a vote but not move the trial crud.review_trial(id, nct_ids[1], False, 'relevant', 'testuser_1', 1) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[1]: self.assertEqual(trials[i]['nct_id'], nct_ids[1]) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'}) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'included') # except for user_id 17 which can move included to relevant crud.review_trial(id, nct_ids[1], False, 'relevant', 'cochranebot', 17, vote_type='down') trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[1]: self.assertEqual(trials[i]['nct_id'], nct_ids[1]) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'cochranebot', 'testuser_1', 'testuser_2'}) self.assertEqual(trials[i]['downvotes'], 1) self.assertEqual(trials[i]['verified'], False) self.assertEqual(trials[i]['relationship'], 'relevant') # if the review is locked and the trial is included, allow a vote crud.review_trial(id, nct_ids[2], False, 'included', 'testuser_1', 1) crud.complete_studies(id, True) crud.review_trial(id, nct_ids[2], False, 'included', 'testuser_2', 2) trials = crud.get_review_trials_fast(id)['reg_trials'] for i, t in enumerate(trials): if t['nct_id'] == nct_ids[2]: self.assertEqual(trials[i]['nct_id'], nct_ids[2]) self.assertEqual(trials[i]['upvotes'], 2) self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'}) self.assertEqual(trials[i]['downvotes'], 0) self.assertEqual(trials[i]['verified'], True) self.assertEqual(trials[i]['relationship'], 'included')