def test_convert_id(self): ec = Client() id = 28795402 article = ec.efetch(db='pubmed', id=id) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') self.assertEqual(crud.convert_id(id, 'doi'), '10.1002/ijc.30922') self.assertEqual(crud.convert_id('10.1002/ijc.30922', 'pmid'), id) article = ec.efetch(db='pubmed', id=24829965) for i, a in enumerate(article): crud.pubmedarticle_to_db(a, 'systematic_reviews') self.assertEqual(crud.convert_id(24829965, 'doi'), None)
def vote(): """ vote for a specified trial @return: success message & updated list of voters for specified trial """ if not current_user.is_authenticated: return "Sorry! This action is only available to logged-in users", 400, { 'ContentType': 'application/json' } current_userid = current_user.db_id data = request.json if utils.is_doi(data['review']): # try to retrieve PMID if potenially DOI data['review'] = crud.convert_id(data['review'], 'pmid') conn = dblib.create_con(VERBOSE=True) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) # check if user has already voted for this trial link_id = crud.get_link_id(data['id'], data['review']) cur.execute("SELECT * FROM votes WHERE link_id = %s AND user_id = %s;", (link_id, current_userid)) current = cur.fetchone() if data['up'] and (not current or (current and current['vote_type'] == 'down')): crud.vote(link_id, 'up', current_userid) if data['down'] and (not current or (current and current['vote_type'] == 'up')): crud.vote(link_id, 'down', current_userid) if current and (data['up'] is False and data['down'] is False) or ( data['up'] is False and data['down'] == 0) or (data['down'] is False and data['up'] == 0): cur.execute("DELETE FROM votes WHERE vote_id = %s;", (current['vote_id'], )) conn.commit() conn.close() return json.dumps({ 'success': True, 'voters': update_voters(data['review'], data['id']) }), 200, { 'ContentType': 'application/json' }
def rel_incl(): """ Move the trial specified in the JSON data from 'relevant' to 'included' @return: success/failure message """ if not current_user.is_authenticated: return "Sorry! This action is only available to logged-in users", 400, { 'ContentType': 'application/json' } data = request.json if utils.is_doi(data['review']): # try to retrieve PMID if potenially DOI data['review'] = crud.convert_id(data['review'], 'pmid') link_id = crud.get_link_id(data['nct_id'], data['review']) crud.change_relationship(link_id, 'included') return json.dumps({ 'success': True, 'message': 'Trial moved successfully' }), 200, { 'ContentType': 'application/json' }
def submit_trial(): """ submit a relevant or included trial for the current article @return: success/failure message """ if not current_user.is_authenticated: return "Sorry! This action is only available to logged-in users", 400, { 'ContentType': 'application/json' } data = request.json conn = dblib.create_con(VERBOSE=True) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) if data['nct_id'] and data['review']: # check if the trial exists already cur.execute("SELECT * FROM tregistry_entries WHERE nct_id =%s;", (data['nct_id'], )) trial_reg_data = cur.fetchone() # if not exists conn.close() if not trial_reg_data: missing = crud.add_missing_trial(data['nct_id']) if not missing: conn.close() return json.dumps({ 'success': False, 'message': 'Unable to retrieve trial' }), 200, { 'ContentType': 'application/json' } if utils.is_doi(data['review']): data['review'] = crud.convert_id(data['review'], 'review_id') result = crud.check_existing_review_trial(data['review'], data['nct_id']) if not result: crud.review_trial(data['review'], data['nct_id'], False, data['relationship'], current_user.nickname, current_user.db_id) return json.dumps({ 'success': True, 'message': 'Added trial successfully', 'data': str(data) }), 200, { 'ContentType': 'application/json' } elif data['relationship'] == result['relationship']: return json.dumps({ 'success': False, 'message': 'Trial already exists', 'move': False }), 200, { 'ContentType': 'application/json' } elif data['relationship'] == 'relevant' and result[ 'relationship'] == 'included': return json.dumps({ 'success': False, 'message': 'Trial is already listed as included', 'move': True }), 200, { 'ContentType': 'application/json' } else: return json.dumps({ 'success': False, 'message': 'Trial already in list of relevant trials', 'move': True }), 200, { 'ContentType': 'application/json' }
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)