Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用数据帧更改fasta文件中的seq name_Python_Pandas_Fasta - Fatal编程技术网

Python 使用数据帧更改fasta文件中的seq name

Python 使用数据帧更改fasta文件中的seq name,python,pandas,fasta,Python,Pandas,Fasta,我有问题,我解释一下 我有一个fasta文件,例如: >seqA AAAAATTTGG >seqB ATTGGGCCG >seqC ATTGGCC >seqD ATTGGACAG 和数据帧: seq name New name seq seqB BOBO seqC JOHN 我simpy想在fasta文件中更改我的ID seq name如果在我的数据帧中有相同的seq name并将其更改为新的名称seq,它将给出:

我有问题,我解释一下

我有一个fasta文件,例如:

>seqA
AAAAATTTGG
>seqB
ATTGGGCCG
>seqC
ATTGGCC
>seqD
ATTGGACAG
和数据帧:

seq name      New name seq
seqB            BOBO
seqC            JOHN
我simpy想在fasta文件中更改我的ID seq name如果在我的数据帧中有相同的seq name并将其更改为新的名称seq,它将给出:

新fasta fil:

>seqA
AAAAATTTGG
>BOBO
ATTGGGCCG
>JOHN
ATTGGCC
>seqD
ATTGGACAG
多谢各位

编辑: 我使用了以下脚本:

blast=pd.read_table("matches_Busco_0035_0042.m8",header=None)
blast.columns = ["qseqid", "Busco_ID", "pident", "length", "mismatch", "gapopen","qstart", "qend", "sstart", "send", "evalue", "bitscore"]

repl = blast[blast.pident > 95]

print(repl)

#substituion dataframe

newfile = []
count = 0

for rec in SeqIO.parse("concatenate_0035_0042_aa2.fa", "fasta"):
    #get corresponding value for record ID from dataframe
    x = repl.loc[repl.seq == rec.id, "Busco_ID"]
    #change record, if not empty
    if x.any():
        rec.name = rec.description = rec.id = x.iloc[0]
        count += 1
    #append record to list
    newfile.append(rec)

#write list into new fasta file
SeqIO.write(newfile, "changedtest.faa", "fasta")
#tell us, how hard you had to work for us
print("I changed {} entries!".format(count))
我得到了以下错误:

Traceback (most recent call last):
  File "Get_busco_blast.py", line 74, in <module>
    x = repl.loc[repl.seq == rec.id, "Busco_ID"]
  File "/usr/local/lib/python3.6/site-packages/pandas/core/generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'seq'
回溯(最近一次呼叫最后一次):
文件“Get_busco_blast.py”,第74行,在
x=repl.loc[repl.seq==rec.id,“Busco_id”]
文件“/usr/local/lib/python3.6/site packages/pandas/core/generic.py”,第3614行,在__
返回对象。\uuuGetAttribute(self,name)
AttributeError:“DataFrame”对象没有属性“seq”
如果已安装,则可以使用
SeqIO
读取/写入fasta文件:

from Bio import SeqIO

#substituion dataframe
repl = pd.DataFrame(np.asarray([["seqB_3652_i36", "Bob"], ["seqC_123_6XXX1", "Patrick"]]), columns = ["seq", "newseq"])

newfile = []
count = 0

for rec in SeqIO.parse("test.faa", "fasta"):
    #get corresponding value for record ID from dataframe
    #repl["seq"] and "newseq" are the pandas column with the old and new sequence names, respectively
    x = repl.loc[repl["seq"] == rec.id, "newseq"]
    #change record, if not empty
    if x.any():
        #append old identifier number to the new id name
        rec.name = rec.description = rec.id = x.iloc[0] + rec.id[rec.id.index("_"):]
        count += 1
    #append record to list
    newfile.append(rec)

#write list into new fasta file
SeqIO.write(newfile, "changedtest.faa", "fasta")
#tell us, how hard you had to work for us
print("I changed {} entries!".format(count))

请注意,此脚本不会检查替换表中的多个条目。如果记录id不在数据帧中,它只接受第一个元素,或者不改变任何内容。

使用类似的方法更容易做到这一点

首先创建一个字典

names = Series(df['seq name'].values,index=df['New seq name']).to_dict()
现在迭代

from Bio import SeqIO
outs = []
for record in SeqIO.parse("orig.fasta", "fasta"):
    record.id = names.get(record.id, default=record.id)
    outs.append(record)
SeqIO.write(open("new.fasta", "w"), outs, "fasta")

我在上面写了一条评论,我有一个问题,谢谢你的帮助。我无法复制你的问题,因为我没有你的文件。但是
repl.seq
,也可以写成
repl[“seq”]
,指的是名为“seq”的pandas列。对于要替换的ID,您生成的数据帧可能具有不同的列名。也许是“qseqid”。然后,脚本必须更改为
repl.qseqid
repl[“qseqid”]
。请随时提供。请删除上面的答案,您应该编辑您的问题。哦,是的,非常感谢:),您知道现在是否可以添加此新的seq ID,但在结尾保留
\u number\u number
部分?例如,如果旧的seq名称是
g134554t1_0035_0042
,并在
newseqname_0035_0042
中对其进行转换?例如,
g45566.t1_0035_0035
into
newseqname_0035_0035
等?我不熟悉这个术语。但是,如果它总是以下划线开头,则可以使用rec.id检索旧id,使用
.index(“”)
)查找第一个外观,使用
id[index:
在该位置切片旧id,并在将其分配给序列字段之前将此部分连接到新名称。很抱歉,我没有做到这一点,我不知道剧本里的那些部分。你能把它加到你的第一个剧本里吗?是的,它总是带有下划线。嗨,谢谢你的帮助,脚本的第一个对齐似乎不起作用,也许a)或类似的东西有问题吗?@Benjamin纠正了一个拼写错误。谢谢你的帮助:)@Benjamin你很受欢迎。祝你的FASTA好运。