<commit_before>import React from 'react';
import $ from 'jquery';
import {getHierarchyData} from "shared/lib/StoreUtils";
//Import jstree.min and style for jstree to work
import 'jstree/dist/jstree.min'; // tslint:disable-line
import 'shared/components/query/styles/jstree/style.css'; // tslint:disable-line
import * as _ from "lodash";
import { remoteData } from "shared/api/remoteData";
import CBioPortalAPIInternal, { GenesetHierarchyInfo } from "shared/api/generated/CBioPortalAPIInternal";
import { observer } from "mobx-react";
import { observable, ObservableMap } from "mobx";
import LoadingIndicator from "shared/components/loadingIndicator/LoadingIndicator";

export interface GenesetsJsTreeProps
{
    initialSelection: string[];
    scoreThreshold: number;
    pvalueThreshold: number;
    percentile: number;
    gsvaProfile: string;
    sampleListId: string|undefined;
    searchValue: string;
    onSelect: (map_geneSet_selected:ObservableMap<boolean>) => void;
}

type JSNode = {
    original: {description: string, geneset: boolean};
};

@observer
export default class GenesetsJsTree extends React.Component<GenesetsJsTreeProps, {}>
{
    tree: Element|null;
    @observable isLoading = true;
    promisedTree: Promise<Element>;
    private readonly map_geneSets_selected = new ObservableMap<boolean>();

    constructor(props: GenesetsJsTreeProps)
    {
        super(props);
        this.map_geneSets_selected.replace(props.initialSelection.map(geneSet => [geneSet, true]));
    }
    
    componentDidMount(){
        this.promisedTree = this.initTree(this.tree as Element); //React only sets the div to null when the component unmounts
        this.promisedTree.then(tree => (this.isLoading = false, tree));
    }
    
    componentDidUpdate(prevProps: GenesetsJsTreeProps){
        if (this.props.scoreThreshold !== prevProps.scoreThreshold ||
            this.props.pvalueThreshold !== prevProps.pvalueThreshold ||
            this.props.percentile !== prevProps.percentile) {
            this.promisedTree = this.promisedTree
                .then(tree => (this.isLoading = true, this.replaceTree(tree)))
                .then(tree => (this.isLoading = false, tree));
        }
        if (this.props.searchValue !== prevProps.searchValue) {
            this.promisedTree = this.promisedTree
                .then(tree => this.searchTree(tree, this.props.searchValue));
        }
    }
    
    /**
     * Transform hierarchy data to the correct format accepted by jsTree
     * 
     * @param result_data
     * @return flatData
     */
    transformHierarchyData(result_data: GenesetHierarchyInfo[]) {
          const data = result_data;
          const flatData = [];
          
          // Leafs can be non-unique, therefore a unique ID is necessary,
          // because selecting a duplicate leaf results in a visualization issue.
          let leafId = 0;
        
          for (const node of data) {
              const nodeName = node.nodeName;
              const nodeParent = node.parentNodeName ? node.parentNodeName : "#";
              
              flatData.push({
                  id : nodeName,
                  parent : nodeParent,
                  text: nodeName,
                  li_attr: {
                      name: nodeName,
                  },
                  geneset : false,
                  state : {
                      opened : true,
                      selected : false,
                  }
              });
              
              // Check if node has any gene sets, and if so, iterate over them<commit_msg>Remove function comment<commit_after>import React from 'react';
import $ from 'jquery';
import {getHierarchyData} from "shared/lib/StoreUtils";
//Import jstree.min and style for jstree to work
import 'jstree/dist/jstree.min'; // tslint:disable-line
import 'shared/components/query/styles/jstree/style.css'; // tslint:disable-line
import * as _ from "lodash";
import { remoteData } from "shared/api/remoteData";
import CBioPortalAPIInternal, { GenesetHierarchyInfo } from "shared/api/generated/CBioPortalAPIInternal";
import { observer } from "mobx-react";
import { observable, ObservableMap } from "mobx";
import LoadingIndicator from "shared/components/loadingIndicator/LoadingIndicator";

export interface GenesetsJsTreeProps
{
    initialSelection: string[];
    scoreThreshold: number;
    pvalueThreshold: number;
    percentile: number;
    gsvaProfile: string;
    sampleListId: string|undefined;
    searchValue: string;
    onSelect: (map_geneSet_selected:ObservableMap<boolean>) => void;
}

type JSNode = {
    original: {description: string, geneset: boolean};
};

@observer
export default class GenesetsJsTree extends React.Component<GenesetsJsTreeProps, {}>
{
    tree: Element|null;
    @observable isLoading = true;
    promisedTree: Promise<Element>;
    private readonly map_geneSets_selected = new ObservableMap<boolean>();

    constructor(props: GenesetsJsTreeProps)
    {
        super(props);
        this.map_geneSets_selected.replace(props.initialSelection.map(geneSet => [geneSet, true]));
    }
    
    componentDidMount(){
        this.promisedTree = this.initTree(this.tree as Element); //React only sets the div to null when the component unmounts
        this.promisedTree.then(tree => (this.isLoading = false, tree));
    }
    
    componentDidUpdate(prevProps: GenesetsJsTreeProps){
        if (this.props.scoreThreshold !== prevProps.scoreThreshold ||
            this.props.pvalueThreshold !== prevProps.pvalueThreshold ||
            this.props.percentile !== prevProps.percentile) {
            this.promisedTree = this.promisedTree
                .then(tree => (this.isLoading = true, this.replaceTree(tree)))
                .then(tree => (this.isLoading = false, tree));
        }
        if (this.props.searchValue !== prevProps.searchValue) {
            this.promisedTree = this.promisedTree
                .then(tree => this.searchTree(tree, this.props.searchValue));
        }
    }
    
    transformHierarchyData(result_data: GenesetHierarchyInfo[]) {
          const data = result_data;
          const flatData = [];
          
          // Leafs can be non-unique, therefore a unique ID is necessary,
          // because selecting a duplicate leaf results in a visualization issue.
          let leafId = 0;
        
          for (const node of data) {
              const nodeName = node.nodeName;
              const nodeParent = node.parentNodeName ? node.parentNodeName : "#";
              
              flatData.push({
                  id : nodeName,
                  parent : nodeParent,
                  text: nodeName,
                  li_attr: {
                      name: nodeName,
                  },
                  geneset : false,
                  state : {
                      opened : true,
                      selected : false,
                  }
              });
              
              // Check if node has any gene sets, and if so, iterate over them
{'fraction_non_alphanumeric': 0.09887608479157775, 'fraction_numerical': 0.0002845354958031014, 'mean_word_length': 2.3364973896535357, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3305651', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1864, 'n_words': 459}