Exemplo n.º 1
0
def annotation_start():
    """Selects a gene in MongoDB and imports it in the Annotation track

    Returns
    -------
    json
        url: url for the apollo window, centered on the gene position
        attributes: chromosome on which the gene is
    """

    DataInstance = Data(ca, session)
    current_gene = DataInstance.get_current_annotation(
        session["user"]["username"])

    if not current_gene:
        all_positions = DataInstance.get_all_positions()
        level = DataInstance.get_user_level(session["user"]["username"])
        restrict_positions = DataInstance.select_genes(level, all_positions)

        # FIXME error if no gene in db
        selected_item = Utils.get_random_items(1, restrict_positions)[0]
        ca.logger.info("Selected gene: {}".format(selected_item))

        db = ca.mongo.db
        fs = gridfs.GridFS(
            db, collection="genes"
        )  # FIXME not sure we really need gridfs (small chunks of gff)
        gff_file = fs.get(selected_item["_id"])

        gff_str = StringIO()
        gff_str.write(gff_file.read().decode())

        gff_str.seek(0)

        ca.logger.info("Loading gff: {}".format(gff_str.read()))

        gff_str.seek(0)

        apollo = ApolloInstance(ca.apollo_url, ca.apollo_admin_email,
                                ca.apollo_admin_password)
        apollo.annotations.load_gff3(
            "%s_%s" % (ca.apollo_org_id, session["user"]["email"]), gff_str)

        time.sleep(1)

        url = "%s/annotator/loadLink?loc=%s:%s..%s&organism=%s_%s" % (
            ca.apollo_url_ext, selected_item["chromosome"],
            selected_item["start"], selected_item["end"], ca.apollo_org_id,
            session["user"]["email"])
        DataInstance.update_current_annotation(session["user"]["username"],
                                               selected_item)
    else:
        selected_item = DataInstance.get_current_annotation(
            session["user"]["username"])
        url = "%s/annotator/loadLink?loc=%s:%s..%s&organism=%s_%s" % (
            ca.apollo_url_ext, selected_item["chromosome"],
            selected_item["start"], selected_item["end"], ca.apollo_org_id,
            session["user"]["email"])

    return {'url': url}
Exemplo n.º 2
0
def get_groups_names():
    """ Get groups names

    Returns
    -------
        dict
            error, error message and list of groups names
    """
    dataInstance = Data(ca, session)
    result = dataInstance.get_groups_names()
    return result
Exemplo n.º 3
0
def set_validated():
    """
    """
    data = request.get_json()
    dataInstance = Data(ca, session)
    gene = dataInstance.set_validated(data["gene"], data["newstatus"])
    if gene["isValidated"] == data["newstatus"]:
        result = {'error': False, 'errorMessage': ""}
    else:
        result = {'error': True, 'errorMessage': "No Update!"}

    return result
Exemplo n.º 4
0
def set_groups_amount():
    """Update the number of groups

    Returns
    -------
    dict
        error, error message and updated number of groups
    """
    data = request.get_json()
    dataInstance = Data(ca, session)
    result = dataInstance.set_number_of_groups(data)
    return result
Exemplo n.º 5
0
def update_group_name():
    """Update the name of a group

    Returns
    -------
    dict
        error, error message and group name
    """
    data = request.get_json()
    dataInstance = Data(ca, session)
    result = dataInstance.update_group_name(data)
    return result
Exemplo n.º 6
0
def get_groups_amount():
    """get the number of groups in the database

    Returns
    -------
    int
        Number of groups in the database
    """
    dataInstance = Data(ca, session)
    groupsAmount = dataInstance.get_number_of_groups()
    result = {'error': False, 'errorMessage': "", 'groupsAmount': groupsAmount}
    return result
Exemplo n.º 7
0
def save_level():
    """
    """
    data = request.get_json()
    dataInstance = Data(current_app, session)
    result = dataInstance.save_level(data)  # TODO not implemented, code lost

    return {
        'truc': result,
        'error': False,
        'errorMessage': "",
    }
Exemplo n.º 8
0
def count_all_genes():
    """Get the quantity of genes to annotate

    Returns
    -------
        dict
            error, error message and quantity of genes to annotate
    """
    error = False
    errorMessage = []
    dataInstance = Data(ca, session)
    result = dataInstance.count_all_genes()
    return {'error': error, 'errorMessage': errorMessage, 'genes': result}
Exemplo n.º 9
0
def admin_get_genes():
    """ Gets all the genes in the database to display them in the admin tab

    Returns
    -------

    json
        error : Boolean
        errorMessage : str
        genes : List
    """
    dataInstance = Data(ca, session)
    gene_list = dataInstance.get_all_positions()
    result = {'error': False, 'errorMessage': "", 'genes': gene_list}
    return result
Exemplo n.º 10
0
def validation():
    """Selects a gene from the answers and imports it in the Annotation track

    Returns
    -------
    json
        url: url for the apollo window, centered on the gene position
        attributes: chromosome on which the gene is
        gene_id : current gene id
    """

    DataInstance = Data(ca, session)
    all_positions = DataInstance.get_not_validated()

    # FIXME error if no gene in db
    selected_item = all_positions[0]
    ca.logger.info("Selected gene: {}".format(selected_item))

    db = ca.mongo.db
    fs = gridfs.GridFS(
        db, collection="answers"
    )  # FIXME not sure we really need gridfs (small chunks of gff)
    gff_file = fs.get(selected_item["_id"])

    gff_str = StringIO()
    gff_str.write(gff_file.read().decode())

    gff_str.seek(0)

    ca.logger.info("Loading gff: {}".format(gff_str.read()))

    gff_str.seek(0)

    apollo = ApolloInstance(ca.apollo_url, ca.apollo_admin_email,
                            ca.apollo_admin_password)
    apollo.organisms.delete_features(
        "%s_%s" % (ca.apollo_org_id, session["user"]["email"]))
    apollo.annotations.load_gff3(
        "%s_%s" % (ca.apollo_org_id, session["user"]["email"]), gff_str)

    time.sleep(1)

    url = "%s/annotator/loadLink?loc=%s:%s..%s&organism=%s_%s" % (
        ca.apollo_url_ext, selected_item["chromosome"], selected_item["start"],
        selected_item["end"], ca.apollo_org_id, session["user"]["email"])
    DataInstance.update_current_annotation(session["user"]["username"],
                                           selected_item)
    return {'url': url, 'gene_id': selected_item["_id"]}
Exemplo n.º 11
0
def get_answers_amount():
    """get the number of annotations in the database

    Return
    ------
    int
        Number of annotations
    """
    dataInstance = Data(ca, session)
    answers_amount = dataInstance.get_number_of_answers()
    result = {
        'error': False,
        'errorMessage': "",
        'answersAmount': answers_amount
    }
    return result
Exemplo n.º 12
0
def set_annotable():
    """ set the received gene as Annotable in the database

    Returns
    -------

    json
        error : Boolean
        errorMessage : str
    """
    data = request.get_json()
    dataInstance = Data(ca, session)
    gene = dataInstance.set_annotable(data["gene"], data["newstatus"])
    if gene["isAnnotable"] == data["newstatus"]:
        result = {'error': False, 'errorMessage': ""}
    else:
        result = {'error': True, 'errorMessage': "No Update!"}

    return result
Exemplo n.º 13
0
def get_level():
    """Get information about levels

    Returns
    -------
    json
    """
    try:
        data_instance = Data(current_app, session)
        all_levels = data_instance.get_level(
        )  # TODO not implemented, code lost
    except Exception as e:
        traceback.print_exc(file=sys.stdout)
        return jsonify({
            'levels': [],
            'error': True,
            'errorMessage': str(e)
        }), 500

    return jsonify({'levels': all_levels, 'error': False, 'errorMessage': ''})
Exemplo n.º 14
0
def annotation_end():
    """gets the new annotation and saves it in mongodb"""
    DataInstance = Data(ca, session)
    current_gene = DataInstance.get_current_annotation(
        session["user"]["username"])

    apollo = ApolloInstance(ca.apollo_url, ca.apollo_admin_email,
                            ca.apollo_admin_password)
    features = apollo.annotations.get_features(
        organism="%s_%s" % (ca.apollo_org_id, session["user"]["email"]),
        sequence=current_gene["chromosome"])["features"]

    gff_file = apollo.annotations.get_gff3(
        features[0]["uniquename"],
        "%s_%s" % (ca.apollo_org_id, session["user"]["email"]))

    DataInstance.store_answers_from_user(session["user"]["username"], gff_file)
    apollo.organisms.delete_features(
        "%s_%s" % (ca.apollo_org_id, session["user"]["email"]))

    return {'error': False, 'errorMessage': 'no error'}
Exemplo n.º 15
0
def get_groups():
    """Get all groups

    Returns
    -------
    json
        users: list of all groups info
        error: True if error, else False
        errorMessage: the error message of error, else an empty string
    """
    try:
        data_instance = Data(current_app, session)
        all_groups = data_instance.get_all_groups()
    except Exception as e:
        traceback.print_exc(file=sys.stdout)
        return jsonify({
            'groups': [],
            'error': True,
            'errorMessage': str(e)
        }), 500

    return jsonify({'groups': all_groups, 'error': False, 'errorMessage': ''})
Exemplo n.º 16
0
def remove_all_genes_from_db():
    """Remove all genes from de db

    Return
    ------
    json
        error : Boolean
        errorMessage: str
    """
    dataInstance = Data(ca, session)
    gene_list = dataInstance.get_all_positions()

    db = ca.mongo.db
    fs = gridfs.GridFS(db, collection="genes")

    for element in gene_list:
        fs.delete(element['_id'])

    result = {
        'error': False,
        'errorMessage': "",
    }
    return result
Exemplo n.º 17
0
def get_top_annotation():
    dataInstance = Data(ca, session)
    result = dataInstance.get_top_annotation()
    return result
Exemplo n.º 18
0
def get_all_answers():
    dataInstance = Data(ca, session)
    answers = dataInstance.get_all_answers()
    result = {'error': False, 'errorMessage': "", 'answer': answers}
    return result
Exemplo n.º 19
0
def create_app(config='config/genocrowd.ini',
               app_name='genocrowd',
               blueprints=None):
    """Create the Genocrowd app

    Parameters
    ----------
    config : str, optional
        Path to the config file
    app_name : str, optional
        Application name
    blueprints : None, optional
        Flask blueprints

    Returns
    -------
    Flask
        Genocrowd Flask application
    """
    conf = configparser.ConfigParser()
    conf.read(config)
    sentry_dsn = None
    try:
        sentry_dsn = conf['sentry']['server_dsn']
    except Exception:
        pass
    if sentry_dsn:
        version = get_distribution('genocrowd').version
        name = get_distribution('genocrowd').project_name
        sentry_sdk.init(dsn=sentry_dsn,
                        release="{}@{}".format(name, version),
                        integrations=[FlaskIntegration(),
                                      CeleryIntegration()])
    app = Flask(app_name, static_folder='static', template_folder='templates')
    app.iniconfig = FlaskIni()
    with app.app_context():
        app.iniconfig.read(config)
        proxy_path = None
        try:
            proxy_path = app.iniconfig.get('genocrowd', 'reverse_proxy_path')
            app.config['REVERSE_PROXY_PATH'] = proxy_path
        except Exception:
            pass
        mongo_dbname = app.iniconfig.get('flask', 'mongo_dbname')
        app.config['MONGO_DBNAME'] = mongo_dbname
        mongo_uri = app.iniconfig.get('flask', 'mongo_uri')
        app.config['MONGO_URI'] = mongo_uri
        if not mongo_uri:
            raise Exception("Missing mongo_uri in config file")
        if not mongo_dbname:
            raise Exception("Missing mongo_dbname in config file")
        app.mongo = PyMongo(app)
        app.bcrypt = Bcrypt(app)
        users = app.mongo.db.users
        app.mongo.db.genes
        app.mongo.db.answers
        groups = app.mongo.db.groups

        app.genocrowd_admin_email = app.iniconfig.get('genocrowd',
                                                      'genocrowd_admin_email')
        app.genocrowd_admin_password = app.iniconfig.get(
            'genocrowd', 'genocrowd_admin_password')

        app.apollo_admin_email = app.iniconfig.get('genocrowd',
                                                   'apollo_admin_email')
        app.apollo_admin_password = app.iniconfig.get('genocrowd',
                                                      'apollo_admin_password')
        app.apollo_dataset_path = app.iniconfig.get('genocrowd',
                                                    'apollo_dataset_path')
        app.apollo_org_id = app.iniconfig.get('genocrowd', 'apollo_org_id')
        app.apollo_url = app.iniconfig.get('genocrowd', 'apollo_url')
        # We don't want ending slash
        if app.apollo_url.endswith("/"):
            app.apollo_url = app.apollo_url[:-1]

        app.apollo_url_ext = app.iniconfig.get('genocrowd', 'apollo_url_ext')
        # We don't want ending slash
        if app.apollo_url_ext.endswith("/"):
            app.apollo_url_ext = app.apollo_url_ext[:-1]

        configure_logging(app)

        if users.find_one() is None:
            # Create default admin user
            local_auth = LocalAuth(app, None)
            local_auth.add_user_to_database('admin', app.genocrowd_admin_email,
                                            app.genocrowd_admin_password,
                                            'admin', 'admin')

        if blueprints is None:
            blueprints = BLUEPRINTS

        for blueprint in blueprints:
            app.register_blueprint(blueprint)

        if groups.find_one() is None:
            # Initiate the groups database
            data = Data(app, None)
            data.initiate_groups()

    if proxy_path:
        ReverseProxyPrefixFix(app)
    return app