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

org.snpeff.pdb.IdMapper Maven / Gradle / Ivy

The newest version!
package org.snpeff.pdb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.snpeff.collections.AutoHashMap;
import org.snpeff.util.Gpr;
import org.snpeff.util.Timer;

/**
 * Map IDs
 *
 * @author pcingola
 */
public class IdMapper {

	boolean verbose;
	int count;

	AutoHashMap> byTrId, byPdbId;
	HashSet entries;

	/**
	 * Remove version form transcript ID
	 */
	public static String transcriptIdNoVersion(String trId) {
		int idx = trId.lastIndexOf('.');
		if (idx < 0) return trId;
		return trId.substring(0, idx);
	}

	public static Set transcriptIds(List ids) {
		if (ids == null) return null;

		// Unique names
		HashSet set = new HashSet();
		for (IdMapperEntry ime : ids)
			set.add(ime.trId);

		return set;
	}

	public IdMapper() {
		ArrayList emptyList = new ArrayList();
		byTrId = new AutoHashMap>(emptyList);
		byPdbId = new AutoHashMap>(emptyList);;
		entries = new HashSet<>();
	}

	public void add(IdMapperEntry ime) {
		if (ime.trId != null) byTrId.getOrCreate(ime.trId).add(ime);
		if (ime.pdbId != null) byPdbId.getOrCreate(ime.pdbId).add(ime);
		entries.add(ime);
	}

	public void addAll(Collection imes) {
		for (IdMapperEntry ime : imes)
			add(ime);
	}

	public List getByPdbId(String id) {
		return byPdbId.get(id);
	}

	public List getByPdbId(String id, String chainId) {
		if (byPdbId.get(id) == null) return null;

		List list = new LinkedList<>();
		for (IdMapperEntry ime : byPdbId.get(id))
			if (chainId.equals(ime.pdbChainId)) list.add(ime);

		return list;
	}

	public List getByTrId(String id) {
		return byTrId.get(id);
	}

	public Collection getEntries() {
		return entries;
	}

	public void load(String fileName) {
		if (verbose) Timer.showStdErr("Loading IDs from file: " + fileName);
		count = 0;
		String lines[] = Gpr.readFile(fileName).split("\n");
		for (String line : lines)
			parseLine(line);

		if (verbose) Timer.showStdErr("Done. Total entries added: " + count);
	}

	/**
	 * Parse a line and add it to this map
	 */
	void parseLine(String line) {
		String fields[] = line.split("\t");

		if (fields.length > 1 && !fields[1].isEmpty()) {
			IdMapperEntry ime = new IdMapperEntry(fields[0], fields[1]);
			add(ime);
			count++;
		}

		if (fields.length > 2 && !fields[2].isEmpty()) {
			IdMapperEntry ime = new IdMapperEntry(fields[0], fields[2]);
			add(ime);
			count++;
		}

	}

	public void setVerbose(boolean verbose) {
		this.verbose = verbose;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (IdMapperEntry im : entries)
			sb.append(im + "\n");
		return sb.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy