org.dishevelled.bio.variant.vcf.header.VcfHeaderLines Maven / Gradle / Ivy
The newest version!
/*
dsh-bio-variant Variants.
Copyright (c) 2013-2024 held jointly by the individual authors.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> http://www.fsf.org/licensing/licenses/lgpl.html
> http://www.opensource.org/licenses/lgpl-license.php
*/
package org.dishevelled.bio.variant.vcf.header;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.dishevelled.bio.variant.vcf.header.VcfHeaderLineParser.isStructured;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.concurrent.Immutable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.dishevelled.bio.variant.vcf.VcfHeader;
/**
* VCF header lines.
*
* @author Michael Heuer
*/
@Immutable
public final class VcfHeaderLines {
/**
* VCF ALT header lines keyed by ID.
*/
private final Map altHeaderLines;
/**
* VCF contig header lines keyed by ID.
*/
private final Map contigHeaderLines;
/**
* VCF FILTER header lines keyed by ID.
*/
private final Map filterHeaderLines;
/**
* VCF FORMAT header lines keyed by ID.
*/
private final Map formatHeaderLines;
/**
* VCF INFO header lines keyed by ID.
*/
private final Map infoHeaderLines;
/**
* VCF META header lines keyed by ID.
*/
private final Map metaHeaderLines;
/**
* VCF PEDIGREE header lines.
*/
private final Set pedigreeHeaderLines;
/**
* VCF SAMPLE header lines keyed by ID.
*/
private final Map sampleHeaderLines;
/**
* VCF file format header line.
*/
private final VcfHeaderLine fileFormat;
/**
* VCF key-value header lines.
*/
private final Set headerLines;
/**
* Structured VCF header lines.
*/
private final Set structuredHeaderLines;
/**
* Create a new VCF header lines.
*
* @param altHeaderLines VCF ALT header lines keyed by ID, must not be null
* @param contigHeaderLines VCF contig header lines keyed by ID, must not be null
* @param filterHeaderLines VCF FILTER header lines keyed by ID, must not be null
* @param formatHeaderLines VCF FORMAT header lines keyed by ID, must not be null
* @param infoHeaderLines VCF INFO header lines keyed by ID, must not be null
* @param metaHeaderLines VCF META header lines keyed by ID, must not be null
* @param pedigreeHeaderLines VCF PEDIGREE header lines, must not be null
* @param sampleHeaderLines VCF SAMPLE header lines keyed by ID, must not be null
* @param fileFormat VCF file format header line, must not be null
* @param headerLines VCF key-value header lines, must not be null
* @param structuredHeaderLines structured VCF header lines, must not be null
*/
private VcfHeaderLines(final Map altHeaderLines,
final Map contigHeaderLines,
final Map filterHeaderLines,
final Map formatHeaderLines,
final Map infoHeaderLines,
final Map metaHeaderLines,
final Set pedigreeHeaderLines,
final Map sampleHeaderLines,
final VcfHeaderLine fileFormat,
final Set headerLines,
final Set structuredHeaderLines) {
checkNotNull(altHeaderLines);
checkNotNull(contigHeaderLines);
checkNotNull(filterHeaderLines);
checkNotNull(formatHeaderLines);
checkNotNull(infoHeaderLines);
checkNotNull(metaHeaderLines);
checkNotNull(pedigreeHeaderLines);
checkNotNull(sampleHeaderLines);
checkNotNull(fileFormat);
checkNotNull(headerLines);
checkNotNull(structuredHeaderLines);
this.altHeaderLines = ImmutableMap.copyOf(altHeaderLines);
this.contigHeaderLines = ImmutableMap.copyOf(contigHeaderLines);
this.filterHeaderLines = ImmutableMap.copyOf(filterHeaderLines);
this.formatHeaderLines = ImmutableMap.copyOf(formatHeaderLines);
this.infoHeaderLines = ImmutableMap.copyOf(infoHeaderLines);
this.metaHeaderLines = ImmutableMap.copyOf(metaHeaderLines);
this.pedigreeHeaderLines = ImmutableSet.copyOf(pedigreeHeaderLines);
this.sampleHeaderLines = ImmutableMap.copyOf(sampleHeaderLines);
this.fileFormat = fileFormat;
this.headerLines = ImmutableSet.copyOf(headerLines);
this.structuredHeaderLines = ImmutableSet.copyOf(structuredHeaderLines);
}
/**
* Return VCF ALT header lines keyed by ID.
*
* @return VCF ALT header lines keyed by ID
*/
public Map getAltHeaderLines() {
return altHeaderLines;
}
/**
* Return VCF contig header lines keyed by ID.
*
* @return VCF contig header lines keyed by ID
*/
public Map getContigHeaderLines() {
return contigHeaderLines;
}
/**
* Return VCF FILTER header lines keyed by ID.
*
* @return VCF FILTER header lines keyed by ID
*/
public Map getFilterHeaderLines() {
return filterHeaderLines;
}
/**
* Return VCF FORMAT header lines keyed by ID.
*
* @return VCF FORMAT header lines keyed by ID
*/
public Map getFormatHeaderLines() {
return formatHeaderLines;
}
/**
* Return VCF INFO header lines keyed by ID.
*
* @return VCF INFO header lines keyed by ID
*/
public Map getInfoHeaderLines() {
return infoHeaderLines;
}
/**
* Return VCF META header lines keyed by ID.
*
* @return VCF META header lines keyed by ID
*/
public Map getMetaHeaderLines() {
return metaHeaderLines;
}
/**
* Return VCF PEDIGREE header lines.
*
* @return VCF PEDIGREE header lines
*/
public Set getPedigreeHeaderLines() {
return pedigreeHeaderLines;
}
/**
* Return VCF SAMPLE header lines keyed by ID.
*
* @return VCF SAMPLE header lines keyed by ID
*/
public Map getSampleHeaderLines() {
return sampleHeaderLines;
}
/**
* Return the VCF file format header line.
*
* @return the VCF file format header line
*/
public VcfHeaderLine getFileFormat() {
return fileFormat;
}
/**
* Return VCF key=value header lines.
*
* @return VCF key=value header lines
*/
public Set getHeaderLines() {
return headerLines;
}
/**
* Return structured VCF ALT header lines.
*
* @return structured VCF header lines
*/
public Set getStructuredHeaderLines() {
return structuredHeaderLines;
}
/**
* Create a new VcfHeader from this VcfHeaderLines.
*
* @return a new VcfHeader from this VcfHeaderLines
*/
public VcfHeader toHeader() {
VcfHeader.Builder builder = VcfHeader.builder();
builder.withFileFormat(fileFormat.toString());
for (VcfHeaderLine line : headerLines) {
builder.withMeta(line.toString());
}
for (VcfAltHeaderLine altHeaderLine : altHeaderLines.values()) {
builder.withMeta(altHeaderLine.toString());
}
for (VcfContigHeaderLine contigHeaderLine : contigHeaderLines.values()) {
builder.withMeta(contigHeaderLine.toString());
}
for (VcfFilterHeaderLine filterHeaderLine : filterHeaderLines.values()) {
builder.withMeta(filterHeaderLine.toString());
}
for (VcfFormatHeaderLine formatHeaderLine : formatHeaderLines.values()) {
builder.withMeta(formatHeaderLine.toString());
}
for (VcfInfoHeaderLine infoHeaderLine : infoHeaderLines.values()) {
builder.withMeta(infoHeaderLine.toString());
}
for (VcfMetaHeaderLine metaHeaderLine : metaHeaderLines.values()) {
builder.withMeta(metaHeaderLine.toString());
}
for (VcfPedigreeHeaderLine pedigreeHeaderLine : pedigreeHeaderLines) {
builder.withMeta(pedigreeHeaderLine.toString());
}
for (VcfSampleHeaderLine sampleHeaderLine : sampleHeaderLines.values()) {
builder.withMeta(sampleHeaderLine.toString());
}
for (VcfStructuredHeaderLine structuredHeaderLine : structuredHeaderLines) {
builder.withMeta(structuredHeaderLine.toString());
}
return builder.build();
}
@Override
public String toString() {
return toHeader().toString();
}
/**
* Create a new VcfHeaderLines from the specified VcfHeader.
*
* @param header VcfHeader, must not be null
* @return a new VcfHeaderLines created from the specified VcfHeader
*/
public static VcfHeaderLines fromHeader(final VcfHeader header) {
checkNotNull(header);
Map altHeaderLines = new HashMap();
Map contigHeaderLines = new HashMap();
Map filterHeaderLines = new HashMap();
Map formatHeaderLines = new HashMap();
Map infoHeaderLines = new HashMap();
Map metaHeaderLines = new HashMap();
Set pedigreeHeaderLines = new HashSet();
Map sampleHeaderLines = new HashMap();
VcfHeaderLine fileFormat = VcfHeaderLine.valueOf("##fileformat=" + header.getFileFormat());
Set headerLines = new HashSet();
Set structuredHeaderLines = new HashSet();
for (String meta : header.getMeta()) {
if (meta.startsWith("##ALT=")) {
VcfAltHeaderLine altHeaderLine = VcfAltHeaderLine.valueOf(meta);
altHeaderLines.put(altHeaderLine.getId(), altHeaderLine);
}
else if (meta.startsWith("##contig=")) {
VcfContigHeaderLine contigHeaderLine = VcfContigHeaderLine.valueOf(meta);
contigHeaderLines.put(contigHeaderLine.getId(), contigHeaderLine);
}
else if (meta.startsWith("##FILTER=")) {
VcfFilterHeaderLine filterHeaderLine = VcfFilterHeaderLine.valueOf(meta);
filterHeaderLines.put(filterHeaderLine.getId(), filterHeaderLine);
}
else if (meta.startsWith("##FORMAT=")) {
VcfFormatHeaderLine formatHeaderLine = VcfFormatHeaderLine.valueOf(meta);
formatHeaderLines.put(formatHeaderLine.getId(), formatHeaderLine);
}
else if (meta.startsWith("##INFO=")) {
VcfInfoHeaderLine infoHeaderLine = VcfInfoHeaderLine.valueOf(meta);
infoHeaderLines.put(infoHeaderLine.getId(), infoHeaderLine);
}
else if (meta.startsWith("##META=")) {
VcfMetaHeaderLine metaHeaderLine = VcfMetaHeaderLine.valueOf(meta);
metaHeaderLines.put(metaHeaderLine.getId(), metaHeaderLine);
}
else if (meta.startsWith("##PEDIGREE=")) {
VcfPedigreeHeaderLine pedigreeHeaderLine = VcfPedigreeHeaderLine.valueOf(meta);
pedigreeHeaderLines.add(pedigreeHeaderLine);
}
else if (meta.startsWith("##SAMPLE=")) {
VcfSampleHeaderLine sampleHeaderLine = VcfSampleHeaderLine.valueOf(meta);
sampleHeaderLines.put(sampleHeaderLine.getId(), sampleHeaderLine);
}
else if (isStructured(meta)) {
VcfStructuredHeaderLine structuredHeaderLine = VcfStructuredHeaderLine.valueOf(meta);
structuredHeaderLines.add(structuredHeaderLine);
}
else if (!meta.startsWith("##fileformat=")) {
VcfHeaderLine headerLine = VcfHeaderLine.valueOf(meta);
headerLines.add(headerLine);
}
}
return new VcfHeaderLines(altHeaderLines,
contigHeaderLines,
filterHeaderLines,
formatHeaderLines,
infoHeaderLines,
metaHeaderLines,
pedigreeHeaderLines,
sampleHeaderLines,
fileFormat,
headerLines,
structuredHeaderLines);
}
// todo: builder?
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy