File: CustomResolver.cs

package info (click to toggle)
libsbml 5.17.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 156,336 kB
  • sloc: cpp: 1,012,766; xml: 314,374; cs: 58,129; ansic: 54,053; python: 27,299; java: 27,152; makefile: 9,721; perl: 9,039; sh: 8,777; ruby: 4,760; php: 202; csh: 3
file content (147 lines) | stat: -rw-r--r-- 4,904 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * @file CustomResolver.cs
 * @brief Demonstrates implementation of a custom resolver for comp models
 * @author Frank Bergmann
 * 
 * <!--------------------------------------------------------------------------
 * This sample program is distributed under a different license than the rest
 * of libSBML.  This program uses the open-source MIT license, as follows:
 *
 * Copyright (c) 2013-2018 by the California Institute of Technology
 * (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
 * and the University of Heidelberg (Germany), with support from the National
 * Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Neither the name of the California Institute of Technology (Caltech), nor
 * of the European Bioinformatics Institute (EMBL-EBI), nor of the University
 * of Heidelberg, nor the names of any contributors, may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * ------------------------------------------------------------------------ -->
 */

using System;
using System.Net;
using libsbmlcs;


public class CustomResolver : SBMLResolver
{
  public CustomResolver()
    : base()
  {
  }


  public CustomResolver(CustomResolver orig)
    : base(orig)
  {
  }


  public override
  SBMLResolver clone()
  {
    return new CustomResolver(this);
  }


  public override
  SBMLDocument resolve(String uri, String baseUri)
  {
    try
    {
      WebClient wc = new WebClient();
      string content = wc.DownloadString(uri);
      return libsbml.readSBMLFromString(content);
    }
    catch
    {
      // couldn't download file handle this somehow
      return null;
    }
  }


  public override
  SBMLUri resolveUri(String uri, String baseUri)
  {
    SBMLUri uriObject = new SBMLUri(uri);
    if (uriObject.getScheme() != "http") return null;
    return uriObject;
  }


  public static void Main(String[] args)
  {
    if (!SBMLExtensionRegistry.isPackageEnabled("comp"))
    {
      Console.WriteLine("This copy of libSBML does not contain the 'comp' extension");
      Console.WriteLine("Unable to proceed with the resolver example the model.");
      Environment.Exit(2);
    }

    // create custom resolver
    CustomResolver resolver = new CustomResolver();

    // add the resolver and store its index, so we can free it later.
    int index = SBMLResolverRegistry.getInstance().addResolver(resolver);

    // create a new document with comp enabled
    SBMLDocument doc = new SBMLDocument(new CompPkgNamespaces());

    // get a hold of a plugin object
    CompSBMLDocumentPlugin plugin = (CompSBMLDocumentPlugin)doc.getPlugin("comp");

    // create an external model definition
    ExternalModelDefinition external = plugin.createExternalModelDefinition();

    // set the source to the URI
    external.setSource("http://www.ebi.ac.uk/biomodels-main/download?mid=BMID000000063853");

    // resolve the model
    Model model = external.getReferencedModel();

    if (model == null)
    {
      Console.Error.WriteLine("couldn't resolve");
      Environment.Exit(2);
    }

    // model is ready to be used now, however, only as long and the document
    // holding the external model definition is still alive and referenced

    Console.WriteLine("Model id: " + model.getId());
    Console.WriteLine("# species: " + model.getNumSpecies());
    Console.WriteLine("# reactions: " + model.getNumReactions());

    // now that we are done get rid of the resolver
    SBMLResolverRegistry.getInstance().removeResolver(index);
    // also clear the resolver instance, just to be sure that it has 
    // no more references to the C# resolver
    SBMLResolverRegistry.deleteResolerRegistryInstance();

    // finally we can get rid of the C# resolver 
    resolver = null;

  }

}