<commit_before>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on Nov 17, 2010
 * Author: <NAME> 
 *
 */

package org.biojava3.protmod;

import java.io.InputStream;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.biojava3.protmod.io.ProteinModificationXmlReader;

/**
 * This class serves as a instance registry by maintaining 
 * a pool of ProteinModification instances.
 * 
 * A list of common protein modifications were preloaded
 * from an XML file.
 * 
 * @author <NAME>
 * @since 3.0
 */
public class ProteinModificationRegistry {
	private static Set<ProteinModification> registry = null;
	private static Map<String, ProteinModification> byId = null;
	private static Map<String, Set<ProteinModification>> byResidId = null;
	private static Map<String, Set<ProteinModification>> byPsimodId = null;
	private static Map<String, Set<ProteinModification>> byPdbccId = null;
	private static Map<String, Set<ProteinModification>> byKeyword = null;
	private static Map<Component, Set<ProteinModification>> byComponent = null;
	private static Map<ModificationCategory, Set<ProteinModification>> byCategory = null;
	private static Map<ModificationOccurrenceType, Set<ProteinModification>> byOccurrenceType = null;
	
	private static String DIR_XML_PTM_LIST = "ptm_list.xml";
	
	/**
	 * register common protein modifications from XML file.
	 */
	private static void registerCommonProteinModifications() {
		try {
			InputStream isXml = ProteinModification.class.getResourceAsStream(DIR_XML_PTM_LIST);
			ProteinModificationXmlReader.registerProteinModificationFromXml(isXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initialization the static variables and register common modifications.
	 */
	public static void init() {
		lazyInit();
	}
	
	/**
	 * Lazy Initialization the static variables and register common modifications. 
	 */
	private static void lazyInit() {
		if (registry==null) {	
			registry = 	new HashSet<ProteinModification>();
			byId = new HashMap<String, ProteinModification>();
			byResidId = new HashMap<String, Set<ProteinModification>>();
			byPsimodId = new HashMap<String, Set<ProteinModification>>();
			byPdbccId = new HashMap<String, Set<ProteinModification>>();
			byKeyword = new HashMap<String, Set<ProteinModification>>();
			byComponent = new HashMap<Component, Set<ProteinModification>>();
			byCategory = new EnumMap<ModificationCategory, Set<ProteinModification>>(
					ModificationCategory.class);
			for (ModificationCategory cat:ModificationCategory.values()) {
				byCategory.put(cat, new HashSet<ProteinModification>());
			}
			byOccurrenceType = new EnumMap<ModificationOccurrenceType, Set<ProteinModification>>(
					ModificationOccurrenceType.class);
			for (ModificationOccurrenceType occ:ModificationOccurrenceType.values()) {
				byOccurrenceType.put(occ, new HashSet<ProteinModification>());
			}
			registerCommonProteinModifications();
		}
	}

	/**
	 * Register a new ProteinModification.
	 */
	public static void register(final ProteinModification modification) {
		if (modification==null) throw new IllegalArgumentException("modification == null!");
		
		lazyInit();

		String id = modification.getId();
		if (byId.containsKey(id)) {
			throw new IllegalArgumentException(id+" has already been registered.");
		}
		
		registry.add(modification);
		byId.put(id, modification);
		
		ModificationCategory cat = modification.getCategory();
		byCategory.get(cat).add(modification);
		
		ModificationOccurrenceType occType = modification.getOccurrenceType();
		byOccurrenceType.get(occType).add(modification);

		
		ModificationCondition condition = modification.getCondition();<commit_msg>now allowing outside user to specify alternative ptm_list.xml<commit_after>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on Nov 17, 2010
 * Author: <NAME> 
 *
 */

package org.biojava3.protmod;

import java.io.InputStream;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.biojava3.protmod.io.ProteinModificationXmlReader;

/**
 * This class serves as a instance registry by maintaining 
 * a pool of ProteinModification instances.
 * 
 * A list of common protein modifications were preloaded
 * from an XML file.
 * 
 * @author <NAME>
 * @since 3.0
 */
public class ProteinModificationRegistry {
	private static Set<ProteinModification> registry = null;
	private static Map<String, ProteinModification> byId = null;
	private static Map<String, Set<ProteinModification>> byResidId = null;
	private static Map<String, Set<ProteinModification>> byPsimodId = null;
	private static Map<String, Set<ProteinModification>> byPdbccId = null;
	private static Map<String, Set<ProteinModification>> byKeyword = null;
	private static Map<Component, Set<ProteinModification>> byComponent = null;
	private static Map<ModificationCategory, Set<ProteinModification>> byCategory = null;
	private static Map<ModificationOccurrenceType, Set<ProteinModification>> byOccurrenceType = null;
	
	private static String DIR_XML_PTM_LIST = "ptm_list.xml";
	
	
	
	/**
	 * register common protein modifications from XML file.
	 */
	private static void registerCommonProteinModifications(InputStream inStream) {
		try {
			
			ProteinModificationXmlReader.registerProteinModificationFromXml(inStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initialization the static variables and register common modifications.
	 */
	public static void init() {
		lazyInit();
		
	}
	
	/** Initialization the static variables and register common modifications.
	 * Allows external user to provide alternative ptm_list.xml file instead of the one contained in this jar file.
	 * 
	 * @param inStream InputStream to a XML file containing the list of PTMs (as in ptm_list.xml)
	 */
	 
	public static void init(InputStream inStream) {
		lazyInit(inStream);
	}
	
	
	
	/**
	 * Lazy Initialization the static variables and register common modifications.
	 * just opens the stream to ptm_list.xml and delegates to lazyInit(InputStream) for parsing. 
	 */
	private static synchronized void lazyInit() {
		InputStream isXml = ProteinModification.class.getResourceAsStream(DIR_XML_PTM_LIST);
		lazyInit(isXml);
	}
	
	
	/**
	 * Lazy Initialization the static variables and register common modifications. 
	 */
	private static synchronized void lazyInit(InputStream inStream) {
		if (registry==null) {	
						
			registry = 	new HashSet<ProteinModification>();
			byId = new HashMap<String, ProteinModification>();
			byResidId = new HashMap<String, Set<ProteinModification>>();
			byPsimodId = new HashMap<String, Set<ProteinModification>>();
			byPdbccId = new HashMap<String, Set<ProteinModification>>();
			byKeyword = new HashMap<String, Set<ProteinModification>>();
			byComponent = new HashMap<Component, Set<ProteinModification>>();
			byCategory = new EnumMap<ModificationCategory, Set<ProteinModification>>(
					ModificationCategory.class);
			for (ModificationCategory cat:ModificationCategory.values()) {
				byCategory.put(cat, new HashSet<ProteinModification>());
			}
			byOccurrenceType = new EnumMap<ModificationOccurrenceType, Set<ProteinModification>>(
					ModificationOccurrenceType.class);
			for (ModificationOccurrenceType occ:ModificationOccurrenceType.values()) {
				byOccurrenceType.put(occ, new HashSet<ProteinModification>());
			}
			registerCommonProteinModifications(inStream);					
		}
	}

	/**
	 * Register a new ProteinModification.
	 */
	public static void register(final ProteinModification modification) {
		if (modification==null) throw new IllegalArgumentException("modification == null!");
		
		lazyInit();

		String id = modification.getId();
		if (byId.containsKey(id)) {
			throw new IllegalArgumentException(id+" has already been registered.");
		}
		
		registry.add(modification);
		byId.put(id, modification);
		
		ModificationCategory cat = modification.getCategory();
		byCategory.get(cat).add(modification);
		
		ModificationOccurrenceType occType = modification.getOccurrenceType();
		byOccurrenceType.get(occType).add(modification);

		
		ModificationCondition condition = modification.getCondition();
{'fraction_non_alphanumeric': 0.0956402406840494, 'fraction_numerical': 0.0021112635912593687, 'mean_word_length': 4.652744630071599, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399811', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2602, 'n_words': 760}