// Path: src/main/java/org/umcn/me/samexternal/SAMSilentReader.java
// public class SAMSilentReader extends SAMFileReader {
// 	
// 	public SAMSilentReader(File samOrBam){
// 		super(samOrBam);
// 		this.setValidationStringency(SAMFileReader.ValidationStringency.SILENT);
// 	}
// 	
// 	public SAMSilentReader(File bam, File index){
// 		super(bam, index);
// 		this.setValidationStringency(SAMFileReader.ValidationStringency.SILENT);
// 	}
// 	
// }


import java.security.InvalidParameterException;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import org.umcn.me.samexternal.SAMSilentReader;
import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMReadGroupRecord;
import net.sf.picard.sam.SamFileHeaderMerger;

package org.umcn.me.util;





public class BAMCollection {

	List<BAMSample> bams = new ArrayList<BAMSample>();
	
	
	public BAMCollection(String[] files, String[] names) throws InvalidParameterException, FileNotFoundException {
		
		this(files, names, false);
		
	}
	
	public BAMCollection(String[] files, String[] names, boolean prefix) throws InvalidParameterException, FileNotFoundException {
				
		if (files.length != names.length){
			throw new InvalidParameterException("Number of files does not equal number of sample names");
		}
		
		for (int i=0; i < files.length; i++){
			
			String fileName = files[i];
			File file = new File(fileName);
			String name = names[i];
			
			if (fileName.isEmpty() || name.isEmpty()){
				throw new InvalidParameterException("Name of file or sample may not be empty");
			}
			if (!file.exists()){
				throw new FileNotFoundException("File does not exist: " + file);
			}
			BAMSample sample = new BAMSample(file, name);
			
			if (prefix) sample.setPrefixReadGroupId(Integer.toString(i) + ".");
			bams.add(sample);
		}
	}
	
	/**
	 * 
	 * @param order
	 * @return SAMFileHeader
	 */
	public SAMFileHeader getMergedHeader(SAMFileHeader.SortOrder order) {
		
		List<SAMFileHeader> headers = new ArrayList<SAMFileHeader>();		
		
		for (BAMSample sample : this.bams){
			List<SAMReadGroupRecord> newReadGroups = new ArrayList<SAMReadGroupRecord>();

// Path: src/main/java/org/umcn/me/samexternal/SAMSilentReader.java
// public class SAMSilentReader extends SAMFileReader {
// 	
// 	public SAMSilentReader(File samOrBam){
// 		super(samOrBam);
// 		this.setValidationStringency(SAMFileReader.ValidationStringency.SILENT);
// 	}
// 	
// 	public SAMSilentReader(File bam, File index){
// 		super(bam, index);
// 		this.setValidationStringency(SAMFileReader.ValidationStringency.SILENT);
// 	}
// 	
// }


// Path: src/main/java/org/umcn/me/util/BAMCollection.java
import java.security.InvalidParameterException;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import org.umcn.me.samexternal.SAMSilentReader;
import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMReadGroupRecord;
import net.sf.picard.sam.SamFileHeaderMerger;

package org.umcn.me.util;





public class BAMCollection {

	List<BAMSample> bams = new ArrayList<BAMSample>();
	
	
	public BAMCollection(String[] files, String[] names) throws InvalidParameterException, FileNotFoundException {
		
		this(files, names, false);
		
	}
	
	public BAMCollection(String[] files, String[] names, boolean prefix) throws InvalidParameterException, FileNotFoundException {
				
		if (files.length != names.length){
			throw new InvalidParameterException("Number of files does not equal number of sample names");
		}
		
		for (int i=0; i < files.length; i++){
			
			String fileName = files[i];
			File file = new File(fileName);
			String name = names[i];
			
			if (fileName.isEmpty() || name.isEmpty()){
				throw new InvalidParameterException("Name of file or sample may not be empty");
			}
			if (!file.exists()){
				throw new FileNotFoundException("File does not exist: " + file);
			}
			BAMSample sample = new BAMSample(file, name);
			
			if (prefix) sample.setPrefixReadGroupId(Integer.toString(i) + ".");
			bams.add(sample);
		}
	}
	
	/**
	 * 
	 * @param order
	 * @return SAMFileHeader
	 */
	public SAMFileHeader getMergedHeader(SAMFileHeader.SortOrder order) {
		
		List<SAMFileHeader> headers = new ArrayList<SAMFileHeader>();		
		
		for (BAMSample sample : this.bams){
			List<SAMReadGroupRecord> newReadGroups = new ArrayList<SAMReadGroupRecord>();

			SAMSilentReader reader = new SAMSilentReader(sample.bam);
