about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBonfaceKilz2020-10-12 14:24:29 +0300
committerBonfaceKilz2020-10-12 14:24:29 +0300
commit276e0381d62b07525e0ebd28528e59be8230c954 (patch)
tree287758f0efa03f76d87a4debb1a50d5e6f806547
parent16cd94dfa0e892576f1c566948b775b954f7f508 (diff)
parent58c3815571f83db0296dc099f1e4c6ff69619f64 (diff)
downloadgenenetwork2-276e0381d62b07525e0ebd28528e59be8230c954.tar.gz
Merge branch 'testing' into build/python3-migration
-rw-r--r--wqflask/base/data_set.py40
-rw-r--r--wqflask/base/trait.py15
-rw-r--r--wqflask/wqflask/collect.py35
-rw-r--r--wqflask/wqflask/correlation/corr_scatter_plot.py11
-rw-r--r--wqflask/wqflask/correlation_matrix/show_corr_matrix.py2
-rw-r--r--wqflask/wqflask/docs.py4
-rw-r--r--wqflask/wqflask/resource_manager.py20
-rw-r--r--wqflask/wqflask/templates/correlation_matrix.html2
-rw-r--r--wqflask/wqflask/templates/correlation_page.html12
-rwxr-xr-xwqflask/wqflask/templates/index_page_orig.html10
-rw-r--r--wqflask/wqflask/views.py4
-rw-r--r--wqflask/wqflask/wgcna/wgcna_analysis.py87
12 files changed, 108 insertions, 134 deletions
diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py
index 295f5c48..0d4ac24b 100644
--- a/wqflask/base/data_set.py
+++ b/wqflask/base/data_set.py
@@ -150,10 +150,12 @@ Publish or ProbeSet. E.g.
             "geno": "Geno",
         }
 
+        group_name = name
         if t in ['pheno', 'other_pheno']:
-            name = name.replace("Publish", "")
+            group_name = name.replace("Publish", "")
 
-        if bool(len(g.db.execute(sql_query_mapping[t].format(name)).fetchone())):
+        results = g.db.execute(sql_query_mapping[t].format(group_name)).fetchone()
+        if results:
             self.datasets[name] = dataset_name_mapping[t]
             self.redis_instance.set("dataset_structure", json.dumps(self.datasets))
             return True
@@ -1154,40 +1156,6 @@ class TempDataSet(DataSet):
         self.fullname = 'Temporary Storage'
         self.shortname = 'Temp'
 
-    @staticmethod
-    def handle_pca(desc):
-        if 'PCA' in desc:
-            # Todo: Modernize below lines
-            desc = desc[desc.rindex(':')+1:].strip()
-        else:
-            desc = desc[:desc.index('entered')].strip()
-        return desc
-
-    def get_desc(self):
-        query = 'SELECT description FROM Temp WHERE Name=%s' % self.name
-        logger.sql(query)
-        g.db.execute(query)
-        desc = g.db.fetchone()[0]
-        desc = self.handle_pca(desc)
-        return desc
-
-    def retrieve_sample_data(self, trait):
-        query = """
-                SELECT
-                        Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id
-                FROM
-                        TempData, Temp, Strain
-                WHERE
-                        TempData.StrainId = Strain.Id AND
-                        TempData.Id = Temp.DataId AND
-                        Temp.name = '%s'
-                Order BY
-                        Strain.Name
-                """ % escape(trait.name)
-
-        logger.sql(query)
-        results = g.db.execute(query).fetchall()
-
 
 def geno_mrna_confidentiality(ob):
     dataset_table = ob.type + "Freeze"
diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py
index a493cfbf..cfc02f8b 100644
--- a/wqflask/base/trait.py
+++ b/wqflask/base/trait.py
@@ -132,7 +132,7 @@ class GeneralTrait(object):
 
     @property
     def description_fmt(self):
-        '''Return a text formated description'''
+        """Return a text formated description"""
         if self.dataset.type == 'ProbeSet':
             if self.description:
                 formatted = self.description
@@ -153,7 +153,7 @@ class GeneralTrait(object):
 
     @property
     def alias_fmt(self):
-        '''Return a text formatted alias'''
+        """Return a text formatted alias"""
 
         alias = 'Not available'
         if getattr(self, "alias", None):
@@ -164,7 +164,7 @@ class GeneralTrait(object):
 
     @property
     def wikidata_alias_fmt(self):
-        '''Return a text formatted alias'''
+        """Return a text formatted alias"""
 
         alias = 'Not available'
         if self.symbol:
@@ -194,12 +194,12 @@ class GeneralTrait(object):
 
     @property
     def location_fmt(self):
-        '''Return a text formatted location
+        """Return a text formatted location
 
         While we're at it we set self.location in case we need it
         later (do we?)
 
-        '''
+        """
 
         if self.chr and self.mb:
             self.location = 'Chr %s @ %s Mb' % (self.chr, self.mb)
@@ -542,7 +542,7 @@ def retrieve_trait_info(trait, dataset, get_qtl_info=False):
 
             if (str(description_display or "") != "" and
                 description_display != 'N/A' and
-                str(target_string or "") != "" and target_string != 'None'):
+                    str(target_string or "") != "" and target_string != 'None'):
                 description_display = description_display + '; ' + target_string.strip()
 
             # Save it for the jinja2 template
@@ -631,7 +631,8 @@ def retrieve_trait_info(trait, dataset, get_qtl_info=False):
                 else:
                     trait.locus = trait.lrs = trait.additive = ""
             if (dataset.type == 'Publish' or dataset.type == "ProbeSet") and str(trait.locus_chr or "") != "" and str(trait.locus_mb or "") != "":
-                trait.LRS_location_repr = LRS_location_repr = 'Chr%s: %.6f' % (trait.locus_chr, float(trait.locus_mb))
+                trait.LRS_location_repr = LRS_location_repr = 'Chr%s: %.6f' % (
+                    trait.locus_chr, float(trait.locus_mb))
                 if str(trait.lrs or "") != "":
                     trait.LRS_score_repr = LRS_score_repr = '%3.1f' % trait.lrs
     else:
diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py
index 9752c8b5..e074a3d8 100644
--- a/wqflask/wqflask/collect.py
+++ b/wqflask/wqflask/collect.py
@@ -1,38 +1,29 @@
-import os
 import hashlib
 import datetime
-import time
-
-import uuid
-import hashlib
-import base64
-
-import urllib.parse
-
 import simplejson as json
 
-from flask import (Flask, g, render_template, url_for, request, make_response,
-                   redirect, flash, jsonify)
+from flask import g
+from flask import render_template
+from flask import url_for
+from flask import request
+from flask import redirect
+from flask import flash
 
 from wqflask import app
-
-from pprint import pformat as pf
-
-from wqflask.database import db_session
-
-from wqflask import model
-
-from utility import Bunch, Struct, hmac
+from utility import hmac
 from utility.formatting import numify
 from utility.redis_tools import get_redis_conn
-Redis = get_redis_conn()
 
-from base.trait import create_trait, retrieve_trait_info, jsonable
+from base.trait import create_trait
+from base.trait import retrieve_trait_info
+from base.trait import jsonable
 from base.data_set import create_dataset
 
-import logging
 from utility.logger import getLogger
+
 logger = getLogger(__name__)
+Redis = get_redis_conn()
+
 
 def process_traits(unprocessed_traits):
     if isinstance(unprocessed_traits, bytes):
diff --git a/wqflask/wqflask/correlation/corr_scatter_plot.py b/wqflask/wqflask/correlation/corr_scatter_plot.py
index 929cd2c9..6ab8c3d8 100644
--- a/wqflask/wqflask/correlation/corr_scatter_plot.py
+++ b/wqflask/wqflask/correlation/corr_scatter_plot.py
@@ -15,8 +15,15 @@ class CorrScatterPlot(object):
     """Page that displays a correlation scatterplot with a line fitted to it"""
 
     def __init__(self, params):
-        self.dataset_1 = data_set.create_dataset(params['dataset_1'])
-        self.dataset_2 = data_set.create_dataset(params['dataset_2'])
+        if "Temp" in params['dataset_1']:
+            self.dataset_1 = data_set.create_dataset(dataset_name = "Temp", dataset_type = "Temp", group_name = params['dataset_1'].split("_")[1])
+        else:
+            self.dataset_1 = data_set.create_dataset(params['dataset_1'])
+        if "Temp" in params['dataset_2']:
+            self.dataset_2 = data_set.create_dataset(dataset_name = "Temp", dataset_type = "Temp", group_name = params['dataset_2'].split("_")[1])
+        else:
+            self.dataset_2 = data_set.create_dataset(params['dataset_2'])
+
         #self.dataset_3 = data_set.create_dataset(params['dataset_3'])
         self.trait_1 = create_trait(name=params['trait_1'], dataset=self.dataset_1)
         self.trait_2 = create_trait(name=params['trait_2'], dataset=self.dataset_2)
diff --git a/wqflask/wqflask/correlation_matrix/show_corr_matrix.py b/wqflask/wqflask/correlation_matrix/show_corr_matrix.py
index f24b9f7f..a394f548 100644
--- a/wqflask/wqflask/correlation_matrix/show_corr_matrix.py
+++ b/wqflask/wqflask/correlation_matrix/show_corr_matrix.py
@@ -124,7 +124,7 @@ class CorrelationMatrix(object):
 
                     if num_overlap < self.lowest_overlap:
                         self.lowest_overlap = num_overlap
-                    if num_overlap == 0:
+                    if num_overlap < 2:
                         corr_result_row.append([target_trait, 0, num_overlap])
                         pca_corr_result_row.append(0)
                     else:
diff --git a/wqflask/wqflask/docs.py b/wqflask/wqflask/docs.py
index 8628b81d..d653c269 100644
--- a/wqflask/wqflask/docs.py
+++ b/wqflask/wqflask/docs.py
@@ -9,7 +9,7 @@ class Docs(object):
 
     def __init__(self, entry, start_vars={}):
         sql = """
-            SELECT Docs.title, Docs.content
+            SELECT Docs.title, CAST(Docs.content AS BINARY)
             FROM Docs
             WHERE Docs.entry LIKE %s
             """
@@ -20,7 +20,7 @@ class Docs(object):
             self.content = ""
         else:
             self.title = result[0]
-            self.content = result[1].encode("latin1")
+            self.content = result[1]
 
         self.editable = "false"
         # ZS: Removing option to edit to see if text still gets vandalized
diff --git a/wqflask/wqflask/resource_manager.py b/wqflask/wqflask/resource_manager.py
index e883d5da..85cbb2fd 100644
--- a/wqflask/wqflask/resource_manager.py
+++ b/wqflask/wqflask/resource_manager.py
@@ -23,16 +23,16 @@ def manage_resource():
         group_masks_with_names = get_group_names(group_masks)
         default_mask = resource_info['default_mask']['data']
         owner_id = resource_info['owner_id']
-        owner_info = get_user_by_unique_column("user_id", owner_id)
-
-        if 'name' in owner_info:
-            owner_display_name = owner_info['full_name']
-        elif 'user_name' in owner_info:
-            owner_display_name = owner_info['user_name']
-        elif 'email_address' in owner_info:
-            owner_display_name = owner_info['email_address']
-        else:
-            owner_display_name = None
+
+        owner_display_name = None
+        if owner_id != "none":
+            owner_info = get_user_by_unique_column("user_id", owner_id)
+            if 'name' in owner_info:
+                owner_display_name = owner_info['full_name']
+            elif 'user_name' in owner_info:
+                owner_display_name = owner_info['user_name']
+            elif 'email_address' in owner_info:
+                owner_display_name = owner_info['email_address']
 
         return render_template("admin/manage_resource.html", owner_name = owner_display_name, resource_id = resource_id, resource_info=resource_info, default_mask=default_mask, group_masks=group_masks_with_names, admin_status=admin_status)
 
diff --git a/wqflask/wqflask/templates/correlation_matrix.html b/wqflask/wqflask/templates/correlation_matrix.html
index 9b96de1c..d556f31a 100644
--- a/wqflask/wqflask/templates/correlation_matrix.html
+++ b/wqflask/wqflask/templates/correlation_matrix.html
@@ -51,7 +51,7 @@
       {% if result[0].name == trait.name and result[0].dataset == trait.dataset %}
       <td nowrap="ON" align="center" bgcolor="#cccccc" style="padding: 3px; line-height: 1.1;"><a href="/show_trait?trait_id={{ trait.name }}&dataset={{ trait.dataset.name }}"><font style="font-size: 12px; color: #3071a9; font-weight: bold;" ><em>n</em><br>{{ result[2] }}</font></a></td>
       {% else %}
-      <td nowrap="ON" align="middle" class="corr_cell" style="padding: 3px; line-height: 1.1;"><a href="/corr_scatter_plot?dataset_1={{ trait.dataset.name }}&dataset_2={{ result[0].dataset.name }}&trait_1={{ trait.name }}&trait_2={{ result[0].name }}"><font style="font-size: 12px; color: #3071a9; font-weight: bold;" ><span class="corr_value">{{ '%0.2f' % result[1] }}</span><br>{{ result[2] }}</font></a></td>
+      <td nowrap="ON" align="middle" class="corr_cell" style="padding: 3px; line-height: 1.1;"><a href="/corr_scatter_plot?dataset_1={% if trait.dataset.name == 'Temp' %}Temp_{{ trait.dataset.group.name }}{% else %}{{ trait.dataset.name }}{% endif %}&dataset_2={% if result[0].dataset.name == 'Temp' %}Temp_{{ result[0].dataset.group.name }}{% else %}{{ result[0].dataset.name }}{% endif %}&trait_1={{ trait.name }}&trait_2={{ result[0].name }}"><font style="font-size: 12px; color: #3071a9; font-weight: bold;" ><span class="corr_value">{{ '%0.2f' % result[1] }}</span><br>{{ result[2] }}</font></a></td>
       {% endif %}
       {% endfor %}
     </tr>
diff --git a/wqflask/wqflask/templates/correlation_page.html b/wqflask/wqflask/templates/correlation_page.html
index 435c0b37..134f15be 100644
--- a/wqflask/wqflask/templates/correlation_page.html
+++ b/wqflask/wqflask/templates/correlation_page.html
@@ -144,7 +144,7 @@
                         <td data-export="{{ trait.description_display }}">{{ trait.description_display }}</TD>
                         <td data-export="{{ trait.location_repr }}" style="white-space: nowrap;">{{ trait.location_repr }}</td>
                         <td data-export="{{ '%0.3f' % trait.mean|float }}" align="right">{{ '%0.3f' % trait.mean|float }}</td>
-                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}"" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={{dataset.name}}&dataset_2={{trait.dataset.name}}&trait_1={{this_trait.name}}&trait_2={{trait.name}}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
+                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}"" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={% if dataset.name == 'Temp' %}Temp_{{ dataset.group.name }}{% else %}{{ dataset.name }}{% endif %}&dataset_2={% if trait.dataset.name == 'Temp' %}Temp_{{ trait.dataset.group.name }}{% else %}{{ trait.dataset.name }}{% endif %}&trait_1={{ this_trait.name }}&trait_2={{ trait.name }}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
                         <td data-export="{{ trait.num_overlap }}" align="right">{{ trait.num_overlap }}</td>
                         <td data-export="{{ '%0.3e'|format(trait.sample_p) }}" align="right">{{ '%0.3e'|format(trait.sample_p) }}</td>
                         {% if trait.lit_corr == "" or trait.lit_corr == 0.000 %}
@@ -164,16 +164,16 @@
                         <td data-export={% if trait.additive != "" %}"{{ '%0.3f' % trait.additive|float }}"{% else %}"N/A"{% endif %} align="right">{% if trait.additive != "" %}{{ '%0.3f' % trait.additive|float }}{% else %}N/A{% endif %}</td>
                       {% elif target_dataset.type == "Publish" %}
                         {% if trait.abbreviation %}
-                        <TD title="{{ trait.abbreviation }}" data-export="{{ trait.abbreviation }}">{% if trait.abbreviation|length > 20 %}{{ trait.abbreviation[:20] }}...{% else %}{{ trait.abbreviation }}{% endif %}</TD>
+                        <td title="{{ trait.abbreviation }}" data-export="{{ trait.abbreviation }}">{% if trait.abbreviation|length > 20 %}{{ trait.abbreviation[:20] }}...{% else %}{{ trait.abbreviation }}{% endif %}</td>
                         {% else %}
-                        <TD data-export="N/A">N/A</TD>
+                        <td data-export="N/A">N/A</td>
                         {% endif %}
                         <td data-export="{{ trait.description_display }}">{% if trait.description_display|length > 70 %}{{ trait.description_display[:70] }}...{% else %}{{ trait.description_display }}{% endif %}</td>
                         {% if trait.authors %}
                         {% set authors_list = trait.authors.split(',') %}
                         <td data-export="{{ trait.authors }}">{% if authors_list|length > 6 %}{{ authors_list[:6]|join(', ') }}, et al.{% else %}{{ trait.authors }}{% endif %}</td>
                         {% else %}
-                        <TD data-export="N/A">N/A</TD>
+                        <td data-export="N/A">N/A</td>
                         {% endif %}
                         <td data-export="{{ trait.pubmed_text }}">
                             {% if trait.pubmed_text != "N/A" %}
@@ -184,7 +184,7 @@
                             {{ trait.pubmed_text }}
                             {% endif %}
                         </td>
-                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={{dataset.name}}&dataset_2={{trait.dataset.name}}&trait_1={{this_trait.name}}&trait_2={{trait.name}}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
+                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}"" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={% if dataset.name == 'Temp' %}Temp_{{ dataset.group.name }}{% else %}{{ dataset.name }}{% endif %}&dataset_2={{ trait.dataset.name }}&trait_1={{ this_trait.name }}&trait_2={{ trait.name }}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
                         <td data-export="{{ trait.num_overlap }}" align="right">{{ trait.num_overlap }}</td>
                         <td data-export="{{ '%0.3e'|format(trait.sample_p) }}" align="right">{{ '%0.3e'|format(trait.sample_p) }}</td>
                         <td data-export="{{ trait.LRS_score_repr }}" align="right">{{ trait.LRS_score_repr }}</td>
@@ -192,7 +192,7 @@
                         <td data-export={% if trait.additive != "" %}"{{ '%0.3f' % trait.additive|float }}"{% else %}"N/A"{% endif %} align="right">{% if trait.additive != "" %}{{ '%0.3f' % trait.additive|float }}{% else %}N/A{% endif %}</td>
                       {% elif target_dataset.type == "Geno" %}
                         <td data-export="{{ trait.location_repr }}" align="right">{{ trait.location_repr }}</TD>
-                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={{dataset.name}}&dataset_2={{trait.dataset.name}}&trait_1={{this_trait.name}}&trait_2={{trait.name}}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
+                        <td data-export="{{ '%0.3f'|format(trait.sample_r) }}"" align="right"><a target="_blank" href="corr_scatter_plot?dataset_1={% if dataset.name == 'Temp' %}Temp_{{ dataset.group.name }}{% else %}{{ dataset.name }}{% endif %}&dataset_2={{ trait.dataset.name }}&trait_1={{ this_trait.name }}&trait_2={{ trait.name }}">{{ '%0.3f'|format(trait.sample_r) }}</a></td>
                         <td data-export="{{ trait.num_overlap }}" align="right">{{ trait.num_overlap }}</td>
                         <td data-export="{{ '%0.3e'|format(trait.sample_p) }}" align="right">{{ '%0.3e'|format(trait.sample_p) }}</td>
                       {% endif %}
diff --git a/wqflask/wqflask/templates/index_page_orig.html b/wqflask/wqflask/templates/index_page_orig.html
index 06b71f53..6b3bec9a 100755
--- a/wqflask/wqflask/templates/index_page_orig.html
+++ b/wqflask/wqflask/templates/index_page_orig.html
@@ -17,13 +17,13 @@
     </header>
 -->
 
-    <div class="container-fluid" style="min-width: 1200px;">
+    <div class="container-fluid" style="min-width: 1210px;">
 
         {{ flash_me() }}
 
         <div class="row" style="width: 100%;">
 
-            <div class="col-xs-5" style="min-width: 530px; max-width: 550px;">
+            <div class="col-xs-4" style="margin-right:50px; min-width: 530px; max-width: 550px;">
                 <section id="search">
                     <div>
                         <h1>Select and search</h1>
@@ -84,7 +84,7 @@
                                     <label for="or_search" class="col-xs-1 control-label" style="padding-left: 0px; padding-right: 0px; width: 65px !important;">Get Any:</label>
                                     <div class="col-xs-10 controls" style="padding-left: 20px;">
                                         <div class="col-8">
-                                          <textarea onkeydown="pressed(event)" name="search_terms_or" rows="1" class="form-control search-query" style="max-width: 550px; width: 450px !important;" id="or_search"></textarea>
+                                          <textarea onkeydown="pressed(event)" name="search_terms_or" rows="1" class="form-control search-query" style="resize: vertical; max-width: 550px; width: 450px !important;" id="or_search"></textarea>
                                         </div>
                                     </div>
                                 </div>
@@ -105,7 +105,7 @@
                                     <label for="and_search" class="col-xs-1 control-label" style="padding-left: 0px; padding-right: 0px; width: 65px !important;">Combined:</label>
                                     <div class="col-xs-10 controls" style="padding-left: 20px;">
                                         <div class="col-8">
-                                          <textarea onkeydown="pressed(event)" name="search_terms_and" rows="1" class="form-control search-query" style="max-width: 550px; width: 450px !important;" id="and_search"></textarea>
+                                          <textarea onkeydown="pressed(event)" name="search_terms_and" rows="1" class="form-control search-query" style="resize: vertical; max-width: 550px; width: 450px !important;" id="and_search"></textarea>
                                         </div>
                                     </div>
                                 </div>
@@ -184,7 +184,7 @@
                 </section>
             </div>
 
-            <div style="padding-left:80px" class="col-xs-4" style="width: 600px !important;">
+            <div class="col-xs-4" style="width: 600px !important;">
 	        <section id="affiliates">
 	            <div class="page-header">
                         <h1>Affiliates</h1>
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py
index c37be418..08673f79 100644
--- a/wqflask/wqflask/views.py
+++ b/wqflask/wqflask/views.py
@@ -48,7 +48,7 @@ from wqflask.search_results import SearchResultPage
 from wqflask.export_traits import export_search_results_csv
 from wqflask.gsearch import GSearch
 from wqflask.update_search_results import GSearch as UpdateGSearch
-from wqflask.docs import Docs
+from wqflask.docs import Docs, update_text
 from wqflask.db_info import InfoPage
 
 from utility import temp_data
@@ -346,7 +346,7 @@ def environments():
 
 @app.route("/update_text", methods=('POST',))
 def update_page():
-    docs.update_text(request.form)
+    update_text(request.form)
     doc = Docs(request.form['entry_type'], request.form)
     return render_template("docs.html", **doc.__dict__)
 
diff --git a/wqflask/wqflask/wgcna/wgcna_analysis.py b/wqflask/wqflask/wgcna/wgcna_analysis.py
index 93742daf..c6e03ec5 100644
--- a/wqflask/wqflask/wgcna/wgcna_analysis.py
+++ b/wqflask/wqflask/wgcna/wgcna_analysis.py
@@ -1,5 +1,8 @@
-# WGCNA analysis for GN2
-# Author / Maintainer: Danny Arends <Danny.Arends@gmail.com>
+"""
+WGCNA analysis for GN2
+
+Author / Maintainer: Danny Arends <Danny.Arends@gmail.com>
+"""
 import base64
 import sys
 import rpy2.robjects as ro                    # R Objects
@@ -16,35 +19,34 @@ from utility import helper_functions
 utils = importr("utils")
 
 # Get pointers to some common R functions
-r_library = ro.r["library"]             # Map the library function
-r_options = ro.r["options"]             # Map the options function
-r_read_csv = ro.r["read.csv"]            # Map the read.csv function
-r_dim = ro.r["dim"]                 # Map the dim function
-r_c = ro.r["c"]                   # Map the c function
-r_cat = ro.r["cat"]                 # Map the cat function
-r_paste = ro.r["paste"]               # Map the paste function
-r_unlist = ro.r["unlist"]              # Map the unlist function
-r_unique = ro.r["unique"]              # Map the unique function
-r_length = ro.r["length"]              # Map the length function
-r_unlist = ro.r["unlist"]              # Map the unlist function
-r_list = ro.r.list                   # Map the list function
-r_matrix = ro.r.matrix                 # Map the matrix function
-r_seq = ro.r["seq"]                 # Map the seq function
-r_table = ro.r["table"]               # Map the table function
-r_names = ro.r["names"]               # Map the names function
-r_sink = ro.r["sink"]                # Map the sink function
-r_is_NA = ro.r["is.na"]               # Map the is.na function
-r_file = ro.r["file"]                # Map the file function
-r_png = ro.r["png"]                 # Map the png function for plotting
-r_dev_off = ro.r["dev.off"]             # Map the dev.off function
+r_library = ro.r["library"]    # Map the library function
+r_options = ro.r["options"]    # Map the options function
+r_read_csv = ro.r["read.csv"]  # Map the read.csv function
+r_dim = ro.r["dim"]            # Map the dim function
+r_c = ro.r["c"]                # Map the c function
+r_cat = ro.r["cat"]            # Map the cat function
+r_paste = ro.r["paste"]        # Map the paste function
+r_unlist = ro.r["unlist"]      # Map the unlist function
+r_unique = ro.r["unique"]      # Map the unique function
+r_length = ro.r["length"]      # Map the length function
+r_unlist = ro.r["unlist"]      # Map the unlist function
+r_list = ro.r.list             # Map the list function
+r_matrix = ro.r.matrix         # Map the matrix function
+r_seq = ro.r["seq"]            # Map the seq function
+r_table = ro.r["table"]        # Map the table function
+r_names = ro.r["names"]        # Map the names function
+r_sink = ro.r["sink"]          # Map the sink function
+r_is_NA = ro.r["is.na"]        # Map the is.na function
+r_file = ro.r["file"]          # Map the file function
+r_png = ro.r["png"]            # Map the png function for plotting
+r_dev_off = ro.r["dev.off"]    # Map the dev.off function
 
 
 class WGCNA(object):
     def __init__(self):
+        # To log output from stdout/stderr to a file add `r_sink(log)`
         print("Initialization of WGCNA")
-        #log = r_file("/tmp/genenetwork_wcgna.log", open = "wt")
-        # r_sink(log)                                  # Uncomment the r_sink() commands to log output from stdout/stderr to a file
-        #r_sink(log, type = "message")
+
         # Load WGCNA - Should only be done once, since it is quite expensive
         r_library("WGCNA")
         r_options(stringsAsFactors=False)
@@ -67,11 +69,12 @@ class WGCNA(object):
         self.r_enableWGCNAThreads()
         self.trait_db_list = [trait.strip()
                               for trait in requestform['trait_list'].split(',')]
-        print(("Retrieved phenotype data from database",
-               requestform['trait_list']))
+        print("Retrieved phenotype data from database",
+              requestform['trait_list'])
         helper_functions.get_trait_db_obs(self, self.trait_db_list)
 
-        self.input = {}           # self.input contains the phenotype values we need to send to R
+        # self.input contains the phenotype values we need to send to R
+        self.input = {}
         # All the strains we have data for (contains duplicates)
         strains = []
         # All the traits we have data for (should not contain duplicates)
@@ -91,7 +94,8 @@ class WGCNA(object):
         r_cat("The number of unique strains:", r_length(uStrainsR), "\n")
         r_cat("The number of unique traits:", r_length(uTraitsR), "\n")
 
-        # rM is the datamatrix holding all the data in R /rows = strains columns = traits
+        # rM is the datamatrix holding all the data in
+        # R /rows = strains columns = traits
         rM = ro.r.matrix(ri.NA_Real, nrow=r_length(uStrainsR), ncol=r_length(
             uTraitsR), dimnames=r_list(uStrainsR, uTraitsR))
         for t in uTraitsR:
@@ -100,7 +104,6 @@ class WGCNA(object):
             for s in uStrainsR:
                 # R uses vectors every single element is a vector
                 strain = s[0]
-                #DEBUG: print(trait, strain, " in python: ", self.input[trait].get(strain), "in R:", rM.rx(strain,trait)[0])
                 rM.rx[strain, trait] = self.input[trait].get(
                     strain)  # Update the matrix location
                 sys.stdout.flush()
@@ -116,19 +119,20 @@ class WGCNA(object):
         # Store the user specified parameters for the output page
         self.results['requestform'] = requestform
 
-        # Calculate soft threshold if the user specified the SoftThreshold variable
+        # Calculate soft threshold if the user specified the
+        # SoftThreshold variable
         if requestform.get('SoftThresholds') is not None:
             powers = [int(threshold.strip())
                       for threshold in requestform['SoftThresholds'].rstrip().split(",")]
             rpow = r_unlist(r_c(powers))
-            print(("SoftThresholds: {} == {}".format(powers, rpow)))
+            print("SoftThresholds: {} == {}".format(powers, rpow))
             self.sft = self.r_pickSoftThreshold(
                 rM, powerVector=rpow, verbose=5)
 
-            print(("PowerEstimate: {}".format(self.sft[0])))
+            print("PowerEstimate: {}".format(self.sft[0]))
             self.results['PowerEstimate'] = self.sft[0]
             if self.sft[0][0] is ri.NA_Integer:
-                print("No power is suitable for the analysis, just use 1")
+                print "No power is suitable for the analysis, just use 1"
                 # No power could be estimated
                 self.results['Power'] = 1
             else:
@@ -141,7 +145,11 @@ class WGCNA(object):
 
         # Create the block wise modules using WGCNA
         network = self.r_blockwiseModules(
-            rM, power=self.results['Power'], TOMType=requestform['TOMtype'], minModuleSize=requestform['MinModuleSize'], verbose=3)
+            rM,
+            power=self.results['Power'],
+            TOMType=requestform['TOMtype'],
+            minModuleSize=requestform['MinModuleSize'],
+            verbose=3)
 
         # Save the network for the GUI
         self.results['network'] = network
@@ -155,8 +163,9 @@ class WGCNA(object):
         self.results['imgloc'] = GENERATED_IMAGE_DIR + self.results['imgurl']
         r_png(self.results['imgloc'], width=1000, height=600, type='cairo-png')
         mergedColors = self.r_labels2colors(network[1])
-        self.r_plotDendroAndColors(network[5][0], mergedColors, "Module colors",
-                                   dendroLabels=False, hang=0.03, addGuide=True, guideHang=0.05)
+        self.r_plotDendroAndColors(network[5][0], mergedColors,
+                                   "Module colors", dendroLabels=False,
+                                   hang=0.03, addGuide=True, guideHang=0.05)
         r_dev_off()
         sys.stdout.flush()
 
@@ -177,6 +186,4 @@ class WGCNA(object):
         template_vars["results"] = self.results
         self.render_image(results)
         sys.stdout.flush()
-        # r_sink(type = "message")                                   # This restores R output to the stdout/stderr
-        # r_sink()                                                   # We should end the Rpy session more or less
         return(dict(template_vars))