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



public class Genome {
	private HashMap<String, Chromosome> _chromosomeMap =new HashMap<String, Chromosome>(23);
	
	public void addRead( String chromosome, AlignedPARCLIPread read ) {
		if( _chromosomeMap.containsKey(chromosome) ) {
			Chromosome currentChromosome = _chromosomeMap.get(chromosome);
			currentChromosome.addRead(read);
			_chromosomeMap.put(chromosome, currentChromosome);
			
		}
		else {
			Chromosome currentChromosome = new Chromosome(chromosome);
			currentChromosome.addRead(read);
			_chromosomeMap.put(chromosome, currentChromosome);
		}
	}
	
	
	public int getUniqueReadCount() {
		int sum = 0;
		Iterator<String> chromIt = _chromosomeMap.keySet().iterator();
		while( chromIt.hasNext() ) {
			Chromosome currentChromosome = _chromosomeMap.get(chromIt.next());
			sum += currentChromosome.getReadList().size();
		}
		return sum;
	}
	public int getReadCount() {
		int sum = 0;
		Iterator<String> chromIt = _chromosomeMap.keySet().iterator();
		while( chromIt.hasNext() ) {
			Chromosome currentChromosome = _chromosomeMap.get(chromIt.next());
			ArrayList<AlignedPARCLIPread> currentReadList = currentChromosome.getReadList();
			Iterator<AlignedPARCLIPread> readIt = currentReadList.iterator();
			while( readIt.hasNext() ) {
				sum += readIt.next().getReadCount();
			}
		}		
		return sum;
	}
	public int getNumberOfChromosomes() {
		return _chromosomeMap.size();
	}
	
	public HashMap<String, Chromosome> getChromosomeMap() {
		return _chromosomeMap;
	}

	
	public void mergeGenome( Genome genome ) {
		HashMap<String, Chromosome> currentChromosomeMap = genome.getChromosomeMap();
		Iterator<String> chromIt = currentChromosomeMap.keySet().iterator();
		while( chromIt.hasNext() ) {
			Chromosome currentChromosome = currentChromosomeMap.get(chromIt.next());
			Iterator<AlignedPARCLIPread> readIt = currentChromosome.getReadList().iterator();
			while( readIt.hasNext() ) {
				this.addRead(currentChromosome.getName(), readIt.next());
				readIt.remove();
			}			
		}
	}
	
	public void printReadCount() {
		Iterator<String> chromIt = _chromosomeMap.keySet().iterator();
		while( chromIt.hasNext() ) {
			Chromosome currentChromosome = _chromosomeMap.get(chromIt.next());
			ArrayList<AlignedPARCLIPread> currentReadList = currentChromosome.getReadList();
			Collections.sort(currentReadList);
			Iterator<AlignedPARCLIPread> readIt = currentReadList.iterator();
			while( readIt.hasNext() ) {
				AlignedPARCLIPread currentRead = readIt.next();
				System.out.print(currentChromosome.getName() + "," + currentRead.getStrand() + "," );
				System.out.print(Long.toString(currentRead.getStartPosition()) + "," + Long.toString(currentRead.getEndPosition()));
				System.out.println("," + Integer.toString(currentRead.getReadCount()));
			}
		}		
		
		
		
		
	}
	
	
}
