Ejemplo n.º 1
0
 /// <summary>
 /// Gets the content of a JSON file to download corresponding to the provided network.
 /// </summary>
 /// <param name="network">The current network.</param>
 /// <param name="stream">The stream to which to write to.</param>
 /// <param name="serviceProvider">The application service provider.</param>
 /// <returns>The content of the JSON file corresponding to the provided network.</returns>
 public static async Task WriteToStreamCyjsFileContent(this Network network, Stream stream, IServiceProvider serviceProvider)
 {
     // Use a new scope.
     using var scope = serviceProvider.CreateScope();
     // Use a new context instance.
     using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
     // Get the required data.
     var data = new CytoscapeViewModel
     {
         Data = new CytoscapeViewModel.CytoscapeData
         {
             Id = network.Id,
             Name = network.Name,
             Description = network.Description
         },
         Elements = new CytoscapeViewModel.CytoscapeElements
         {
             Nodes = context.NetworkNodes
                 .Where(item => item.Network == network)
                 .Where(item => item.Type == NetworkNodeType.None)
                 .Select(item => item.Node)
                 .Select(item => new
                 {
                     Id = item.Id,
                     Name = item.Name,
                     Classes = item.NetworkNodes
                         .Where(item1 => item1.Network == network)
                         .Select(item => item.Type.ToString().ToLower())
                 })
                 .AsEnumerable()
                 .Select(item => new CytoscapeViewModel.CytoscapeElements.CytoscapeNode
                 {
                     Data = new CytoscapeViewModel.CytoscapeElements.CytoscapeNode.CytoscapeNodeData
                     {
                         Id = item.Id,
                         Name = item.Name,
                         Type = string.Join(",", item.Classes.OrderBy(item => item))
                     }
                 }),
             Edges = context.NetworkEdges
                 .Where(item => item.Network == network)
                 .Select(item => item.Edge)
                 .Select(item => new
                 {
                     Id = item.Id,
                     Name = item.Name,
                     SourceNodeId = item.EdgeNodes
                         .Where(item1 => item1.Type == EdgeNodeType.Source)
                         .Select(item1 => item1.Node)
                         .Where(item1 => item1 != null)
                         .Select(item1 => item1.Id)
                         .FirstOrDefault(),
                     TargetNodeId = item.EdgeNodes
                         .Where(item1 => item1.Type == EdgeNodeType.Target)
                         .Select(item1 => item1.Node)
                         .Where(item1 => item1 != null)
                         .Select(item1 => item1.Id)
                         .FirstOrDefault()
                 })
                 .AsEnumerable()
                 .Select(item => new CytoscapeViewModel.CytoscapeElements.CytoscapeEdge
                 {
                     Data = new CytoscapeViewModel.CytoscapeElements.CytoscapeEdge.CytoscapeEdgeData
                     {
                         Id = item.Id,
                         Name = item.Name,
                         Source = item.SourceNodeId,
                         Target = item.TargetNodeId
                     }
                 })
         }
     };
     // Write the data corresponding to the file.
     await JsonSerializer.SerializeAsync(stream, data, new JsonSerializerOptions { IgnoreNullValues = true });
 }
 /// <summary>
 /// Gets the content of a JSON file to download corresponding to the provided control path.
 /// </summary>
 /// <param name="controlPath">The current control path.</param>
 /// <param name="stream">The stream to which to write to.</param>
 /// <param name="serviceProvider">The application service provider.</param>
 /// <returns>The content of the JSON file corresponding to the provided control path.</returns>
 public static async Task WriteToStreamCyjsFileContent(this ControlPath controlPath, Stream stream, IServiceProvider serviceProvider)
 {
     // Use a new scope.
     using var scope = serviceProvider.CreateScope();
     // Use a new context instance.
     using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
     // Get the control data.
     var analysis = context.ControlPaths
                    .Where(item => item == controlPath)
                    .Select(item => item.Analysis)
                    .First();
     var controlNodes = context.ControlPaths
                        .Where(item => item == controlPath)
                        .Select(item => item.Paths)
                        .SelectMany(item => item)
                        .Select(item => item.PathNodes)
                        .SelectMany(item => item)
                        .Where(item => item.Type == PathNodeType.Source)
                        .Select(item => item.Node.Id)
                        .Where(item => !string.IsNullOrEmpty(item))
                        .ToHashSet();
     var controlEdges = context.ControlPaths
                        .Where(item => item == controlPath)
                        .Select(item => item.Paths)
                        .SelectMany(item => item)
                        .Select(item => item.PathEdges)
                        .SelectMany(item => item)
                        .Select(item => item.Edge.Id)
                        .Where(item => !string.IsNullOrEmpty(item))
                        .ToHashSet();
     // Return the view model.
     var data = new CytoscapeViewModel
     {
         Data = new CytoscapeViewModel.CytoscapeData
         {
             Id          = controlPath.Id,
             Name        = $"{controlPath.Analysis.Name} - Control Path",
             Description = controlPath.Analysis.Description
         },
         Elements = new CytoscapeViewModel.CytoscapeElements
         {
             Nodes = context.PathNodes
                     .Where(item => item.Path.ControlPath == controlPath)
                     .Where(item => item.Type == PathNodeType.None)
                     .Select(item => item.Node)
                     .Select(item => new
             {
                 Id      = item.Id,
                 Name    = item.Name,
                 Classes = item.AnalysisNodes
                           .Where(item1 => item1.Analysis == analysis)
                           .Select(item1 => item1.Type.ToString().ToLower())
             })
                     .AsEnumerable()
                     .Select(item => new CytoscapeViewModel.CytoscapeElements.CytoscapeNode
             {
                 Data = new CytoscapeViewModel.CytoscapeElements.CytoscapeNode.CytoscapeNodeData
                 {
                     Id   = item.Id,
                     Name = item.Name,
                     Type = string.Join(",", item.Classes.Concat(controlNodes.Contains(item.Id) ? new List <string> {
                         "control"
                     } : new List <string> {
                     }).OrderBy(item => item))
                 }
             }),
             Edges = context.PathEdges
                     .Where(item => item.Path.ControlPath == controlPath)
                     .Select(item => item.Edge)
                     .Select(item => new
             {
                 Id           = item.Id,
                 Name         = item.Name,
                 SourceNodeId = item.EdgeNodes
                                .Where(item1 => item1.Type == EdgeNodeType.Source)
                                .Select(item1 => item1.Node)
                                .Where(item1 => item1 != null)
                                .Select(item1 => item1.Id)
                                .FirstOrDefault(),
                 TargetNodeId = item.EdgeNodes
                                .Where(item1 => item1.Type == EdgeNodeType.Target)
                                .Select(item1 => item1.Node)
                                .Where(item1 => item1 != null)
                                .Select(item1 => item1.Id)
                                .FirstOrDefault()
             })
                     .AsEnumerable()
                     .Select(item => new CytoscapeViewModel.CytoscapeElements.CytoscapeEdge
             {
                 Data = new CytoscapeViewModel.CytoscapeElements.CytoscapeEdge.CytoscapeEdgeData
                 {
                     Id     = item.Id,
                     Name   = item.Name,
                     Source = item.SourceNodeId,
                     Target = item.TargetNodeId,
                     Type   = string.Join(",", controlEdges.Contains(item.Id) ? new List <string> {
                         "control"
                     } : new List <string> {
                     })
                 }
             })
         }
     };
     // Write the data corresponding to the file.
     await JsonSerializer.SerializeAsync(stream, data, new JsonSerializerOptions { IgnoreNullValues = true });
 }