gt: 						int parentID = Integer.parseInt(value);
context: /*
   * Concept profile generation tool suite
   * Copyright (C) 2015 Biosemantics Group, Erasmus University Medical Center,
   *  Rotterdam, The Netherlands
   * 
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   * 
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>
   */
  
  package org.erasmusmc.ontology;
  
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import org.erasmusmc.ids.DatabaseID;
  import org.erasmusmc.ontology.Concept.DisambiguationType;
  import org.erasmusmc.ontology.ontologyutilities.OntologyCurator;
  import org.erasmusmc.utilities.ReadTextFile;
  import org.erasmusmc.utilities.StringUtilities;
  import org.erasmusmc.utilities.WriteTextFile;
  
  public class OntologyFileLoader {
  	public static boolean semanticTypesNegative = true;
  
  	private boolean loadTermsOnly = false;
  	private String version = "UNKNOWN";
  
  	public static void main(String[] args) {
  		String source = null;
  		String target = null;
  
  		try {
  			if (args.length == 0) {
  				throw new IllegalArgumentException();
  			}
  
  			source = args[0];
  			target = source.replaceAll("\\.psf$", ".ontology");
  
  			if (source.equals(target)) {
  				throw new IllegalArgumentException();
  			}
  		} catch (IllegalArgumentException e) {
  			System.out.println("Usage: some_file.psf");
  			System.out.println("The output file some_file.ontology will be created.");
  			System.exit(0);
  		}
  
  		String name = "UMLS";
  		convertPSFToOntology(source, target, name, 3000000);
  	}
  
  	public static void convertPSFToOntology(String source, String target, String name, int minConceptID) {
  		OntologyPSFLoader psfLoader = new OntologyPSFLoader();
  		psfLoader.loadFromPSF(source);
  		OntologyStore ontology = psfLoader.ontology;
  		ontology.setName(name);
  		OntologyCurator curator = new OntologyCurator();
  		curator.curateAndPrepare(ontology);
  		if (minConceptID != -1)
  			for (Concept concept : ontology)
  				if (concept.getID() > 0)
  					if (concept.getID() < minConceptID)
  						concept.setDisambiguationType(DisambiguationType.loose);
  					else
  						concept.setDisambiguationType(DisambiguationType.strict);
  
  		OntologyFileLoader loader = new OntologyFileLoader();
  		loader.save(ontology, target);
  	}
  
  	public static void convertOntologyFileToPsf(String source, String target, String name) {
  		OntologyFileLoader loader = new OntologyFileLoader();
  		OntologyStore ontology = loader.load(source);
  		ontology.setName(name);
  		OntologyCurator curator = new OntologyCurator();
  		curator.curateAndPrepare(ontology);	
  		boolean doVoc = true;
  		boolean doSem = true;
  		try {
  			FileOutputStream PSFFile = new FileOutputStream(target);
  			BufferedWriter bufferedWrite = new BufferedWriter(new OutputStreamWriter(PSFFile, "UTF-8"), 1000000);
  			StringBuffer firstline = new StringBuffer();
  			firstline.append("LEVEL|");
  			if (doVoc) {
  				firstline.append("VOC|");
  			}
  			if (doSem) {
  				firstline.append("SEM|");
  			}
  			firstline.append("DEFAULT|0");
  			bufferedWrite.write(firstline.toString());
  			bufferedWrite.newLine();
  
  			// Add concepts
  			Iterator<Concept> values = ontology.getConceptIterator();
  			while (values.hasNext()) {
  				Concept concept = values.next();
  				StringBuffer line = new StringBuffer();
  				line.append(0 + "|");
  				if (concept.ID < 0){
  					line.append("|"+"|"+concept.getName());
  				}else {
  
  					if (doVoc) {
  						line.append(relatedToString(ontology, concept.getID(), DefaultTypes.fromVocabulary) + "|");
  					}
  					if (doSem) {
  						line.append(semrelatedToString(ontology, concept.getID(), DefaultTypes.isOfSemanticType) + "|");
  					}
  
  					if (concept.terms != null) {
  						if (concept.terms.size() != 0) {
  							line.append(StringUtilities.escape(concept.terms.get(0).text));
  						}
  						for (int j = 1; j < concept.terms.size(); j++) {
  							line.append(";" + StringUtilities.escape(concept.terms.get(j).text));
  						}
  					}
  					else {
  						line.append(StringUtilities.escape(concept.getName()));
  					}
  					boolean hasDef = false;
  					if (!((concept.definition == null) || concept.definition.equals(""))) {
  						line.append("?" + StringUtilities.escape(concept.definition));
  						hasDef = true;
  					}
  					// Append the database identifiers (if any)
  					List<DatabaseID> databaseIDs = ontology.getDatabaseIDsForConcept(concept.getID());
  					if (databaseIDs != null) {
  						boolean first = true;
  						for (DatabaseID databaseID: databaseIDs) {
  							if (first && !hasDef) {
  								first = false;
  								line.append("?");
  							} else
  								line.append("\\;");
  							line.append(databaseID.database);
  							line.append("_");
  							line.append(StringUtilities.escape(databaseID.ID));
  						}
  					}
  				}
  				line.append("|" + Integer.toString(concept.ID));
  				bufferedWrite.write(line.toString());
  				bufferedWrite.newLine();
  			}
  
  			// Add hierarchy:
  			Iterator<Concept> conceptIterator = ontology.getConceptIterator();
  			while (conceptIterator.hasNext()) {
  				Concept concept = conceptIterator.next();
  				List<Relation> relations = ontology.getRelationsForConceptAsSubject(concept.getID(), DefaultTypes.isParentOf);
  				if (relations != null && relations.size() != 0) {
  					StringBuffer line = new StringBuffer();
  					line.append("H|");
  					Iterator<Relation> relationIterator = relations.iterator();
  					while (relationIterator.hasNext()) {
  						Relation relation = relationIterator.next();
  						line.append(relation.object);
  						if (relationIterator.hasNext())
  							line.append(";");
  					}
  					line.append("|");
  					line.append(concept.getID());
  					bufferedWrite.write(line.toString());
  					bufferedWrite.newLine();
  				}
  			}
  			bufferedWrite.flush();
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  
  	}
  
  	private static String relatedToString(OntologyStore ontology,int conceptID, int relationType) {
  		List<Relation> vocs = ontology.getRelationsForConceptAsSubject(conceptID, relationType);
  
  		StringBuffer buffer = new StringBuffer();
  		if (vocs.size() != 0)
  			buffer.append(ontology.getConcept(vocs.get(0).object).getName());
  		for (int i = 1; i < vocs.size(); i++) {
  			buffer.append(";");
  			buffer.append(ontology.getConcept(vocs.get(i).object).getName());
  		}
  		return buffer.toString();
  	}
  	private static String semrelatedToString(OntologyStore ontology,int conceptID, int relationType) {
  		List<Relation> sems = ontology.getRelationsForConceptAsSubject(conceptID, relationType);
  
  		StringBuffer buffer = new StringBuffer();
  		if (sems.size() != 0 && ontology.getConcept(sems.get(0).object) == null)
  			System.out.println(sems.get(0).toString());
  
  		if (sems.size() != 0) {
  			Integer id = ontology.getConcept(sems.get(0).object).getID();
  			if (semanticTypesNegative)
  				id = -id;
  			buffer.append(id);
  		}
  		for (int i = 1; i < sems.size(); i++) {
  			buffer.append(";");
  			Integer id = ontology.getConcept(sems.get(i).object).getID();
  			if (semanticTypesNegative)
  				id = -id;
  			buffer.append(id);
  		}
  		return buffer.toString();
  	}
  
  	public void setLoadTermsOnly(boolean value) {
  		loadTermsOnly = value;
  	}
  
  	public boolean getLoadTermsOnly() {
  		return loadTermsOnly;
  	}
  
  	public String getVersion() {
  		return version;
  	}
  
  	public OntologyStore load(String filename) {
  		File file = new File(filename);
  		if (!file.exists()){
  			System.err.println("File not found: " + filename);
  			return null;
  		}
  
  		OntologyStore ontology = new OntologyStore();
  
  		Iterator<String> iterator = new ReadTextFile(filename).iterator();
  		loadHeader(iterator, ontology);
  		loadBody(iterator, ontology);
  
  		return ontology;
  	}
  
  	private void loadBody(Iterator<String> iterator, OntologyStore ontology) {
  		Map<String, Integer> vocs = new HashMap<String, Integer>();
  		Concept concept = new Concept(null);
  		concept.setTerms(new ArrayList<TermStore>());
  		String nameSpace = null;
  		while (iterator.hasNext()) {
  			String line = iterator.next();
  			String key = extractKey(line);
  			if (key != null) {
  				if (key.equals("--")) {
  					((ArrayList<TermStore>) concept.getTerms()).trimToSize();
  					((ArrayList<DatabaseID>) ontology.getDatabaseIDsForConcept(concept.getID())).trimToSize();
  					((ArrayList<Relation>) ontology.getRelationsForConceptAsSubject(concept.getID())).trimToSize();
  					ontology.setConcept(concept);
  					concept = new Concept(null);
  					concept.setTerms(new ArrayList<TermStore>());
  					nameSpace = null;
  				} else {
  					String value = extractValue(line);
  					if (key.equals("NS")) {
  						nameSpace = value;
  					} else if (key.equals("ID")) {
  						if (nameSpace != null && nameSpace.equals("SemType")) {
  							concept.setID(-Integer.parseInt(value));
  						} else if (nameSpace != null && nameSpace.equals("Voc")) {
  							concept.setID(getVocID(vocs, ontology, value));
  							concept.setName(value); // Dirty mapping for OntologyStore: store Voc Id in name field
  						} else
  							concept.setID(Integer.parseInt(value));
  					} else if (key.equals("NA") && !loadTermsOnly) {
  						if (nameSpace != null && nameSpace.equals("Voc"))
  							// Dirty mapping for OntogyStore: store voc name in definition field
  							concept.setDefinition(unescape(value));
  						else
  							concept.setName(unescape(value));
  					} else if (key.equals("TM")) {
  						concept.getTerms().add(parseTerm(value));
  					} else if (key.equals("DF") && !loadTermsOnly) {
  						concept.setDefinition(unescape(value));
  					} else if (key.equals("DB") && !loadTermsOnly) {
  						ontology.setDatabaseIDForConcept(concept.getID(), DatabaseID.parseString2DatabaseID(value));
  					} else if (key.equals("ST") && !loadTermsOnly) {
  						int semTypeID = -Integer.parseInt(value);
  						ontology.setRelation(new Relation(concept.getID(), DefaultTypes.isOfSemanticType, semTypeID));
  					} else if (key.equals("VO") && !loadTermsOnly) {
  						int vocID = getVocID(vocs, ontology, value);
  						ontology.setRelation(new Relation(concept.getID(), DefaultTypes.fromVocabulary, vocID));
  					} else if (key.equals("PA") && !loadTermsOnly) {
  