org.pharmgkb.parser.vcf.model.BaseMetadata Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vcf-parser Show documentation
Show all versions of vcf-parser Show documentation
A strict streaming parser for VCF 4.1/4.2.
The newest version!
package org.pharmgkb.parser.vcf.model;
import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.pharmgkb.parser.vcf.VcfFormatException;
import org.pharmgkb.parser.vcf.VcfUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* VCF metadata in the format XXX=<key=value,key=value,...>.
* The properties are case-sensitive.
* @author Douglas Myers-Turnbull
*/
public class BaseMetadata {
private static final Logger sf_logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final Map m_properties;
public BaseMetadata(@Nonnull Map properties) {
for (Map.Entry entry : properties.entrySet()) {
if (entry.getKey().contains("\n") || entry.getValue().contains("\n")) {
throw new VcfFormatException("INFO [[[" + entry.getKey() + "=" + entry.getValue() + "]]] contains a newline");
}
}
m_properties = properties;
}
@Nullable
public String getPropertyUnquoted(@Nonnull String key) {
String got = m_properties.get(key);
if (got == null) {
return null;
}
return VcfUtils.unquote(got);
}
@Nullable
public String getPropertyRaw(@Nonnull String key) {
return m_properties.get(key);
}
@Nonnull
public Map getPropertiesUnquoted() {
Map map = new HashMap<>();
for (Map.Entry entry : m_properties.entrySet()) {
map.put(entry.getKey(), VcfUtils.unquote(entry.getValue()));
}
return map;
}
@Nonnull
public Map getPropertiesRaw() {
return m_properties;
}
@Nonnull
public Set getPropertyKeys() {
return m_properties.keySet();
}
public void putAndQuoteProperty(@Nonnull String key, @Nullable String value) {
if (value == null) {
m_properties.remove(key);
} else {
m_properties.put(key, VcfUtils.quote(value));
}
}
public void putPropertyRaw(@Nonnull String key, @Nullable String value) {
m_properties.put(key, value);
}
/**
* Should be used only for base classes.
* Logs a warning if this metadata contains a property key not in the array passed.
* @param names An array of permitted property keys
*/
protected void ensureNoExtras(@Nonnull String... names) {
Set set = new HashSet<>();
Collections.addAll(set, names);
m_properties.keySet().stream().filter(property -> !set.contains(property)).forEach(property -> {
sf_logger.warn("Metadata line contains unexpected property {}", property);
});
}
}