gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
一个csv文件,第一列是id,第二列是表型值,后面的每一列都是每个snp在每个材料的基因型,只考虑纯和基因型,不考虑杂合基因型和NN缺失,每个snp应该只有两种基因型,用两种基因…
- User
一个csv文件,第一列是id,第二列是表型值,后面的每一列都是每个snp在每个材料的基因型,只考虑纯和基因型,不考虑杂合基因型和NN缺失,每个snp应该只有两种基因型,用两种基因型的表型平均值去比较,平均值较高的为snp的较好基因型,从而计算每个snp的较好基因型,输出到一个新的csv,使用python实现
- Assistant
假设原始csv文件名为"input.csv",新csv文件名为"output.csv",代码实现如下:
import csv # 读取原始文件并处理数据 with open('input.csv', 'r') as f: reader = csv.reader(f) data = list(reader) # 提取表型数据,格式为{id: 表型值} phenotypes = {row[0]: float(row[1]) for row in data} # 提取每个snp在每个材料的基因型数据,格式为{snp_id: {material_id: [基因型1, 基因型2]}} genotypes = {} for row in data: snp_id = row[2] material_id = row[0] genotype = [x.strip() for x in row[3:] if x.strip() != 'NN'] if len(genotype) == 2: if snp_id not in genotypes: genotypes[snp_id] = {} genotypes[snp_id][material_id] = genotype # 计算每个snp的较好基因型 best_genotypes = {} for snp_id in genotypes: best_genotype = None best_mean_phenotype = 0 for genotype in genotypes[snp_id].values(): # 计算基因型的表型平均值 mean_phenotype = sum([phenotypes[material_id] for material_id in genotypes[snp_id] if genotypes[snp_id][material_id] == genotype])/len(genotype) if mean_phenotype > best_mean_phenotype: best_mean_phenotype = mean_phenotype best_genotype = genotype best_genotypes[snp_id] = best_genotype # 将结果输出到新文件 with open('output.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['SNP ID', 'Best Genotype']) for snp_id, best_genotype in best_genotypes.items(): writer.writerow([snp_id, '/'.join(best_genotype)])