1 /**
2 * @file setNamesFromIds.cpp
3 * @brief Utility program, renaming all Names to match their ids.
4 *
5 * @author Frank T. Bergmann
6 *
7 * <!--------------------------------------------------------------------------
8 * This sample program is distributed under a different license than the rest
9 * of libSBML. This program uses the open-source MIT license, as follows:
10 *
11 * Copyright (c) 2013-2018 by the California Institute of Technology
12 * (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
13 * and the University of Heidelberg (Germany), with support from the National
14 * Institutes of Health (USA) under grant R01GM070923. All rights reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 * DEALINGS IN THE SOFTWARE.
33 *
34 * Neither the name of the California Institute of Technology (Caltech), nor
35 * of the European Bioinformatics Institute (EMBL-EBI), nor of the University
36 * of Heidelberg, nor the names of any contributors, may be used to endorse
37 * or promote products derived from this software without specific prior
38 * written permission.
39 * ------------------------------------------------------------------------ -->
40 */
41
42
43 #include <iostream>
44 #include <sstream>
45 #include <vector>
46 #include <string>
47
48 #include <sbml/SBMLTypes.h>
49 #include <sbml/common/extern.h>
50 #include <sbml/common/operationReturnValues.h>
51 #include "util.h"
52
53
54 using namespace std;
55 LIBSBML_CPP_NAMESPACE_USE
56
57
58 /**
59 * The NameIdTransformer class transforms the name of a given SBase
60 * element by replacing it with its id.
61 *
62 * It won't do anything if the name is the same as the id, or if
63 * no id is set.
64 */
65 class NameIdTransformer : public IdentifierTransformer
66 {
67 public:
NameIdTransformer()68 NameIdTransformer()
69 : IdentifierTransformer()
70 {
71 }
72
73 /**
74 * The actual transform implementation
75 */
transform(SBase * element)76 int transform(SBase* element)
77 {
78 // return in case we don't have a valid element
79 if (element == NULL || element->getTypeCode() == SBML_LOCAL_PARAMETER)
80 return LIBSBML_OPERATION_SUCCESS;
81
82 // or if there is nothing to do
83 if (!element->isSetId() || element->getId() == element->getName())
84 return LIBSBML_OPERATION_SUCCESS;
85
86 // set it
87 element->setName(element->getId());
88
89
90 return LIBSBML_OPERATION_SUCCESS;
91
92 }
93
94 };
95
96 BEGIN_C_DECLS
97
98 int
main(int argc,char * argv[])99 main (int argc, char* argv[])
100 {
101 if (argc != 3)
102 {
103 cout << endl << "Usage: setNamesFromIds filename output" << endl << endl;
104 return 1;
105 }
106
107 const char* filename = argv[1];
108 const char* output = argv[2];
109
110
111 SBMLDocument* document;
112 SBMLReader reader;
113 #ifdef __BORLANDC__
114 unsigned long start, stop;
115 #else
116 unsigned long long start, stop;
117 #endif
118
119 start = getCurrentMillis();
120 document = reader.readSBML(filename);
121 stop = getCurrentMillis();
122
123 unsigned int errors = document->getNumErrors(LIBSBML_SEV_ERROR);
124
125 cout << endl;
126 cout << " filename: " << filename << endl;
127 cout << " read time (ms): " << stop - start << endl;
128
129 if (errors > 0)
130 {
131 cout << " error(s): " << errors << endl;
132 document->printErrors(cerr);
133 delete document;
134 return errors;
135 }
136
137 start = stop;
138
139 // get a list of all elements, as we will need to know all identifiers
140 List* allElements = document->getAllElements();
141
142 // create the transformer
143 NameIdTransformer trans;
144
145 // rename the identifiers (using the elements we already gathered before)
146 document->getModel()->renameIDs(allElements, &trans);
147 stop = getCurrentMillis();
148 cout << " rename time (ms): " << stop - start << endl;
149 start = stop;
150
151 // write to file
152 writeSBMLToFile(document, output);
153 stop = getCurrentMillis();
154 cout << " write time (ms): " << stop - start << endl;
155 cout << endl;
156
157 delete allElements;
158 delete document;
159 return errors;
160 }
161
162 END_C_DECLS
163