remove some trailing whitespace
[htsworkflow.git] / htsworkflow / submission / trackhub_submission.py
index 7bca809a926798d87cae8f6b23486b1de3c7b23a..3aa4a96ae26fa96c44e0c0f58c67503890fe38dc 100644 (file)
@@ -1,5 +1,7 @@
 import logging
 import os
+from pprint import pformat
+import string
 import re
 
 import RDF
@@ -9,9 +11,9 @@ from htsworkflow.submission.submission import Submission
 from htsworkflow.util.rdfhelp import \
      fromTypedNode, \
      geoSoftNS, \
-     stripNamespace, \
      submissionOntology
 from htsworkflow.util.url import parse_ssh_url
+from htsworkflow.util.ucsc import bigWigInfo
 
 from django.conf import settings
 from django.template import Context, loader
@@ -83,7 +85,7 @@ class TrackHubSubmission(Submission):
             name=self.sanitize_name(self.name),
             short_label = self.sanitize_name(self.name),
             long_label = str(self.name),
-            tracktype="bigWig",
+            tracktype="bed 3",
             dragAndDrop='subtracks',
             visibility='full',
         )
@@ -103,15 +105,24 @@ class TrackHubSubmission(Submission):
             track_name = self.make_track_name(track)
 
             track_subgroup = self.make_track_subgroups(subgroups, track)
-
-            newtrack = Track(
-                name=track_name,
-                tracktype = str(track['file_type']),
-                url= hub_url + str(track['relative_path']),
-                short_label=str(track['library_id']),
-                long_label=track_name,
-                subgroups=track_subgroup,
-                )
+            track_type = self.make_track_type(track)
+
+            if 'file_label' in track:
+                track_label = self.sanitize_name(track['file_label'])
+            else:
+                track_label = track_name
+
+            attributes = {
+                'name': track_name,
+                'tracktype': track_type,
+                'url': hub_url + str(track['relative_path']),
+                'short_label': str(track['library_id']),
+                'long_label': str(track_label),
+                'subgroups': track_subgroup,
+            }
+
+            LOGGER.debug('track attributes: %s', pformat(attributes))
+            newtrack = Track(**attributes)
             view.add_tracks([newtrack])
 
         results = hub.render()
@@ -130,13 +141,20 @@ class TrackHubSubmission(Submission):
         """
         current_view_type = str(track['output_type'])
         if not view or current_view_type != view.name:
-            view = ViewTrack(
-                name=current_view_type,
-                view=current_view_type,
-                visibility='squish',
-                short_label=current_view_type,
-                tracktype=str(track['file_type']),
-            )
+            attributes = {
+                'name': current_view_type,
+                'view': current_view_type,
+                'visibility': str(track.get('visibility', 'squish')),
+                'short_label': current_view_type,
+                'tracktype': str(track['file_type'])
+            }
+            maxHeightPixels = track.get('maxHeightPixels')
+            if maxHeightPixels:
+                attributes['maxHeightPixels'] = str(maxHeightPixels)
+            autoScale = track.get('autoScale')
+            if autoScale:
+                attributes['autoScale'] = str(autoScale)
+            view = ViewTrack(**attributes)
             composite.add_view(view)
             view_type = current_view_type
         return view
@@ -154,12 +172,11 @@ class TrackHubSubmission(Submission):
         return str(template.render(context))
 
     def make_track_name(self, track):
-        name = '{}_{}_{}'.format(
+        return '{}_{}_{}'.format(
             track['library_id'],
             track['replicate'],
             track['output_type'],
         )
-        return name
 
     def make_track_subgroups(self, subgroups, track):
         track_subgroups = {}
@@ -169,21 +186,50 @@ class TrackHubSubmission(Submission):
                 track_subgroups[k] = value
         return track_subgroups
 
+    def make_track_type(self, track):
+        """Further annotate tracktype.
+
+        bigWig files can have additional information. Add it if we can
+        """
+        track_type = track['file_type']
+        if track_type.lower() == 'bigwig':
+            # something we can enhance
+            info = bigWigInfo(track['relative_path'])
+            if info.min is not None and info.max is not None:
+                track_type = '{} {} {}'.format(track_type, int(info.min), int(info.max))
+
+        LOGGER.debug("track_type: %s", track_type)
+        return str(track_type)
+
     def add_subgroups(self, composite):
         """Add subgroups to composite track"""
         search = [ ('htswlib:cell_line', 'cell'),
+                   ('encode3:rna_type', 'rna_type'),
+                   ('encode3:protocol', 'protocol'),
                    ('htswlib:replicate', 'replicate'),
                    ('encode3:library_id', 'library_id'),
                    ('encode3:assay', 'assay'),
-                   ('encode3:rna_type', 'rna_type'),
-                   ('encode3:protocol', 'protocol'),
                  ]
         subgroups = []
         names = []
+        sortorder = []
+        dimnames = ('dim{}'.format(x) for x in string.ascii_uppercase)
+        dimensions = []
+        filtercomposite = []
         for term, name in search:
-            subgroups.append(self.make_subgroupdefinition(term, name))
-            names.append(name)
+            definitions = self.make_subgroupdefinition(term, name)
+            if definitions:
+                subgroups.append(definitions)
+                names.append(name)
+                sortorder.append("{}=+".format(name))
+                d = dimnames.next()
+                dimensions.append("{}={}".format(d, name))
+                filtercomposite.append("{}=multi".format(d))
+
         composite.add_subgroups(subgroups)
+        composite.add_params(sortOrder=' '.join(sortorder))
+        composite.add_params(dimensions=' '.join(dimensions))
+        composite.add_params(filterComposite=' '.join(filtercomposite))
         return names
 
 
@@ -198,11 +244,14 @@ class TrackHubSubmission(Submission):
             value = str(row['name'])
             values[self.sanitize_name(value)] = value
 
-        return SubGroupDefinition(
-                name=name,
-                label=name,
-                mapping=values,
-        )
+        if values:
+            return SubGroupDefinition(
+                    name=name,
+                    label=name,
+                    mapping=values,
+            )
+        else:
+            return None
 
     def get_tracks(self):
         """Collect information needed to describe trackhub tracks.
@@ -239,7 +288,6 @@ class TrackHubSubmission(Submission):
         return name
 
     def get_manifest_metadata(self, analysis_node):
-
         query_template = loader.get_template('trackhub_manifest.sparql')
 
         context = Context({