# Path: nanopore/metaAnalyses/abstractMetaAnalysis.py
# class AbstractMetaAnalysis(Target):
#     """Base class to for meta-analysis targets. Inherit this class to create a meta-analysis.
#     """
#     def __init__(self, outputDir, experiments):
#         Target.__init__(self)
#         self.experiments = experiments
#         self.outputDir = outputDir
#         
#         #Quadruples of (readFastqFile, readType, referenceFastaFile, mapper) to pairs of (analyses, resultsDir)
#         self.experimentHash = {}
#         #Mappers
#         self.mappers = set()
#         #Read file readType double
#         self.readFastqFiles = set()
#         #Reference files
#         self.referenceFastaFiles = set()
#         #readTypes
#         self.readTypes = set()
#         #base mappers (lastz, last, bwa, blasr)
#         self.baseMappers = set()
#         
#         #Store all this stuff
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)] = (analyses, resultsDir)
#             self.mappers.add(mapper)
#             self.readFastqFiles.add((readFastqFile, readType))
#             self.referenceFastaFiles.add(referenceFastaFile)
#             self.readTypes.add(readType)
#             self.baseMappers.add(re.findall("[A-Z][a-z]*", mapper.__name__)[0])
# 
# Path: nanopore/analyses/utils.py
# def samToBamFile(samInputFile, bamOutputFile):
#     """Converts a sam file to a bam file (sorted)
#     """
#     samfile = pysam.Samfile(samInputFile, "r" )
#     bamfile = pysam.Samfile(bamOutputFile, "wb", template=samfile)
#     for line in samfile:
#         bamfile.write(line)
# 
#     samfile.close()
#     bamfile.close()


from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from sonLib.bioio import system
from nanopore.analyses.utils import samToBamFile
import os, sys, glob
import pysam
	"""
	fasta reader
	"""
	def __init__(self):
		self.id = None
		self.seq = ''
		self.length = ''
	  
	@staticmethod 
	def readline(linein):
		seqobj = Fastaseq()
		for line in linein:
			if len(line)==0: 
				print >> sys.stderr, 'empty line'
				continue
			if line.startswith('>'):
				if seqobj.id is None:
					seqobj.id = line.rstrip()
					continue
				else:
					yield seqobj
					seqobj = Fastaseq()
					seqobj.id = line.rstrip()
			else:
				seqobj.seq += line.rstrip('\n\r').upper()
		yield seqobj

class CustomTrackAssemblyHub(AbstractMetaAnalysis):
	"""moves mapping.sorted.bam and mapping.sorted.bam.bai files to a folder and creates trackDb.txt"""
	def __init__(self, outputDir, experiments):

# Path: nanopore/metaAnalyses/abstractMetaAnalysis.py
# class AbstractMetaAnalysis(Target):
#     """Base class to for meta-analysis targets. Inherit this class to create a meta-analysis.
#     """
#     def __init__(self, outputDir, experiments):
#         Target.__init__(self)
#         self.experiments = experiments
#         self.outputDir = outputDir
#         
#         #Quadruples of (readFastqFile, readType, referenceFastaFile, mapper) to pairs of (analyses, resultsDir)
#         self.experimentHash = {}
#         #Mappers
#         self.mappers = set()
#         #Read file readType double
#         self.readFastqFiles = set()
#         #Reference files
#         self.referenceFastaFiles = set()
#         #readTypes
#         self.readTypes = set()
#         #base mappers (lastz, last, bwa, blasr)
#         self.baseMappers = set()
#         
#         #Store all this stuff
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)] = (analyses, resultsDir)
#             self.mappers.add(mapper)
#             self.readFastqFiles.add((readFastqFile, readType))
#             self.referenceFastaFiles.add(referenceFastaFile)
#             self.readTypes.add(readType)
#             self.baseMappers.add(re.findall("[A-Z][a-z]*", mapper.__name__)[0])
# 
# Path: nanopore/analyses/utils.py
# def samToBamFile(samInputFile, bamOutputFile):
#     """Converts a sam file to a bam file (sorted)
#     """
#     samfile = pysam.Samfile(samInputFile, "r" )
#     bamfile = pysam.Samfile(bamOutputFile, "wb", template=samfile)
#     for line in samfile:
#         bamfile.write(line)
# 
#     samfile.close()
#     bamfile.close()


# Path: nanopore/metaAnalyses/customTrackAssemblyHub.py
from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from sonLib.bioio import system
from nanopore.analyses.utils import samToBamFile
import os, sys, glob
import pysam

	"""
	fasta reader
	"""
	def __init__(self):
		self.id = None
		self.seq = ''
		self.length = ''
	  
	@staticmethod 
	def readline(linein):
		seqobj = Fastaseq()
		for line in linein:
			if len(line)==0: 
				print >> sys.stderr, 'empty line'
				continue
			if line.startswith('>'):
				if seqobj.id is None:
					seqobj.id = line.rstrip()
					continue
				else:
					yield seqobj
					seqobj = Fastaseq()
					seqobj.id = line.rstrip()
			else:
				seqobj.seq += line.rstrip('\n\r').upper()
		yield seqobj

class CustomTrackAssemblyHub(AbstractMetaAnalysis):
	"""moves mapping.sorted.bam and mapping.sorted.bam.bai files to a folder and creates trackDb.txt"""
	def __init__(self, outputDir, experiments):

		AbstractMetaAnalysis.__init__(self, outputDir, experiments)