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

org.snpeff.sam.SamHeader Maven / Gradle / Ivy

The newest version!
package org.snpeff.sam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Sam header
 * 
 * @author pcingola
 */
public class SamHeader implements Iterable {

	HashMap> recordsByType;
	ArrayList lines;

	public SamHeader() {
		recordsByType = new HashMap>();
		lines = new ArrayList();
	}

	/**
	 * Add a record to the header
	 */
	public void add(SamHeaderRecord samHeaderRecord) {
		ArrayList records = getRecords(samHeaderRecord.getRecordTypeCode());
		records.add(samHeaderRecord);
	}

	public void addHeaderRecord(String line) {
		if( !line.startsWith("@") ) throw new RuntimeException("Record type must start with '@'. Header line: " + line);

		// Add line
		lines.add(line);

		// Parse records
		SamHeaderRecord shr = null;
		if( line.startsWith("@SQ") ) shr = new SamHeaderRecordSq(line);

		if( shr != null ) add(shr);
	}

	/**
	 * Get a list of records for this 'recordType'
	 * @param recordType
	 * @return A new empty list is created if no records are available
	 */
	public ArrayList getRecords(String recordType) {
		ArrayList records = recordsByType.get(recordType);
		if( records == null ) {
			records = new ArrayList();
			recordsByType.put(recordType, records);
		}
		return records;
	}

	@Override
	public Iterator iterator() {
		return lines.iterator();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy