最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python腳本提取基因組指定位置序列

 更新時(shí)間:2022年07月01日 10:38:59   作者:陳光輝_花生所  
這篇文章主要為大家介紹了使用Python腳本提取基因組指定位置序列的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

在基因組分析中,我們經(jīng)常會(huì)有這么一個(gè)需求,就是在一個(gè)fasta文件中提取一些序列出來(lái)。有時(shí)這些序列是一段完整的序列,而有時(shí)僅僅為原fasta文件中某段序列的一部分。特別是當(dāng)數(shù)據(jù)量很多時(shí),使用肉眼去挑選序列會(huì)很吃力,那么這時(shí)我們就可以通過(guò)簡(jiǎn)單的編程去實(shí)現(xiàn)了。

例如此處在網(wǎng)盤附件中給定了某物種的全基因組序列(0-refer/ Bacillus_subtilis.str168.fasta),及其基因組gff注釋文件(0-refer/ Bacillus_subtilis.str168.gff)。

假設(shè)在這里我們對(duì)該物種進(jìn)行研究,通過(guò)gff注釋文件中的基因功能描述字段,加上對(duì)相關(guān)資料的查閱等,定位到了一些特定的基因。

接下來(lái)我們期望基于gff文件中對(duì)這些基因位置的描述,在全基因組序列fasta文件中將這些基因找到并提取出來(lái),得到一個(gè)新的fasta文件,新文件中只包含目的基因序列。

請(qǐng)使用python3編寫一個(gè)可以實(shí)現(xiàn)該功能的腳本。

示例

一個(gè)示例腳本如下(可參見(jiàn)網(wǎng)盤附件“seq_select1.py”)。

為了實(shí)現(xiàn)以上目的,我們首先需要準(zhǔn)備一個(gè)txt文件(以下稱其為list文件,示例list.txt可參見(jiàn)網(wǎng)盤附件),基于gff文件中所記錄的基因位置信息,填入類似以下的內(nèi)容(列與列之間以tab分隔)。

#下列內(nèi)容保存到list.txt
gene46   NC_000964.3  42917  43660  +
NP_387934.1  NC_000964.3    59504  60070    +
yfmC  NC_000964.3  825787   826734  -
cds821  NC_000964.3  885844  886173 -

第1列,給所要獲取的新序列命個(gè)名稱;

第2列,所要獲取的序列所在原序列ID;

第3列,所要獲取的序列在原序列中的起始位置;

第4列,所要獲取的序列在原序列中的終止位置;

第5列,所要獲取的序列位于原序列的正鏈(+)或負(fù)鏈(-)。

之后根據(jù)輸入文件,即輸入fasta文件及記錄所要獲取序列位置的list文件中的內(nèi)容,編輯py腳本。

打開fasta文件“Bacillus_subtilis.scaffolds.fasta”,使用循環(huán)逐行讀取其中的序列id及堿基序列,并將每條序列的所有堿基合并為一個(gè)字符串;將序列id及該序列合并后的堿基序列以字典的形式存儲(chǔ)(字典樣式{'id':'base'})。

打開list文件“list.txt”,讀取其中的內(nèi)容,存儲(chǔ)到字典中。字典的鍵為list文件中的第1列內(nèi)容;字典的值為list文件中第2-5列的內(nèi)容,并按tab分割得到一個(gè)列表,包含4個(gè)字符分別代表list文件中第2-5列的信息)。

最后根據(jù)讀取的list文件中序列位置信息,在讀取的基因組中截取目的基因序列。由于某些基因序列可能位于基因組負(fù)鏈中,需取其反向互補(bǔ)序列,故首先定義一個(gè)函數(shù)rev(),用于在后續(xù)調(diào)用得到反向互補(bǔ)序列。在輸出序列名稱時(shí),還可選是否將該序列的位置信息一并輸出(name_detail = True/False)。

<pre class="r" style="overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; margin-top: 0px; margin-bottom: 10px; padding: 9.5px; border-radius: 4px; background-color: rgb(245, 245, 245); box-sizing: border-box; overflow: auto; font-size: 13px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; border: 1px solid rgb(204, 204, 204); font-family: "Times New Roman";">#!/usr/bin/env python3

# -*- coding: utf-8 -*-
#初始傳遞命令
input_file = 'Bacillus_subtilis.str168.fasta'
list_file = 'list.txt'
output_file = 'gene.fasta'
name_detail = True
##讀取文件
#讀取基因組序列
seq_file = {}
with open(input_file, 'r') as input_fasta:
    for line in input_fasta:
        line = line.strip()
        if line[0] == '>':
            seq_id = line.split()[0]
            seq_file[seq_id] = ''
        else:
            seq_file[seq_id] += line
input_fasta.close()
#讀取列表文件
list_dict = {}
with open(list_file, 'r') as list_table:
    for line in list_table:
        if line.strip():
            line = line.strip().split('\t')
            list_dict[line[0]] = [line[1], int(line[2]) - 1, int(line[3]), line[4]]
list_table.close()
##截取序列并輸出
#定義函數(shù),用于截取反向互補(bǔ)
def rev(seq):
    base_trans = {'A':'T', 'C':'G', 'T':'A', 'G':'C', 'N':'N', 'a':'t', 'c':'g', 't':'a', 'g':'c', 'n':'n'}
    rev_seq = list(reversed(seq))
    rev_seq_list = [base_trans[k] for k in rev_seq]
    rev_seq = ''.join(rev_seq_list)
    return(rev_seq)
#截取序列并輸出
output_fasta = open(output_file, 'w')
for key,value in list_dict.items():
    if name_detail:
        print('>' + key, '[' + value[0], value[1] + 1, value[2], value[3] + ']', file = output_fasta)
    else:
        print('>' + key, file = output_fasta)
    seq = seq_file['>' + value[0]][value[1]:value[2]]
    if value[3] == '+':
        print(seq, file = output_fasta)
    elif value[3] == '-':
        seq = rev(seq)
        print(seq, file = output_fasta)
output_fasta.close()</pre>

編輯該腳本后運(yùn)行,輸出新的fasta文件“gene.fasta”,其中的序列即為我們所想要得到的目的基因序列。

擴(kuò)展:

網(wǎng)盤附件“seq_select.py”為添加了命令傳遞行的python3腳本,可在shell中直接進(jìn)行目標(biāo)文件的I/O處理。該腳本可指定輸入fasta序列文件以及記錄有所需提取序列位置的列表文件,輸出的新fasta文件即為提取出的序列。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#導(dǎo)入模塊,初始傳遞命令、變量等
import argparse
parser = argparse.ArgumentParser(description = '\n該腳本用于在基因組特定位置截取序列,需額外輸入記錄有截取序列信息的列表文件', add_help = False, usage = '\npython3 seq_select.py -i [input.fasta] -o [output.fasta] -l [list]\npython3 seq_select.py --input [input.fasta] --output [output.fasta] --list [list]')
required = parser.add_argument_group('必選項(xiàng)')
optional = parser.add_argument_group('可選項(xiàng)')
required.add_argument('-i', '--input', metavar = '[input.fasta]', help = '輸入文件,fasta 格式', required = True)
required.add_argument('-o', '--output', metavar = '[output.fasta]', help = '輸出文件,fasta 格式', required = True)
required.add_argument('-l', '--list', metavar = '[list]', help = '記錄“新序列名稱/序列所在原序列ID/序列起始位置/序列終止位置/正鏈(+)或負(fù)鏈(-)”的文件,以 tab 作為分隔', required = True)
optional.add_argument('--detail', action = 'store_true', help = '若該參數(shù)存在,則在輸出 fasta 的每條序列 id 中展示序列在原 fasta 中的位置信息', required = False)
optional.add_argument('-h', '--help', action = 'help', help = '幫助信息')
args = parser.parse_args()
##讀取文件
#讀取基因組序列
seq_file = {}
with open(args.input, 'r') as input_fasta:
    for line in input_fasta:
        line = line.strip()
        if line[0] == '>':
            seq_id = line.split()[0]
            seq_file[seq_id] = ''
        else:
            seq_file[seq_id] += line
input_fasta.close()
#讀取列表文件
list_dict = {}
with open(args.list, 'r') as list_file:
    for line in list_file:
        if line.strip():
            line = line.strip().split('\t')
            list_dict[line[0]] = [line[1], int(line[2]) - 1, int(line[3]), line[4]]
list_file.close()
##截取序列并輸出
#定義函數(shù),用于截取反向互補(bǔ)
def rev(seq):
    base_trans = {'A':'T', 'C':'G', 'T':'A', 'G':'C', 'a':'t', 'c':'g', 't':'a', 'g':'c'}
    rev_seq = list(reversed(seq))
    rev_seq_list = [base_trans[k] for k in rev_seq]
    rev_seq = ''.join(rev_seq_list)
    return(rev_seq)
#截取序列并輸出
output_fasta = open(args.output, 'w')
for key,value in list_dict.items():
    if args.detail:
        print('>' + key, '[' + value[0], value[1] + 1, value[2], value[3] + ']', file = output_fasta)
    else:
        print('>' + key, file = output_fasta)
    seq = seq_file['>' + value[0]][value[1]:value[2]]
    if value[3] == '+':
        print(seq, file = output_fasta)
    elif value[3] == '-':
        seq = rev(seq)
        print(seq, file = output_fasta)
output_fasta.close()

適用上述示例中的測(cè)試文件,運(yùn)行該腳本的方式如下。

#python3 seq_select.py -h
python3 seq_select.py -i Bacillus_subtilis.str168.fasta -l list.txt -o gene.fasta --detail

源碼提取鏈接: https://pan.baidu.com/s/1kUhBTmpDonCskwmpNIJPkA?pwd=ih9n

提取碼: ih9n

以上就是使用Python腳本提取基因組指定位置序列的詳細(xì)內(nèi)容,更多關(guān)于python提取基因組位置序列的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 查找list中的某個(gè)元素的所有的下標(biāo)方法

    Python 查找list中的某個(gè)元素的所有的下標(biāo)方法

    今天小編就為大家分享一篇Python 查找list中的某個(gè)元素的所有的下標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Pandas高級(jí)教程之Pandas中的GroupBy操作

    Pandas高級(jí)教程之Pandas中的GroupBy操作

    通常來(lái)說(shuō)groupby操作可以分為三部分:分割數(shù)據(jù),應(yīng)用變換和和合并數(shù)據(jù),本文將會(huì)詳細(xì)講解Pandas中的groupby操作,感興趣的朋友一起看看吧
    2021-07-07
  • python?ast模塊詳析與用法

    python?ast模塊詳析與用法

    這篇文章主要給大家介紹了關(guān)于python?ast模塊詳析與用法的相關(guān)資料, Python的ast(Abstract Syntax Trees,抽象語(yǔ)法樹)模塊是一個(gè)內(nèi)置模塊,用于解析Python代碼并生成語(yǔ)法樹,需要的朋友可以參考下
    2023-07-07
  • Python數(shù)據(jù)結(jié)構(gòu)列表

    Python數(shù)據(jù)結(jié)構(gòu)列表

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)列表,本文重點(diǎn)內(nèi)容主要是對(duì)列表數(shù)據(jù)結(jié)構(gòu)的使用,在Python中,序列是一組按順序排列的值。Python?有?3?種內(nèi)置的序列類型:字符串、?元組和列表,下面一起進(jìn)入文章了解更詳細(xì)內(nèi)容吧,需要的小伙伴可以參考一下</P><P>
    2021-12-12
  • pycharm中安裝git遇到的問(wèn)題及解決

    pycharm中安裝git遇到的問(wèn)題及解決

    在PyCharm中安裝Git時(shí)遇到問(wèn)題,按照視頻步驟操作后發(fā)現(xiàn)沒(méi)有g(shù)it選項(xiàng),重新檢查設(shè)置,發(fā)現(xiàn)git目錄配置錯(cuò)誤,重新選擇正確的目錄后,通過(guò)Test確認(rèn)無(wú)誤,在使用commit提交時(shí)遇到錯(cuò)誤,按刷新按鈕即可解決
    2024-11-11
  • Python設(shè)計(jì)模式之代理模式簡(jiǎn)單示例

    Python設(shè)計(jì)模式之代理模式簡(jiǎn)單示例

    這篇文章主要介紹了Python設(shè)計(jì)模式之代理模式,簡(jiǎn)單說(shuō)明了代理模式的概念、原理,并結(jié)合Python實(shí)例形式分析了代理模式的相關(guān)定義與使用技巧,需要的朋友可以參考下
    2018-01-01
  • Python?3.12安裝庫(kù)報(bào)錯(cuò)解決方案

    Python?3.12安裝庫(kù)報(bào)錯(cuò)解決方案

    這篇文章主要介紹了Python?3.12安裝庫(kù)報(bào)錯(cuò)的解決方案,講解了Python?3.12移除pkgutil.ImpImporter支持導(dǎo)致的AttributeError錯(cuò)誤,并提供了兩種解決方案,需要的朋友可以參考下
    2025-03-03
  • 用Python遍歷C盤dll文件的方法

    用Python遍歷C盤dll文件的方法

    這篇文章主要介紹了用Python遍歷C盤dll文件的方法,用fnmatch模塊實(shí)現(xiàn)起來(lái)非常簡(jiǎn)單,需要的朋友可以參考下
    2015-05-05
  • macOS M1(Apple Silicon)安裝配置Conda環(huán)境的具體實(shí)現(xiàn)

    macOS M1(Apple Silicon)安裝配置Conda環(huán)境的具體實(shí)現(xiàn)

    由于常用的Anaconda和Miniconda現(xiàn)在都沒(méi)有提供M1處理器支持的conda環(huán)境,以下是conda-forge提供的miniforge,感興趣的可以了解一下
    2021-08-08
  • Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    這篇文章主要介紹了Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評(píng)論

延川县| 晴隆县| 正宁县| 沂南县| 扎兰屯市| 清流县| 玉屏| 新泰市| 武胜县| 龙江县| 长武县| 淳安县| 岳池县| 三原县| 玛沁县| 综艺| 霍邱县| 丘北县| 白河县| 高淳县| 正定县| 海兴县| 澳门| 突泉县| 岳西县| 徐州市| 东丽区| 民乐县| 响水县| 崇信县| 东阿县| 通化县| 玉田县| 福安市| 瓦房店市| 彩票| 揭阳市| 廊坊市| 大宁县| 丽江市| 辽中县|