import csv


def readcsv():
	listret = []
		
	with open('combined-files.csv', 'rt') as f:
		csv_reader = csv.reader(f)
		flines = list(csv_reader)[2:]
	for x in range(len(flines)):
		if flines[x][0] != "":
			listret.append([flines[x]])
		else:
			auxlist = [flines[x][-2],flines[x][-1]]
			if auxlist != ["",""]:
				listret[-1].append(auxlist)
		
	return listret


def extract_sublist(lst1,lst2):
	retlist = []
	for pdb in lst1:
		auxvar = 0
		for molecule in pdb:
			try:
				if float(molecule[-1]) in lst2: 
					auxvar = 1
			except:
				pass
		if auxvar == 1:
			retlist.append(pdb)
	return retlist


def acess_methods(lst):
	dict_ret = {}
	for pdb in lst:
		if pdb[0][2] not in dict_ret:
			method = pdb[0][2]
			em_res = pdb[0][1]
			xray_res = pdb[0][5] 
			pdbid = pdb[0][0]
			
			try:
				aux2 = ""
				if float(xray_res) > 100:
					aux2 = xray_res[0]
					aux2 = aux2 + '.'
					aux2 = aux2 + xray_res[1:]
					xray_res = float(aux2)
			except:
				pass	
				
			dict_ret.update({method:[ [pdbid], [em_res] , [xray_res], 1] })
		else:
			method = pdb[0][2]
			em_res = pdb[0][1]
			xray_res = pdb[0][5] 
			pdbid = pdb[0][0]
			
			
			try:
				aux2 = ""
				if float(xray_res) > 100:
					aux2 = xray_res[0]
					aux2 = aux2 + '.'
					aux2 = aux2 + xray_res[1:]
					xray_res = float(aux2)
			except:
				pass	
				
			dict_ret[method][0].append(pdbid) 
			dict_ret[method][1].append(em_res) # em res
			dict_ret[method][2].append(xray_res) # xray res
			dict_ret[method][3]+=1
			
	return dict_ret
		
	
def extract_methods_covid19(dict_):
	

	xray = 'X-RAY DIFFRACTION'
	print('\n\n\t\t',xray)
	
	print('\t\tTotal>',len(dict_[xray][2]))
	for x in range(len(dict_[xray][2])):
		try:
			dict_[xray][2][x] = float(dict_[xray][2][x])
		except:
			print (dict_[xray][0][x])
	print('\t\tAvg Res',sum(dict_[xray][2])/len(dict_[xray][2]))
	print('\n\n')
	
	
	
	em = 'ELECTRON MICROSCOPY'
	print('\n\n\t\t',em)
	
	emres = dict_[em][1]
	print('\t\tTotal>',len(emres))
	auxlist = []
	for x in range(len(emres)):
		try:
			auxlist.append(float(emres[x]))
		except: # caso vazio, ou com mais de uma resolucao
			if emres[x] == '':
				pass
			else:
				if ',' in emres[x]:
					for y in (emres[x].split(',')):
						auxlist.append(float(y))
	print('\t\tAvg Res',sum(auxlist)/len(auxlist))
	
def extract_methods_given_taxid(lst):	
	x = extract_sublist(data,lst)
	print('There is a total of {x} pdbs'.format(x=len(x)))
	x_dict = acess_methods(x)
	for el in x_dict:
		print ('\t\tMethod {x}:{y}'.format(x=el, y = x_dict[el][3]))

	extract_methods_covid19(x_dict)
	
	
	
	
# Main program
	
data = readcsv()[:-1]

print("COVID19")
COVID19 = [2697049] # 2538
extract_methods_given_taxid(COVID19)


print ('\n\n\n\nCRIMEAN-CONGO')
Crimean_Congo_haemorrhagic_fever_virus = [1980519, 11593, 652961]
extract_methods_given_taxid(Crimean_Congo_haemorrhagic_fever_virus)


print ('\n\n\n\nEBOLA')
Ebola_virus = [1570291, 128952, 128951, 128947, 129000]
extract_methods_given_taxid(Ebola_virus)



print ('\n\n\n\nMARBURG')
Marburg_virus_disease = [11269, 186537, 1708253, 1708252, 33727, 33728, 482820, 378809, 378830]
extract_methods_given_taxid(Marburg_virus_disease)




print ('\n\n\n\LASSA FEVER')
Lassa_fever = [11620, 11621, 11622]
extract_methods_given_taxid(Lassa_fever)
	
	
print ('\n\n\n\MERS')
MERS_CoV = [1335626, 1263720, 1235996, 1298362, 1306931, 1495253]
extract_methods_given_taxid(MERS_CoV)
	
	
print ('\n\n\n\SARS')
SARS = [694009, 227984, 2901879,227859, 228404,242743, 235410,229993, 255730]
extract_methods_given_taxid(SARS)
	
print ('\n\n\n\nNIPAH')
Nipah = [121791]
extract_methods_given_taxid(Nipah)
	
print ('\n\n\n\HENIPA')
Henipaviral_diseases = [260964, 63330, 665602, 1979176, 1979179, 1221391]
extract_methods_given_taxid(Henipaviral_diseases)
		

print ('\n\n\nRIFT')		
Rift_Valley_fever = [11588, 11589]
extract_methods_given_taxid(Rift_Valley_fever)


print ('\n\n\nZIKA')		
Zika = [64320, 2043570, 2316109, 2316109, 2043570]
extract_methods_given_taxid(Zika)


print ('\n\n\nHIV1')		
Hiv1 = [11676, 11685, 11678]
extract_methods_given_taxid(Hiv1)


print ('\n\n\nHIV2')		
Hiv2 = [11709, 11720, 11721, 11717, 11713]
extract_methods_given_taxid(Hiv2)







