linux统计fasta序列个数,test_xia3:统计fasta文件中特定碱基序列的数目

本文介绍了四种使用Python统计fasta文件中特定序列'AACA'出现次数的方法。包括不支持序列换行的方法,转换fasta格式后再统计,通过建立字典处理多行序列,以及更精简的实现方式。每种方法都提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的:统计fasta中序列AACA的数量

1、方法1,不支持序列换行

'''

satistics Num "AACA" by fasta file

not support each_basic by multi-line

demoDATA:

>chr1

AAAA

>chr2

AAAA

'''

import re

input = "test.fa"

seq_sum={}

with open(input, 'r')as seq_file:

for line in seq_file:

if line.startswith('>'):

seq_id = line[1:-1]

seq_sum[seq_id] = 0

else:

it = len(re.findall("AACA", line))

# print(it)

seq_sum[seq_id] = it

print(seq_sum)

2、先将fa转换格式,再统计

import re

input = "xtt_3.fa"

output = "out.txt"

dicCount = {}

file = open(input, "r")

out = open(output, "w")

for line in file:

line = line.strip()

if line.startswith(">"):

out.write("\n{0}\n".format(line))

else:

out.write("{0}".format(line))

out.close()

file.close()

out1 = open(output, "r")

for line in out1:

line = line.strip()

if len(line) != 0:

if line.startswith(">"):

a = line[1:]

dicCount[a] = 0

else:

count = re.findall("AACA", line)

dicCount[a] = len(count)

print(dicCount)'

3、方法三,建立字典

'''

satistics Num "AACA" by fasta file

support each_basic by multi-line

'''

import re

input = 'xtt_3.fa'

output = '3/xtt_3_out.txt'

seq = {}

seq_sum = {}

with open(input, 'r')as seq_file:

for line in seq_file:

if line.startswith('>'):

name = line.split()[0][1:]

seq[name] = ""

else:

seq[name] = seq[name] + line.replace("\n", "")

for i in seq:

count = len(re.findall("AACA", seq[i]))

seq_sum[i] = count

print(seq_sum)

with open(output, 'w') as newfile:

newfile.write("ID\tcounts\n")

for key, value in seq_sum.items():

newfile.write(key + '\t' + str(value) + '\n')

4、方法4 精简版

'''

write by xialei

satistics Num "AACA" by fasta file

support each_basic by multi-line

'''

import re

seq_sum = {}

input = '3/xtt_3.fa'

with open(input, 'r') as f:

l = f.read()

# chr_ = re.split('>(.*)\n',l)

ls = l.split('>')

ls.remove("")

for each in ls:

print(each)

k = re.findall('(chr.*)\n', each)[0]

it = len(re.findall("AACA", each.replace('\n', '')))

seq_sum[k] = it

print(seq_sum)

Linux中查看一个FASTA文件特定序列,你可以使用`less`命令或者直接从命令行通过文本编辑器如`grep`配合正则表达式来查找。这提供两种方法: 1. **使用`less`命令**: - 打开终端,定位到包含FASTA文件的目录,然后运行以下命令,输入你要查找的序列名称前缀(例如“ATGC”): ``` less your_file.fasta | grep -i "你的序列前缀" ``` - 按下 `/` 键开始搜索模式,输入你的序列,然后按回车键。`n` 键向下滚动找到匹配的行。 2. **使用`sed`命令**: 如果你知道确切的起始位置(从第几行开始),可以尝试`sed`配合`awk`: ``` sed -n '你的开始行数,$p' your_file.fasta | awk '/你的序列/{print}' ``` 把“你的开始行数”替换为实际的行数,把“你的序列”替换为你要找的序列3. **使用`bioawk`工具(生物信息学专用工具)**: 如果你的文件很大,并且经常需要处理这类任务,可以安装`bioawk`。它是一个基于AWK的工具,专门用于解析生物学数据。安装完成后,你可以这样做: ``` bioawk '/^>你的序列名称/ { print $0; }' your_file.fasta ``` 其中,“你的序列名称”应替换为你想要查找的实际序列名。 注意,在上述命令中,你需要将“你的文件名”、“你的序列前缀”和“你的序列名称”替换为你的实际文件名、序列前缀或名称。同时,确保你的环境已安装了必要的工具。如果你只是想快速浏览整个文件,`less`命令通常更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值