Exemplo n.º 1
0
def details():
    pagelimit = current_user.ngs_per_page
    page = request.args.get('page', 1, type=int)
    table = request.args.get('table', '', str)
    id = request.args.get('id')
    target = models_table_name_dictionary.get(table, None)
    if table == 'analysis':
        return redirect(url_for('ngs.analysis', id=id))
    if not target: abort(404)
    try:
        if table == 'sequence_round':
            id = id[1:-1].split(',')
            id = tuple(int(i) for i in id)
        else:
            id = int(id)
    except:
        abort(404)

    entry = target.query.get(id)
    if not entry: abort(404)

    if table == 'selection':
        query_result = Rounds.query.filter_by(selection_id=id).order_by(
            Rounds.id.desc()).paginate(page, pagelimit, False)

    elif table == 'round':
        query_result = SeqRound.query.filter_by(rounds_id=id).order_by(
            SeqRound.count.desc()).paginate(page, pagelimit, False)

    if table in ['selection', 'round']:
        next_url = url_for(
            'ngs.details',
            table=table,
            id=id,
            page=query_result.next_num,
        ) if query_result.has_next else None
        prev_url = url_for(
            'ngs.details',
            table=table,
            id=id,
            page=query_result.prev_num,
        ) if query_result.has_prev else None
        totalpages = query_result.total
        start, end = pagination_gaps(page, totalpages, pagelimit)
        page_url = [(i, url_for('ngs.details', table=table, page=i, id=id))
                    for i in range(start, end + 1)]
        return render_template('ngs/details.html',
                               title=f'{entry.name}-Details',
                               entry=entry,
                               table=table,
                               query_result=query_result,
                               next_url=next_url,
                               prev_url=prev_url,
                               active=page,
                               page_url=page_url)

    return render_template('ngs/details.html',
                           title=f'{entry.name}-Details',
                           entry=entry,
                           table=table)
def lev_distance_search(query,table):
    #table can be sequence, primer, known_sequence
    query=query.strip()
    target = models_table_name_dictionary.get(table)
    seq_atr_name = dict(primer='sequence', known_sequence='rep_seq',
                   sequence='aptamer_seq').get(table)
    result = []
    total = target.query.count()
    have_more=True
    page = 0
    pagelimit = 10000
    task = partial(dynamic_lev_distance,fix_seq=query,diff_ratio=0.3)
    while have_more:
        page+=1
        searchcontent = db.session.query(
            getattr(target, 'id'), getattr(target, seq_atr_name)).paginate(page,pagelimit,False)
        if not searchcontent.has_next:
            have_more = False
        searchcontent = searchcontent.items
        tempresult = poolwrapper(task,[i[1] for i in searchcontent],callback=_set_task_progress,
                                 progress_gap=((page-1)*pagelimit / total * 99, min(99, page*pagelimit / total * 99)))

        for _dis, (_id, _s) in zip(tempresult, searchcontent):
            if _dis != None:
                result.append((_id, _dis))
    result.sort(key=lambda x:x[1])
    result = result[0:50]
    _set_task_progress(100)
    return dict(result=result,query=query)
Exemplo n.º 3
0
def get_bar_progress():
    ids = request.json['barlist']
    progresses = dict.fromkeys(ids)
    for id in ids:
        table, index = id.split(':')
        if table != 'task':
            indx = int(index)
        t = models_table_name_dictionary.get(table, None)
        if t:
            progress = t.query.get(index).progress
            progresses[id] = progress
    return jsonify(progresses)
Exemplo n.º 4
0
def details_image(funcname):
    id = request.args.get('id', 0, int)
    target = models_table_name_dictionary.get(funcname, None)
    entry = target.query.get(id)
    if funcname == 'round':
        fig = entry.plot_pie()
    if funcname == 'sequence':
        fig = entry.plot_structure()
    if funcname == 'known_sequence':
        fig = entry.plot_structure()
    buf = io.BytesIO()
    fig.savefig(buf, format="svg")
    plt_byte = buf.getvalue()
    buf.close()
    return Response(plt_byte, mimetype="image/svg+xml")
Exemplo n.º 5
0
def edit():
    if request.method == 'GET':
        edit_redirect_url = request.referrer or '/'
    else:
        edit_redirect_url = request.form.get('edit_redirect_url', '/')
    toadd = request.args.get('toadd')
    id = request.args.get('id')
    if toadd == 'analysis':
        return redirect(url_for('ngs.analysis', id=id))
    try:
        if toadd == 'sequence_round':
            id = id[1:-1].split(',')
            id = tuple(int(i) for i in id)
        else:
            id = int(id)
    except:
        abort(404)
    datalist = {}
    datalist = load_datalist(toadd)
    if toadd == 'ngs_sample_group':
        return redirect(
            url_for('ngs.addsample',
                    id=id,
                    edit_redirect_url=edit_redirect_url))
    elif toadd in ngs_edit_form_dictionary.keys():
        target = models_table_name_dictionary.get(toadd, None)
        formtemplate = ngs_edit_form_dictionary.get(toadd, None)
        if target and formtemplate:
            newitem = target.query.get(id)
            form = formtemplate(newitem)
    else:
        return redirect(edit_redirect_url)
    if request.method == 'GET':
        form.load_obj(id)
    if form.validate_on_submit():
        form.populate_obj(id)
        db.session.commit()
        flash('Your Edit to <{}> was saved.'.format(newitem), 'success')
        return redirect(edit_redirect_url)

    return render_template('ngs/add.html',
                           title='NGS - Edit',
                           table=toadd,
                           form=form,
                           datalist=datalist,
                           edit_redirect_url=edit_redirect_url)
Exemplo n.º 6
0
def delete():
    edit_redirect_url = request.referrer or '/'
    table = request.args.get('table')
    id = request.args.get('todelete', 0, int)
    if table != 'sequence_round':
        target = models_table_name_dictionary.get(table)
        todelete = target.query.get(id)
        if todelete.haschildren():
            flash('Cannot delete {}'.format(todelete), 'warning')
        else:
            try:
                db.session.delete(todelete)
                db.session.commit()
                flash('{} was deleted.'.format(todelete), 'success')
            except IntegrityError as e:
                db.session.rollback()
                flash('Cannot delete {}. Exception: {}.'.format(todelete, e),
                      'danger')
    else:
        flash('Cannot delete sequence.')
    return redirect(edit_redirect_url)
Exemplo n.º 7
0
def edit():
    data = request.json
    table, field, id, value = data['table'], data['field'], data['id'], data[
        'data']
    target = models_table_name_dictionary.get(table, None)
    try:
        item = target.query.get(id)
        setattr(item, field, value)
        db.session.commit()
        messages = [('success',
                     'Edit to {}\'s {} was saved.'.format(item, field))]
        if table == 'slide':
            file = path.join(current_app.config['PPT_SOURCE_FOLDER'],
                             item.ppt.path + '.pptx')
            set_slide_notes(file, item.page, field, value)
    except Exception as e:
        messages = [
            ('danger',
             'Edit to {}\'s {} failed. Error: {}'.format(item, field, e))
        ]
    return jsonify(html=render_template('flash_messages.html',
                                        messages=messages),
                   tag=getattr(item, 'tag', None),
                   note=item.note)
Exemplo n.º 8
0
def browse():
    pagelimit = current_user.ngs_per_page
    table = request.args.get('table')
    id = request.args.get('id', 0, type=int)
    page = request.args.get('page', 1, type=int)
    target = models_table_name_dictionary.get(table, None)
    if target:
        if id:
            if table == 'round':
                entries = target.query.filter_by(selection_id=id).order_by(
                    target.id.desc()).paginate(page, pagelimit, False)
            elif table == 'sequence_round':
                entries = target.query.filter_by(rounds_id=id).order_by(
                    target.count.desc()).paginate(page, pagelimit, False)
                # entries = [SequenceDisplay(i.sequence,i.count,i.count/rd.totalread,rd.round_name) for i in r]
            else:
                entries = target.query.order_by(target.id.desc()).paginate(
                    page, pagelimit, False)
        else:
            if table == 'sequence_round':
                sequences = db.session.query(Sequence.id).filter(
                    db.and_(Sequence.note.isnot(None),
                            Sequence.note != '')).subquery()
                maxcounts = db.session.query(
                    SeqRound.sequence_id,
                    func.max(SeqRound.count).label('count')).filter(
                        SeqRound.sequence_id.in_(sequences)).group_by(
                            SeqRound.sequence_id).subquery()
                entries = SeqRound.query.join(
                    Sequence, SeqRound.sequence_id == Sequence.id).filter(
                        SeqRound.sequence_id == maxcounts.c.sequence_id,
                        SeqRound.count == maxcounts.c.count).order_by(
                            Sequence.date.desc()).paginate(
                                page, pagelimit, False)
            else:
                entries = target.query.order_by(target.id.desc()).paginate(
                    page, pagelimit, False)

        nextcontent = {
            'round': 'sequence_round',
            'selection': 'round'
        }.get(table)
        kwargs = {}
        if id: kwargs.update(id=id)
        totalpages = entries.total
        start, end = pagination_gaps(page, totalpages, pagelimit)

        next_url = url_for(
            'ngs.browse', table=table, page=entries.next_num, **
            kwargs) if entries.has_next else None
        prev_url = url_for(
            'ngs.browse', table=table, page=entries.prev_num, **
            kwargs) if entries.has_prev else None
        page_url = [(i, url_for('ngs.browse', table=table, page=i, **kwargs))
                    for i in range(start, end + 1)]
        return render_template('ngs/browse.html',
                               title='NGS ' + (table.capitalize() or ' '),
                               entries=entries.items,
                               next_url=next_url,
                               prev_url=prev_url,
                               table=table,
                               nextcontent=nextcontent,
                               page_url=page_url,
                               active=page)
    return render_template('ngs/browse.html',
                           title='NGS',
                           entries=[],
                           table='')
Exemplo n.º 9
0
def index():
    """
    main browsing page
    """
    pagelimit = current_user.slide_per_page
    thumbnail = current_user.thumbnail
    table = request.args.get('table', None)
    if not table:
        return redirect(url_for('ppt.index', table='project'))

    id = request.args.get('id', 0, type=int)
    page = request.args.get('page', 1, type=int)
    target = models_table_name_dictionary.get(table, None)

    tag = request.args.get('tag', None)
    if table == 'tags' and tag == None:
        return render_template(
            'ppt/index.html',
            title=(table.upper() or ' ') + '-View',
            table=table,
            entries=Slide.tags_list(500, True),
        )

    author = request.args.get('author', None)
    if table == 'authors' and author == None:
        return render_template(
            'ppt/index.html',
            title=(table.upper() or ' ') + '-View',
            table=table,
            entries=Slide.authors_list(),
        )

    if table in ['trash', 'tags', 'authors']:
        target = Slide

    tags_list = Slide.tags_list()
    if target:
        if id:
            if table == 'ppt':
                entries = target.query.filter_by(project_id=id).order_by(
                    target.date.desc()).paginate(page, pagelimit, False)
            elif table == 'slide':
                entries = target.query.filter_by(ppt_id=id).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            else:
                entries = target.query.order_by(target.date.desc()).paginate(
                    page, pagelimit, False)
        else:
            if table == 'trash':
                entries = target.query.filter_by(ppt_id=None).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'tags':
                table = 'slide'
                entries = target.query.filter(
                    target.tag.contains(tag)).order_by(
                        target.date.desc(),
                        target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'authors':
                table = 'slide'
                author = None if author == 'Anonymous' else author
                entries = target.query.filter(
                    target.author == author).order_by(
                        target.date.desc(),
                        target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'slide':
                entries = target.query.filter(target.ppt_id != None).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            else:
                entries = target.query.order_by(target.date.desc()).paginate(
                    page, pagelimit, False)
        nextcontent = {'ppt': 'slide', 'project': 'ppt'}.get(table)
        kwargs = {}
        if id:
            kwargs.update(id=id)
        totalpages = entries.total
        start, end = pagination_gaps(page, totalpages, pagelimit, gap=15)

        next_url = url_for(
            'ppt.index', table=table, page=entries.next_num, **
            kwargs) if entries.has_next else None
        prev_url = url_for(
            'ppt.index', table=table, page=entries.prev_num, **
            kwargs) if entries.has_prev else None
        page_url = [(i, url_for('ppt.index', table=table, page=i, **kwargs))
                    for i in range(start, end + 1)]
        return render_template('ppt/index.html',
                               title=(table.upper() or ' ') + '-View',
                               entries=entries.items,
                               next_url=next_url,
                               prev_url=prev_url,
                               table=table,
                               nextcontent=nextcontent,
                               tags_list=tags_list,
                               page_url=page_url,
                               active=page,
                               id=id,
                               thumbnail=thumbnail)

    return render_template('ppt/index.html',
                           title=(table.upper() or ' ') + '-View',
                           table=table,
                           tags_list=tags_list,
                           thumbnail=thumbnail)
Exemplo n.º 10
0
def ngs_serach_handler(form):
    pagelimit = current_user.ngs_per_page
    table = form.search_field.data
    target = models_table_name_dictionary.get(table)
    method = form.search_method.data
    page = request.args.get('page', 1, int)
    kwargs = dict(request.args)
    kwargs.pop('page', None)
    nextcontent = {'round': 'sequence_round', 'selection': 'round'}.get(table)
    seqdict = dict(primer='sequence',
                   known_sequence='rep_seq',
                   sequence='aptamer_seq')
    if method == 'text':
        if table == 'sequence':
            table = 'sequence_round'
            sequences = db.session.query(Sequence.id).filter(
                Sequence.note.contains(form.q.data)).subquery()
            maxcounts = db.session.query(
                SeqRound.sequence_id,
                func.max(SeqRound.count).label('count')).filter(
                    SeqRound.sequence_id.in_(sequences)).group_by(
                        SeqRound.sequence_id).subquery()
            result = SeqRound.query.join(
                Sequence, SeqRound.sequence_id == Sequence.id).filter(
                    SeqRound.sequence_id == maxcounts.c.sequence_id,
                    SeqRound.count == maxcounts.c.count).order_by(
                        Sequence.date.desc())
            result = result.paginate(page, pagelimit, False)
            total = result.total
            entries = result.items

            # subqry = db.session.query(func.max(sr.count)).filter(sr.sequence_id.in_([1,2,3,4,5]))
            # sr.query.filter(sr.sequence.in_([1,2,3,4,5],sr.count==subqry)).all()

            # subq=db.session.query(sr.sequence_id,func.max(sr.count).label('count')).filter(sr.sequence_id.in_([1,2,3,4,5])).group_by(sr.sequence_id).subquery()
            # sr.query.filter(sr.sequence_id==subq.c.sequence_id,sr.count==subq.c.count).all()

        else:
            entries, total = target.search(form.q.data, page, pagelimit)
    elif method == 'name':
        q = form.q.data
        if table == 'sequence':
            table = 'sequence_round'
            try:
                seqid = convert_string_to_id(q)
            except:
                seqid = 0
            result = SeqRound.query.filter(
                SeqRound.sequence_id == seqid).order_by(SeqRound.count.desc())
        else:
            namedict = dict(analysis="name",
                            known_sequence='sequence_name',
                            selection='selection_name',
                            round='round_name',
                            primer='name',
                            ngs_sample_group='name')
            result = target.query.filter(
                getattr(target, namedict[table]).contains(q)).order_by(
                    target.id.desc())
        result = result.paginate(page, pagelimit, False)
        total = result.total
        entries = result.items
    elif method == 'sequence':
        seq = form.q.data
        if not validate_sequence(seq):
            flash('Sequence not valid. Only ATCG is allowed.', 'warning')
            return redirect(request.referrer)
        revseq = reverse_comp(seq)
        seqdict = dict(primer='sequence',
                       known_sequence='rep_seq',
                       sequence='aptamer_seq')
        stmt = or_(
            getattr(target, seqdict[table]).contains(seq),
            getattr(target, seqdict[table]).contains(revseq))

        if table == 'sequence':
            table = 'sequence_round'
            result = SeqRound.query.filter(
                SeqRound.sequence_id.in_(
                    db.session.query(target.id).filter(stmt))).order_by(
                        SeqRound.count.desc())
        else:
            result = target.query.filter(stmt).order_by(target.id.desc())
        result = result.paginate(page, pagelimit, False)
        total = result.total
        entries = result.items

    elif method == 'distance':
        seq = form.q.data
        if not validate_sequence(seq):
            flash('Sequence not valid. Only ATCG is allowed.', 'warning')
            return redirect(request.referrer)
        taskid = current_user.launch_search(seq, table)
        target = models_table_name_dictionary.get(table)
        total = target.query.count()
        # render a template to display progress bar with task id, after done display result page.
        return render_template('ngs/task_progress.html',
                               task_id=taskid,
                               table=table,
                               total=total)
    start, end = pagination_gaps(page, total, pagelimit)
    next_url = url_for('main.search', page=page +
                       1, **kwargs) if total > page * pagelimit else None
    prev_url = url_for('main.search', page=page -
                       1, **kwargs) if page > 1 else None
    page_url = [(i, url_for('main.search', page=i, **kwargs))
                for i in range(start, end + 1)]
    return render_template('ngs/browse.html',
                           title='Search Result',
                           table=table,
                           nextcontent=nextcontent,
                           entries=entries,
                           next_url=next_url,
                           prev_url=prev_url,
                           page_url=page_url,
                           active=page)