Program that renames all SIds that also have names specified. The new identifiers will be derived from the name, with all invalid characters removed.
using System.Collections.Generic;
public class SetIdFromNames : IdentifierTransformer
{
public static int Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("{0}Usage: setIdFromNames filename output{0}{0}", Environment.NewLine);
return 1;
}
string filename = args[0];
string output = args[1];
long start = DateTime.Now.Ticks;
SBMLDocument document = libsbml.readSBMLFromFile(filename);
long stop = DateTime.Now.Ticks;
Console.WriteLine();
Console.WriteLine(" filename: {0}", filename);
Console.WriteLine(" read time (ms): {0}", TimeSpan.FromTicks(stop - start).TotalMilliseconds);
long errors = document.getNumErrors(libsbml.LIBSBML_SEV_ERROR);
if (errors > 0)
{
Console.WriteLine(" error(s): {0}", errors);
document.printErrors();
return (int)errors;
}
SBaseList allElements = document.getListOfAllElements();
var allIds = getAllIds(allElements);
var trans = new SetIdFromNames(allIds);
start = DateTime.Now.Ticks;
document.getModel().renameIDs(allElements, trans);
stop = DateTime.Now.Ticks;
Console.WriteLine(" rename time (ms): {0}", TimeSpan.FromTicks(stop - start).TotalMilliseconds);
start = DateTime.Now.Ticks;
libsbml.writeSBMLToFile(document, output);
stop = DateTime.Now.Ticks;
Console.WriteLine(" write time (ms): {0}", TimeSpan.FromTicks(stop - start).TotalMilliseconds);
Console.WriteLine();
return 0;
}
public SetIdFromNames(List<string> ids)
{
existingIds = ids;
}
public string NameToSbmlId(string name)
{
var IdStream = new StringBuilder();
int count = 0;
int end = name.Length;
if ('0' <= name[count] && name[count] <= '9')
{
IdStream.Append('_');
}
for (; count != end; ++count)
{
if (0x80 == (name[count] & 0xc0))
{
continue;
}
if (('0' <= name[count] && name[count] <= '9') ||
('a' <= name[count] && name[count] <= 'z') ||
('A' <= name[count] && name[count] <= 'Z'))
{
IdStream.Append(name[count]);
}
else
{
IdStream.Append('_');
}
}
string Id = IdStream.ToString();
if (Id[Id.Length - 1] != '_')
{
return Id;
}
return Id.Substring(0, Id.Length - 1);
}
string getValidIdForName(string name)
{
string baseString = NameToSbmlId(name);
string id = baseString;
int count = 1;
while (existingIds.Contains(id))
{
id = string.Format("{0}_{1}", baseString, count);
++count;
}
return id;
}
readonly List<string> existingIds;
public override int transform(SBase element)
{
if (element == null || element.getTypeCode() == libsbml.SBML_LOCAL_PARAMETER)
return libsbml.LIBSBML_OPERATION_SUCCESS;
if (!element.isSetName() || element.getId() == element.getName())
return libsbml.LIBSBML_OPERATION_SUCCESS;
string newId = getValidIdForName(element.getName());
element.setId(newId);
existingIds.Add(newId);
return libsbml.LIBSBML_OPERATION_SUCCESS;
}
public static List<string> getAllIds(SBaseList allElements)
{
var result = new List<string>();
if (allElements == null || allElements.getSize() == 0)
return result;
for (uint i = 0; i < allElements.getSize(); ++i)
{
SBase current = allElements.get(i);
if (current.isSetId() && current.getTypeCode() != libsbml.SBML_LOCAL_PARAMETER)
{
result.Add(current.getId());
}
}
return result;
}
}