Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataSetViewModel"/> class.
        /// </summary>
        /// <param name="dialogService">A dialog service for opening dialogs from the view model</param>
        public DataSetViewModel(IMainDialogService dialogService)
        {
            this.dialogService           = dialogService;
            this.ReadyToClose            = false;
            this.IdFileOpen              = false;
            this.SelectedPrSm            = new PrSm();
            this.ScanViewModel           = new ScanViewModel(dialogService, new List <PrSm>());
            this.CreateSequenceViewModel = new CreateSequenceViewModel(dialogService)
            {
                SelectedDataSetViewModel = this
            };

            this.CreateSequenceViewModel.CreateAndAddPrSmCommand.Subscribe(
                _ => this.ScanViewModel.Data.Add(this.CreateSequenceViewModel.SelectedPrSm));

            // Remove filter by raw file name from ScanViewModel filters
            this.ScanViewModel.Filters.Remove(this.ScanViewModel.Filters.FirstOrDefault(f => f.Name == "Raw File Name"));

            // When a PrSm is selected from the ScanViewModel, update the SelectedPrSm for this data set
            this.ScanViewModel.WhenAnyValue(x => x.SelectedPrSm).Where(prsm => prsm != null).Subscribe(x => this.SelectedPrSm = x);

            // When the scan number in the selected prsm changes, the selected scan in the xic plots should update
            this.WhenAnyValue(x => x.SelectedPrSm)
            .Where(_ => this.SelectedPrSm != null && this.SpectrumViewModel != null && this.XicViewModel != null)
            .Subscribe(prsm =>
            {
                this.SpectrumViewModel.UpdateSpectra(prsm.Scan, this.SelectedPrSm.PrecursorMz);
                this.XicViewModel.SetSelectedScan(prsm.Scan);
                this.XicViewModel.ZoomToScan(prsm.Scan);
            });

            var prsmObservable = this.WhenAnyValue(x => x.SelectedPrSm).Where(prsm => prsm != null);

            prsmObservable
            .Select(prsm => prsm.GetFragmentationSequence()).Where(fragSeq => fragSeq != null)
            .Subscribe(fragSeq =>
            {
                this.SpectrumViewModel.FragmentationSequence = fragSeq;
                this.XicViewModel.FragmentationSequence      = fragSeq;
            });

            // When the prsm changes, update the Scan View Model.
            prsmObservable.Subscribe(prsm => this.ScanViewModel.SelectedPrSm = prsm);

            // When the prsm updates, update the prsm in the sequence creator
            prsmObservable.Subscribe(prsm => this.CreateSequenceViewModel.SelectedPrSm = prsm);

            // When the prsm updates, update the feature map
            prsmObservable.Where(_ => this.FeatureMapViewModel != null).Subscribe(prsm => this.FeatureMapViewModel.FeatureMapViewModel.SelectedPrSm = prsm);

            // When prsm updates, subscribe to scan updates
            prsmObservable.Subscribe(prsm =>
            {
                prsm.WhenAnyValue(x => x.Scan, x => x.PrecursorMz)
                .Where(x => x.Item1 > 0 && x.Item2 > 0 && this.SpectrumViewModel != null)
                .Subscribe(x => this.SpectrumViewModel.UpdateSpectra(x.Item1, x.Item2));
                prsm.WhenAnyValue(x => x.Scan).Where(scan => scan > 0 && this.XicViewModel != null)
                .Subscribe(scan => this.XicViewModel.SetSelectedScan(scan));
            });

            // When a new prsm is created by CreateSequenceViewModel, update SelectedPrSm
            this.CreateSequenceViewModel.WhenAnyValue(x => x.SelectedPrSm).Subscribe(prsm => this.SelectedPrSm = prsm);

            // When IDs are filtered in the ScanViewModel, update feature map with new IDs
            this.ScanViewModel.WhenAnyValue(x => x.FilteredData).Where(_ => this.FeatureMapViewModel != null).Subscribe(data => this.FeatureMapViewModel.UpdateIds(data));

            // Toggle instrument data when ShowInstrumentData setting is changed.
            IcParameters.Instance.WhenAnyValue(x => x.ShowInstrumentData).Select(async x => await this.ScanViewModel.ToggleShowInstrumentDataAsync(x, (PbfLcMsRun)this.LcMs)).Subscribe();

            // When product ion tolerance or ion correlation threshold change, update scorer factory
            IcParameters.Instance.WhenAnyValue(x => x.ProductIonTolerancePpm, x => x.IonCorrelationThreshold)
            .Subscribe(
                x =>
            {
                var scorer = new ScorerFactory(x.Item1, Constants.MinCharge, Constants.MaxCharge, x.Item2);
                this.CreateSequenceViewModel.ScorerFactory = scorer;
                this.ScanViewModel.ScorerFactory           = scorer;
            });

            // When an ID file has been opened, turn on the unidentified scan filter
            this.WhenAnyValue(x => x.IdFileOpen)
            .Where(idFileOpen => idFileOpen)
            .Select(_ => this.ScanViewModel.Filters.FirstOrDefault(f => f.Name == "Hide Unidentified Scans"))
            .Where(f => f != null)
            .Subscribe(f => f.Selected = true);

            // Start MsPf Search Command
            var startMsPfSearchCommand = ReactiveCommand.Create();

            startMsPfSearchCommand.Subscribe(_ => this.StartMsPfSearchImplementation());
            this.StartMsPfSearchCommand = startMsPfSearchCommand;

            // Close command verifies that the user wants to close the dataset, then sets ReadyToClose to true if they are
            var closeCommand = ReactiveCommand.Create();

            closeCommand.Subscribe(_ =>
            {
                this.ReadyToClose =
                    dialogService.ConfirmationBox(
                        string.Format("Are you sure you would like to close {0}?", this.Title), string.Empty);
            });
            this.CloseCommand = closeCommand;
        }