{"id":598,"date":"2024-03-25T12:08:57","date_gmt":"2024-03-25T04:08:57","guid":{"rendered":"https:\/\/linguopeng.top\/?p=598"},"modified":"2024-03-25T12:09:27","modified_gmt":"2024-03-25T04:09:27","slug":"python-%e6%a0%b9%e6%8d%aegff%e6%8f%90%e5%8f%96%e5%9f%ba%e5%9b%a0%e7%bb%84%e6%8c%87%e5%ae%9a%e4%bd%8d%e7%bd%ae%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/linguopeng.top\/?p=598","title":{"rendered":"\u6839\u636egff\u4fe1\u606f\u63d0\u53d6\u57fa\u56e0\u7ec4\u6307\u5b9a\u4f4d\u7f6e\u5e8f\u5217"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\r\n# -*- coding: utf-8 -*-\r\n\r\nimport argparse\r\n\r\nparser = argparse.ArgumentParser(description='\u8be5\u811a\u672c\u7528\u4e8e\u5728\u57fa\u56e0\u7ec4\u7279\u5b9a\u4f4d\u7f6e\u622a\u53d6\u5e8f\u5217\uff0c\u9700\u989d\u5916\u8f93\u5165\u8bb0\u5f55\u6709\u622a\u53d6\u5e8f\u5217\u4fe1\u606f\u7684\u5217\u8868\u6587\u4ef6', add_help=False, usage='\\npython3 seq_select.py -i &#91;input.fasta] -o &#91;output.fasta] -l &#91;list]\\npython3 seq_select.py --input &#91;input.fasta] --output &#91;output.fasta] --list &#91;list]')\r\nrequired = parser.add_argument_group('\u5fc5\u9009\u9879')\r\noptional = parser.add_argument_group('\u53ef\u9009\u9879')\r\nrequired.add_argument('-i', '--input', metavar='&#91;input.fasta]', help='\u8f93\u5165\u6587\u4ef6\uff0cfasta \u683c\u5f0f', required=True)\r\nrequired.add_argument('-o', '--output', metavar='&#91;output.fasta]', help='\u8f93\u51fa\u6587\u4ef6\uff0cfasta \u683c\u5f0f', required=True)\r\nrequired.add_argument('-l', '--list', metavar='&#91;list]', help='\u8bb0\u5f55\u201c\u65b0\u5e8f\u5217\u540d\u79f0\/\u5e8f\u5217\u6240\u5728\u539f\u5e8f\u5217ID\/\u5e8f\u5217\u8d77\u59cb\u4f4d\u7f6e\/\u5e8f\u5217\u7ec8\u6b62\u4f4d\u7f6e\/\u6b63\u94fe\uff08+\uff09\u6216\u8d1f\u94fe\uff08-\uff09\u201d\u7684\u6587\u4ef6\uff0c\u4ee5 tab \u4f5c\u4e3a\u5206\u9694', required=True)\r\noptional.add_argument('--detail', action='store_true', help='\u82e5\u8be5\u53c2\u6570\u5b58\u5728\uff0c\u5219\u5728\u8f93\u51fa fasta \u7684\u6bcf\u6761\u5e8f\u5217 id \u4e2d\u5c55\u793a\u5e8f\u5217\u5728\u539f fasta \u4e2d\u7684\u4f4d\u7f6e\u4fe1\u606f', required=False)\r\noptional.add_argument('-h', '--help', action='help', help='\u5e2e\u52a9\u4fe1\u606f')\r\nargs = parser.parse_args()\r\n\r\nseqs = {}\r\nwith open(args.input, 'r') as f:\r\n    for line in f:\r\n        line = line.strip()\r\n        if line.startswith('>'):\r\n            seq_id = line.split()&#91;0]\r\n            seqs&#91;seq_id] = ''\r\n        else:\r\n            seqs&#91;seq_id] += line\r\n\r\nlists = {}\r\nwith open(args.list, 'r') as f:\r\n    for line in f:\r\n        parts = line.strip().split('\\t')\r\n        if len(parts) >= 5:  # \u68c0\u67e5\u5217\u8868\u662f\u5426\u5305\u542b\u81f3\u5c115\u4e2a\u5143\u7d20\r\n            lists&#91;parts&#91;0]] = &#91;parts&#91;1], int(parts&#91;2]) - 1, int(parts&#91;3]), parts&#91;4]]\r\n        else:\r\n            print(f\"\u8b66\u544a\uff1a\u5217\u8868\u6587\u4ef6\u4e2d\u6709\u4e0d\u5b8c\u6574\u7684\u884c\uff0c\u884c\u5185\u5bb9\u4e3a\uff1a{line.strip()}\")\r\n\r\ndef rev(seq):\r\n    base_trans = {'A':'T', 'C':'G', 'T':'A', 'G':'C', 'a':'t', 'c':'g', 't':'a', 'g':'c'}\r\n    return ''.join(&#91;base_trans.get(base, base) for base in reversed(seq)])\r\n\r\nwith open(args.output, 'w') as f:\r\n    for key, value in lists.items():\r\n        if args.detail:\r\n            print(f'>{key} &#91;{value&#91;0]} {value&#91;1] + 1} {value&#91;2]} {value&#91;3]}]', file=f)\r\n        else:\r\n            print(f'>{key}', file=f)\r\n        \r\n        seq = seqs&#91;f'>{value&#91;0]}']&#91;value&#91;1]:value&#91;2]]\r\n        if value&#91;3] == '+':\r\n            print(seq, file=f)\r\n        elif value&#91;3] == '-':\r\n            seq = rev(seq)\r\n            print(seq, file=f)\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># python3 seq_select.py -h\r\n# python3 seq_select.py -i Bacillus_subtilis.str168.fasta -l list.txt -o gene.fasta --detail\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/2.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"872\" height=\"227\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-600\"\/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/3.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"103\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-601\"\/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"679\" height=\"235\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-602\"\/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=598"}],"version-history":[{"count":2,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}