예제 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Get the current user.
            var user = await _userManager.GetUserAsync(User);

            // Define the view.
            View = new ViewModel
            {
                IsUserAuthenticated = user != null,
                SampleItems         = _context.Samples
                                      .Where(item => item.SampleDatabases.Any(item1 => item1.Database.DatabaseType.Name == "PPI"))
                                      .Select(item => new SampleItemModel
                {
                    Id          = item.Id,
                    Name        = item.Name,
                    Description = item.Description
                }),
                NodeDatabases = _context.Databases
                                .Where(item => item.DatabaseType.Name == "PPI")
                                .Where(item => item.IsPublic || item.DatabaseUsers.Any(item1 => item1.User == user))
                                .Where(item => item.DatabaseNodeFields.Any(item1 => item1.IsSearchable)),
                EdgeDatabases = _context.Databases
                                .Where(item => item.DatabaseType.Name == "PPI")
                                .Where(item => item.IsPublic || item.DatabaseUsers.Any(item1 => item1.User == user))
                                .Where(item => item.DatabaseEdges.Any()),
                SeedNodeCollections = _context.NodeCollections
                                      .Where(item => item.NodeCollectionDatabases.Any(item1 => item1.Database.DatabaseType.Name == "PPI"))
                                      .Where(item => item.NodeCollectionDatabases.Any(item1 => item1.Database.IsPublic || item1.Database.DatabaseUsers.Any(item2 => item2.User == user)))
            };
            // Check if there weren't any node databases available.
            if (View.NodeDatabases == null || !View.NodeDatabases.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: A new network can't be created, as there are no protein databases available.";
                // Redirect to the index page.
                return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Networks/Index"));
            }
            // Check if there weren't any edge databases available.
            if (View.EdgeDatabases == null || !View.EdgeDatabases.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: A new network can't be created, as there are no interaction databases available.";
                // Redirect to the index page.
                return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Networks/Index"));
            }
            // Check if the reCaptcha is valid.
            if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The reCaptcha verification failed.");
                // Return the page.
                return(Page());
            }
            // Check if the provided model isn't valid.
            if (!ModelState.IsValid)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields.");
                // Redisplay the page.
                return(Page());
            }
            // Check if the public availability isn't valid.
            if (!View.IsUserAuthenticated && !Input.IsPublic)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "You are not logged in, so the network must be set as public.");
                // Redisplay the page.
                return(Page());
            }
            // Try to get the algorithm.
            try
            {
                // Get the algorithm.
                var algorithm = EnumerationExtensions.GetEnumerationValue <NetworkAlgorithm>(Input.Algorithm);
            }
            catch (Exception)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The network generation algorithm couldn't be determined from the provided string.");
                // Redisplay the page.
                return(Page());
            }
            // Check if the algorithm is not valid.
            if (!Enum.GetValues(typeof(NetworkAlgorithm)).Cast <NetworkAlgorithm>().Where(item => item != NetworkAlgorithm.None).Select(item => item.ToString()).Contains(Input.Algorithm))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The network generation algorithm is not valid.");
                // Redisplay the page.
                return(Page());
            }
            // Try to deserialize the node database data.
            if (!Input.NodeDatabaseData.TryDeserializeJsonObject <IEnumerable <string> >(out var nodeDatabaseIds) || nodeDatabaseIds == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The provided protein database data could not be deserialized.");
                // Redisplay the page.
                return(Page());
            }
            // Check if there weren't any node database IDs provided.
            if (!nodeDatabaseIds.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "At least one protein database ID must be provided.");
                // Redisplay the page.
                return(Page());
            }
            // Try to get the node databases with the provided IDs.
            var nodeDatabases = View.NodeDatabases.Where(item => nodeDatabaseIds.Contains(item.Id));

            // Check if there weren't any node databases found.
            if (!nodeDatabases.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "No protein databases could be found with the provided ID(s).");
                // Redisplay the page.
                return(Page());
            }
            // Try to deserialize the edge database data.
            if (!Input.EdgeDatabaseData.TryDeserializeJsonObject <IEnumerable <string> >(out var edgeDatabaseIds) || edgeDatabaseIds == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The provided interaction database data could not be deserialized.");
                // Redisplay the page.
                return(Page());
            }
            // Check if there weren't any edge database IDs provided.
            if (!edgeDatabaseIds.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "At least one interaction database ID must be provided.");
                // Redisplay the page.
                return(Page());
            }
            // Try to get the edge databases with the provided IDs.
            var edgeDatabases = View.EdgeDatabases.Where(item => edgeDatabaseIds.Contains(item.Id));

            // Check if there weren't any edge databases found.
            if (!edgeDatabases.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "No interaction databases could be found with the provided ID(s).");
                // Redisplay the page.
                return(Page());
            }
            // Try to deserialize the seed node collection data.
            if (!Input.SeedNodeCollectionData.TryDeserializeJsonObject <IEnumerable <string> >(out var seedNodeCollectionIds) || seedNodeCollectionIds == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The provided seed protein collection data could not be deserialized.");
                // Redisplay the page.
                return(Page());
            }
            // Try to get the seed node collections with the provided IDs.
            var seedNodeCollections = View.SeedNodeCollections.Where(item => seedNodeCollectionIds.Contains(item.Id));

            // Try to deserialize the seed data.
            if (!Input.SeedNodeData.TryDeserializeJsonObject <IEnumerable <string> >(out var items) || items == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The provided seed data could not be deserialized.");
                // Redisplay the page.
                return(Page());
            }
            // Check if there weren't any items found.
            if (!items.Any() && !seedNodeCollections.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "No items could be found within the provided seed data or the selected seed protein collections.");
                // Redisplay the page.
                return(Page());
            }
            // Serialize the seed data.
            var data = JsonSerializer.Serialize(items
                                                .Select(item => new NetworkNodeInputModel
            {
                Node = new NodeInputModel
                {
                    Id = item
                },
                Type = "Seed"
            }));
            // Define a new task.
            var task = new NetworksTask
            {
                Scheme    = HttpContext.Request.Scheme,
                HostValue = HttpContext.Request.Host.Value,
                Items     = new List <NetworkInputModel>
                {
                    new NetworkInputModel
                    {
                        Name             = Input.Name,
                        Description      = Input.Description,
                        IsPublic         = Input.IsPublic,
                        Algorithm        = Input.Algorithm,
                        Data             = data,
                        NetworkDatabases = nodeDatabases
                                           .Select(item => item.Id)
                                           .Select(item => new NetworkDatabaseInputModel
                        {
                            Database = new DatabaseInputModel
                            {
                                Id = item
                            },
                            Type = "Node"
                        })
                                           .Concat(edgeDatabases
                                                   .Select(item => item.Id)
                                                   .Select(item => new NetworkDatabaseInputModel
                        {
                            Database = new DatabaseInputModel
                            {
                                Id = item
                            },
                            Type = "Edge"
                        })),
                        NetworkUsers = View.IsUserAuthenticated ?
                                       new List <NetworkUserInputModel>
                        {
                            new NetworkUserInputModel
                            {
                                User = new UserInputModel
                                {
                                    Id = user.Id
                                }
                            }
                        } :
                        new List <NetworkUserInputModel>(),
                        NetworkNodeCollections = seedNodeCollections
                                                 .Select(item => item.Id)
                                                 .Select(item => new NetworkNodeCollectionInputModel
                        {
                            NodeCollection = new NodeCollectionInputModel
                            {
                                Id = item
                            },
                            Type = "Seed"
                        })
                    }
                }
            };
            // Define the IDs of the created items.
            var ids = Enumerable.Empty <string>();

            // Try to run the task.
            try
            {
                // Run the task.
                ids = await task.CreateAsync(_serviceProvider, CancellationToken.None);
            }
            catch (Exception exception)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, exception.Message);
                // Redisplay the page.
                return(Page());
            }
            // Check if there wasn't any ID returned.
            if (ids != null && ids.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = $"Success: 1 PPI network defined successfully with the ID \"{ids.First()}\" and scheduled for generation.";
                // Redirect to the index page.
                return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Networks/Details/Index", new { id = ids.First() }));
            }
            // Display a message.
            TempData["StatusMessage"] = $"Success: 1 PPI network defined successfully and scheduled for generation.";
            // Redirect to the index page.
            return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Networks/Index"));
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Get the current user.
            var user = await _userManager.GetUserAsync(User);

            // Define the view.
            View = new ViewModel
            {
                IsUserAuthenticated = user != null,
                SampleItems         = _context.Samples
                                      .Where(item => item.SampleDatabases.Any(item1 => item1.Database.DatabaseType.Name == "Generic"))
                                      .Select(item => new SampleItemModel
                {
                    Id          = item.Id,
                    Name        = item.Name,
                    Description = item.Description
                })
            };
            // Get the available databases.
            var databases = _context.Databases
                            .Where(item => item.DatabaseType.Name == "Generic")
                            .Where(item => item.IsPublic || item.DatabaseUsers.Any(item1 => item1.User == user));

            // Check if there weren't any databases available.
            if (!databases.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: A new network can't be created, as there are no databases available.";
                // Redirect to the index page.
                return(RedirectToPage("/Content/DatabaseTypes/Generic/Created/Networks/Index"));
            }
            // Check if the reCaptcha is valid.
            if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The reCaptcha verification failed.");
                // Return the page.
                return(Page());
            }
            // Check if the provided model isn't valid.
            if (!ModelState.IsValid)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields.");
                // Redisplay the page.
                return(Page());
            }
            // Check if the public availability isn't valid.
            if (!View.IsUserAuthenticated && !Input.IsPublic)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "You are not logged in, so the network must be set as public.");
                // Redisplay the page.
                return(Page());
            }
            // Try to deserialize the seed data.
            if (!Input.SeedEdgeData.TryDeserializeJsonObject <IEnumerable <EdgeItemModel> >(out var items) || items == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The provided seed data could not be deserialized.");
                // Redisplay the page.
                return(Page());
            }
            // Check if there weren't any items found.
            if (!items.Any())
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "No edges could be found within the provided seed data.");
                // Redisplay the page.
                return(Page());
            }
            // Serialize the seed data.
            var data = JsonSerializer.Serialize(items
                                                .Select(item => new NetworkEdgeInputModel
            {
                Edge = new EdgeInputModel
                {
                    EdgeNodes = new List <EdgeNodeInputModel>
                    {
                        new EdgeNodeInputModel
                        {
                            Node = new NodeInputModel
                            {
                                Id = item.SourceNode
                            },
                            Type = "Source"
                        },
                        new EdgeNodeInputModel
                        {
                            Node = new NodeInputModel
                            {
                                Id = item.TargetNode
                            },
                            Type = "Target"
                        }
                    }
                }
            }));
            // Define a new task.
            var task = new NetworksTask
            {
                Scheme    = HttpContext.Request.Scheme,
                HostValue = HttpContext.Request.Host.Value,
                Items     = new List <NetworkInputModel>
                {
                    new NetworkInputModel
                    {
                        Name             = Input.Name,
                        Description      = Input.Description,
                        IsPublic         = Input.IsPublic,
                        Algorithm        = NetworkAlgorithm.None.ToString(),
                        Data             = data,
                        NetworkDatabases = databases
                                           .Select(item => item.Id)
                                           .Select(item => new NetworkDatabaseInputModel
                        {
                            Database = new DatabaseInputModel
                            {
                                Id = item
                            },
                            Type = "Node"
                        })
                                           .Concat(databases
                                                   .Select(item => item.Id)
                                                   .Select(item => new NetworkDatabaseInputModel
                        {
                            Database = new DatabaseInputModel
                            {
                                Id = item
                            },
                            Type = "Edge"
                        })),
                        NetworkUsers = View.IsUserAuthenticated ?
                                       new List <NetworkUserInputModel>
                        {
                            new NetworkUserInputModel
                            {
                                User = new UserInputModel
                                {
                                    Id = user.Id
                                }
                            }
                        } :
                        new List <NetworkUserInputModel>()
                    }
                }
            };
            // Define the IDs of the created items.
            var ids = Enumerable.Empty <string>();

            // Try to run the task.
            try
            {
                // Run the task.
                ids = await task.CreateAsync(_serviceProvider, CancellationToken.None);
            }
            catch (Exception exception)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, exception.Message);
                // Redisplay the page.
                return(Page());
            }
            // Check if there wasn't any ID returned.
            if (ids != null && ids.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = $"Success: 1 generic network defined successfully with the ID \"{ids.First()}\" and scheduled for generation.";
                // Redirect to the index page.
                return(RedirectToPage("/Content/DatabaseTypes/Generic/Created/Networks/Details/Index", new { id = ids.First() }));
            }
            // Display a message.
            TempData["StatusMessage"] = $"Success: 1 generic network of type defined successfully and scheduled for generation.";
            // Redirect to the index page.
            return(RedirectToPage("/Content/DatabaseTypes/Generic/Created/Networks/Index"));
        }