Exemple #1
0
def edit(project,
         name,
         user_id,
         sequence_id=None,
         tracks=None,
         isPublic=False,
         circles=None):
    '''
    Like create but edit an existing project.
    '''
    project.name = name
    if sequence_id is not None:
        project.sequence_id = sequence_id
    project.user_id = user_id
    project.is_public = isPublic
    DBSession.add(project)
    DBSession.flush()

    project.tracks = []
    if tracks is not None:
        for track_id in tracks:
            t = DBSession.query(Track).filter(Track.id == track_id).first()
            project.tracks.append(t)

    if circles is not None:  # adding circle with the read permission by default
        project._circle_right = []
        for circle in circles:
            _add_read_right(project, circle.id)

    DBSession.add(project)
    DBSession.flush()
    return project
Exemple #2
0
def edit(project, name, user_id, sequence_id=None, tracks=None, isPublic=False, circles=None):
    '''
    Like create but edit an existing project.
    '''
    project.name=name
    if sequence_id is not None:
        project.sequence_id = sequence_id
    project.user_id = user_id
    project.is_public = isPublic
    DBSession.add(project)
    DBSession.flush()

    project.tracks = []
    if tracks is not None:
        for track_id in tracks :
            t = DBSession.query(Track).filter(Track.id == track_id).first()
            project.tracks.append(t)
    
   
    if circles is not None: # adding circle with the read permission by default
        project._circle_right = []
        for circle in circles : _add_read_right(project, circle.id)
    
    DBSession.add(project)
    DBSession.flush()
    return project
Exemple #3
0
def e(project=None, project_id=None, name=None, track_ids=None, circle_ids=None):
    if project is None:
        project = DBSession.query(Project).filter(Project.id == project_id).first()

    if name is not None:
        project.name = name

    if track_ids is not None:
        project.tracks = []
        for tid in track_ids:
            t = DBSession.query(Track).filter(Track.id == tid).first()
            project.tracks.append(t)

    if circle_ids is not None:
        if not isinstance(circle_ids, list):
            circle_ids = [circle_ids]
        circle_ids = [int(i) for i in circle_ids]
        to_del = []
        for shared in project._circle_right:
            if shared.circle.id not in circle_ids:
                to_del.append(shared)
        for d in to_del:
            DBSession.delete(d)
            project._circle_right.remove(d)
        DBSession.flush()

        shared_ids = [c.id for c in project.shared_circles]
        read_right = DBSession.query(Right).filter(Right.id == constants.rights['read']['id']).first()
        for new_id in circle_ids:
            if new_id not in shared_ids:
                add_right(project=project, circle_id=new_id, right=read_right)
        DBSession.flush()
    DBSession.add(project)
    DBSession.flush()
    return project
Exemple #4
0
def add_read_right(project, circle_id):    
    '''
    Add the ``read`` right to the project & circle specified. Flush the database
    '''
    _add_read_right(project, circle_id)
    DBSession.add(project)
    DBSession.flush()
Exemple #5
0
 def validation(*args, **kw):
     for arg in args_list:
         if arg not in kw:
             raise Exception('Missing args for the validation %s' % kw)
     user = DBSession.query(User).filter(
         User.email == str(kw['mail'])).first()
     if user.key != str(kw['key']):
         raise Exception('User not valid %s' % kw)
     project = DBSession.query(Project).filter(
         Project.id == int(kw['pid'])).first()
     if project.key != str(kw['pkey']):
         raise Exception('Project not valid %s' % kw)
     job = Job()
     job.user_id = user.id
     job.project_id = project.id
     job.status = 'PENDING'
     job.ext_task_id = kw['task_id']
     job.bioscript_url = handler.job.task_url(kw['task_id'])
     if 'plugin_info' in kw:
         info = json.loads(kw['plugin_info'])
         job.name = info['title']
         job.description = info['description']
     else:
         job.name = 'Job %s from bioscript' % kw['task_id']
         job.description = 'Description available at %s' % handler.job.task_url(
             kw['task_id'])
     DBSession.add(job)
     DBSession.flush()
     return {}
Exemple #6
0
def change_rights(project_id, circle_id, rights=None):
    '''
    Modify the right associated to a project to a group.
    If any right is added, automatically add read right.
    @param project_id : the project id
    @param circle_id : the circle id
    @param rights : the right to update
    '''
    project = DBSession.query(Project).filter(Project.id == project_id).first()
    rc_assocs = get_circle_right_assocs(circle_id, project_id)
    
    
    for rc in rc_assocs:
        if rc.circle.id == int(circle_id) :
            project._circle_right.remove(rc)
            DBSession.delete(rc)
            DBSession.flush()

    if rights is not None:
        _add_read_right(project, circle_id)
        for right_name in rights:
            if right_name != constants.right_read :
                right = DBSession.query(Right).filter(Right.name == right_name).first()
                cr_assoc = _get_circle_right_assoc(right, circle_id, project_id)
                project._circle_right.append(cr_assoc)

    DBSession.add(project)
    DBSession.flush()
Exemple #7
0
 def validation(*args, **kw):
     for arg in args_list:
         if arg not in kw:
             raise Exception('Missing args for the validation %s' % kw)
     user = DBSession.query(User).filter(User.email == str(kw['mail'])).first()
     if user.key != str(kw['key']):
         raise Exception('User not valid %s' % kw)
     project = DBSession.query(Project).filter(Project.id == int(kw['pid'])).first()
     if project.key != str(kw['pkey']):
         raise Exception('Project not valid %s' % kw)
     job = Job()
     job.user_id = user.id
     job.project_id = project.id
     job.status = 'PENDING'
     job.ext_task_id = kw['task_id']
     job.bioscript_url = handler.job.task_url(kw['task_id'])
     if 'plugin_info' in kw:
         info = json.loads(kw['plugin_info'])
         job.name = info['title']
         job.description = info['description']
     else:
         job.name = 'Job %s from bioscript' % kw['task_id']
         job.description = 'Description available at %s' % handler.job.task_url(kw['task_id'])
     DBSession.add(job)
     DBSession.flush()
     return {}
Exemple #8
0
def add_read_right(project, circle_id):
    '''
    Add the ``read`` right to the project & circle specified. Flush the database
    '''
    _add_read_right(project, circle_id)
    DBSession.add(project)
    DBSession.flush()
Exemple #9
0
def change_rights(project_id, circle_id, rights=None):
    '''
    Modify the right associated to a project to a group.
    If any right is added, automatically add read right.
    @param project_id : the project id
    @param circle_id : the circle id
    @param rights : the right to update
    '''
    project = DBSession.query(Project).filter(Project.id == project_id).first()
    rc_assocs = get_circle_right_assocs(circle_id, project_id)

    for rc in rc_assocs:
        if rc.circle.id == int(circle_id):
            project._circle_right.remove(rc)
            DBSession.delete(rc)
            DBSession.flush()

    if rights is not None:
        _add_read_right(project, circle_id)
        for right_name in rights:
            if right_name != constants.right_read:
                right = DBSession.query(Right).filter(
                    Right.name == right_name).first()
                cr_assoc = _get_circle_right_assoc(right, circle_id,
                                                   project_id)
                project._circle_right.append(cr_assoc)

    DBSession.add(project)
    DBSession.flush()
Exemple #10
0
def add_read_right_to_circles_ids(project, ids):
    '''
     Add the ``read`` right to the project & circles specified. Flush the database
    '''  
    for id in ids:
        _add_read_right(project, id)
    DBSession.add(project)
    DBSession.flush()
Exemple #11
0
def add_read_right_to_circles_ids(project, ids):
    '''
     Add the ``read`` right to the project & circles specified. Flush the database
    '''
    for id in ids:
        _add_read_right(project, id)
    DBSession.add(project)
    DBSession.flush()
Exemple #12
0
def add_tracks(project, track_ids):
    '''
    Add a list of track to the project specified.
    '''
    for track_id in track_ids:
        track_ = DBSession.query(Track).filter(Track.id == track_id).first()
        project.tracks.append(track_)
    DBSession.add(project)
    DBSession.flush()
Exemple #13
0
def add_tracks(project, track_ids):
    '''
    Add a list of track to the project specified.
    '''
    for track_id in track_ids:
        track_ = DBSession.query(Track).filter(Track.id == track_id).first()
        project.tracks.append(track_)
    DBSession.add(project)
    DBSession.flush()
Exemple #14
0
def create_tmp_user(mail):
    user = User()
    user.name = constants.tmp_user_name
    user.email = mail
    user.firstname = ''
    user_group = DBSession.query(Group).filter(Group.id == constants.group_users_id).first()
    user_group.users.append(user)
    DBSession.add(user)
    DBSession.flush()
    return user
Exemple #15
0
    def index(self, *args, **kw):

        seq_form = form.NewSequenceForm(action=url('/sequences')).req()
        species = genrep.get_species()

        m = {}
        sp_to_add = []
        for sp in species:
            assemblies = genrep.get_assemblies_not_created_from_species_id(sp.id)
            if len(assemblies) > 0:
                m[sp.id] = [(ass.id, ass.name) for ass in assemblies]
                sp_to_add.append(sp)
        sp_opts =  [(sp.id, sp.species) for sp in sp_to_add]
        seq_form.child.children[1].options = sp_opts
        value = {'smapping' : json.dumps(m)}
        seq_form.value = value
        grid = datagrid.sequences_grid


        if request.method == 'GET':
            sequences = DBSession.query(Sequence).all()
            seq_grid = [util.to_datagrid(grid, sequences, "Sequences", len(sequences)>0)]

            return dict(page="sequences", widget=seq_form, grid=seq_grid)
        else :
            species_id = kw['species']
            assembly_id = kw['assembly']
            # look if the species already exist in GDV, else create it
            species = DBSession.query(Species).filter(Species.id == species_id).first()
            if not species:
                species = handler.genrep.get_species_by_id(species_id)
                current_sp = Species()
                current_sp.id = species.id
                current_sp.name = species.species
                DBSession.add(current_sp)
                DBSession.flush()
                current_sp = DBSession.query(Species).filter(Species.id == species_id).first()
                flash( '''Species created: %s'''%( current_sp ))

            # look if the assembly not already created, else create it
            if not DBSession.query(Sequence).filter(Sequence.id == assembly_id).first():
                assembly = handler.genrep.get_assembly_by_id(assembly_id)
                seq = Sequence()
                seq.id = assembly_id
                seq.name = assembly.name
                seq.species_id = species_id
                DBSession.add(seq)
                DBSession.flush()
                seq = DBSession.query(Sequence).filter(Sequence.id == assembly_id).first()
                handler.sequence.add_new_sequence(seq)
                flash( '''Sequence created: %s'''%( seq ))
            DBSession.flush()
            sequences = DBSession.query(Sequence).all()
            seq_grid = [util.to_datagrid(grid, sequences, "Sequences", len(sequences)>0)]
            return dict(page="sequences", widget=seq_form, grid=seq_grid)
Exemple #16
0
def create_tmp_user(mail):
    user = User()
    user.name = constants.tmp_user_name
    user.email = mail
    user.firstname = ''
    user_group = DBSession.query(Group).filter(
        Group.id == constants.group_users_id).first()
    user_group.users.append(user)
    DBSession.add(user)
    DBSession.flush()
    return user
Exemple #17
0
def create_admin(name):
    '''
    Create a new admin circle.
    @param name : the name
    '''
    c = Circle()
    c.name = name
    c.description = 'Circle created with Tequila'
    c.admin = True
    DBSession.add(c)
    DBSession.flush()
    return c
Exemple #18
0
    def save(self, project_id, color, description, locations):
        user = handler.user.get_user_in_session(request)

        if not checker.check_permission(user=user,
                                        project_id=project_id,
                                        right_id=constants.right_upload_id):
            flash(
                'You must have %s permission to delete the project.' %
                constants.right_upload, 'error')
            return {'save': 'failed'}

        #print "save %s, color %s, desc %s loc %s" % (project_id, color, description, locations)
        ''' For the moment, there is only one selection per project'''
        sel = DBSession.query(Selection).filter(
            Selection.project_id == project_id).first()
        if sel is None:
            sel = Selection()
            sel.project_id = project_id

        sel.description = description
        sel.color = color
        DBSession.add(sel)
        DBSession.flush()

        locations_ids = []
        # add locations
        for loc in json.loads(locations):
            obj = None
            if 'id' in loc:
                obj = DBSession.query(Location).join(
                    Selection.locations).filter(
                        and_(Selection.id == sel.id,
                             Location.id == loc.get('id'))).first()

            if obj is None:
                obj = Location()

            obj.chromosome = loc.get('chr')
            obj.start = loc.get('start')
            obj.end = loc.get('end')
            obj.description = loc.get('desc', 'No description')
            obj.selection = sel
            DBSession.add(obj)
            DBSession.flush()
            locations_ids.append(obj.id)
        # remove not saved ones
        loc_to_remove = DBSession.query(Location).filter(
            not_(Location.id.in_(locations_ids))).all()
        for l in loc_to_remove:
            DBSession.delete(l)
        DBSession.flush()
        return {"saved": "ok"}
Exemple #19
0
def edit(c, name, description, creator, users=None):
    c.name = name
    c.description = description
    c.creator_id = creator.id
    c.users = []
    if users is not None:
        for user_id in users:
            if not int(user_id) == creator.id:
                u = DBSession.query(User).filter(User.id == user_id).first()
                c.users.append(u)
    c.users.append(creator)
    DBSession.add(c)
    DBSession.flush()
Exemple #20
0
    def callback(self, *args, **kw):
        print kw
        job = DBSession.query(Job).filter(Job.ext_task_id == kw['task_id']).first()
        project = DBSession.query(Project).filter(Project.id == int(kw['pid'])).first()
        if project is None or project.key != str(kw['pkey']):
            raise Exception('Project not valid')
        if job.project_id != project.id:
            raise Exception('Job not valid')

        status = str(kw['status'])
        job.status = status
        if status.lower() in ['error', 'failed']:
            job.traceback = kw['error']
        elif status == 'SUCCESS':
            results = json.loads(kw['results'])
            for result in results:
                bres = Bresults()
                bres.job_id = job.id
                bres.output_type = str(result.get('type', 'not defined'))
                bres.is_file = result.get('is_file', False)
                path = str(result.get('path', ''))
                bres.path = path
                bres.data = str(result.get('value', ''))

                is_track = result.get('type', '') == 'track'
                if is_track:
                    out = os.path.join(filemanager.temporary_directory(), os.path.split(path)[-1])
                    fileinfo = filemanager.FileInfo(inputtype='fsys', inpath=path, outpath=out, admin=False)
                    sequence = project.sequence
                    user = DBSession.query(User).filter(User.key == str(kw['key'])).first()
                    if user.email != str(kw['mail']):
                        raise Exception("Wrong user")
                    user_info = {'id': user.id, 'name': user.name, 'email': user.email}
                    sequence_info = {'id': sequence.id, 'name': sequence.name}

                    t = Track()
                    t.name = fileinfo.trackname
                    t.sequence_id = sequence.id
                    t.user_id = user.id
                    DBSession.add(t)
                    DBSession.flush()
                    async = tasks.new_input.delay(user_info, fileinfo, sequence_info, t.id, project.id)
                    t.task_id = async.task_id
                    bres.track_id = t.id

                bres.is_track = is_track
                DBSession.add(bres)
        DBSession.flush()
        return {}
Exemple #21
0
def _get_circle_right_assoc(right, circle_id, project_id):
    '''
    Get the ``RightCircleAssociation`` corresponding
    to the right and circle_id and project_id given
    '''
    cr_assoc = DBSession.query(RightCircleAssociation).filter(
        and_(RightCircleAssociation.right_id == right.id,
             RightCircleAssociation.circle_id == circle_id,
             RightCircleAssociation.project_id == project_id)).first()
    if cr_assoc is None:
        cr_assoc = RightCircleAssociation()
        cr_assoc.circle_id = circle_id
        cr_assoc.right = right
        cr_assoc.project_id = project_id
        DBSession.add(cr_assoc)
    return cr_assoc
Exemple #22
0
def _get_circle_right_assoc(right, circle_id, project_id):
    '''
    Get the ``RightCircleAssociation`` corresponding
    to the right and circle_id and project_id given
    '''
    cr_assoc = DBSession.query(RightCircleAssociation).filter(
                                    and_(RightCircleAssociation.right_id == right.id,
                                         RightCircleAssociation.circle_id == circle_id,
                                         RightCircleAssociation.project_id == project_id
                                         )).first()
    if cr_assoc is None:
        cr_assoc = RightCircleAssociation()
        cr_assoc.circle_id = circle_id
        cr_assoc.right = right
        cr_assoc.project_id = project_id
        DBSession.add(cr_assoc)
    return cr_assoc
Exemple #23
0
    def save(self, project_id, color, description, locations):
        user = handler.user.get_user_in_session(request)

        if not checker.check_permission(user=user, project_id=project_id, right_id=constants.right_upload_id):
            flash('You must have %s permission to delete the project.' % constants.right_upload, 'error')
            return {'save': 'failed'}

        #print "save %s, color %s, desc %s loc %s" % (project_id, color, description, locations)
        ''' For the moment, there is only one selection per project'''
        sel = DBSession.query(Selection).filter(Selection.project_id == project_id).first()
        if sel is None:
            sel = Selection()
            sel.project_id = project_id

        sel.description = description
        sel.color = color
        DBSession.add(sel)
        DBSession.flush()

        locations_ids = []
        # add locations
        for loc in json.loads(locations):
            obj = None
            if 'id' in loc:
                obj = DBSession.query(Location).join(Selection.locations).filter(
                        and_(Selection.id == sel.id, Location.id == loc.get('id'))).first()

            if obj is None:
                obj = Location()

            obj.chromosome = loc.get('chr')
            obj.start = loc.get('start')
            obj.end = loc.get('end')
            obj.description = loc.get('desc', 'No description')
            obj.selection = sel
            DBSession.add(obj)
            DBSession.flush()
            locations_ids.append(obj.id)
        # remove not saved ones
        loc_to_remove = DBSession.query(Location).filter(not_(Location.id.in_(locations_ids))).all()
        for l in loc_to_remove:
            DBSession.delete(l)
        DBSession.flush()
        return {"saved": "ok"}
Exemple #24
0
def e(project=None,
      project_id=None,
      name=None,
      track_ids=None,
      circle_ids=None):
    if project is None:
        project = DBSession.query(Project).filter(
            Project.id == project_id).first()

    if name is not None:
        project.name = name

    if track_ids is not None:
        project.tracks = []
        for tid in track_ids:
            t = DBSession.query(Track).filter(Track.id == tid).first()
            project.tracks.append(t)

    if circle_ids is not None:
        if not isinstance(circle_ids, list):
            circle_ids = [circle_ids]
        circle_ids = [int(i) for i in circle_ids]
        to_del = []
        for shared in project._circle_right:
            if shared.circle.id not in circle_ids:
                to_del.append(shared)
        for d in to_del:
            DBSession.delete(d)
            project._circle_right.remove(d)
        DBSession.flush()

        shared_ids = [c.id for c in project.shared_circles]
        read_right = DBSession.query(Right).filter(
            Right.id == constants.rights['read']['id']).first()
        for new_id in circle_ids:
            if new_id not in shared_ids:
                add_right(project=project, circle_id=new_id, right=read_right)
        DBSession.flush()
    DBSession.add(project)
    DBSession.flush()
    return project
Exemple #25
0
def add_new_sequence(sequence):
    '''
    Method called when a new sequence is created on GDV.
    It should import fast from JBrowse
    '''
    print 'add new sequence'
    file_url = Assembly(sequence).get_sqlite_url()
    print file_url
    out = os.path.join(filemanager.temporary_directory(), 'Genes.sql')
    fileinfo = filemanager.FileInfo(inputtype='url',
        inpath=file_url, trackname='Genes', extension='sql', outpath=out, admin=True)
    print fileinfo
    user = DBSession.query(User).filter(User.key == constants.admin_user_key()).first()
    user_info = {'id': user.id, 'name': user.name, 'email': user.email}
    sequence_info = {'id': sequence.id, 'name': sequence.name}

    # track
    t = Track()
    t.name = fileinfo.trackname
    t.sequence_id = sequence.id
    t.user_id = user.id
    DBSession.add(t)
    DBSession.flush()
    # send task
    async = tasks.new_input.delay(user_info, fileinfo, sequence_info, t.id)
    t.task_id = async.task_id
    DBSession.add(t)

    sequence.default_tracks.append(t)
    DBSession.add(sequence)
    DBSession.flush()
Exemple #26
0
def add_new_sequence(sequence):
    '''
    Method called when a new sequence is created on GDV.
    It should import fast from JBrowse
    '''
    print 'add new sequence'
    file_url = Assembly(sequence).get_sqlite_url()
    print file_url
    out = os.path.join(filemanager.temporary_directory(), 'Genes.sql')
    fileinfo = filemanager.FileInfo(inputtype='url',
                                    inpath=file_url,
                                    trackname='Genes',
                                    extension='sql',
                                    outpath=out,
                                    admin=True)
    print fileinfo
    user = DBSession.query(User).filter(
        User.key == constants.admin_user_key()).first()
    user_info = {'id': user.id, 'name': user.name, 'email': user.email}
    sequence_info = {'id': sequence.id, 'name': sequence.name}

    # track
    t = Track()
    t.name = fileinfo.trackname
    t.sequence_id = sequence.id
    t.user_id = user.id
    DBSession.add(t)
    DBSession.flush()
    # send task
    async = tasks.new_input.delay(user_info, fileinfo, sequence_info, t.id)
    t.task_id = async .task_id
    DBSession.add(t)

    sequence.default_tracks.append(t)
    DBSession.add(sequence)
    DBSession.flush()
Exemple #27
0
    def create(self, *args, **kw):
        user = handler.user.get_user_in_session(request)
        # get sequence
        sequence = None
        project_id = None
        debug('Create track %s' % kw)

        debug('Find sequence', 1)
        if 'assembly' in kw and kw.get('assembly'):
            assembly = int(kw.get('assembly'))
            sequence = DBSession.query(Sequence).filter(Sequence.id == assembly).first()
        elif 'sequence_id' in kw and kw.get('sequence_id'):
            sequence_id = int(kw.get('sequence_id'))
            sequence = DBSession.query(Sequence).filter(Sequence.id == sequence_id).first()
        elif 'sequence_id' in kw and kw.get('sequence_id'):
            sequence_id = int(kw.get('sequence_id'))
            sequence = DBSession.query(Sequence).filter(Sequence.id == sequence_id).first()
        elif 'project_id' in kw and kw.get('project_id'):
            project_id = int(kw.get('project_id'))
            project = DBSession.query(Project).filter(Project.id == project_id).first()
            if project is None:
                return reply.error(request, 'Project with id %s not found on GDV.' % project_id, tg.url('./new', {'pid': project_id}), {})
            sequence = DBSession.query(Sequence).filter(Sequence.id == project.sequence_id).first()
        if not sequence:
            return reply.error(request, 'Sequence not found on GDV.', tg.url('./new'), {'pid': project_id})
        debug('%s' % sequence.name, 2)

        # know if file is comming from url, fileupload or filsystem
        filetoget = None
        inputtype = None
        debug('Find inputtype', 1)
        if 'url' in kw and kw.get('url'):
            debug('from url', 2)
            filetoget = kw.get('url')
            if not filetoget.startswith('http://'):
                filetoget = 'http://%s' % filetoget
            inputtype = 'url'
        elif 'fsys' in kw and kw.get('fsys'):
            debug('fsys', 2)
            filetoget = kw.get('fsys')
            inputtype = 'fsys'
        elif 'file_upload' in kw and kw.get('file_upload') is not None and kw['file_upload'] != '':
            debug('file upload %s' % kw.get('file_upload'), 2)
            filetoget = kw.get('file_upload')
            inputtype = 'fu'
        if filetoget is None:
            return reply.error(request, 'No file to upload', tg.url('./new'), {'pid': project_id})

        debug('file2get: %s, intype: %s' % (filetoget, inputtype), 2)
        # get file name
        debug('Find track name', 1)
        trackname = None
        if 'trackname' in kw and kw.get('trackname'):
            debug('i params', 2)
            trackname = kw.get('trackname')
        else:
            if inputtype == 'url':
                debug('trackname from url', 2)
                trackname = os.path.split(filetoget)[1].split('?')[0]
            elif inputtype == 'fsys':
                debug('trackname from fsys', 2)
                trackname = os.path.split(filetoget)[-1]
            elif inputtype == 'fu':
                debug('trackname from fu', 2)
                trackname = filetoget.filename
        if trackname is None:
            return reply.error(request, 'No trackname found', tg.url('./new'), {'pid': project_id})

        debug('%s' % trackname, 2)
        # get extension
        extension = None
        if 'extension' in kw and kw.get('extension'):
            extension = kw.get('extension')
        elif 'ext' in kw and kw.get('ext'):
            extension = kw.get('ext')
        else:
            extension = os.path.splitext(trackname)[-1]
            if extension is None or extension == '':
                if inputtype == 'url':
                    debug('extension from url', 2)
                    extension = os.path.splitext(os.path.split(filetoget)[1].split('?')[0])[-1]
                elif inputtype == 'fsys':
                    debug('extension from fsys', 2)
                    extension = os.path.splitext(os.path.split(filetoget)[-1])[-1]
                elif inputtype == 'fu':
                    debug('extension from fu', 2)
                    extension = os.path.splitext(filetoget.filename)[-1]
        if extension is None or extension == '':
            return reply.error(request, 'No extension found', tg.url('./new'), {'pid': project_id})
        if extension.startswith('.'):
            extension = extension[1:]
        extension = extension.lower()
        # is the track "admin"
        admin = False
        if 'track_admin' in kw and kw.get('track_admin'):
            # check if really admin
            group_admin = DBSession.query(Group).filter(Group.id == constants.group_admins_id).first()
            if user in group_admin.users:
                admin = True
        debug('admin: %s' % admin, 1)
        #store information in a dummy object
        out = os.path.join(filemanager.temporary_directory(), trackname)
        fileinfo = filemanager.FileInfo(inputtype=inputtype, inpath=filetoget, trackname=trackname, extension=extension, outpath=out, admin=admin)
        debug('fileinfo: %s' % fileinfo, 1)
        # get additionnal information
        user_info = {'id': user.id, 'name': user.name, 'email': user.email}
        sequence_info = {'id': sequence.id, 'name': sequence.name}

        #upload the track it's from file_upload
        if inputtype == 'fu':
            debug('Download', 1)
            fileinfo.download()

        debug('fileinfo: %s' % fileinfo, 1)
        # create a track that the user can see something
        t = Track()
        t.name = fileinfo.trackname
        t.sequence_id = sequence.id
        t.user_id = user.id
        DBSession.add(t)
        DBSession.flush()
        # send task
        async = tasks.new_input.delay(user_info, fileinfo, sequence_info, t.id, project_id)
        t.task_id = async.task_id
        DBSession.add(t)
        DBSession.flush()
        debug('End create')
        return reply.normal(request, 'Processing launched.', '/tracks/', {'track_id': t.id, 'pid': project_id})
Exemple #28
0
    def index(self, *args, **kw):

        seq_form = form.NewSequenceForm(action=url('/sequences')).req()
        species = genrep.get_species()

        m = {}
        sp_to_add = []
        for sp in species:
            assemblies = genrep.get_assemblies_not_created_from_species_id(
                sp.id)
            if len(assemblies) > 0:
                m[sp.id] = [(ass.id, ass.name) for ass in assemblies]
                sp_to_add.append(sp)
        sp_opts = [(sp.id, sp.species) for sp in sp_to_add]
        seq_form.child.children[1].options = sp_opts
        value = {'smapping': json.dumps(m)}
        seq_form.value = value
        grid = datagrid.sequences_grid

        if request.method == 'GET':
            sequences = DBSession.query(Sequence).all()
            seq_grid = [
                util.to_datagrid(grid, sequences, "Sequences",
                                 len(sequences) > 0)
            ]

            return dict(page="sequences", widget=seq_form, grid=seq_grid)
        else:
            species_id = kw['species']
            assembly_id = kw['assembly']
            # look if the species already exist in GDV, else create it
            species = DBSession.query(Species).filter(
                Species.id == species_id).first()
            if not species:
                species = handler.genrep.get_species_by_id(species_id)
                current_sp = Species()
                current_sp.id = species.id
                current_sp.name = species.species
                DBSession.add(current_sp)
                DBSession.flush()
                current_sp = DBSession.query(Species).filter(
                    Species.id == species_id).first()
                flash('''Species created: %s''' % (current_sp))

            # look if the assembly not already created, else create it
            if not DBSession.query(Sequence).filter(
                    Sequence.id == assembly_id).first():
                assembly = handler.genrep.get_assembly_by_id(assembly_id)
                seq = Sequence()
                seq.id = assembly_id
                seq.name = assembly.name
                seq.species_id = species_id
                DBSession.add(seq)
                DBSession.flush()
                seq = DBSession.query(Sequence).filter(
                    Sequence.id == assembly_id).first()
                handler.sequence.add_new_sequence(seq)
                flash('''Sequence created: %s''' % (seq))
            DBSession.flush()
            sequences = DBSession.query(Sequence).all()
            seq_grid = [
                util.to_datagrid(grid, sequences, "Sequences",
                                 len(sequences) > 0)
            ]
            return dict(page="sequences", widget=seq_form, grid=seq_grid)
Exemple #29
0
def prepare_view(project_id, *args, **kw):
    from pygdv import handler

    # Get the project
    project = DBSession.query(Project).filter(Project.id == project_id).first()
    tracks = project.success_tracks

    # prepare track that are not yet initialised
    for t in tracks:
        handler.track.init(t)
    DBSession.flush()
    seq = project.sequence
    default_tracks = [d for d in seq.default_tracks if d.status == constants.SUCCESS]
    all_tracks = tracks + default_tracks

    # Track names must be different
    trackNames = []
    for t in all_tracks:
        while t.name in trackNames:
            ind = 0
            while(t.name[-(ind + 1)].isdigit()):
                ind += 1
            cpt = t.name[-ind:]
            try:
                cpt = int(cpt)
            except ValueError:
                cpt = 0
            cpt += 1

            tmp_name = t.name
            if ind > 0:
                tmp_name = t.name[:-ind]
            t.name = tmp_name + str(cpt)

        t.accessed
        DBSession.add(t)
        DBSession.flush()
        trackNames.append(t.name)

    # prepare javascript parameters
    refSeqs = 'refSeqs = %s' % json.dumps(jb.ref_seqs(project.sequence_id))
    trackInfo = 'trackInfo = %s' % json.dumps(jb.track_info(all_tracks, assembly_id=project.sequence_id))
    parameters = 'var b = new Browser(%s)' % jb.browser_parameters(
        constants.data_root(), constants.style_root(), constants.image_root(), ','.join([track.name for track in all_tracks]))

    style_control = '''function getFeatureStyle(type, div){
            div.style.backgroundColor='#3333D7';div.className='basic';
            switch(type){
            %s
            }};
            ''' % jb.features_style(all_tracks)

    selections = 'init_locations = %s' % handler.selection.selections(project_id)
    # prepare _gdv_info
    info = {}
    prefix = tg.config.get('prefix')
    if prefix:
        info['prefix'] = prefix
    info['proxy'] = tg.config.get('main.proxy')
    info['sequence_id'] = project.sequence_id
    info['admin'] = kw.get('admin', True)
    info['plug_url'] = url('/plugins')
    info['project_key'] = project.key
    info['project_id'] = project.id
    info['bioscript'] = bioscript_parameters('oplist')

    if 'mode' in kw:
        info['mode'] = kw.get('mode')

    info = json.dumps(info)

    control = 'b.showTracks();initGDV(b, %s, %s);' % (project.id, info)

    # add location if it's in url
    if 'loc' in kw:
        control += 'b.navigateTo("%s");' % kw.get('loc')

    # prepare jobs list
    jobs = 'init_jobs = {}'
    op = '{}'
    if 'plugin.service.url' in tg.config:
        try:
            op = handler.job.operation_list()
        except urllib2.URLError:
            pass
    return dict(species_name=project.species.name,
        nr_assembly_id=project.sequence_id,
        project_id=project.id,
        is_admin=True,
        ref_seqs=refSeqs,
        track_info=trackInfo,
        parameters=parameters,
        style_control=style_control,
        control=control,
        selections=selections,
        jobs=jobs,
        page='view',
        oplist=op)
Exemple #30
0
def prepare_view(project_id, *args, **kw):
    from pygdv import handler

    # Get the project
    project = DBSession.query(Project).filter(Project.id == project_id).first()
    tracks = project.success_tracks

    # prepare track that are not yet initialised
    for t in tracks:
        handler.track.init(t)
    DBSession.flush()
    seq = project.sequence
    default_tracks = [
        d for d in seq.default_tracks if d.status == constants.SUCCESS
    ]
    all_tracks = tracks + default_tracks

    # Track names must be different
    trackNames = []
    for t in all_tracks:
        while t.name in trackNames:
            ind = 0
            while (t.name[-(ind + 1)].isdigit()):
                ind += 1
            cpt = t.name[-ind:]
            try:
                cpt = int(cpt)
            except ValueError:
                cpt = 0
            cpt += 1

            tmp_name = t.name
            if ind > 0:
                tmp_name = t.name[:-ind]
            t.name = tmp_name + str(cpt)

        t.accessed
        DBSession.add(t)
        DBSession.flush()
        trackNames.append(t.name)

    # prepare javascript parameters
    refSeqs = 'refSeqs = %s' % json.dumps(jb.ref_seqs(project.sequence_id))
    trackInfo = 'trackInfo = %s' % json.dumps(
        jb.track_info(all_tracks, assembly_id=project.sequence_id))
    parameters = 'var b = new Browser(%s)' % jb.browser_parameters(
        constants.data_root(), constants.style_root(), constants.image_root(),
        ','.join([track.name for track in all_tracks]))

    style_control = '''function getFeatureStyle(type, div){
            div.style.backgroundColor='#3333D7';div.className='basic';
            switch(type){
            %s
            }};
            ''' % jb.features_style(all_tracks)

    selections = 'init_locations = %s' % handler.selection.selections(
        project_id)
    # prepare _gdv_info
    info = {}
    prefix = tg.config.get('prefix')
    if prefix:
        info['prefix'] = prefix
    info['proxy'] = tg.config.get('main.proxy')
    info['sequence_id'] = project.sequence_id
    info['admin'] = kw.get('admin', True)
    info['plug_url'] = url('/plugins')
    info['project_key'] = project.key
    info['project_id'] = project.id
    info['bioscript'] = bioscript_parameters('oplist')

    if 'mode' in kw:
        info['mode'] = kw.get('mode')

    info = json.dumps(info)

    control = 'b.showTracks();initGDV(b, %s, %s);' % (project.id, info)

    # add location if it's in url
    if 'loc' in kw:
        control += 'b.navigateTo("%s");' % kw.get('loc')

    # prepare jobs list
    jobs = 'init_jobs = {}'
    op = '{}'
    if 'plugin.service.url' in tg.config:
        try:
            op = handler.job.operation_list()
        except urllib2.URLError:
            pass
    return dict(species_name=project.species.name,
                nr_assembly_id=project.sequence_id,
                project_id=project.id,
                is_admin=True,
                ref_seqs=refSeqs,
                track_info=trackInfo,
                parameters=parameters,
                style_control=style_control,
                control=control,
                selections=selections,
                jobs=jobs,
                page='view',
                oplist=op)
Exemple #31
0
    def auth(self, came_from='/', **kw):
        '''
        Fetch user back from tequila.
        Validate the key from tequila.
        Log user.
        '''
        if not 'key' in kw:
            raise redirect(came_from)

        # take parameters
        key = kw.get('key')
        environ = request.environ
        authentication_plugins = environ['repoze.who.plugins']
        identifier = authentication_plugins['ticket']
        secret = identifier.secret
        cookiename = identifier.cookie_name
        remote_addr = environ['REMOTE_ADDR']
        # get user
        principal = tequila.validate_key(key, 'tequila.epfl.ch')
        if principal is None:
            raise redirect('./login')
        tmp_user = self.build_user(principal)
        mail = tmp_user.email
        # log or create him
        user = DBSession.query(User).filter(
            User.email == tmp_user.email).first()
        if user is None:
            user_group = DBSession.query(Group).filter(
                Group.id == constants.group_users_id).first()
            user_group.users.append(tmp_user)
            DBSession.add(tmp_user)
            DBSession.flush()
            #transaction.commit()
            user = DBSession.query(User).filter(User.email == mail).first()
            flash('Your account has been created')
            DBSession.flush()
            self.build_circles_with_user(tmp_user, principal)
            DBSession.flush()
            #transaction.commit()
        elif user.name == constants.tmp_user_name:
            user.name = tmp_user.name
            user.firstname = tmp_user.firstname
            user._set_date(datetime.datetime.now())
            #user_group = DBSession.query(Group).filter(Group.id == constants.group_users_id).first()
            #user_group.users.append(tmp_user)
            flash('Your account has been created')
            DBSession.flush()
            self.build_circles_with_user(tmp_user, principal, user)
            DBSession.flush()
            #transaction.commit()
        else:
            flash('Welcome back', 'notice')
            self.check_circles_with_user(user, principal)

        # look if an user is admin or not
        admins = tg.config.get('admin.mails')
        group_admins = DBSession.query(Group).filter(
            Group.id == constants.group_admins_id).first()
        if user.email in admins:
            user not in group_admins.users and group_admins.users.append(user)
        else:
            user in group_admins.users and group_admins.users.remove(user)
        DBSession.flush()
        # create the authentication ticket
        user = DBSession.query(User).filter(User.email == mail).first()

        userdata = "%s|%s" % (user.id, user in group_admins.users)

        ticket = auth_tkt.AuthTicket(secret,
                                     user.email,
                                     remote_addr,
                                     tokens=token,
                                     user_data=userdata,
                                     time=None,
                                     cookie_name=cookiename,
                                     secure=True)
        val = ticket.cookie_value()
        # set it in the cookies
        response.set_cookie(cookiename,
                            value=val,
                            max_age=None,
                            path='/',
                            domain=None,
                            secure=False,
                            httponly=False,
                            comment=None,
                            expires=None,
                            overwrite=False)
        transaction.commit()
        raise redirect(came_from)
Exemple #32
0
    def callback(self, *args, **kw):
        print kw
        job = DBSession.query(Job).filter(
            Job.ext_task_id == kw['task_id']).first()
        project = DBSession.query(Project).filter(
            Project.id == int(kw['pid'])).first()
        if project is None or project.key != str(kw['pkey']):
            raise Exception('Project not valid')
        if job.project_id != project.id:
            raise Exception('Job not valid')

        status = str(kw['status'])
        job.status = status
        if status.lower() in ['error', 'failed']:
            job.traceback = kw['error']
        elif status == 'SUCCESS':
            results = json.loads(kw['results'])
            for result in results:
                bres = Bresults()
                bres.job_id = job.id
                bres.output_type = str(result.get('type', 'not defined'))
                bres.is_file = result.get('is_file', False)
                path = str(result.get('path', ''))
                bres.path = path
                bres.data = str(result.get('value', ''))

                is_track = result.get('type', '') == 'track'
                if is_track:
                    out = os.path.join(filemanager.temporary_directory(),
                                       os.path.split(path)[-1])
                    fileinfo = filemanager.FileInfo(inputtype='fsys',
                                                    inpath=path,
                                                    outpath=out,
                                                    admin=False)
                    sequence = project.sequence
                    user = DBSession.query(User).filter(
                        User.key == str(kw['key'])).first()
                    if user.email != str(kw['mail']):
                        raise Exception("Wrong user")
                    user_info = {
                        'id': user.id,
                        'name': user.name,
                        'email': user.email
                    }
                    sequence_info = {'id': sequence.id, 'name': sequence.name}

                    t = Track()
                    t.name = fileinfo.trackname
                    t.sequence_id = sequence.id
                    t.user_id = user.id
                    DBSession.add(t)
                    DBSession.flush()
                    async = tasks.new_input.delay(user_info, fileinfo,
                                                  sequence_info, t.id,
                                                  project.id)
                    t.task_id = async .task_id
                    bres.track_id = t.id

                bres.is_track = is_track
                DBSession.add(bres)
        DBSession.flush()
        return {}
Exemple #33
0
    def auth(self, came_from='/', **kw):
        '''
        Fetch user back from tequila.
        Validate the key from tequila.
        Log user.
        '''
        if not 'key' in kw:
            raise redirect(came_from)

        # take parameters
        key = kw.get('key')
        environ = request.environ
        authentication_plugins = environ['repoze.who.plugins']
        identifier = authentication_plugins['ticket']
        secret = identifier.secret
        cookiename = identifier.cookie_name
        remote_addr = environ['REMOTE_ADDR']
        # get user
        principal = tequila.validate_key(key, 'tequila.epfl.ch')
        if principal is None:
            raise redirect('./login')
        tmp_user = self.build_user(principal)
        mail = tmp_user.email
        # log or create him
        user = DBSession.query(User).filter(User.email == tmp_user.email).first()
        if user is None:
            user_group = DBSession.query(Group).filter(Group.id == constants.group_users_id).first()
            user_group.users.append(tmp_user)
            DBSession.add(tmp_user)
            DBSession.flush()
            #transaction.commit()
            user = DBSession.query(User).filter(User.email == mail).first()
            flash('Your account has been created')
            DBSession.flush()
            self.build_circles_with_user(tmp_user, principal)
            DBSession.flush()
            #transaction.commit()
        elif user.name == constants.tmp_user_name:
            user.name = tmp_user.name
            user.firstname = tmp_user.firstname
            user._set_date(datetime.datetime.now())
            #user_group = DBSession.query(Group).filter(Group.id == constants.group_users_id).first()
            #user_group.users.append(tmp_user)
            flash('Your account has been created')
            DBSession.flush()
            self.build_circles_with_user(tmp_user, principal, user)
            DBSession.flush()
            #transaction.commit()
        else:
            flash('Welcome back', 'notice')
            self.check_circles_with_user(user, principal)

        # look if an user is admin or not
        admins = tg.config.get('admin.mails')
        group_admins = DBSession.query(Group).filter(Group.id == constants.group_admins_id).first()
        if user.email in admins:
            user not in group_admins.users and group_admins.users.append(user)
        else:
            user in group_admins.users and group_admins.users.remove(user)
        DBSession.flush()
        # create the authentication ticket
        user = DBSession.query(User).filter(User.email == mail).first()

        userdata = "%s|%s" % (user.id, user in group_admins.users)

        ticket = auth_tkt.AuthTicket(
                                       secret, user.email, remote_addr, tokens=token,
                                       user_data=userdata, time=None, cookie_name=cookiename,
                                       secure=True)
        val = ticket.cookie_value()
        # set it in the cookies
        response.set_cookie(
                     cookiename,
                     value=val,
                     max_age=None,
                     path='/',
                     domain=None,
                     secure=False,
                     httponly=False,
                     comment=None,
                     expires=None,
                     overwrite=False)
        transaction.commit()
        raise redirect(came_from)