# -*- coding: utf-8 -*-
"""
Created on Thu Jan  3 13:17:52 2019

@author: Yzi



使用机器学习方法进行的  序列比对尝试1
"""
import random

from deap import base
from deap import creator
from deap import tools
#import numpy as np
import ReadData
import Sequence
from sklearn.metrics import roc_auc_score
import Distance


#name="human_muscle"
name="fly_blastoderm"
#name="human_HBB"

## 获取数据集 整个数据集，正数据集，负数据集 都是序列，没有标签
#datasets,pos,neg=rd.getData2("fly_blastoderm")
  ## 获取数据 kmer 从2-->6
print("---------",name,"------------")
rd=ReadData.ReadData()

datasets,pos,neg=rd.getData2(name)
## 正例数据集条数
possize=len(pos)
## 合并两个list
datasets=pos+neg


sq=Sequence.Sequence()

flag=True
    ## 获取整个数据集的kmer集合,dict
    
    ## 获取字典集合
d2set,d2dic=sq.getSeqKerSet(datasets,2)
d3set,d3dic=sq.getSeqKerSet(datasets,3)
d4set,d4dic=sq.getSeqKerSet(datasets,4)
d5set,d5dic=sq.getSeqKerSet(datasets,5)
d6set,d6dic=sq.getSeqKerSet(datasets,6)


## d2 以整个数据集计算 计算频率 不需要标准化，用于计算单个k的其他距离定义方法
d2freLis,d2arf=sq.getSeqfreq(datasets,2,d2dic)
d3freLis,d3arf=sq.getSeqfreq(datasets,3,d3dic)
d4freLis,d4arf=sq.getSeqfreq(datasets,4,d4dic)
d5freLis,d5arf=sq.getSeqfreq(datasets,5,d5dic)
d6freLis,d6arf=sq.getSeqfreq(datasets,6,d6dic)


### 将2-6 合成一个字典,并计算权重

size=len(d2freLis)
## 标准化后合并
freqLis=[None]*size
for i in range(len(d2freLis)):
    freqLis[i]=dict(sq.normdata_max_min(d2freLis[i]),**(sq.normdata_max_min(d3freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d4freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d5freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d6freLis[i])))

# 计算权重
weightMax=sq.getWeight(freqLis)


    ## d2 特征 count 所有数据集
d2countLis,d2arc=sq.getSeqCount(datasets,2,d2dic)
d3countLis,d3arc=sq.getSeqCount(datasets,3,d3dic)
d4countLis,d4arc=sq.getSeqCount(datasets,4,d4dic)
d5countLis,d5arc=sq.getSeqCount(datasets,5,d5dic)
d6countLis,d6arc=sq.getSeqCount(datasets,6,d6dic)



## 将 字典 离散 二值化
for i in range(len(d2countLis)):
    for key in dict.keys(d2countLis[0]):
        if d2countLis[i][key]>0:
            d2countLis[i][key]=1
for i in range(len(d3countLis)):
    for key in dict.keys(d3countLis[0]):
        if d3countLis[i][key]>0:
            d3countLis[i][key]=1
for i in range(len(d4countLis)):
    for key in dict.keys(d4countLis[0]):
        if d4countLis[i][key]>0:
            d4countLis[i][key]=1
for i in range(len(d5countLis)):
    for key in dict.keys(d5countLis[0]):
        if d5countLis[i][key]>0:
            d5countLis[i][key]=1
for i in range(len(d6countLis)):
    for key in dict.keys(d6countLis[0]):
        if d6countLis[i][key]>0:
            d6countLis[i][key]=1






## 标准化过程： 标准化 count
for i in range(len(datasets)):
    d2countLis[i]=sq.normdata_max_min(d2countLis[i])
    d3countLis[i]=sq.normdata_max_min(d3countLis[i])
    d4countLis[i]=sq.normdata_max_min(d4countLis[i])
    d5countLis[i]=sq.normdata_max_min(d5countLis[i])
    d6countLis[i]=sq.normdata_max_min(d6countLis[i])
    
## 标准化后合并
countLis=[None]*size
for i in range(len(d2freLis)):
    countLis[i]=dict(d2countLis[i],**(d3countLis[i]))
    countLis[i]=dict(countLis[i],**(d4countLis[i]))
    countLis[i]=dict(countLis[i],**(d5countLis[i]))
    countLis[i]=dict(countLis[i],**(d6countLis[i]))

weightSize=len(countLis[0])    

## 构造训练集合和测试集合
# 构造对集合
pairPoslist=[]
pairNeglist=[]
for i in range(possize):
    # 装填正例
    for j in range(i+1,possize):
        tpos=[i,j,1]
        pairPoslist.append(tpos)
# 装填负例
for i in range(possize):
    # 装填正例
    for j in range(i+1,possize):
        tneg=[i+possize,j+possize,0]
        pairNeglist.append(tneg)


## 产生随机种子，并将数据集打乱
#randnum = random.randint(0,100)
#random.seed(randnum)
## 打乱正负序列集 
random.shuffle(pairPoslist)
random.shuffle(pairNeglist)

## 划分训练集合和测试集合  并设置标签
trainLis=[]
trainlabel=[]
testLis=[]
testlabel=[]
trainPosSize=int(0.8*len(pairPoslist))
for i in range(len(pairPoslist)):
    if i<=trainPosSize:
        trainLis.append(pairPoslist[i])
        trainlabel.append(1)
        trainLis.append(pairNeglist[i])
        trainlabel.append(0)
    else:
        testLis.append(pairPoslist[i])
        testlabel.append(1)
        testLis.append(pairNeglist[i])
        testlabel.append(0)

##-----------------------------------------------------------------------------    


creator.create("FitnessMax", base.Fitness, weights=(1.0,))   
creator.create("Individual", list, fitness=creator.FitnessMax)    

toolbox = base.Toolbox()    


toolbox.register("attr_bool", random.random)   

## 设置权重个数
toolbox.register("individual", tools.initRepeat, creator.Individual,    #tools.initRepeat是干
    toolbox.attr_bool, weightSize)


toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 


# register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)

def evalOneMax(individual):
    weight = individual 
#    su=sum(weight)
#    for i in range(len(weight)):
#        weight[i]=(weight[i]+np.spacing(1))/su+np.spacing(1)
    sim=[]
    ## 计算权重
  
    for i in range(len(trainLis)):
        tmp_sim=0
        count=0
        for key in sorted(countLis[0]):
             tmp_sim=tmp_sim+countLis[trainLis[i][0]][key]*countLis[trainLis[i][1]][key]*weight[count]
             count=count+1
        sim.append(tmp_sim) 
    auc=roc_auc_score(trainlabel, sim)
    return auc,


#    return float(np.sum(np.abs(np.array(sim)-label))),
    
#----------
# Operator registration
#----------
# register the goal / fitness function
# 这里的toolbox register语句的理解：注册了一个函数evaluae依据的是后面的evalOneMax 理通了!!!
toolbox.register("evaluate", evalOneMax)

# register a mutation operator with a probability to
# flip each attribute/gene of 0.05
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)

# operator for selecting individuals for breeding the next
# generation: each individual of the current generation
# is replaced by the 'fittest' (best) of three individuals
# drawn randomly from the current generation.
toolbox.register("select", tools.selTournament, tournsize=4)    #这里选择的tournsize又是什么意思呢？

#----------

def main():
    random.seed(64)
    # hash(64) is used
    # random.seed方法的作用是给随机数对象一个种子值，用于产生随机序列。
    # 对于同一个种子值的输入，之后产生的随机数序列也一样。
    # 通常是把时间秒数等变化值作为种子值，达到每次运行产生的随机系列都不一样
    # create an initial population of 300 individuals (where
    # each individual is a list of integers)
    pop = toolbox.population(n=100)    #定义了300个个体的种群！！！
    
#    print(pop[0][0])
    for i in range(len(pop)):
        su=sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j]=pop[i][j]/su

    # CXPB  is the probability with which two individuals
    #       are crossed
    #
    # MUTPB is the probability for mutating an individual
    #
    # NGEN  is the number of generations for which the
    #       evolution runs   进化运行的代数！
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    print("  Evaluated %i individuals" % len(pop))   
    
    # Begin the evolution      
    for g in range(NGEN):
        print("-- Generation %i --" % g)
        
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))
        
        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:

            # mutate an individual with probability MUTPB
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
    
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        
        print("Evaluated %i individuals" % len(invalid_ind))
        
        # The population is entirely replaced by the offspring
        pop[:] = offspring
        
        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        
        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        print("  Std %s" % std)
    
    print("-- End of (successful) evolution ---")
    
    best_ind = tools.selBest(pop, 1)[0]
#    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    
    tp=sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i]=best_ind[i]/tp
    return best_ind
#    file= open('test.txt', 'w')
#    for w in best_ind:
#        file.write(str(w))
#        file.write('\n')
#    file.close()

if __name__ == "__main__":
    
    print(len(freqLis[0]))
    dis =Distance.Distance()
    print("GA---------------------------")
    ## 归一化处理
    w=main()

    print(w)
    print(len(w))
    sim=[]
    ## 计算权重  结合遗传算法
    sim=[]
    inner2=[]
    inner3=[]
    inner4=[]
    inner5=[]
    inner6=[]
    
    sim=[]
    ## 计算权重
  
    for i in range(len(testLis)):
        tmp_sim=0
        count=0
        for key in sorted(countLis[0]):
            tmp_sim=tmp_sim+countLis[testLis[i][0]][key]*countLis[testLis[i][1]][key]*w[count]
            count=count+1
        sim.append(tmp_sim)               
    auc=roc_auc_score(testlabel, sim)
    
    
    print(auc)
    print(len(d2countLis[0])+len(d3countLis[0])+len(d4countLis[0])+len(d5countLis[0])+len(d6countLis[0]))

    for k in range(2,7):
        ## eu:
       
        feature="d"+str(k)+"freLis"
        print("----------------EU-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.EuD_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        print("----------------Ma-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.manhattan_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        
        print("----------------kld-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.KLD_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        print("----------------pcc-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.pcc_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
    
        print("----------------cosine-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.cosine_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        print("----------------D2-----------------")
        print("k=",k)
        feature="d"+str(k)+"countLis"
        
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.getD2_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)


3d11e0299588236cd58435bccd0a2054794e8148
<mask token>


def evalOneMax(individual):
    weight = individual
    sim = []
    for i in range(len(trainLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[trainLis[i][0]][key] * countLis[
                trainLis[i][1]][key] * weight[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(trainlabel, sim)
    return auc,


<mask token>


def main():
    random.seed(64)
    pop = toolbox.population(n=100)
    for i in range(len(pop)):
        su = sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j] = pop[i][j] / su
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    print('Start of evolution')
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    print('  Evaluated %i individuals' % len(pop))
    for g in range(NGEN):
        print('-- Generation %i --' % g)
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        print('Evaluated %i individuals' % len(invalid_ind))
        pop[:] = offspring
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean ** 2) ** 0.5
        print('  Min %s' % min(fits))
        print('  Max %s' % max(fits))
        print('  Avg %s' % mean)
        print('  Std %s' % std)
    print('-- End of (successful) evolution ---')
    best_ind = tools.selBest(pop, 1)[0]
    tp = sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i] = best_ind[i] / tp
    return best_ind


<mask token>

<mask token>
print('---------', name, '------------')
<mask token>
for i in range(len(d2freLis)):
    freqLis[i] = dict(sq.normdata_max_min(d2freLis[i]), **sq.
        normdata_max_min(d3freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d4freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d5freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d6freLis[i]))
<mask token>
for i in range(len(d2countLis)):
    for key in dict.keys(d2countLis[0]):
        if d2countLis[i][key] > 0:
            d2countLis[i][key] = 1
for i in range(len(d3countLis)):
    for key in dict.keys(d3countLis[0]):
        if d3countLis[i][key] > 0:
            d3countLis[i][key] = 1
for i in range(len(d4countLis)):
    for key in dict.keys(d4countLis[0]):
        if d4countLis[i][key] > 0:
            d4countLis[i][key] = 1
for i in range(len(d5countLis)):
    for key in dict.keys(d5countLis[0]):
        if d5countLis[i][key] > 0:
            d5countLis[i][key] = 1
for i in range(len(d6countLis)):
    for key in dict.keys(d6countLis[0]):
        if d6countLis[i][key] > 0:
            d6countLis[i][key] = 1
for i in range(len(datasets)):
    d2countLis[i] = sq.normdata_max_min(d2countLis[i])
    d3countLis[i] = sq.normdata_max_min(d3countLis[i])
    d4countLis[i] = sq.normdata_max_min(d4countLis[i])
    d5countLis[i] = sq.normdata_max_min(d5countLis[i])
    d6countLis[i] = sq.normdata_max_min(d6countLis[i])
<mask token>
for i in range(len(d2freLis)):
    countLis[i] = dict(d2countLis[i], **d3countLis[i])
    countLis[i] = dict(countLis[i], **d4countLis[i])
    countLis[i] = dict(countLis[i], **d5countLis[i])
    countLis[i] = dict(countLis[i], **d6countLis[i])
<mask token>
for i in range(possize):
    for j in range(i + 1, possize):
        tpos = [i, j, 1]
        pairPoslist.append(tpos)
for i in range(possize):
    for j in range(i + 1, possize):
        tneg = [i + possize, j + possize, 0]
        pairNeglist.append(tneg)
random.shuffle(pairPoslist)
random.shuffle(pairNeglist)
<mask token>
for i in range(len(pairPoslist)):
    if i <= trainPosSize:
        trainLis.append(pairPoslist[i])
        trainlabel.append(1)
        trainLis.append(pairNeglist[i])
        trainlabel.append(0)
    else:
        testLis.append(pairPoslist[i])
        testlabel.append(1)
        testLis.append(pairNeglist[i])
        testlabel.append(0)
creator.create('FitnessMax', base.Fitness, weights=(1.0,))
creator.create('Individual', list, fitness=creator.FitnessMax)
<mask token>
toolbox.register('attr_bool', random.random)
toolbox.register('individual', tools.initRepeat, creator.Individual,
    toolbox.attr_bool, weightSize)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register('mate', tools.cxTwoPoint)


def evalOneMax(individual):
    weight = individual
    sim = []
    for i in range(len(trainLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[trainLis[i][0]][key] * countLis[
                trainLis[i][1]][key] * weight[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(trainlabel, sim)
    return auc,


toolbox.register('evaluate', evalOneMax)
toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
toolbox.register('select', tools.selTournament, tournsize=4)


def main():
    random.seed(64)
    pop = toolbox.population(n=100)
    for i in range(len(pop)):
        su = sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j] = pop[i][j] / su
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    print('Start of evolution')
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    print('  Evaluated %i individuals' % len(pop))
    for g in range(NGEN):
        print('-- Generation %i --' % g)
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        print('Evaluated %i individuals' % len(invalid_ind))
        pop[:] = offspring
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean ** 2) ** 0.5
        print('  Min %s' % min(fits))
        print('  Max %s' % max(fits))
        print('  Avg %s' % mean)
        print('  Std %s' % std)
    print('-- End of (successful) evolution ---')
    best_ind = tools.selBest(pop, 1)[0]
    tp = sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i] = best_ind[i] / tp
    return best_ind


if __name__ == '__main__':
    print(len(freqLis[0]))
    dis = Distance.Distance()
    print('GA---------------------------')
    w = main()
    print(w)
    print(len(w))
    sim = []
    sim = []
    inner2 = []
    inner3 = []
    inner4 = []
    inner5 = []
    inner6 = []
    sim = []
    for i in range(len(testLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[testLis[i][0]][key] * countLis[testLis
                [i][1]][key] * w[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(testlabel, sim)
    print(auc)
    print(len(d2countLis[0]) + len(d3countLis[0]) + len(d4countLis[0]) +
        len(d5countLis[0]) + len(d6countLis[0]))
    for k in range(2, 7):
        feature = 'd' + str(k) + 'freLis'
        print('----------------EU-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.EuD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------Ma-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.manhattan_feature(eval(feature)[testLis[i][0]],
                eval(feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------kld-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.KLD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------pcc-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.pcc_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------cosine-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.cosine_feature(eval(feature)[testLis[i][0]], eval
                (feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------D2-----------------')
        print('k=', k)
        feature = 'd' + str(k) + 'countLis'
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.getD2_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)

<mask token>
name = 'fly_blastoderm'
print('---------', name, '------------')
rd = ReadData.ReadData()
datasets, pos, neg = rd.getData2(name)
possize = len(pos)
datasets = pos + neg
sq = Sequence.Sequence()
flag = True
d2set, d2dic = sq.getSeqKerSet(datasets, 2)
d3set, d3dic = sq.getSeqKerSet(datasets, 3)
d4set, d4dic = sq.getSeqKerSet(datasets, 4)
d5set, d5dic = sq.getSeqKerSet(datasets, 5)
d6set, d6dic = sq.getSeqKerSet(datasets, 6)
d2freLis, d2arf = sq.getSeqfreq(datasets, 2, d2dic)
d3freLis, d3arf = sq.getSeqfreq(datasets, 3, d3dic)
d4freLis, d4arf = sq.getSeqfreq(datasets, 4, d4dic)
d5freLis, d5arf = sq.getSeqfreq(datasets, 5, d5dic)
d6freLis, d6arf = sq.getSeqfreq(datasets, 6, d6dic)
size = len(d2freLis)
freqLis = [None] * size
for i in range(len(d2freLis)):
    freqLis[i] = dict(sq.normdata_max_min(d2freLis[i]), **sq.
        normdata_max_min(d3freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d4freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d5freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d6freLis[i]))
weightMax = sq.getWeight(freqLis)
d2countLis, d2arc = sq.getSeqCount(datasets, 2, d2dic)
d3countLis, d3arc = sq.getSeqCount(datasets, 3, d3dic)
d4countLis, d4arc = sq.getSeqCount(datasets, 4, d4dic)
d5countLis, d5arc = sq.getSeqCount(datasets, 5, d5dic)
d6countLis, d6arc = sq.getSeqCount(datasets, 6, d6dic)
for i in range(len(d2countLis)):
    for key in dict.keys(d2countLis[0]):
        if d2countLis[i][key] > 0:
            d2countLis[i][key] = 1
for i in range(len(d3countLis)):
    for key in dict.keys(d3countLis[0]):
        if d3countLis[i][key] > 0:
            d3countLis[i][key] = 1
for i in range(len(d4countLis)):
    for key in dict.keys(d4countLis[0]):
        if d4countLis[i][key] > 0:
            d4countLis[i][key] = 1
for i in range(len(d5countLis)):
    for key in dict.keys(d5countLis[0]):
        if d5countLis[i][key] > 0:
            d5countLis[i][key] = 1
for i in range(len(d6countLis)):
    for key in dict.keys(d6countLis[0]):
        if d6countLis[i][key] > 0:
            d6countLis[i][key] = 1
for i in range(len(datasets)):
    d2countLis[i] = sq.normdata_max_min(d2countLis[i])
    d3countLis[i] = sq.normdata_max_min(d3countLis[i])
    d4countLis[i] = sq.normdata_max_min(d4countLis[i])
    d5countLis[i] = sq.normdata_max_min(d5countLis[i])
    d6countLis[i] = sq.normdata_max_min(d6countLis[i])
countLis = [None] * size
for i in range(len(d2freLis)):
    countLis[i] = dict(d2countLis[i], **d3countLis[i])
    countLis[i] = dict(countLis[i], **d4countLis[i])
    countLis[i] = dict(countLis[i], **d5countLis[i])
    countLis[i] = dict(countLis[i], **d6countLis[i])
weightSize = len(countLis[0])
pairPoslist = []
pairNeglist = []
for i in range(possize):
    for j in range(i + 1, possize):
        tpos = [i, j, 1]
        pairPoslist.append(tpos)
for i in range(possize):
    for j in range(i + 1, possize):
        tneg = [i + possize, j + possize, 0]
        pairNeglist.append(tneg)
random.shuffle(pairPoslist)
random.shuffle(pairNeglist)
trainLis = []
trainlabel = []
testLis = []
testlabel = []
trainPosSize = int(0.8 * len(pairPoslist))
for i in range(len(pairPoslist)):
    if i <= trainPosSize:
        trainLis.append(pairPoslist[i])
        trainlabel.append(1)
        trainLis.append(pairNeglist[i])
        trainlabel.append(0)
    else:
        testLis.append(pairPoslist[i])
        testlabel.append(1)
        testLis.append(pairNeglist[i])
        testlabel.append(0)
creator.create('FitnessMax', base.Fitness, weights=(1.0,))
creator.create('Individual', list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register('attr_bool', random.random)
toolbox.register('individual', tools.initRepeat, creator.Individual,
    toolbox.attr_bool, weightSize)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register('mate', tools.cxTwoPoint)


def evalOneMax(individual):
    weight = individual
    sim = []
    for i in range(len(trainLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[trainLis[i][0]][key] * countLis[
                trainLis[i][1]][key] * weight[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(trainlabel, sim)
    return auc,


toolbox.register('evaluate', evalOneMax)
toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
toolbox.register('select', tools.selTournament, tournsize=4)


def main():
    random.seed(64)
    pop = toolbox.population(n=100)
    for i in range(len(pop)):
        su = sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j] = pop[i][j] / su
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    print('Start of evolution')
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    print('  Evaluated %i individuals' % len(pop))
    for g in range(NGEN):
        print('-- Generation %i --' % g)
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        print('Evaluated %i individuals' % len(invalid_ind))
        pop[:] = offspring
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean ** 2) ** 0.5
        print('  Min %s' % min(fits))
        print('  Max %s' % max(fits))
        print('  Avg %s' % mean)
        print('  Std %s' % std)
    print('-- End of (successful) evolution ---')
    best_ind = tools.selBest(pop, 1)[0]
    tp = sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i] = best_ind[i] / tp
    return best_ind


if __name__ == '__main__':
    print(len(freqLis[0]))
    dis = Distance.Distance()
    print('GA---------------------------')
    w = main()
    print(w)
    print(len(w))
    sim = []
    sim = []
    inner2 = []
    inner3 = []
    inner4 = []
    inner5 = []
    inner6 = []
    sim = []
    for i in range(len(testLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[testLis[i][0]][key] * countLis[testLis
                [i][1]][key] * w[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(testlabel, sim)
    print(auc)
    print(len(d2countLis[0]) + len(d3countLis[0]) + len(d4countLis[0]) +
        len(d5countLis[0]) + len(d6countLis[0]))
    for k in range(2, 7):
        feature = 'd' + str(k) + 'freLis'
        print('----------------EU-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.EuD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------Ma-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.manhattan_feature(eval(feature)[testLis[i][0]],
                eval(feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------kld-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.KLD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------pcc-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.pcc_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------cosine-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.cosine_feature(eval(feature)[testLis[i][0]], eval
                (feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------D2-----------------')
        print('k=', k)
        feature = 'd' + str(k) + 'countLis'
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.getD2_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)

<mask token>
import random
from deap import base
from deap import creator
from deap import tools
import ReadData
import Sequence
from sklearn.metrics import roc_auc_score
import Distance
name = 'fly_blastoderm'
print('---------', name, '------------')
rd = ReadData.ReadData()
datasets, pos, neg = rd.getData2(name)
possize = len(pos)
datasets = pos + neg
sq = Sequence.Sequence()
flag = True
d2set, d2dic = sq.getSeqKerSet(datasets, 2)
d3set, d3dic = sq.getSeqKerSet(datasets, 3)
d4set, d4dic = sq.getSeqKerSet(datasets, 4)
d5set, d5dic = sq.getSeqKerSet(datasets, 5)
d6set, d6dic = sq.getSeqKerSet(datasets, 6)
d2freLis, d2arf = sq.getSeqfreq(datasets, 2, d2dic)
d3freLis, d3arf = sq.getSeqfreq(datasets, 3, d3dic)
d4freLis, d4arf = sq.getSeqfreq(datasets, 4, d4dic)
d5freLis, d5arf = sq.getSeqfreq(datasets, 5, d5dic)
d6freLis, d6arf = sq.getSeqfreq(datasets, 6, d6dic)
size = len(d2freLis)
freqLis = [None] * size
for i in range(len(d2freLis)):
    freqLis[i] = dict(sq.normdata_max_min(d2freLis[i]), **sq.
        normdata_max_min(d3freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d4freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d5freLis[i]))
    freqLis[i] = dict(freqLis[i], **sq.normdata_max_min(d6freLis[i]))
weightMax = sq.getWeight(freqLis)
d2countLis, d2arc = sq.getSeqCount(datasets, 2, d2dic)
d3countLis, d3arc = sq.getSeqCount(datasets, 3, d3dic)
d4countLis, d4arc = sq.getSeqCount(datasets, 4, d4dic)
d5countLis, d5arc = sq.getSeqCount(datasets, 5, d5dic)
d6countLis, d6arc = sq.getSeqCount(datasets, 6, d6dic)
for i in range(len(d2countLis)):
    for key in dict.keys(d2countLis[0]):
        if d2countLis[i][key] > 0:
            d2countLis[i][key] = 1
for i in range(len(d3countLis)):
    for key in dict.keys(d3countLis[0]):
        if d3countLis[i][key] > 0:
            d3countLis[i][key] = 1
for i in range(len(d4countLis)):
    for key in dict.keys(d4countLis[0]):
        if d4countLis[i][key] > 0:
            d4countLis[i][key] = 1
for i in range(len(d5countLis)):
    for key in dict.keys(d5countLis[0]):
        if d5countLis[i][key] > 0:
            d5countLis[i][key] = 1
for i in range(len(d6countLis)):
    for key in dict.keys(d6countLis[0]):
        if d6countLis[i][key] > 0:
            d6countLis[i][key] = 1
for i in range(len(datasets)):
    d2countLis[i] = sq.normdata_max_min(d2countLis[i])
    d3countLis[i] = sq.normdata_max_min(d3countLis[i])
    d4countLis[i] = sq.normdata_max_min(d4countLis[i])
    d5countLis[i] = sq.normdata_max_min(d5countLis[i])
    d6countLis[i] = sq.normdata_max_min(d6countLis[i])
countLis = [None] * size
for i in range(len(d2freLis)):
    countLis[i] = dict(d2countLis[i], **d3countLis[i])
    countLis[i] = dict(countLis[i], **d4countLis[i])
    countLis[i] = dict(countLis[i], **d5countLis[i])
    countLis[i] = dict(countLis[i], **d6countLis[i])
weightSize = len(countLis[0])
pairPoslist = []
pairNeglist = []
for i in range(possize):
    for j in range(i + 1, possize):
        tpos = [i, j, 1]
        pairPoslist.append(tpos)
for i in range(possize):
    for j in range(i + 1, possize):
        tneg = [i + possize, j + possize, 0]
        pairNeglist.append(tneg)
random.shuffle(pairPoslist)
random.shuffle(pairNeglist)
trainLis = []
trainlabel = []
testLis = []
testlabel = []
trainPosSize = int(0.8 * len(pairPoslist))
for i in range(len(pairPoslist)):
    if i <= trainPosSize:
        trainLis.append(pairPoslist[i])
        trainlabel.append(1)
        trainLis.append(pairNeglist[i])
        trainlabel.append(0)
    else:
        testLis.append(pairPoslist[i])
        testlabel.append(1)
        testLis.append(pairNeglist[i])
        testlabel.append(0)
creator.create('FitnessMax', base.Fitness, weights=(1.0,))
creator.create('Individual', list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register('attr_bool', random.random)
toolbox.register('individual', tools.initRepeat, creator.Individual,
    toolbox.attr_bool, weightSize)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register('mate', tools.cxTwoPoint)


def evalOneMax(individual):
    weight = individual
    sim = []
    for i in range(len(trainLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[trainLis[i][0]][key] * countLis[
                trainLis[i][1]][key] * weight[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(trainlabel, sim)
    return auc,


toolbox.register('evaluate', evalOneMax)
toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
toolbox.register('select', tools.selTournament, tournsize=4)


def main():
    random.seed(64)
    pop = toolbox.population(n=100)
    for i in range(len(pop)):
        su = sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j] = pop[i][j] / su
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    print('Start of evolution')
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    print('  Evaluated %i individuals' % len(pop))
    for g in range(NGEN):
        print('-- Generation %i --' % g)
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        print('Evaluated %i individuals' % len(invalid_ind))
        pop[:] = offspring
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean ** 2) ** 0.5
        print('  Min %s' % min(fits))
        print('  Max %s' % max(fits))
        print('  Avg %s' % mean)
        print('  Std %s' % std)
    print('-- End of (successful) evolution ---')
    best_ind = tools.selBest(pop, 1)[0]
    tp = sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i] = best_ind[i] / tp
    return best_ind


if __name__ == '__main__':
    print(len(freqLis[0]))
    dis = Distance.Distance()
    print('GA---------------------------')
    w = main()
    print(w)
    print(len(w))
    sim = []
    sim = []
    inner2 = []
    inner3 = []
    inner4 = []
    inner5 = []
    inner6 = []
    sim = []
    for i in range(len(testLis)):
        tmp_sim = 0
        count = 0
        for key in sorted(countLis[0]):
            tmp_sim = tmp_sim + countLis[testLis[i][0]][key] * countLis[testLis
                [i][1]][key] * w[count]
            count = count + 1
        sim.append(tmp_sim)
    auc = roc_auc_score(testlabel, sim)
    print(auc)
    print(len(d2countLis[0]) + len(d3countLis[0]) + len(d4countLis[0]) +
        len(d5countLis[0]) + len(d6countLis[0]))
    for k in range(2, 7):
        feature = 'd' + str(k) + 'freLis'
        print('----------------EU-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.EuD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------Ma-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.manhattan_feature(eval(feature)[testLis[i][0]],
                eval(feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------kld-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.KLD_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------pcc-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.pcc_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------cosine-----------------')
        print('k=', k)
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.cosine_feature(eval(feature)[testLis[i][0]], eval
                (feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)
        print('----------------D2-----------------')
        print('k=', k)
        feature = 'd' + str(k) + 'countLis'
        sim = []
        for i in range(len(testLis)):
            tmp = 1 / dis.getD2_feature(eval(feature)[testLis[i][0]], eval(
                feature)[testLis[i][1]])
            sim.append(tmp)
        auc = roc_auc_score(testlabel, sim)
        print(auc)

# -*- coding: utf-8 -*-
"""
Created on Thu Jan  3 13:17:52 2019

@author: Yzi



使用机器学习方法进行的  序列比对尝试1
"""
import random

from deap import base
from deap import creator
from deap import tools
#import numpy as np
import ReadData
import Sequence
from sklearn.metrics import roc_auc_score
import Distance


#name="human_muscle"
name="fly_blastoderm"
#name="human_HBB"

## 获取数据集 整个数据集，正数据集，负数据集 都是序列，没有标签
#datasets,pos,neg=rd.getData2("fly_blastoderm")
  ## 获取数据 kmer 从2-->6
print("---------",name,"------------")
rd=ReadData.ReadData()

datasets,pos,neg=rd.getData2(name)
## 正例数据集条数
possize=len(pos)
## 合并两个list
datasets=pos+neg


sq=Sequence.Sequence()

flag=True
    ## 获取整个数据集的kmer集合,dict
    
    ## 获取字典集合
d2set,d2dic=sq.getSeqKerSet(datasets,2)
d3set,d3dic=sq.getSeqKerSet(datasets,3)
d4set,d4dic=sq.getSeqKerSet(datasets,4)
d5set,d5dic=sq.getSeqKerSet(datasets,5)
d6set,d6dic=sq.getSeqKerSet(datasets,6)


## d2 以整个数据集计算 计算频率 不需要标准化，用于计算单个k的其他距离定义方法
d2freLis,d2arf=sq.getSeqfreq(datasets,2,d2dic)
d3freLis,d3arf=sq.getSeqfreq(datasets,3,d3dic)
d4freLis,d4arf=sq.getSeqfreq(datasets,4,d4dic)
d5freLis,d5arf=sq.getSeqfreq(datasets,5,d5dic)
d6freLis,d6arf=sq.getSeqfreq(datasets,6,d6dic)


### 将2-6 合成一个字典,并计算权重

size=len(d2freLis)
## 标准化后合并
freqLis=[None]*size
for i in range(len(d2freLis)):
    freqLis[i]=dict(sq.normdata_max_min(d2freLis[i]),**(sq.normdata_max_min(d3freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d4freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d5freLis[i])))
    freqLis[i]=dict(freqLis[i],**(sq.normdata_max_min(d6freLis[i])))

# 计算权重
weightMax=sq.getWeight(freqLis)


    ## d2 特征 count 所有数据集
d2countLis,d2arc=sq.getSeqCount(datasets,2,d2dic)
d3countLis,d3arc=sq.getSeqCount(datasets,3,d3dic)
d4countLis,d4arc=sq.getSeqCount(datasets,4,d4dic)
d5countLis,d5arc=sq.getSeqCount(datasets,5,d5dic)
d6countLis,d6arc=sq.getSeqCount(datasets,6,d6dic)



## 将 字典 离散 二值化
for i in range(len(d2countLis)):
    for key in dict.keys(d2countLis[0]):
        if d2countLis[i][key]>0:
            d2countLis[i][key]=1
for i in range(len(d3countLis)):
    for key in dict.keys(d3countLis[0]):
        if d3countLis[i][key]>0:
            d3countLis[i][key]=1
for i in range(len(d4countLis)):
    for key in dict.keys(d4countLis[0]):
        if d4countLis[i][key]>0:
            d4countLis[i][key]=1
for i in range(len(d5countLis)):
    for key in dict.keys(d5countLis[0]):
        if d5countLis[i][key]>0:
            d5countLis[i][key]=1
for i in range(len(d6countLis)):
    for key in dict.keys(d6countLis[0]):
        if d6countLis[i][key]>0:
            d6countLis[i][key]=1






## 标准化过程： 标准化 count
for i in range(len(datasets)):
    d2countLis[i]=sq.normdata_max_min(d2countLis[i])
    d3countLis[i]=sq.normdata_max_min(d3countLis[i])
    d4countLis[i]=sq.normdata_max_min(d4countLis[i])
    d5countLis[i]=sq.normdata_max_min(d5countLis[i])
    d6countLis[i]=sq.normdata_max_min(d6countLis[i])
    
## 标准化后合并
countLis=[None]*size
for i in range(len(d2freLis)):
    countLis[i]=dict(d2countLis[i],**(d3countLis[i]))
    countLis[i]=dict(countLis[i],**(d4countLis[i]))
    countLis[i]=dict(countLis[i],**(d5countLis[i]))
    countLis[i]=dict(countLis[i],**(d6countLis[i]))

weightSize=len(countLis[0])    

## 构造训练集合和测试集合
# 构造对集合
pairPoslist=[]
pairNeglist=[]
for i in range(possize):
    # 装填正例
    for j in range(i+1,possize):
        tpos=[i,j,1]
        pairPoslist.append(tpos)
# 装填负例
for i in range(possize):
    # 装填正例
    for j in range(i+1,possize):
        tneg=[i+possize,j+possize,0]
        pairNeglist.append(tneg)


## 产生随机种子，并将数据集打乱
#randnum = random.randint(0,100)
#random.seed(randnum)
## 打乱正负序列集 
random.shuffle(pairPoslist)
random.shuffle(pairNeglist)

## 划分训练集合和测试集合  并设置标签
trainLis=[]
trainlabel=[]
testLis=[]
testlabel=[]
trainPosSize=int(0.8*len(pairPoslist))
for i in range(len(pairPoslist)):
    if i<=trainPosSize:
        trainLis.append(pairPoslist[i])
        trainlabel.append(1)
        trainLis.append(pairNeglist[i])
        trainlabel.append(0)
    else:
        testLis.append(pairPoslist[i])
        testlabel.append(1)
        testLis.append(pairNeglist[i])
        testlabel.append(0)

##-----------------------------------------------------------------------------    


creator.create("FitnessMax", base.Fitness, weights=(1.0,))   
creator.create("Individual", list, fitness=creator.FitnessMax)    

toolbox = base.Toolbox()    


toolbox.register("attr_bool", random.random)   

## 设置权重个数
toolbox.register("individual", tools.initRepeat, creator.Individual,    #tools.initRepeat是干
    toolbox.attr_bool, weightSize)


toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 


# register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)

def evalOneMax(individual):
    weight = individual 
#    su=sum(weight)
#    for i in range(len(weight)):
#        weight[i]=(weight[i]+np.spacing(1))/su+np.spacing(1)
    sim=[]
    ## 计算权重
  
    for i in range(len(trainLis)):
        tmp_sim=0
        count=0
        for key in sorted(countLis[0]):
             tmp_sim=tmp_sim+countLis[trainLis[i][0]][key]*countLis[trainLis[i][1]][key]*weight[count]
             count=count+1
        sim.append(tmp_sim) 
    auc=roc_auc_score(trainlabel, sim)
    return auc,


#    return float(np.sum(np.abs(np.array(sim)-label))),
    
#----------
# Operator registration
#----------
# register the goal / fitness function
# 这里的toolbox register语句的理解：注册了一个函数evaluae依据的是后面的evalOneMax 理通了!!!
toolbox.register("evaluate", evalOneMax)

# register a mutation operator with a probability to
# flip each attribute/gene of 0.05
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)

# operator for selecting individuals for breeding the next
# generation: each individual of the current generation
# is replaced by the 'fittest' (best) of three individuals
# drawn randomly from the current generation.
toolbox.register("select", tools.selTournament, tournsize=4)    #这里选择的tournsize又是什么意思呢？

#----------

def main():
    random.seed(64)
    # hash(64) is used
    # random.seed方法的作用是给随机数对象一个种子值，用于产生随机序列。
    # 对于同一个种子值的输入，之后产生的随机数序列也一样。
    # 通常是把时间秒数等变化值作为种子值，达到每次运行产生的随机系列都不一样
    # create an initial population of 300 individuals (where
    # each individual is a list of integers)
    pop = toolbox.population(n=100)    #定义了300个个体的种群！！！
    
#    print(pop[0][0])
    for i in range(len(pop)):
        su=sum(pop[i])
        for j in range(len(pop[i])):
            pop[i][j]=pop[i][j]/su

    # CXPB  is the probability with which two individuals
    #       are crossed
    #
    # MUTPB is the probability for mutating an individual
    #
    # NGEN  is the number of generations for which the
    #       evolution runs   进化运行的代数！
    CXPB, MUTPB, NGEN = 0.6, 0.3, 50
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    print("  Evaluated %i individuals" % len(pop))   
    
    # Begin the evolution      
    for g in range(NGEN):
        print("-- Generation %i --" % g)
        
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))
        
        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:

            # mutate an individual with probability MUTPB
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
    
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        
        print("Evaluated %i individuals" % len(invalid_ind))
        
        # The population is entirely replaced by the offspring
        pop[:] = offspring
        
        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        
        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        print("  Std %s" % std)
    
    print("-- End of (successful) evolution ---")
    
    best_ind = tools.selBest(pop, 1)[0]
#    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    
    tp=sum(best_ind)
    for i in range(len(best_ind)):
        best_ind[i]=best_ind[i]/tp
    return best_ind
#    file= open('test.txt', 'w')
#    for w in best_ind:
#        file.write(str(w))
#        file.write('\n')
#    file.close()

if __name__ == "__main__":
    
    print(len(freqLis[0]))
    dis =Distance.Distance()
    print("GA---------------------------")
    ## 归一化处理
    w=main()

    print(w)
    print(len(w))
    sim=[]
    ## 计算权重  结合遗传算法
    sim=[]
    inner2=[]
    inner3=[]
    inner4=[]
    inner5=[]
    inner6=[]
    
    sim=[]
    ## 计算权重
  
    for i in range(len(testLis)):
        tmp_sim=0
        count=0
        for key in sorted(countLis[0]):
            tmp_sim=tmp_sim+countLis[testLis[i][0]][key]*countLis[testLis[i][1]][key]*w[count]
            count=count+1
        sim.append(tmp_sim)               
    auc=roc_auc_score(testlabel, sim)
    
    
    print(auc)
    print(len(d2countLis[0])+len(d3countLis[0])+len(d4countLis[0])+len(d5countLis[0])+len(d6countLis[0]))

    for k in range(2,7):
        ## eu:
       
        feature="d"+str(k)+"freLis"
        print("----------------EU-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.EuD_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        print("----------------Ma-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.manhattan_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        
        print("----------------kld-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.KLD_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        print("----------------pcc-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.pcc_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
    
        print("----------------cosine-----------------")
        print("k=",k)
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.cosine_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)
        
        
        print("----------------D2-----------------")
        print("k=",k)
        feature="d"+str(k)+"countLis"
        
        sim=[]
        for i in range(len(testLis)):
            tmp=1/(dis.getD2_feature(eval(feature)[testLis[i][0]],eval(feature)[testLis[i][1]]))
            sim.append(tmp)
        auc=roc_auc_score(testlabel, sim)  
        print(auc)

