All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.biopax.paxtools.pattern.miner.OutputColumn Maven / Gradle / Ivy

Go to download

BioPAX Pattern Search library. Also, converts BioPAX model to SIF (simple binary interactions) text format.

The newest version!
package org.biopax.paxtools.pattern.miner;

import org.biopax.paxtools.controller.PathAccessor;

import java.util.*;

/**
 * This class is used for defining a custom column in the te
 * @author Ozgun Babur
 */
public class OutputColumn
{
	/**
	 * Type of the output column.
	 */
	private Type type;
	private PathAccessor[] accessors;

	/**
	 * Constructor with output type. The field parameter can either be a value among
	 * OutputValue.Type enum (excluding the value CUSTOM), or a PathAccessor string to
	 * apply to mediator objects.
	 * @param field type of the custom field
	 */
	public OutputColumn(String field)
	{
		if (field == null)
		{
			throw new IllegalArgumentException("The field parameter has to be specified");
		}

		try
		{
			type = Type.valueOf(field.toUpperCase());
			return;
		}
		catch (IllegalArgumentException e){}

		if (type == Type.CUSTOM)
		{
			throw new IllegalArgumentException("The \"custom\" type should be stated " +
				"implicitly by passing path accessor strings as parameter.");
		}

		if (type == null)
		{
			type = Type.CUSTOM;
			String[] param = field.split(";");
			accessors = new PathAccessor[param.length];
			for (int i = 0; i < accessors.length; i++)
			{
				if (!param[i].contains("/"))
				{
					throw new IllegalArgumentException("The parameter column field is not " +
						"recognized as a pre-defined type. It also does not qualify as a path" +
						" accessor argument string.");
				}

				accessors[i] = new PathAccessor(param[i]);
			}
		}
	}

	/**
	 * Get the string to write in the output file.
	 * @param inter the binary interaction
	 * @return column value
	 */
	public String getColumnValue(SIFInteraction inter)
	{
		switch (type)
		{
			case MEDIATOR: return concat(inter.getMediatorIDs());
			case PATHWAY: return concat(inter.getPathwayNames());
			case PATHWAY_URI: return concat(inter.getPathwayUris());
			case PUBMED: return concat(inter.getPublicationIDs(true));
			case PMC: return concat(inter.getPublicationIDs(false));
			case RESOURCE: return concat(inter.getDataSources());
			case SOURCE_LOC: return concat(inter.getCellularLocationsOfSource());
			case TARGET_LOC: return concat(inter.getCellularLocationsOfTarget());
			case COMMENTS: return concat(inter.getMediatorComments());
			case CUSTOM:
			{
				Set set = new HashSet<>();
				for (PathAccessor acc : accessors)
				{
					for (Object o : acc.getValueFromBeans(inter.mediators))
					{
						set.add(o.toString());
					}
				}
				List list = new ArrayList<>(set);
				Collections.sort(list);
				return concat(list);
			}
			default: throw new RuntimeException("Unhandled type: " + type +
				". This shouldn't be happening.");
		}
	}

	/**
	 * Concatenates the given collection of strings into a single string where values are separated
	 * with a semicolon.
	 * @param col string collection
	 * @return concatenated string
	 */
	private String concat(Collection col)
	{
		StringBuilder b = new StringBuilder();
		boolean first = true;
		for (String s : col)
		{
			if (first) first = false;
			else b.append(";");

			b.append(s);
		}
		return b.toString();
	}

	/**
	 * Type of the output column.
	 */
	public enum Type
	{
		MEDIATOR,
		PUBMED,
		PMC,
		COMMENTS,
		PATHWAY,
		PATHWAY_URI,
		RESOURCE,
		SOURCE_LOC,
		TARGET_LOC,
		CUSTOM;

		public static Type getType(String name)
		{
			for (Type type : values())
			{
				if (type.name().equalsIgnoreCase(name)) return type;
			}
			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy