async _loadProject()

in client/client/app/shared/projectContext/index.js [1679:1905]


    async _loadProject(reference, tracks, tracksState, tracksReordering, shouldAddAnnotationTracks) {
        let referenceDidChange = false;
        const oldVcfFiles = this.vcfTracks || [];
        const oldGeneFiles = this.geneTracks || [];
        if (!reference && !tracks && tracksState && tracksReordering) {
            return {referenceDidChange, vcfFilesChanged: false, geneFilesDidChanged: false, recoveredTracksState: undefined};
        }
        if (tracks || tracksState) {
            if (tracks) {
                this._tracks = tracks;
            } else if (tracksState) {
                await this.refreshReferences();
                const mapOpenByUrlTracksFn = (trackState) => ({
                    id: decodeURIComponent(trackState.bioDataItemId),
                    indexPath: decodeURIComponent(trackState.index),
                    bioDataItemId: decodeURIComponent(trackState.bioDataItemId),
                    duplicateId: decodeURIComponent(`${trackState.duplicateId || ''}`),
                    projectId: trackState.projectId || '',
                    projectIdNumber: trackState.projectIdNumber,
                    name: decodeURIComponent(trackState.bioDataItemId),
                    isLocal: trackState.isLocal,
                    format: trackState.format,
                    openByUrl: true
                });
                const __tracks = tracksState.filter(ts => ts.isLocal).map(mapOpenByUrlTracksFn);
                __tracks.forEach(t => {
                    const [__reference] = this._references.filter(r => r.name.toLowerCase() === t.projectId.toLowerCase());
                    if (__reference) {
                        t.reference = __reference;
                    }
                });
                const projectsIds = tracksState.reduce((ids, track) => {
                    if (!track.isLocal && ids.filter(t => t === track.projectId).length === 0) {
                        return [...ids, track.projectId];
                    }
                    return ids;
                }, []);
                const allTracksFromProjectIds = tracksState.filter(ts => ts.bioDataItemId === undefined).reduce((ids, track) => {
                    if (!track.isLocal && ids.filter(t => t === track.projectId).length === 0) {
                        return [...ids, track.projectId];
                    }
                    return ids;
                }, []);
                const predefinedTracksFromProjectIds = tracksState.filter(ts => ts.bioDataItemId !== undefined && PREDEFINED_TRACKS_SELECTORS.indexOf(ts.bioDataItemId.toLowerCase()) >= 0).reduce((ids, track) => {
                    if (!track.isLocal && ids.filter(t => t === track.projectId).length === 0) {
                        return [...ids, track.projectId];
                    }
                    return ids;
                }, []);
                if (!this._datasets || this._datasets.length === 0) {
                    await this.refreshDatasets();
                }
                for (let i = 0; i < projectsIds.length; i++) {
                    const _project = this.findDatasetByName(projectsIds[i]) || await this.projectDataService.getProject(projectsIds[i]);
                    if (_project && (!_project.reference || !reference || (reference.name !== undefined && reference.name.toLowerCase() === _project.reference.name.toLowerCase()))) {
                        const items = _project._lazyItems ? _project._lazyItems : _project.items;
                        if (allTracksFromProjectIds.indexOf(projectsIds[i]) >= 0) {
                            const [trackState] = tracksState.filter(ts => ts.projectId === projectsIds[i]);
                            const index = tracksState.indexOf(trackState);
                            if (index >= 0) {
                                const fn = (item) => ({
                                    bioDataItemId: item.name,
                                    duplicateId: item.duplicateId,
                                    projectId: _project.name || '',
                                    projectIdNumber: _project.id
                                });
                                tracksState.splice(index, 1, ...items.filter(item => item.format !== 'REFERENCE').map(fn));
                            }
                        }
                        if (predefinedTracksFromProjectIds.indexOf(projectsIds[i]) >= 0) {
                            const [trackState] = tracksState.filter(ts => ts.projectId === projectsIds[i]);
                            const index = tracksState.indexOf(trackState);
                            if (index >= 0) {
                                const fn = (item) => ({
                                    bioDataItemId: item.name,
                                    duplicateId: item.duplicateId,
                                    projectId: _project.name || '',
                                    projectIdNumber: _project.id
                                });
                                const predefinedTracks = [];
                                switch (trackState.bioDataItemId.toLowerCase()) {
                                    case REFERENCE_TRACK_SELECTOR: {
                                        const [__ref] = items.filter(r => r.format === 'REFERENCE');
                                        if (__ref) {
                                            predefinedTracks.push(__ref);
                                        }
                                    }
                                        break;
                                    case GENOME_TRACKS_SELECTOR: {
                                        const [__ref] = items.filter(r => r.format === 'REFERENCE');
                                        if (__ref) {
                                            predefinedTracks.push(__ref);
                                            if (__ref.geneFile) {
                                                const [__geneTrack] = items.filter(r => r.format === 'GENE' && r.name.toLowerCase() === __ref.geneFile.name.toLowerCase());
                                                if (__geneTrack) {
                                                    predefinedTracks.push(__geneTrack);
                                                }
                                            } else {
                                                const [__predefinedRef] = this._references.filter(r => r.name.toLowerCase() === __ref.name.toLowerCase());
                                                if (__predefinedRef && __predefinedRef.geneFile) {
                                                    const [__geneTrack] = items.filter(r => r.format === 'GENE' && r.name.toLowerCase() === __predefinedRef.geneFile.name.toLowerCase());
                                                    if (__geneTrack) {
                                                        predefinedTracks.push(__geneTrack);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                        break;
                                }
                                tracksState.splice(index, 1, ...predefinedTracks.map(fn));
                            }
                        }
                        tracksState.forEach(t => {
                            const [track] = items.filter(track => track.name &&
                                track.format &&
                                t.bioDataItemId !== undefined &&
                                (t.bioDataItemId.toString().toLowerCase() === track.name.toLowerCase() ||
                                    t.bioDataItemId.toString().toLowerCase() === track.bioDataItemId.toString().toLowerCase())
                                && t.projectId.toString().toLowerCase() === projectsIds[i].toString().toLowerCase()
                            );
                            if (track) {
                                __tracks.push({...track, duplicateId: t.duplicateId});
                            }
                        });
                    } else {
                        const wrongStates = tracksState.filter(ts => !ts.isLocal && ts.projectId === projectsIds[i]);
                        for (let i = 0; i < wrongStates.length; i++) {
                            const index = tracksState.indexOf(wrongStates[i]);
                            tracksState.splice(index, 1);
                        }
                    }
                }
                if (__tracks.filter(t => t.format === 'REFERENCE').length === 0) {
                    const [trackWithReference] = __tracks.filter(t => t.reference);
                    if (trackWithReference) {
                        trackWithReference.reference.projectId = trackWithReference.projectId;
                        __tracks.push(trackWithReference.reference);
                        tracksState.splice(0, 0, {
                            bioDataItemId: trackWithReference.reference.name,
                            duplicateId: trackWithReference.reference.duplicateId,
                            projectId: trackWithReference.projectId,
                            projectIdNumber: trackWithReference.projectIdNumber,
                        });
                    }
                }
                this._tracks = __tracks;
            }
            if (!reference) {
                const [referenceCandidate] = this._tracks.filter(t => t.format === 'REFERENCE');
                if (referenceCandidate) {
                    reference = {
                        name: referenceCandidate.name
                    };
                }
            }
            referenceDidChange = await this._changeReference(reference, shouldAddAnnotationTracks);
            if (shouldAddAnnotationTracks && this._reference && this._reference.annotationFiles) {
                const [referenceTrack] = this._tracks.filter(t => t.format === 'REFERENCE');
                let index = this._tracks.indexOf(referenceTrack);
                if (index < 0) {
                    index = this._tracks.length - 1;
                }
                for (let i = 0; i < this._reference.annotationFiles.length; i++) {
                    const annotationFile = this._reference.annotationFiles[i];
                    if (!annotationFile.selected) {
                        continue;
                    }
                    annotationFile.isLocal = true;
                    annotationFile.projectId = '';
                    if (this._tracks.filter(t => t.name.toLowerCase() === annotationFile.name.toLowerCase() && t.format.toLowerCase() === annotationFile.format.toLowerCase()).length === 0) {
                        this._tracks.push(annotationFile);
                        const savedState = this.getTrackState((annotationFile.name || '').toLowerCase(), '');
                        tracksState.splice(index + 1, 0, Object.assign(savedState || {}, {
                            bioDataItemId: annotationFile.name,
                            duplicateId: annotationFile.duplicateId,
                            projectId: '',
                            isLocal: true,
                            format: annotationFile.format
                        }));
                        index++;
                    }
                }
            }
            this._containsVcfFiles = this.vcfTracks.length > 0;
        } else if (tracks === null || reference === null) {
            this._tracks = [];
            referenceDidChange = await this._changeReference(null);
            this._containsVcfFiles = false;
            this._vcfInfo = [];
            this._vcfSampleAliases = {};
            this._vcfSampleInfo = {};
            this._infoFields = [];
            this._totalPagesCountVariations = 0;
            this._variationsPointer = null;
            this._variationsPointerPage = null;
            this._currentPageVariations = FIRST_PAGE;
            this._lastPageVariations = FIRST_PAGE;
            this._firstPageVariations = FIRST_PAGE;
            this._hasMoreVariations = true;
            this._variantsPageError = null;
            this._variantsDataByChromosomes = [];
            this._variantsDataByQuality = [];
            this._variantsDataByType = [];
        }
        let vcfFilesChanged = oldVcfFiles.length !== this.vcfTracks.length;
        if (!vcfFilesChanged) {
            for (let i = 0; i < oldVcfFiles.length; i++) {
                if (this.vcfTracks.filter(t => t.bioDataItemId.toString().toLowerCase() === oldVcfFiles[i].bioDataItemId.toString().toLowerCase()
                    && t.projectId.toLowerCase() === oldVcfFiles[i].projectId.toLowerCase()).length === 0) {
                    vcfFilesChanged = true;
                    break;
                }
            }
        }
        let geneFilesDidChanged = oldGeneFiles.length !== this.geneTracks.length;
        if (!geneFilesDidChanged) {
            for (let i = 0; i < oldGeneFiles.length; i++) {
                if (this.geneTracks.filter(t => t.bioDataItemId.toString().toLowerCase() === oldGeneFiles[i].bioDataItemId.toString().toLowerCase()
                    && t.projectId.toLowerCase() === oldGeneFiles[i].projectId.toLowerCase()).length === 0) {
                    geneFilesDidChanged = true;
                    break;
                }
            }
        }
        return {referenceDidChange, vcfFilesChanged, geneFilesDidChanged, recoveredTracksState: tracksState};
    }