GMOD / jbrowse

JBrowse 1, a full-featured genome browser built with JavaScript and HTML5. For JBrowse 2, see https://github.com/GMOD/jbrowse-components.
http://jbrowse.org
Other
463 stars 199 forks source link

Sort features by start location for BAM and CRAM tracks #1471

Closed maxime-bc closed 4 years ago

maxime-bc commented 4 years ago

Hello everybody,

I would like to develop a plugin to answer this issue.

I tried to inherit the renderFeatures function from Alignment2.js in order to sort the features (fRects) according to their alignment start by clicking on an extra feature menu item.

In the development console of my browser, I can see that the features are sorted as wanted but in the end the features are still displayed in the same order as before sorting.

With what I want to achieve, I also need to find a way to trigger the sort of the features using the action key from the extra menu item.

This is my current piece of code :

define( ['dojo/_base/declare', 'JBrowse/View/Track/Alignments2'],
function(declare, Alignments2) {
  return declare( Alignments2, {

    _defaultConfig: function() {
      var opts=this.inherited(arguments); //call the parent classes function

      opts["menuTemplate"].push( // add an extra menu item to the array returned from parent class function
      {
        label: "BAM Alignment",
        action: function(event){
          const alignmentStart = this.feature.record._refRegion.start;
          // Trigger features sort
        },
        iconClass: "dijitIconUndo",
        disabled: false
      });

      return opts;
    },

    renderFeatures: function( args, fRects ) {
      fRects.sort((a, b) => {
        var result = 0;
        if( a && b )
                result = b.f.record.alignmentStart - a.f.record.alignmentStart;
        else if( a )
                result = -1;
        else
                result = 1;
        return result;
      });
      var opts=this.inherited(arguments);
    }
  });
});

Am I modifying the right function ?

Would it be possible to have more information regarding the display of features within JBrowse? What classes do you think I should inherit and which functions should I modify?

Thanks in advance for your help, Best regards.

cmdcolin commented 4 years ago

By the time this renderFeatures is called, you can see you are already getting fRects, which is the exact rectangle in screen coordinates that the feature was given by the layout. It will not matter how that list of rectangles is sorted. I can't necessarily point you to the best place to do it properly but here is a couple notes

An important part of the "Sort by" function that I only learned recently is that it sorts the features that cross a so-called "center line" e.g. the center of the view. This means for example the "Sort by base" function means it is the base in the middle of the view. "Sort by start" is sorting the reads that cross the middle of the view by their start coordinate.

That has some amount of impact on how this feature is implemented.

Now, if you are applying the sort, you may look at modifying the storeClass BAM.js and/or CRAM.js and make it so that you call the featureCallback for each feature in sorted order, and it will need to be connected to the coordinate of the so called "center line" which you can maybe compute from JBrowse.view.visibleRegion()

cmdcolin commented 4 years ago

For adding menu items that could be fine, I normally do it like this though https://github.com/elsiklab/gccontent/blob/master/js/View/Track/GCContent.js#L42

cmdcolin commented 4 years ago

Also, instead of deeply poking into the "feature.record.alignmentStart" etc. I'd recommend calling things like feature.get('start') https://jbrowse.org/docs/faq.html#how-do-i-access-data-about-my-features-in-my-callback-or-plugin

cmdcolin commented 4 years ago

Closed by accident

maxime-bc commented 4 years ago

Thanks !

cmdcolin commented 4 years ago

Sure thing, let us know if you have any more questions or get any results, this would be a great feature to have

cmdcolin commented 4 years ago

We have added this feature in JBrowse 2 which is in alpha state just FYI

cmdcolin commented 4 years ago

https://github.com/GMOD/jbrowse-components repo

cmdcolin commented 4 years ago

It is likely that we will not backport this ourselves but PR are still welcome