remove some trailing whitespace
[htsworkflow.git] / htsworkflow / submission / trackhub_submission.py
index 5003fc0fa27772bb542f4d40e15e11a69a97c5db..3aa4a96ae26fa96c44e0c0f58c67503890fe38dc 100644 (file)
@@ -1,5 +1,6 @@
 import logging
 import os
+from pprint import pformat
 import string
 import re
 
@@ -10,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
@@ -104,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()
@@ -131,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
@@ -155,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 = {}
@@ -170,6 +186,21 @@ 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'),
@@ -257,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({