about summary refs log tree commit diff
path: root/wqflask/base
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/base')
-rw-r--r--wqflask/base/data_set.py44
-rw-r--r--wqflask/base/trait.py245
-rw-r--r--wqflask/base/webqtlConfig.py5
3 files changed, 157 insertions, 137 deletions
diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py
index cab708ef..2272b6ee 100644
--- a/wqflask/base/data_set.py
+++ b/wqflask/base/data_set.py
@@ -65,6 +65,9 @@ logger = getLogger(__name__ )
 DS_NAME_MAP = {}
 
 def create_dataset(dataset_name, dataset_type = None, get_samplelist = True, group_name = None):
+    if dataset_name == "Temp":
+        dataset_type = "Temp"
+
     if not dataset_type:
         dataset_type = Dataset_Getter(dataset_name)
 
@@ -131,7 +134,7 @@ Publish or ProbeSet. E.g.
                                 ProbeSetFreeze.Name = "{0}"
                             """.format(name)
 
-            results = g.db.execute(geno_query).fetchall()
+            results = g.db.execute(mrna_expr_query).fetchall()
             if len(results):
                 self.datasets[name] = "ProbeSet"
                 Redis.set("dataset_structure", json.dumps(self.datasets))
@@ -165,12 +168,11 @@ Publish or ProbeSet. E.g.
 
             geno_query =    """
                                 SELECT
-                                    GenoFreezeId
+                                    GenoFreeze.Id
                                 FROM
                                     GenoFreeze
                                 WHERE
                                     GenoFreeze.Name = "{0}"
-                                {1}
                             """.format(name)
 
             results = g.db.execute(geno_query).fetchall()
@@ -487,25 +489,18 @@ class DatasetGroup(object):
 
 def datasets(group_name, this_group = None):
     key = "group_dataset_menu:v2:" + group_name
-    logger.debug("key is2:", key)
     dataset_menu = []
-    logger.debug("[tape4] webqtlConfig.PUBLICTHRESH:", webqtlConfig.PUBLICTHRESH)
-    logger.debug("[tape4] type webqtlConfig.PUBLICTHRESH:", type(webqtlConfig.PUBLICTHRESH))
     the_results = fetchall('''
          (SELECT '#PublishFreeze',PublishFreeze.FullName,PublishFreeze.Name
           FROM PublishFreeze,InbredSet
           WHERE PublishFreeze.InbredSetId = InbredSet.Id
             and InbredSet.Name = '%s'
-            and PublishFreeze.public > %s
-            and PublishFreeze.confidentiality < 1
           ORDER BY PublishFreeze.Id ASC)
          UNION
          (SELECT '#GenoFreeze',GenoFreeze.FullName,GenoFreeze.Name
           FROM GenoFreeze, InbredSet
           WHERE GenoFreeze.InbredSetId = InbredSet.Id
-            and InbredSet.Name = '%s'
-            and GenoFreeze.public > %s
-            and GenoFreeze.confidentiality < 1)
+            and InbredSet.Name = '%s')
          UNION
          (SELECT Tissue.Name, ProbeSetFreeze.FullName,ProbeSetFreeze.Name
           FROM ProbeSetFreeze, ProbeFreeze, InbredSet, Tissue
@@ -513,12 +508,10 @@ def datasets(group_name, this_group = None):
             and ProbeFreeze.TissueId = Tissue.Id
             and ProbeFreeze.InbredSetId = InbredSet.Id
             and InbredSet.Name like %s
-            and ProbeSetFreeze.public > %s
-            and ProbeSetFreeze.confidentiality < 1
           ORDER BY Tissue.Name, ProbeSetFreeze.OrderList DESC)
-        ''' % (group_name, webqtlConfig.PUBLICTHRESH,
-              group_name, webqtlConfig.PUBLICTHRESH,
-              "'" + group_name + "'", webqtlConfig.PUBLICTHRESH))
+        ''' % (group_name,
+              group_name,
+              "'" + group_name + "'"))
 
     sorted_results = sorted(the_results, key=lambda kv: kv[0])
 
@@ -638,29 +631,25 @@ class DataSet(object):
 
         """
 
-
         try:
             if self.type == "ProbeSet":
                 query_args = tuple(escape(x) for x in (
-                    str(webqtlConfig.PUBLICTHRESH),
                     self.name,
                     self.name,
                     self.name))
 
                 self.id, self.name, self.fullname, self.shortname, self.data_scale, self.tissue = fetch1("""
-SELECT ProbeSetFreeze.Id, ProbeSetFreeze.Name, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, ProbeSetFreeze.DataScale, Tissue.Name
-FROM ProbeSetFreeze, ProbeFreeze, Tissue
-WHERE ProbeSetFreeze.public > %s
-AND ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id
-AND ProbeFreeze.TissueId = Tissue.Id
-AND (ProbeSetFreeze.Name = '%s' OR ProbeSetFreeze.FullName = '%s' OR ProbeSetFreeze.ShortName = '%s')
+    SELECT ProbeSetFreeze.Id, ProbeSetFreeze.Name, ProbeSetFreeze.FullName, ProbeSetFreeze.ShortName, ProbeSetFreeze.DataScale, Tissue.Name
+    FROM ProbeSetFreeze, ProbeFreeze, Tissue
+    WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id
+    AND ProbeFreeze.TissueId = Tissue.Id
+    AND (ProbeSetFreeze.Name = '%s' OR ProbeSetFreeze.FullName = '%s' OR ProbeSetFreeze.ShortName = '%s')
                 """ % (query_args),"/dataset/"+self.name+".json",
             lambda r: (r["id"],r["name"],r["full_name"],r["short_name"],r["data_scale"],r["tissue"])
                 )
             else:
                 query_args = tuple(escape(x) for x in (
                     (self.type + "Freeze"),
-                    str(webqtlConfig.PUBLICTHRESH),
                     self.name,
                     self.name,
                     self.name))
@@ -669,9 +658,8 @@ AND (ProbeSetFreeze.Name = '%s' OR ProbeSetFreeze.FullName = '%s' OR ProbeSetFre
                 self.id, self.name, self.fullname, self.shortname = fetchone("""
                         SELECT Id, Name, FullName, ShortName
                         FROM %s
-                        WHERE public > %s AND
-                             (Name = '%s' OR FullName = '%s' OR ShortName = '%s')
-                  """ % (query_args))
+                        WHERE (Name = '%s' OR FullName = '%s' OR ShortName = '%s')
+                    """ % (query_args))
 
         except TypeError:
             logger.debug("Dataset {} is not yet available in GeneNetwork.".format(self.name))
diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py
index e454c593..8e11c11d 100644
--- a/wqflask/base/trait.py
+++ b/wqflask/base/trait.py
@@ -1,12 +1,12 @@
 from __future__ import absolute_import, division, print_function
 
+import os
 import string
 import resource
 import codecs
 import requests
-
-import redis
-Redis = redis.StrictRedis()
+import random
+import urllib
 
 from base import webqtlConfig
 from base.webqtlCaseData import webqtlCaseData
@@ -14,7 +14,10 @@ from base.data_set import create_dataset
 from db import webqtlDatabaseFunction
 from utility import webqtlUtil
 from utility import hmac
-from utility.tools import GN2_BASE_URL
+from utility.authentication_tools import check_resource_availability
+from utility.tools import GN2_BASE_URL, GN_VERSION
+from utility.redis_tools import get_redis_conn, get_resource_id, get_resource_info
+Redis = get_redis_conn()
 
 from wqflask import app
 
@@ -22,11 +25,36 @@ import simplejson as json
 from MySQLdb import escape_string as escape
 from pprint import pformat as pf
 
-from flask import Flask, g, request, url_for
+from flask import Flask, g, request, url_for, redirect, make_response, render_template
 
 from utility.logger import getLogger
 logger = getLogger(__name__ )
 
+def create_trait(**kw):
+    assert bool(kw.get('dataset')) != bool(kw.get('dataset_name')), "Needs dataset ob. or name";
+
+    permitted = True
+    if kw.get('name'):
+        if kw.get('dataset_name'):
+            if kw.get('dataset_name') != "Temp":
+                dataset = create_dataset(kw.get('dataset_name'))
+        else:
+            dataset = kw.get('dataset')
+
+        if kw.get('dataset_name') != "Temp":
+            if dataset.type == 'Publish':
+                permissions = check_resource_availability(dataset, kw.get('name'))
+            else:
+                permissions = check_resource_availability(dataset)
+
+    if "view" in permissions['data']:
+        the_trait = GeneralTrait(**kw)
+        if the_trait.dataset.type != "Temp":
+            the_trait = retrieve_trait_info(the_trait, the_trait.dataset, get_qtl_info=kw.get('get_qtl_info'))
+        return the_trait
+    else:
+        return None
+
 class GeneralTrait(object):
     """
     Trait class defines a trait in webqtl, can be either Microarray,
@@ -51,6 +79,7 @@ class GeneralTrait(object):
         self.haveinfo = kw.get('haveinfo', False)
         self.sequence = kw.get('sequence')         # Blat sequence, available for ProbeSet
         self.data = kw.get('data', {})
+        self.view = True
 
         # Sets defaults
         self.locus = None
@@ -76,8 +105,6 @@ class GeneralTrait(object):
 
         # Todo: These two lines are necessary most of the time, but perhaps not all of the time
         # So we could add a simple if statement to short-circuit this if necessary
-        if self.dataset.type != "Temp":
-            self = retrieve_trait_info(self, self.dataset, get_qtl_info=get_qtl_info)
         if get_sample_info != False:
             self = retrieve_sample_data(self, self.dataset)
 
@@ -117,6 +144,7 @@ class GeneralTrait(object):
                 formatted = self.post_publication_description
         else:
             formatted = "Not available"
+
         return formatted
 
     @property
@@ -213,26 +241,28 @@ def get_sample_data():
     trait = params['trait']
     dataset = params['dataset']
 
-    trait_ob = GeneralTrait(name=trait, dataset_name=dataset)
-
-    trait_dict = {}
-    trait_dict['name'] = trait
-    trait_dict['db'] = dataset
-    trait_dict['type'] = trait_ob.dataset.type
-    trait_dict['group'] = trait_ob.dataset.group.name
-    trait_dict['tissue'] = trait_ob.dataset.tissue
-    trait_dict['species'] = trait_ob.dataset.group.species
-    trait_dict['url'] = url_for('show_trait_page', trait_id = trait, dataset = dataset)
-    trait_dict['description'] = trait_ob.description_display
-    if trait_ob.dataset.type == "ProbeSet":
-        trait_dict['symbol'] = trait_ob.symbol
-        trait_dict['location'] = trait_ob.location_repr
-    elif trait_ob.dataset.type == "Publish":
-        if trait_ob.pubmed_id:
-            trait_dict['pubmed_link'] = trait_ob.pubmed_link
-        trait_dict['pubmed_text'] = trait_ob.pubmed_text
-
-    return json.dumps([trait_dict, {key: value.value for key, value in trait_ob.data.iteritems() }])
+    trait_ob = create_trait(name=trait, dataset_name=dataset)
+    if trait_ob:
+        trait_dict = {}
+        trait_dict['name'] = trait
+        trait_dict['db'] = dataset
+        trait_dict['type'] = trait_ob.dataset.type
+        trait_dict['group'] = trait_ob.dataset.group.name
+        trait_dict['tissue'] = trait_ob.dataset.tissue
+        trait_dict['species'] = trait_ob.dataset.group.species
+        trait_dict['url'] = url_for('show_trait_page', trait_id = trait, dataset = dataset)
+        trait_dict['description'] = trait_ob.description_display
+        if trait_ob.dataset.type == "ProbeSet":
+            trait_dict['symbol'] = trait_ob.symbol
+            trait_dict['location'] = trait_ob.location_repr
+        elif trait_ob.dataset.type == "Publish":
+            if trait_ob.pubmed_id:
+                trait_dict['pubmed_link'] = trait_ob.pubmed_link
+            trait_dict['pubmed_text'] = trait_ob.pubmed_text
+
+        return json.dumps([trait_dict, {key: value.value for key, value in trait_ob.data.iteritems() }])
+    else:
+        return None
     
 def jsonable(trait):
     """Return a dict suitable for using as json
@@ -347,90 +377,94 @@ def jsonable_table_row(trait, dataset_name, index):
     else:
         return dict()
 
+
 def retrieve_trait_info(trait, dataset, get_qtl_info=False):
     assert dataset, "Dataset doesn't exist"
-    
+
+    resource_id = get_resource_id(dataset, trait.name)
     if dataset.type == 'Publish':
-        query = """
-                SELECT
-                        PublishXRef.Id, InbredSet.InbredSetCode, Publication.PubMed_ID,
-                        Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description,
-                        Phenotype.Pre_publication_abbreviation, Phenotype.Post_publication_abbreviation, PublishXRef.mean,
-                        Phenotype.Lab_code, Phenotype.Submitter, Phenotype.Owner, Phenotype.Authorized_Users,
-                        Publication.Authors, Publication.Title, Publication.Abstract,
-                        Publication.Journal, Publication.Volume, Publication.Pages,
-                        Publication.Month, Publication.Year, PublishXRef.Sequence,
-                        Phenotype.Units, PublishXRef.comments
-                FROM
-                        PublishXRef, Publication, Phenotype, PublishFreeze, InbredSet
-                WHERE
-                        PublishXRef.Id = %s AND
-                        Phenotype.Id = PublishXRef.PhenotypeId AND
-                        Publication.Id = PublishXRef.PublicationId AND
-                        PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND
-                        PublishXRef.InbredSetId = InbredSet.Id AND
-                        PublishFreeze.Id = %s
-                """ % (trait.name, dataset.id)
+        the_url = "http://localhost:8080/run-action?resource={}&user={}&branch=data&action=view".format(resource_id, g.user_session.user_id)
+    else:
+        the_url = "http://localhost:8080/run-action?resource={}&user={}&branch=data&action=view&trait={}".format(resource_id, g.user_session.user_id, trait.name)
 
-        logger.sql(query)
-        trait_info = g.db.execute(query).fetchone()
-
-
-    #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name
-    #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms.
-    elif dataset.type == 'ProbeSet':
-        display_fields_string = ', ProbeSet.'.join(dataset.display_fields)
-        display_fields_string = 'ProbeSet.' + display_fields_string
-        query = """
-                SELECT %s
-                FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef
-                WHERE
-                        ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND
-                        ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
-                        ProbeSetFreeze.Name = '%s' AND
-                        ProbeSet.Name = '%s'
-                """ % (escape(display_fields_string),
-                       escape(dataset.name),
-                       escape(str(trait.name)))
-        logger.sql(query)
-        trait_info = g.db.execute(query).fetchone()
-    #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name
-    # to avoid the problem of same marker name from different species.
-    elif dataset.type == 'Geno':
-        display_fields_string = string.join(dataset.display_fields,',Geno.')
-        display_fields_string = 'Geno.' + display_fields_string
-        query = """
-                SELECT %s
-                FROM Geno, GenoFreeze, GenoXRef
-                WHERE
-                        GenoXRef.GenoFreezeId = GenoFreeze.Id AND
-                        GenoXRef.GenoId = Geno.Id AND
-                        GenoFreeze.Name = '%s' AND
-                        Geno.Name = '%s'
-                """ % (escape(display_fields_string),
-                       escape(dataset.name),
-                       escape(trait.name))
-        logger.sql(query)
-        trait_info = g.db.execute(query).fetchone()
-    else: #Temp type
-        query = """SELECT %s FROM %s WHERE Name = %s"""
-        logger.sql(query)
-        trait_info = g.db.execute(query,
-                                  (string.join(dataset.display_fields,','),
-                                               dataset.type, trait.name)).fetchone()
+    try:
+        response = requests.get(the_url).content
+        trait_info = json.loads(response)
+    except: #ZS: I'm assuming the trait is viewable if the try fails for some reason; it should never reach this point unless the user has privileges, since that's dealt with in create_trait
+        if dataset.type == 'Publish':
+            query = """
+                    SELECT
+                            PublishXRef.Id, InbredSet.InbredSetCode, Publication.PubMed_ID,
+                            Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description,
+                            Phenotype.Pre_publication_abbreviation, Phenotype.Post_publication_abbreviation, PublishXRef.mean,
+                            Phenotype.Lab_code, Phenotype.Submitter, Phenotype.Owner, Phenotype.Authorized_Users,
+                            Publication.Authors, Publication.Title, Publication.Abstract,
+                            Publication.Journal, Publication.Volume, Publication.Pages,
+                            Publication.Month, Publication.Year, PublishXRef.Sequence,
+                            Phenotype.Units, PublishXRef.comments
+                    FROM
+                            PublishXRef, Publication, Phenotype, PublishFreeze
+                    WHERE
+                            PublishXRef.Id = %s AND
+                            Phenotype.Id = PublishXRef.PhenotypeId AND
+                            Publication.Id = PublishXRef.PublicationId AND
+                            PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND
+                            PublishFreeze.Id = %s
+                    """ % (trait.name, dataset.id)
+
+            logger.sql(query)
+            trait_info = g.db.execute(query).fetchone()
+
+        #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name
+        #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms.
+        elif dataset.type == 'ProbeSet':
+            display_fields_string = ', ProbeSet.'.join(dataset.display_fields)
+            display_fields_string = 'ProbeSet.' + display_fields_string
+            query = """
+                    SELECT %s
+                    FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef
+                    WHERE
+                            ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND
+                            ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
+                            ProbeSetFreeze.Name = '%s' AND
+                            ProbeSet.Name = '%s'
+                    """ % (escape(display_fields_string),
+                        escape(dataset.name),
+                        escape(str(trait.name)))
+            logger.sql(query)
+            trait_info = g.db.execute(query).fetchone()
+        #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name
+        # to avoid the problem of same marker name from different species.
+        elif dataset.type == 'Geno':
+            display_fields_string = string.join(dataset.display_fields,',Geno.')
+            display_fields_string = 'Geno.' + display_fields_string
+            query = """
+                    SELECT %s
+                    FROM Geno, GenoFreeze, GenoXRef
+                    WHERE
+                            GenoXRef.GenoFreezeId = GenoFreeze.Id AND
+                            GenoXRef.GenoId = Geno.Id AND
+                            GenoFreeze.Name = '%s' AND
+                            Geno.Name = '%s'
+                    """ % (escape(display_fields_string),
+                        escape(dataset.name),
+                        escape(trait.name))
+            logger.sql(query)
+            trait_info = g.db.execute(query).fetchone()
+        else: #Temp type
+            query = """SELECT %s FROM %s WHERE Name = %s"""
+            logger.sql(query)
+            trait_info = g.db.execute(query,
+                                    (string.join(dataset.display_fields,','),
+                                                dataset.type, trait.name)).fetchone()
 
     if trait_info:
         trait.haveinfo = True
-
-        #XZ: assign SQL query result to trait attributes.
         for i, field in enumerate(dataset.display_fields):
-            holder = trait_info[i]
+            holder =  trait_info[i]
             # if isinstance(trait_info[i], basestring):
-            #     logger.debug("HOLDER:", holder)
-            #     logger.debug("HOLDER2:", holder.decode(encoding='latin1'))
-            #     holder = unicode(trait_info[i], "utf-8", "ignore")
-            if isinstance(trait_info[i], basestring):
-                holder = holder.encode('latin1')
+            #     holder = unicode(holder.strip(codecs.BOM_UTF8), 'utf-8', "ignore")
+
             setattr(trait, field, holder)
 
         if dataset.type == 'Publish':
@@ -449,13 +483,6 @@ def retrieve_trait_info(trait, dataset, get_qtl_info=False):
             if trait.confidential:
                 trait.abbreviation = trait.pre_publication_abbreviation
                 trait.description_display = trait.pre_publication_description
-
-                #if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(
-                #        privilege=self.dataset.privilege,
-                #        userName=self.dataset.userName,
-                #        authorized_users=self.authorized_users):
-                #
-                #    description = self.pre_publication_description
             else:
                 trait.abbreviation = trait.post_publication_abbreviation
                 if description:
diff --git a/wqflask/base/webqtlConfig.py b/wqflask/base/webqtlConfig.py
index 018d5d54..3d86bc22 100644
--- a/wqflask/base/webqtlConfig.py
+++ b/wqflask/base/webqtlConfig.py
@@ -17,6 +17,10 @@ DEBUG = 1
 #USER privilege
 USERDICT = {'guest':1,'user':2, 'admin':3, 'root':4}
 
+#Set privileges
+SUPER_PRIVILEGES = {'data': ['no-access', 'view', 'edit'], 'metadata': ['no-access', 'view', 'edit'], 'admin': ['not-admin', 'edit-access', 'edit-admins']}
+DEFAULT_PRIVILEGES = {'data': ['no-access', 'view'], 'metadata': ['no-access', 'view'], 'admin': ['not-admin']}
+
 #minimum number of informative strains
 KMININFORMATIVE = 5
 
@@ -58,6 +62,7 @@ PROTEIN_ATLAS_URL = "http://www.proteinatlas.org/search/%s"
 OPEN_TARGETS_URL = "https://genetics.opentargets.org/gene/%s"
 UNIPROT_URL = "https://www.uniprot.org/uniprot/%s"
 RGD_URL = "https://rgd.mcw.edu/rgdweb/elasticResults.html?term=%s&category=Gene&species=%s"
+PHENOGEN_URL = "https://phenogen.org/gene.jsp?speciesCB=Rn&auto=Y&geneTxt=%s&genomeVer=rn6&section=geneEQTL"
 
 # Temporary storage (note that this TMPDIR can be set as an
 # environment variable - use utility.tools.TEMPDIR when you