def get_enzyme_data(query):
    seq_fields = [
        'id', 'enzyme_type', 'enzyme_name', 'sequence', 'sequence_unavailable',
        'accession', 'pdb', 'mutant_of', 'notes', 'papers', 'owner',
        'other_names'
    ]
    enzyme_data = list(
        Sequence.objects(query).only(
            *seq_fields).order_by('enzyme_type').as_pymongo())
    owners_dict = {}
    for i, data in enumerate(enzyme_data):
        enzyme_data[i]['_id'] = str(enzyme_data[i]['_id'])
        enzyme_data[i]['sequence_unavailable'] = str(
            enzyme_data[i]['sequence_unavailable']).replace('False', '')

        if 'papers' not in enzyme_data[i]:
            enzyme_data[i]['papers'] = 0
        else:
            enzyme_data[i]['papers'] = len(enzyme_data[i]['papers'])

        if 'owner' in enzyme_data[i]:
            owner_id = str(enzyme_data[i]['owner'])
            if owner_id not in owners_dict:
                owner = User.objects(id=enzyme_data[i]['owner'])[0]
                owners_dict[owner_id] = f"{owner.first_name} {owner.last_name}"
            enzyme_data[i]['owner'] = owners_dict[owner_id]
        else:
            enzyme_data[i]['owner'] = ''

        if 'sequence' in enzyme_data[i]:
            if len(enzyme_data[i]['sequence']) > 50:
                enzyme_data[i][
                    'sequence'] = enzyme_data[i]['sequence'][0:50] + "..."

    return enzyme_data
def enzyme_teams():
    teams = []
    team_info = {}

    contributor_role = Role.objects(name='contributor')[0]
    contributors = User.objects(roles=contributor_role).select_related()

    for user in contributors:
        user_string = f"{user.first_name} {user.last_name}"
        affiliation_string = f"{user.affiliation}"
        for enzyme_type in user.enzyme_teams:
            if enzyme_type.enzyme_type not in teams:
                teams.append(enzyme_type.enzyme_type)
                team_info[enzyme_type.enzyme_type] = {'full_name': enzyme_type.full_name,
                                                      'enzyme_champions': [],
                                                      'team_members': [],
                                                      'progress': calculate_paper_progress.get_enzyme_paper_progress(enzyme_type)}

            if enzyme_type in user.enzyme_champion:
                team_info[enzyme_type.enzyme_type]['enzyme_champions'].append([user_string, affiliation_string])
            else:
                team_info[enzyme_type.enzyme_type]['team_members'].append([user_string, affiliation_string])

    teams.sort()

    return render_template('enzyme_teams.html', teams=teams, team_info=team_info)
Beispiel #3
0
    def inject_login_mode():
        inject_dict = {}
        inject_dict['login_mode'] = app.config['USE_EMAIL_CONFIRMATION']

        if current_user.is_authenticated:
            user = User.objects(id=current_user.id).select_related()[0]
            if user.has_role('enzyme_teams') and user.enzyme_teams is not None:
                inject_dict['enzyme_teams'] = [enz_type_obj.enzyme_type for enz_type_obj in user.enzyme_teams]
            if user.has_role('enzyme_champion') and user.enzyme_champion is not None:
                inject_dict['enzyme_champion'] = [enz_type_obj.enzyme_type for enz_type_obj in user.enzyme_champion]
            if user.has_role('contributor'):
                inject_dict['user_papers_need_data'] = len(Paper.objects(Q(owner=user) & (Q(status='Data required') | Q(status='Enzymes need protein sequences') | Q(status='Issues need to be resolved'))))
                inject_dict['user_seqs_need_data'] = len(Sequence.objects(Q(owner=user) & ((Q(sequence=None)|Q(sequence='')) & (Q(sequence_unavailable__ne=True)))))

            inject_dict['total_team_notifications'] = 0
            inject_dict['team_notifications'] = {}
            inject_dict['champ_seq_notifications'] = {}
            inject_dict['champ_notifications'] = {}

            if 'enzyme_teams' in inject_dict:
                for enz_type in inject_dict['enzyme_teams']:
                    num_papers = len(Paper.objects(Q(tags=enz_type) & Q(owner=None) & (Q(status='Data required') | Q(status='Enzymes need protein sequences'))))
                    inject_dict['team_notifications'][enz_type] = num_papers
                    inject_dict['total_team_notifications'] += num_papers
            if 'enzyme_champion' in inject_dict:
                for enz_type in inject_dict['enzyme_champion']:
                    num_papers = len(Paper.objects(Q(tags=enz_type) & Q(status='Complete - Awaiting review')))
                    num_seqs = len(Sequence.objects(Q(enzyme_type=enz_type) & ((Q(sequence=None)|Q(sequence='')) & (Q(sequence_unavailable__ne=True)))))
                    inject_dict['champ_notifications'][enz_type] = num_papers
                    inject_dict['champ_seq_notifications'][enz_type] = num_seqs
                    inject_dict['total_team_notifications'] += num_papers + num_seqs

        return inject_dict
Beispiel #4
0
def get_usernames(user):
    usernames = []
    if len(User.objects(
            last_name=user.last_name)) == 1 and len(user.last_name) > 2:
        usernames.append(str(user.last_name).lower())
    if len(User.objects(
            first_name=user.first_name)) == 1 and len(user.first_name) > 2:
        usernames.append(str(user.first_name).lower())

    usernames.append(f"{user.first_name} {user.last_name}".lower())
    usernames.append(
        f"{user.first_name} {user.last_name}, {user.affiliation}".lower())

    for name in usernames:
        if len(name) > 3:
            usernames.remove(name)

    return usernames
def check_paper_permission(user_id, paper):
    user = User.objects(id=user_id).select_related()[0]

    if user.has_role('super_contributor'):
        return True
    if paper.owner == user:
        return True
    if user.has_role('enzyme_champion'):
        champ_types = [e.enzyme_type for e in user.enzyme_champion]
        if any(i in champ_types for i in paper.tags):
            return True
    return False
def check_seq_permissions(user_id, seq):
    user = User.objects(id=user_id).select_related()[0]

    if user.has_role('super_contributor'):
        return True
    if seq.owner == user:
        return True
    if user.has_role('enzyme_champion'):
        champ_types = [e.enzyme_type for e in user.enzyme_champion]
        if seq.enzyme_type in champ_types:
            return True
    return False
def admin_set_owner():
    paper = Paper.objects(id=request.form['paper_id'])[0]
    new_owner_id = request.form['new_owner_id']
    print(new_owner_id)
    new_owner = User.objects(id=new_owner_id)[0]

    paper.owner = new_owner
    paper.save()

    result = {'status': 'success', 'msg': 'Paper owner updated', 'issues': []}

    return jsonify(result=result)
def get_admin_dict(paper):
    admin_dict = {}

    contributor_choices = [(f"{c.first_name} {c.last_name}", str(c.id)) for c in User.contributors()]
    admin_dict['contributors'] = [('Unassigned', '')] + contributor_choices

    if paper.owner != None:
        admin_dict['owner'] = str(paper.owner.id)
    else:
        admin_dict['owner'] = ''

    return admin_dict
Beispiel #9
0
def task_add_sequence_data(df):
    users = User.objects()

    for i, row in df.iterrows():
        seq_query = Sequence.objects(enzyme_name=row['enzyme_name'])
        if len(seq_query) != 0:
            seq = seq_query[0]
            if row['sequence'] != '' and row['sequence'] is not None:
                seq.sequence = str(row['sequence'])

            for user in users:
                usernames = get_usernames(user)
                if does_username_match(usernames, row['added_by']):
                    seq.added_by = user
                    seq.owner = user

            seq.save()
Beispiel #10
0
def process_papers_dict(papers_data, show_owner=True):
    owners_dict = {}
    for i, data in enumerate(papers_data):
        papers_data[i]['_id'] = str(papers_data[i]['_id'])

        if show_owner == False:
            if 'owner' in papers_data[i]:
                papers_data[i].pop('owner')

        else:
            if 'owner' in papers_data[i]:
                owner_id = str(papers_data[i]['owner'])
                if owner_id not in owners_dict:
                    owner = User.objects(id=papers_data[i]['owner'])[0]
                    owners_dict[
                        owner_id] = f"{owner.first_name} {owner.last_name}"
                papers_data[i]['owner'] = owners_dict[owner_id]
            else:
                papers_data[i]['owner'] = '-'

    return papers_data
Beispiel #11
0
def task_assign_papers():
    users = User.objects()
    papers = Paper.objects()

    for paper in papers:
        paper_status.update_status(paper)

    for user in users:
        usernames = get_usernames(user)
        for paper in papers:
            if paper.added_by is None or paper.added_by == '':
                activities = Activity.objects(paper=paper)
                for activity in activities:
                    if does_username_match(usernames,
                                           activity.added_by_string):
                        activity.added_by = user
                        activity.save()

                        if paper.added_by is None or paper.added_by == '':
                            paper.added_by = user
                            paper.owner = user
                            paper.save()
def leaderboard():
    contributor_role = Role.objects(name='contributor')[0]
    contributors = User.objects(roles=contributor_role)

    papers_dict = {}
    sequence_dict = {}
    activity_dict = {}
    for user in contributors:
        username = f"{user.first_name} {user.last_name}, {user.affiliation}"
        num_papers = len(Paper.objects(owner=user))
        num_sequences = len(Sequence.objects(owner=user))
        papers_dict[username] = num_papers
        sequence_dict[username] = num_sequences

    papers_dict = {k: v for k, v in sorted(papers_dict.items(), key=lambda item: item[1], reverse=True)}
    papers_dict = {k: v for k, v in papers_dict.items() if v != 0}
    sequence_dict = {k: v for k, v in sorted(sequence_dict.items(), key=lambda item: item[1], reverse=True)}
    sequence_dict = {k: v for k, v in sequence_dict.items() if v != 0}


    return render_template('leaderboard.html',
                           top_papers=papers_dict,
                           top_sequences=sequence_dict)
Beispiel #13
0
def make_added_by_user_dict():
    added_by_dict = {}
    users = User.objects()
    for user in users:
        added_by_dict[f"{user.first_name} {user.last_name}"] = user
    return added_by_dict
def create_paper():
    form = PaperInfo()

    if 'doi' in session:
        doi = session['doi']
    else:
        doi = ''

    if form.validate_on_submit() == False:
        user = User.objects(id=current_user.id)[0]
        papers = Paper.objects(doi__iexact=doi)
        if len(papers) != 0:

            paper = papers[0]
            if paper.owner == user or user.has_role('super_contributor'):
                flash("Paper already in the database", 'success')
                return redirect(
                    url_for("biocatdb.submission_main_page",
                            paper_id=paper.id))

            elif paper.owner == None:
                flash(
                    'Paper already in database, self-assign the paper to add data',
                    'warning')
                return redirect(url_for("biocatdb.papers_that_need_data"))

            elif paper.owner != user and not user.has_role(
                    'super_contributor'):
                flash(
                    "Paper already in the database and is assigned to another user",
                    'danger')

            else:
                flash("error", 'danger')
                return redirect(url_for("biocatdb.launch_add_paper"))

        else:
            title, authors_list, journal, date, cite_mini = papers_crossref.get_metadata_from_crossref(
                doi)
            if cite_mini == '':
                title, authors_list, journal, date, cite_mini = papers_functions.query_pubmed(
                    doi)
            form.title.data = title
            form.authors.data = papers_functions.list_to_string(authors_list)
            form.journal.data = journal
            form.date.data = date
            form.short_cit.data = cite_mini
            form.doi.data = doi.replace(' ', '').lower()

            can_self_assign = papers_functions.can_self_assign(user)

            if cite_mini == '':
                flash(
                    "Paper not found in crossref, pubmed or db, please add manually",
                    'fail')
            else:
                flash("Paper found, please check information", 'success')

            return render_template(
                'add_paper_workflow/edit_paper_information.html',
                form=form,
                can_self_assign=can_self_assign)

    else:
        user = User.objects(id=current_user.id)[0]
        session.pop('doi')
        if form.self_assign.data == True:
            owner = user
        else:
            owner = None

        new_paper = Paper(doi=form.doi.data.replace(' ', '').lower(),
                          short_citation=form.short_cit.data,
                          title=form.title.data,
                          html='https://doi.org/' + form.doi.data,
                          journal=form.journal.data,
                          date=form.date.data,
                          tags=form.tags.data.split(', '),
                          authors=form.authors.data.split(', '),
                          owner=owner,
                          added_by=user,
                          status='Data required')
        new_paper.save()
        flash("Paper saved", 'success')

        if owner == user:
            return redirect(
                url_for("biocatdb.submission_main_page",
                        paper_id=new_paper.id))

        else:
            return redirect(url_for("biocatdb.launch_add_paper"))