#!usr/bin/python
import os
import numpy as np

import common
from interpret import qcfile, funcfile, analyzefiles









def runAll(args):

	print('\n\n\nYou have requested to analyze CNV call data')
	print('\tWARNING:')
	print('\t\tIF USING ANY REFERENCES OTHER THAN THOSE I PROVIDE I CANNOT GUARANTEE RESULT ACCURACY')
	print('\n')


	
	
	
	#Set up environment#
	args.AnalysisDirectory = common.fixDirName(args.AnalysisDirectory)
	
	
	
	folderDict = {'LowessBinCounts': args.lowess, 
		    'Segments': args.segments, 
		    'PipelineStats': args.countstats}
	
	for i in list(folderDict.keys()):
		if not folderDict[i]:
			folderDict[i] = args.AnalysisDirectory + i + '/'
		else:
			folderDict[i] = common.fixDirName(folderDict[i])
	
	
	
	QCdir = args.AnalysisDirectory + 'QC/'
	CNVdir = args.AnalysisDirectory + 'CNVlists/'
	summaryDir = args.AnalysisDirectory + 'CNVsummary/'
	CNplotDir = args.AnalysisDirectory + 'CopyNumberProfilePlots/'
	#ChromPlotDir = args.AnalysisDirectory + 'ChromosomeCopyNumberPlots/'
	#summaryPlotDir = args.AnalysisDirectory + 'CombinedSamplesPlots/'
	
	for i in [args.AnalysisDirectory, QCdir, CNVdir, summaryDir, CNplotDir]:#, ChromPlotDir]#, summaryPlotDir]:
		common.makeDir(i)
	
	
	
	#get list of samples to process 
		#will involve checking infofile (if present) and whether required input files exist
	sampleFiles = common.getSampleList(folderDict['Segments'], args.samples, 'segments')
	sampleNames = [x.split('/')[-1].split('.')[0] for x in sampleFiles]

	info = common.importInfoFile(args.infofile, args.columns, 'interpret')

	if args.infofile:
		refArray = info
	else:
		thisDtype = info
		refArray = np.array(
			[ (x, 1, 'unk',) for x in sampleNames],
			dtype=thisDtype)
		

	
	
	
	#QC assessment#
	argList = [(x, args.species, folderDict['PipelineStats'], folderDict['LowessBinCounts'], folderDict['Segments'], QCdir) for x in sampleNames]
	common.daemon(qcfile.runQCone, argList, 'assess sample quality')

	analysisSamples = []
	ploidyDict = {}
	genderDict = {}
	
	mergeQCfile = QCdir + 'ALL_SAMPLES.QC.txt'
	OUT = open(mergeQCfile, 'w')
	OUT.write('Name\tReads\tMAPD\tCS\tPloidy\tGender\tPASS\n')
	
	for i in sampleNames:
		IN = open(QCdir + i + '.qcTEMP.txt', 'r')
		data = IN.readline()	
		OUT.write(data)
		
		data = data.rstrip().split('\t')
		if data[-1] == 'True':
			analysisSamples.append(i)
			ploidyDict[i] = float(data[4])
			genderDict[i] = data[-2]
		
		IN.close()
		os.remove(QCdir + i + '.qcTEMP.txt')
		
	OUT.close()
	
	
	
	#FUnC: CNV filtering#
#	funcfile.FUnCone(analysisSamples[0], args.species, folderDict['Segments'], CNVdir, 
#			 ploidyDict[analysisSamples[0]], genderDict[analysisSamples[0]])
	argList = [(x, args.species, folderDict['Segments'], CNVdir, ploidyDict[x], genderDict[x]) for x in analysisSamples]
	common.daemon(funcfile.FUnCone, argList, ' remove unreliable CNV calls')
	
	raise SystemExit
	
	
	
	#CNV analysis#
	argList = [(x, folderDict['Segments'], folderDict['LowessBinCounts'], CNplotDir, ploidyDict[x], genderDict[x]) for x in analysisSamples]
	common.daemon(analyzefiles.analyzeOne, argList, ' create summary file(s)')
	
	#there really was more I indented to add, but, seriously, if you've gotten far enough to find this message you could code it yourself, and I have a real job now#
	
	print('\nCNV analysis complete\n\n\n')
	
	
	
	
	
	

@@ -2859,26 +2859,8 @@
 )%0A%09%0A
-%09raise SystemExit%0A
 %09%0A%09%0A
@@ -2877,16 +2877,192 @@
 alysis#%0A
+%09analyzefiles.analyzeOne(analysisSamples%5B0%5D, folderDict%5B'Segments'%5D, folderDict%5B'LowessBinCounts'%5D, CNplotDir, ploidyDict%5BanalysisSamples%5B0%5D%5D, genderDict%5BanalysisSamples%5B0%5D%5D)%0A#
 %09argList
@@ -3183,32 +3183,33 @@
 nalysisSamples%5D%0A
+#
 %09common.daemon(a
@@ -3275,169 +3275,24 @@
 %0A%09%0A%09
-#there really was more I indented to add, but, seriously, if you've gotten far enough to find this message you could code it yourself, and I have a real job now#
+raise SystemExit
 %0A%09%0A%09

Update runinterpret.py