Advertisement

SNP基因芯片基因分型2 - 提取分析基因组区域的SNP芯片数据

阅读量:

本文介绍如何对SNP芯片数据,提取分析基因组区域的SNP芯片数据,用于后续介绍的基于SNP芯片的基因分型。

往期文章:

SNP基因芯片基因分型1 - 解析SNP芯片各样本等位基因数据

1. 提取分析基因组区域的SNP芯片数据程序基本用法

将上一步骤的结果文件作为输入文件,提取指定基因组区域的芯片数据。

复制代码
    # 运行方式, 默认dataType为ASA
    # --input: SNP基因芯片基因分型1结果文件路径
    # --output: 输出结果文件路径
    # --region: 提取基因组区域, 格式: chr16:100000-200000
    python extract_region.py \
    --input /path/XXX_ASA_v1_FinalReport_standard_new.txt \
    --output /path/region.txt
    --region "chr16:100000-200000"
    
    
      
      
      
      
      
      
      
      
    

2. python主程序代码

复制代码
    # extract_region.py
    import re
    import optparse
    import pandas as pd
    
    def extract_region(dataframe: pd.Dataframe, region: str):
    """提取指定的基因组区域"""
    search_object = re.search(r'(chr)*(\d+|X|Y|x|y|):(\d+)-(\d+)', region)
    # 获取正则表达匹配字符
    _, chrom_index, begin, end = search_object.groups()
    
    dataframe['position'] = dataframe['position'].astype(int)
    # 提取指定染色体和开始结束区域
    dataframe_region = dataframe[(dataframe['chromosome'].astype(str) == chrom_index) & (dataframe['position'] >= int(begin)) & (dataframe['position'] <= int(end))]
    
    # 返回排序后的dataframe
    return dataframe_region.sort_values(by="position", ascending=True)
    
    
    if __name__ == '__main__':
    parse = optparse.OptionParser(usage='"%prog"')
    parse.add_option("--input", dest="input", default='na', help="The path of input file")
    parse.add_option("--output", dest="output", default='na', help="The path of output file")
    parse.add_option("--region", dest="region", default='na', help="The region")
    options, args = parse.parse_args()
    if options.input.endswith(('.tsv', '.txt')):
        dataframe_parser = pd.read_table(options.input, sep='\t', low_memory=False)
    elif options.input.endswith(('.xls', 'xlsx')):
        dataframe_parser = pd.read_excel(options.input, low_memory=False)
    else:
        raise Exception("tsv, xlsx, xls be support only as intput format.")
    
    # 提取指定区域dataframe后保存
    dataframe_object = extract_region(dataframe=dataframe_parser, region=options.region)
    
    if options.output.endswith((".tsv", ".txt")):
        dataframe_object.to_csv(options.output, sep='\t', index=False)
    elif options.output.endswith((".xls", ".xlsx")):
        dataframe_object.to_excel(options.output, index=False)
    else:
        raise Exception("tsv, xlsx, xls be support only as output format.")
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

全部评论 (0)

还没有任何评论哟~