美文网首页
根据上一行的ID增加到下一行缺失ID的元素中

根据上一行的ID增加到下一行缺失ID的元素中

作者: 徐诗芬 | 来源:发表于2021-10-28 22:57 被阅读0次

    基因组上传中遇到的问题之一:

    In annotation file, each features should have ID information in the ninth column如图所示:只有部分地方有ID,其他地方没有需要补充。

    1.png

    根据上一行的ID修改文件脚本如下:小脚本没有过于复杂的包或者函数,主要是涉及到关于元素的片切和拼接,根据个人所需来添加。

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    """
        author: xushifen
        script: 批量从gff文件读取第9列信息,缺乏ID信息的补充上一行的ID信息
        date:2021.10.28
    """
    def usage():
        print('Usage: python add_ID.py [in_file] [out_file]')
    
    
    def main():
        inf = open('test.gff.txt', 'r')
        ouf = open('test.gff.new.txt', 'w')
    
        for line in inf:
            nine_word = line.strip().split("\t")[8]  # 取第九列元素
            flag = nine_word.split('; ', 1)[0]  # 取第九列的一个元素
            if flag.startswith("ID="):
                ouf.write(line)
                last_flag = flag
            else:
                nine_word_new = last_flag + '; ' + line.strip().split("\t")[8]
                new_list = line.strip().split("\t")[:8]
                new_list.append(nine_word_new)
                str = '\t'
                newline = str.join(new_list)
                ouf.write(newline + '\n')
        inf.close()
        ouf.close()
    try:
        main()
    except IndexError:
        usage()
    

    测试结果如下:


    2.png

    相关文章

      网友评论

          本文标题:根据上一行的ID增加到下一行缺失ID的元素中

          本文链接:https://www.haomeiwen.com/subject/dlybaltx.html