欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

引言

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

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

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

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

請使用python3編寫一個可以實現(xiàn)該功能的腳本。

示例

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

為了實現(xiàn)以上目的,我們首先需要準(zhǔn)備一個txt文件(以下稱其為list文件,示例list.txt可參見網(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列,給所要獲取的新序列命個名稱;

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

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

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

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

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

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

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

最后根據(jù)讀取的list文件中序列位置信息,在讀取的基因組中截取目的基因序列。由于某些基因序列可能位于基因組負鏈中,需取其反向互補序列,故首先定義一個函數(shù)rev(),用于在后續(xù)調(diào)用得到反向互補序列。在輸出序列名稱時,還可選是否將該序列的位置信息一并輸出(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ù),用于截取反向互補
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>

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

擴展:

網(wǎng)盤附件“seq_select.py”為添加了命令傳遞行的python3腳本,可在shell中直接進行目標(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('必選項')
optional = parser.add_argument_group('可選項')
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/序列起始位置/序列終止位置/正鏈(+)或負鏈(-)”的文件,以 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ù),用于截取反向互補
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()

適用上述示例中的測試文件,運行該腳本的方式如下。

#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腳本提取基因組指定位置序列的詳細內(nèi)容,更多關(guān)于python提取基因組位置序列的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 20招讓你的Python飛起來!

    20招讓你的Python飛起來!

    20招讓你的 Python飛起來!這篇文章主要為大家詳細介紹了Python性能優(yōu)化的20條建議,感興趣的小伙伴們可以參考一下
    2016-09-09
  • python爬蟲用request庫處理cookie的實例講解

    python爬蟲用request庫處理cookie的實例講解

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python爬蟲用request庫處理cookie的實例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • Python中的面向?qū)ο缶幊淘斀?下)

    Python中的面向?qū)ο缶幊淘斀?下)

    這篇文章主要介紹了Python中的面向?qū)ο缶幊淘斀?下),本文講解了繼承、super關(guān)鍵字、重寫、多重繼承、類、實例和其他對象的內(nèi)建函數(shù)、私有化等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • python提取頁面內(nèi)url列表的方法

    python提取頁面內(nèi)url列表的方法

    這篇文章主要介紹了python提取頁面內(nèi)url列表的方法,涉及Python操作頁面元素的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python configparser模塊配置文件過程解析

    Python configparser模塊配置文件過程解析

    這篇文章主要介紹了Python configparser模塊配置文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • PyQt6中自定義浮點型滑塊類的實現(xiàn)

    PyQt6中自定義浮點型滑塊類的實現(xiàn)

    在PyQt6中,滑塊是常用的用戶界面元素之一,用于選擇數(shù)值范圍,本文主要介紹了PyQt6中自定義浮點型滑塊類的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • python3.7 openpyxl 刪除指定一列或者一行的代碼

    python3.7 openpyxl 刪除指定一列或者一行的代碼

    這篇文章主要介紹了python3.7 openpyxl 刪除指定一列或者一行,文中通過代碼給大家介紹了python3 openpyxl基本操作,代碼簡單易懂,需要的朋友可以參考下
    2019-10-10
  • python版單鏈表反轉(zhuǎn)

    python版單鏈表反轉(zhuǎn)

    這篇文章主要為大家詳細介紹了python版單鏈表反轉(zhuǎn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python爬蟲系列Selenium定向爬取虎撲籃球圖片詳解

    python爬蟲系列Selenium定向爬取虎撲籃球圖片詳解

    這篇文章主要介紹了python爬蟲系列Selenium定向爬取虎撲籃球圖片詳解,具有一定參考價值,喜歡的朋友可以了解下。
    2017-11-11
  • 使用Python將Mysql的查詢數(shù)據(jù)導(dǎo)出到文件的方法

    使用Python將Mysql的查詢數(shù)據(jù)導(dǎo)出到文件的方法

    今天小編就為大家分享一篇關(guān)于使用Python將Mysql的查詢數(shù)據(jù)導(dǎo)出到文件的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02

最新評論