[英]Is there any python/unix command specifically to delete a line after reading a file? (I'm dealing with 64.2 GB file)
我是一名生物信息學家,我正在處理一個非常大的文本文件。 文件大小為 64.2 GB。 我對我的文本文件進行了字數統計,並在相當長的一段時間后得到了這個結果。
1052454251 1052456168 64199706147 GRCh38.fa
我的問題是我只想從這個文件中刪除幾行。 我在谷歌搜索 python 命令以從文本文件中刪除任何特定行,但幾乎所有命令都建議編寫完整文件,同時只跳過我們要刪除的部分。
甚至在參考 google 之前,我也采用了相同的方法,但最初我遇到了內存問題(因為我使用了 readlines() 函數並試圖一次性讀取整個文件,所以我的系統被掛了)。 然后我一次讀取一行文件並過濾我需要的行。 但是,這種方法非常耗時,因為我的輸入文件是 64 GB。
任何人都可以建議是否有任何特定的 python/unix 命令可以從文件中只刪除特定的行?
有關我的問題的更多信息,我的輸入文件包含 fasta 格式的完整人類基因組序列。 看起來像這樣
>chr1 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chr2 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chr3 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
.
.
.
>chr22 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chrM 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chrX 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chrY 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
這里以“>”開頭的行稱為標題。 其余的行包含實際的 DNA 序列。
緊接在“>”之后,我有染色體名稱(chr1 或 chr2 或 chr3 或 .... chrM 或 chrX 或 chrY)。 我只想刪除 chrM 行和它們下面的 DNA 序列行。 所以我的輸出文件應該看起來像
>chr1 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chr2 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chr3 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
.
.
.
.
>chr22 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chrX 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
>chrY 和一些描述
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
ATTAGATCGGCTGATG....
這是我寫的代碼。
from memory_profiler import profile
@profile #This is just to check how much memory is used.
def memcheck():
g=open("chrMremoved.fa",'w')
to_write=1
with open("GRCh38.fa") as f:
for i in f:
if(i[0]==">"):
if(i[0:5]==">chrM"):
to_write=0
else:
to_write=1
if(to_write==1):
g.write(i)
g.close()
if __name__ == "__main__":
memcheck()
此外,由於我的大部分工作都圍繞着分析龐大的數據集。 如果有人建議我一些關於如何在 python 中處理大型數據集的技巧(比如編寫內存高效和時間高效的代碼),這對我會有幫助。
有時我遇到了我的 python 代碼被殺死的問題,我搜索了谷歌,發現這是因為 RAM 中的內存較少。 請指導我在這種情況下我能做些什么。
使用像seqkit這樣的專用工具按標題模式過濾您的序列。
seqkit grep -v -p chrM GRCh38.fa -o chrMremoved.fa
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.