You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular中格式化可下载的.ped文件?

解决Angular中生成标准PED文件格式的问题

首先,你需要明确PED文件的规范格式:它是纯文本表格格式,每一行代表一个个体,列通常用制表符(\t)分隔,核心必填列顺序为:

  • 家庭ID
  • 个体ID
  • 父亲ID(无则填0)
  • 母亲ID(无则填0)
  • 性别编码(1=男性,2=女性,0=未知)
  • 表型编码(1=正常,2=患病,0=未知)

额外可按需添加基因型数据列,格式保持一致即可。

接下来,你需要把API返回的数组数据转换为符合上述格式的多行字符串,替换原来的单行data变量:

步骤1:映射API数据到PED字段

假设你的API返回的个体数据结构类似:

interface ApiIndividual {
  familyId: string;
  personId: string;
  fatherId?: string;
  motherId?: string;
  gender: 'male' | 'female' | 'unknown';
  phenotype: 'normal' | 'affected' | 'unknown';
  // 可选:基因型字段
  // genotype1: string;
  // genotype2: string;
}

步骤2:生成PED格式的多行字符串

在Angular组件的.ts文件中,替换原有的data变量生成逻辑:

// 假设apiData是从API获取的个体数组
const apiData: ApiIndividual[] = await this.yourApiService.getFamilyData(this.familyId);

// 转换每个个体为PED格式的一行
const pedRows = apiData.map(individual => {
  // 处理缺失值,确保符合PED规范
  const familyId = individual.familyId || this.familyId;
  const fatherId = individual.fatherId || '0';
  const motherId = individual.motherId || '0';
  
  // 转换性别为PED标准编码
  const genderCode = individual.gender === 'male' ? '1' : 
                     individual.gender === 'female' ? '2' : '0';
  
  // 转换表型为PED标准编码
  const phenotypeCode = individual.phenotype === 'normal' ? '1' : 
                        individual.phenotype === 'affected' ? '2' : '0';
  
  // 拼接行(如果需要基因型,在末尾追加列即可)
  return `${familyId}\t${individual.personId}\t${fatherId}\t${motherId}\t${genderCode}\t${phenotypeCode}`;
});

// 将所有行用换行符连接,形成完整的PED内容
const data = pedRows.join('\n');

// 后续生成Blob和文件URL的逻辑保持不变
const blob = new Blob([data], { type: 'application/octet-stream' });
this.fileUrl = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));

关键注意事项

  • 用制表符(\t)分隔列比空格更可靠,避免字段值含空格时出现格式混乱;
  • 缺失的亲属ID必须填0,不能留空;
  • 性别和表型必须使用PED标准数字编码,否则外部系谱工具可能无法识别;
  • 如需添加基因型数据,直接在每行末尾追加对应列即可,列间用制表符分隔。

这样生成的PED文件就是标准类表格格式,每行对应一个个体,外部工具可正常解析生成系谱图。

内容的提问来源于stack exchange,提问作者Faraz Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 01:25:18