Ejemplo n.º 1
0
def view_collection():
    params = request.args

    uc_id = params['uc_id']
    uc = (collection for collection in g.user_session.user_collections
          if collection["id"] == uc_id).next()
    traits = uc["members"]

    trait_obs = []
    json_version = []

    for atrait in traits:
        name, dataset_name = atrait.split(':')
        if dataset_name == "Temp":
            group = name.split("_")[2]
            dataset = create_dataset(dataset_name,
                                     dataset_type="Temp",
                                     group_name=group)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
        else:
            dataset = create_dataset(dataset_name)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
            trait_ob = trait.retrieve_trait_info(trait_ob,
                                                 dataset,
                                                 get_qtl_info=True)
        trait_obs.append(trait_ob)

        json_version.append(trait.jsonable(trait_ob))

    collection_info = dict(trait_obs=trait_obs, uc=uc)

    if "json" in params:
        return json.dumps(json_version)
    else:
        return render_template("collections/view.html", **collection_info)
def view_collection():
    params = request.args
    print("PARAMS in view collection:", params)

    if "uc_id" in params:
        uc_id = params['uc_id']
        uc = model.UserCollection.query.get(uc_id)
        traits = json.loads(uc.members)
    else:
        user_collections = json.loads(Redis.get(user_manager.AnonUser().key))
        this_collection = {}
        for collection in user_collections:
            if collection['id'] == params['collection_id']:
                this_collection = collection
                break
        #this_collection = user_collections[params['collection_id']]
        traits = this_collection['members']

    print("in view_collection traits are:", traits)

    trait_obs = []
    json_version = []

    for atrait in traits:
        name, dataset_name = atrait.split(':')
        if dataset_name == "Temp":
            group = name.split("_")[2]
            dataset = create_dataset(dataset_name, dataset_type = "Temp", group_name = group)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
        else:
            dataset = create_dataset(dataset_name)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
            trait_ob = trait.retrieve_trait_info(trait_ob, dataset, get_qtl_info=True)
        trait_obs.append(trait_ob)

        json_version.append(trait.jsonable(trait_ob))

    if "uc_id" in params:
        collection_info = dict(trait_obs=trait_obs,
                               uc = uc)
    else:
        collection_info = dict(trait_obs=trait_obs,
                               collection_name=this_collection['name'])
    if "json" in params:
        print("json_version:", json_version)
        return json.dumps(json_version)
    else:
        return render_template("collections/view.html",
                           **collection_info
                           )
Ejemplo n.º 3
0
    def gen_search_result(self):
        """
        Get the info displayed in the search result table from the set of results computed in
        the "search" function

        """
        self.trait_list = []
        json_trait_list = []

        species = webqtlDatabaseFunction.retrieve_species(
            self.dataset.group.name)
        # result_set represents the results for each search term; a search of
        # "shh grin2b" would have two sets of results, one for each term
        logger.debug("self.results is:", pf(self.results))
        for index, result in enumerate(self.results):
            if not result:
                continue

            #### Excel file needs to be generated ####

            #logger.debug("foo locals are:", locals())
            trait_id = result[0]
            this_trait = trait.GeneralTrait(dataset=self.dataset,
                                            name=trait_id,
                                            get_qtl_info=True,
                                            get_sample_info=False)
            self.trait_list.append(this_trait)
            json_trait_list.append(
                trait.jsonable_table_row(this_trait, self.dataset.name,
                                         index + 1))

        self.json_trait_list = json.dumps(json_trait_list)
Ejemplo n.º 4
0
def view_collection():
    params = request.args

    if g.user_session.logged_in and "uc_id" in params:
        uc_id = params['uc_id']
        uc = (collection for collection in g.user_session.user_collections if collection["id"] == uc_id).next()
        traits = uc["members"]
    else:
        user_collections = json.loads(Redis.get(user_manager.AnonUser().key))
        this_collection = {}
        for collection in user_collections:
            if collection['id'] == params['collection_id']:
                this_collection = collection
                break

        traits = this_collection['members']

    trait_obs = []
    json_version = []

    for atrait in traits:
        name, dataset_name = atrait.split(':')
        if dataset_name == "Temp":
            group = name.split("_")[2]
            dataset = create_dataset(dataset_name, dataset_type = "Temp", group_name = group)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
        else:
            dataset = create_dataset(dataset_name)
            trait_ob = trait.GeneralTrait(name=name, dataset=dataset)
            trait_ob = trait.retrieve_trait_info(trait_ob, dataset, get_qtl_info=True)
        trait_obs.append(trait_ob)

        json_version.append(trait.jsonable(trait_ob))

    if "uc_id" in params:
        collection_info = dict(trait_obs=trait_obs,
                               uc = uc)
    else:
        collection_info = dict(trait_obs=trait_obs,
                               collection_name=this_collection['name'])
    if "json" in params:
        return json.dumps(json_version)
    else:
        return render_template("collections/view.html",
                           **collection_info
                           )
Ejemplo n.º 5
0
def view_collection():
    params = request.args
    print("PARAMS in view collection:", params)

    if "uc_id" in params:
        uc_id = params['uc_id']
        uc = model.UserCollection.query.get(uc_id)
        traits = json.loads(uc.members)
        print("traits are:", traits)
    else:
        traits = AnonCollection().get_traits()

    print("in view_collection traits are:", traits)

    trait_obs = []
    json_version = []

    for atrait in traits:
        print("atrait is:", atrait)
        name, dataset_name = atrait.split(':')

        trait_ob = trait.GeneralTrait(name=name, dataset_name=dataset_name)
        trait_ob.retrieve_info(get_qtl_info=True)
        trait_ob.get_info()
        trait_obs.append(trait_ob)

        json_version.append(trait_ob.jsonable())
        #json_version.append(dict(name=trait_ob.name,
        #                         description=trait_ob.description_display,
        #                         location=trait_ob.location_repr,
        #                         mean=trait_ob.mean,
        #                         lrs_score=trait_ob.LRS_score_repr,
        #                         lrs_location=trait_ob.LRS_location_repr))
        #                         dis=trait_ob.description))
        #json_version.append(trait_ob.__dict__th)

    print("trait_obs:", trait_obs)

    if "uc_id" in params:
        collection_info = dict(trait_obs=trait_obs, uc=uc)
    else:
        collection_info = dict(trait_obs=trait_obs)
    if "json" in params:
        print("json_version:", json_version)
        return json.dumps(json_version)
    else:
        return render_template("collections/view.html", **collection_info)
    def run_analysis(self, requestform):
        print("Starting CTL analysis on dataset")
        self.trait_db_list = [trait.strip() for trait in requestform['trait_list'].split(',')]
        self.trait_db_list = [x for x in self.trait_db_list if x]

        print("strategy:", requestform.get("strategy"))
        strategy = requestform.get("strategy")

        print("nperm:", requestform.get("nperm"))
        nperm = int(requestform.get("nperm"))

        print("parametric:", requestform.get("parametric"))
        parametric = bool(requestform.get("parametric"))

        print("significance:", requestform.get("significance"))
        significance = float(requestform.get("significance"))

        # Get the name of the .geno file belonging to the first phenotype
        datasetname = self.trait_db_list[0].split(":")[1]
        dataset = data_set.create_dataset(datasetname)

        genofilelocation = locate(dataset.group.name + ".geno", "genotype")
        parser = genofile_parser.ConvertGenoFile(genofilelocation)
        parser.process_csv()
        print(dataset.group)
        # Create a genotype matrix
        individuals = parser.individuals
        markers = []
        markernames = []
        for marker in parser.markers:
          markernames.append(marker["name"])
          markers.append(marker["genotypes"])

        genotypes = list(itertools.chain(*markers))
        print(len(genotypes) / len(individuals), "==", len(parser.markers))

        rGeno = r_t(ro.r.matrix(r_unlist(genotypes), nrow=len(markernames), ncol=len(individuals), dimnames = r_list(markernames, individuals), byrow=True))

        # Create a phenotype matrix
        traits = []
        for trait in self.trait_db_list:
          print("retrieving data for", trait)
          if trait != "":
            ts = trait.split(':')
            gt = TRAIT.GeneralTrait(name = ts[0], dataset_name = ts[1])
            gt = TRAIT.retrieve_sample_data(gt, dataset, individuals)
            for ind in individuals:
              if ind in gt.data.keys():
                traits.append(gt.data[ind].value)
              else:
                traits.append("-999")

        rPheno = r_t(ro.r.matrix(r_as_numeric(r_unlist(traits)), nrow=len(self.trait_db_list), ncol=len(individuals), dimnames = r_list(self.trait_db_list, individuals), byrow=True))

        print(rPheno)

        # Use a data frame to store the objects
        rPheno = r_data_frame(rPheno, check_names = False)
        rGeno = r_data_frame(rGeno, check_names = False)

        # Debug: Print the genotype and phenotype files to disk
        #r_write_table(rGeno, "~/outputGN/geno.csv")
        #r_write_table(rPheno, "~/outputGN/pheno.csv")

        # Perform the CTL scan
        res = self.r_CTLscan(rGeno, rPheno, strategy = strategy, nperm = nperm, parametric = parametric, ncores = 6)

        # Get significant interactions
        significant = self.r_CTLsignificant(res, significance = significance)

        # Create an image for output
        self.results = {}
        self.results['imgurl1'] = webqtlUtil.genRandStr("CTLline_") + ".png"
        self.results['imgloc1'] = GENERATED_IMAGE_DIR + self.results['imgurl1']

        self.results['ctlresult'] = significant
        self.results['requestform'] = requestform             # Store the user specified parameters for the output page

        # Create the lineplot
        r_png(self.results['imgloc1'], width=1000, height=600, type='cairo-png')
        self.r_lineplot(res, significance = significance)
        r_dev_off()

        n = 2                                                 # We start from 2, since R starts from 1 :)
        for trait in self.trait_db_list:
          # Create the QTL like CTL plots
          self.results['imgurl' + str(n)] = webqtlUtil.genRandStr("CTL_") + ".png"
          self.results['imgloc' + str(n)] = GENERATED_IMAGE_DIR + self.results['imgurl' + str(n)]
          r_png(self.results['imgloc' + str(n)], width=1000, height=600, type='cairo-png')
          self.r_plotCTLobject(res, (n-1), significance = significance, main='Phenotype ' + trait)
          r_dev_off()
          n = n + 1

        # Flush any output from R
        sys.stdout.flush()

        # Create the interactive graph for cytoscape visualization (Nodes and Edges)
        print(type(significant))
        if not type(significant) == ri.RNULLType:
          for x in range(len(significant[0])):
            print(significant[0][x], significant[1][x], significant[2][x])            # Debug to console
            tsS = significant[0][x].split(':')                                        # Source
            tsT = significant[2][x].split(':')                                        # Target
            gtS = TRAIT.GeneralTrait(name = tsS[0], dataset_name = tsS[1])            # Retrieve Source info from the DB
            gtT = TRAIT.GeneralTrait(name = tsT[0], dataset_name = tsT[1])            # Retrieve Target info from the DB
            self.addNode(gtS)
            self.addNode(gtT)
            self.addEdge(gtS, gtT, significant, x)

            significant[0][x] = gtS.symbol + " (" + gtS.name + ")"                    # Update the trait name for the displayed table
            significant[2][x] = gtT.symbol + " (" + gtT.name + ")"                    # Update the trait name for the displayed table

        self.elements = json.dumps(self.nodes_list + self.edges_list)
Ejemplo n.º 7
0
    def gen_search_result(self):
        """
        Get the info displayed in the search result table from the set of results computed in
        the "search" function

        """
        trait_list = []
        json_trait_list = []

        species = webqtlDatabaseFunction.retrieve_species(
            self.dataset.group.name)
        # result_set represents the results for each search term; a search of
        # "shh grin2b" would have two sets of results, one for each term
        logger.debug("self.results is:", pf(self.results))

        for index, result in enumerate(self.results):
            if not result:
                continue

            #### Excel file needs to be generated ####

            trait_dict = {}
            trait_id = result[0]
            trait_dict['index'] = index + 1
            this_trait = trait.GeneralTrait(dataset=self.dataset,
                                            name=trait_id,
                                            get_qtl_info=True,
                                            get_sample_info=False)
            trait_dict['name'] = this_trait.name
            if this_trait.dataset.type == "Publish":
                trait_dict['display_name'] = this_trait.display_name
            else:
                trait_dict['display_name'] = this_trait.name
            trait_dict['dataset'] = this_trait.dataset.name
            trait_dict['hmac'] = hmac.data_hmac('{}:{}'.format(
                this_trait.name, this_trait.dataset.name))
            if this_trait.dataset.type == "ProbeSet":
                trait_dict['symbol'] = this_trait.symbol
                trait_dict[
                    'description'] = this_trait.description_display.decode(
                        'utf-8', 'replace')
                trait_dict['location'] = this_trait.location_repr
                trait_dict['mean'] = "N/A"
                trait_dict['additive'] = "N/A"
                if this_trait.mean != "" and this_trait.mean != None:
                    trait_dict['mean'] = '%.3f' % this_trait.mean
                trait_dict['lrs_score'] = this_trait.LRS_score_repr
                trait_dict['lrs_location'] = this_trait.LRS_location_repr
                if this_trait.additive != "":
                    trait_dict['additive'] = '%.3f' % this_trait.additive
            elif this_trait.dataset.type == "Geno":
                trait_dict['location'] = this_trait.location_repr
            elif this_trait.dataset.type == "Publish":
                trait_dict['description'] = this_trait.description_display
                trait_dict['authors'] = this_trait.authors
                trait_dict['pubmed_id'] = "N/A"
                if this_trait.pubmed_id:
                    trait_dict['pubmed_id'] = this_trait.pubmed_id
                    trait_dict['pubmed_link'] = this_trait.pubmed_link
                trait_dict['pubmed_text'] = this_trait.pubmed_text
                trait_dict['mean'] = "N/A"
                if this_trait.mean != "" and this_trait.mean != None:
                    trait_dict['mean'] = '%.3f' % this_trait.mean
                trait_dict['lrs_score'] = this_trait.LRS_score_repr
                trait_dict['lrs_location'] = this_trait.LRS_location_repr
                trait_dict['additive'] = "N/A"
                if this_trait.additive != "":
                    trait_dict['additive'] = '%.3f' % this_trait.additive
            trait_list.append(trait_dict)
            #json_trait_list.append(trait.jsonable_table_row(this_trait, self.dataset.name, index + 1))

        self.trait_list = json.dumps(trait_list)