text: #!/usr/bin/env python
  #_*_coding:utf-8_*_
  
  import re
  
  AAGroup = {
  	2:['HRKQNEDSTGPACVIM', 'LFYW'],
  	3:['HRKQNEDSTGPACVIM', 'LFY', 'W'],
  	4:['HRKQNEDSTGPA', 'CIV', 'MLFY', 'W'],
  	5:['HRKQNEDSTGPA', 'CV', 'IML', 'FY', 'W'],
  	6:['HRKQNEDSTPA', 'G', 'CV', 'IML', 'FY', 'W'],
  	7:['HRKQNEDSTA', 'G', 'P', 'CV', 'IML', 'FY', 'W'],
  	8:['HRKQSTA', 'NED', 'G', 'P', 'CV', 'IML', 'FY', 'W'],
  	9:['HRKQ', 'NED', 'ASTG', 'P', 'C', 'IV', 'MLF', 'Y', 'W'],
  	10:['RKHSA', 'Q', 'NED', 'G', 'P', 'C', 'TIV', 'MLF', 'Y', 'W'],
  	11:['RKQ', 'NG', 'ED', 'AST', 'P', 'C', 'IV', 'HML', 'F', 'Y', 'W'],
  	12:['RKQ', 'ED', 'NAST', 'G', 'P', 'C', 'IV', 'H', 'ML', 'F', 'Y', 'W'],
  	13:['RK', 'QE', 'D', 'NG', 'HA', 'ST', 'P', 'C', 'IV', 'ML', 'F', 'Y', 'W'],
  	14:['R', 'K', 'QE', 'D', 'NG', 'HA', 'ST', 'P', 'C', 'IV', 'ML', 'F', 'Y', 'W'],
  	15:['R', 'K', 'QE', 'D', 'NG', 'HA', 'ST', 'P', 'C', 'IV', 'M', 'L', 'F', 'Y', 'W'],
  	16:['R', 'K', 'Q', 'E', 'D', 'NG', 'HA', 'ST', 'P', 'C', 'IV', 'M', 'L', 'F', 'Y', 'W'],
  	17:['R', 'K', 'Q', 'E', 'D', 'NG', 'HA', 'S', 'T', 'P', 'C', 'IV', 'M', 'L', 'F', 'Y', 'W'],
  	18:['R', 'K', 'Q', 'E', 'D', 'NG', 'HA', 'S', 'T', 'P', 'C', 'I', 'V', 'M', 'L', 'F', 'Y', 'W'],
  	19:['R', 'K', 'Q', 'E', 'D', 'NG', 'H', 'A', 'S', 'T', 'P', 'C', 'I', 'V', 'M', 'L', 'F', 'Y', 'W'],
  	20:['R', 'K', 'Q', 'E', 'D', 'N', 'G', 'H', 'A', 'S', 'T', 'P', 'C', 'I', 'V', 'M', 'L', 'F', 'Y', 'W'],
  }
  
  '''
  for key in AAGroup:
  	aa = set(list(''.join(AAGroup[key])))
  	if len(aa) != 20:
  		print(key, 1)
  	if key != len(AAGroup[key]):
  		print(key, 2)
  	print(''.join(AAGroup[key]))
  '''
  
  def gapModel(fastas, myDict, gDict, gNames, ktuple, glValue):
  	encodings = []
  	header = ['#']
  
  	if ktuple == 1:
  		header = header + [g + '_gap' + str(glValue) for g in gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence), glValue+1):
  				numDict[gDict[myDict[sequence[j]]]] = numDict.get(gDict[myDict[sequence[j]]], 0) + 1
  
  			for g in gNames:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	if ktuple == 2:
  		header = header + [g1 + '_' + g2 + '_gap' + str(glValue) for g1 in gNames for g2 in gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence), glValue + 1):
  				if j+1 < len(sequence):
  					numDict[gDict[myDict[sequence[j]]]+'_'+gDict[myDict[sequence[j+1]]]] = numDict.get(gDict[myDict[sequence[j]]]+'_'+gDict[myDict[sequence[j+1]]], 0) + 1
  
  			for g in [g1+'_'+g2 for g1 in gNames for g2 in gNames]:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	if ktuple == 3:
  		header = header + [g1 + '_' + g2 + '_' + g3 + '_gap' + str(glValue) for g1 in gNames for g2 in gNames for g3 in gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence), glValue + 1):
  				if j + 1 < len(sequence) and j + 2 < len(sequence):
  					numDict[gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + 1]]] + '_' + gDict[myDict[sequence[j + 2]]]] = numDict.get(gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + 1]]] + '_' + gDict[myDict[sequence[j + 2]]], 0) + 1
  
  			for g in [g1 + '_' + g2 + '_' +g3 for g1 in gNames for g2 in gNames for g3 in gNames]:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	return encodings
  
  def lambdaModel(fastas, myDict, gDict, gNames, ktuple, glValue):
  	if glValue == 0:
  		print('Warning: the lambda value should not be zero in "lambda correlation" model')
  		return 0
  
  	encodings = []
  	header = ['#']
  
  	if ktuple == 1:
  		header = header + [g + '_LC' + str(glValue) for g in gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence)):
  				numDict[gDict[myDict[sequence[j]]]] = numDict.get(gDict[myDict[sequence[j]]], 0) + 1
  
  			for g in gNames:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	if ktuple == 2:
  		header = header + [g1 + '_' + g2 + '_LC' + str(glValue) for g1 in gNames for g2 in gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence)):
  				if j + glValue < len(sequence):
  					numDict[gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + glValue]]]] = numDict.get(
  						gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + glValue]]], 0) + 1
  
  			for g in [g1 + '_' + g2 for g1 in gNames for g2 in gNames]:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	if ktuple == 3:
  		header = header + [g1 + '_' + g2 + '_' + g3 + '_LC' + str(glValue) for g1 in gNames for g2 in gNames for g3 in
  						   gNames]
  		encodings.append(header)
  		for i in fastas:
  			name, sequence = i[0], re.sub('-', '', i[1])
  			code = [name]
  			numDict = {}
  			for j in range(0, len(sequence)):
  				if j + glValue < len(sequence) and j + 2*glValue < len(sequence):
  					numDict[gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + glValue]]] + '_' + gDict[
  						myDict[sequence[j + 2*glValue]]]] = numDict.get(
  						gDict[myDict[sequence[j]]] + '_' + gDict[myDict[sequence[j + glValue]]] + '_' + gDict[
  							myDict[sequence[j + 2*glValue]]], 0) + 1
  
  			for g in [g1 + '_' + g2 + '_' + g3 for g1 in gNames for g2 in gNames for g3 in gNames]:
  				code.append(numDict.get(g, 0))
  			encodings.append(code)
  
  	return encodings
  
  def type1(fastas, subtype, raactype, ktuple, glValue):
  	if raactype not in AAGroup:
  		print('Error: the "--raactype" value is not correct.')
  		return 0
  
  	# index each amino acids to their group
  	myDict = {}
  	for i in range(len(AAGroup[raactype])):
  		for aa in AAGroup[raactype][i]:
  			myDict[aa] = i
  
  	gDict = {}
  	gNames = []
  	for i in range(len(AAGroup[raactype])):
  		gDict[i] = '3B.G.'+str(i+1)
  		gNames.append('3B.G.'+str(i+1))
  
  	encodings = []
  	if subtype == 'g-gap':
  		encodings = gapModel(fastas, myDict, gDict, gNames, ktuple, glValue)
  	else:
  		encodings = lambdaModel(fastas, myDict, gDict, gNames, ktuple, glValue)
  
  	return encodings