通过添加重复

时间:2018-05-09 18:38:46

标签: python pandas sequence

我实际上有一个数据框:

old_name       new_name      pident       length
gene1_0035_0042            geneA         100          560
gene2_0035_0042            geneA         100          545
gene3_0042_0035            geneB         99           356
gene4_0042_0035            geneB         97           256
gene6_0035_0042            geneB         96           567

这里是fasta文件(例如):

>gene1_0035_0042
ATTGAC
>gene2_0035_0042 
ATGAGCC
>gene3_0042_0035
AGCCAG
>gene4_0042_0035
AGCCAT
>gene6_0035_0042
AGCCATG

实际上我编写了一个脚本,通过执行以下操作在fasta文件中替换new_name序列:(qseqid = old_nameBusco_ID = new_names在ex)。

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

repl = blast[blast.pident > 95]

repl.to_csv("busco_blast_non-rename.txt",sep='\t')

qseqid=repl.ix[:,0]
Busco_ID=repl.ix[:,1]

newfile = []
count = 0
running_inds = {}

for rec in SeqIO.parse("concatenate_0035_0042_dna2.fa", "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["qseqid"] == rec.id, "Busco_ID"]
    #change record, if not empty
    if x.any():
        #append old identifier number to the new id name
        running = running_inds.get(x.iloc[0], 1) # Get the running index for this sequence
        running_inds[x.iloc[0]] = running + 1
        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, "concatenate_with_busco_names_0035_0042_dna.fa", "fasta")
#tell us, how hard you had to work for us
print("I changed {} entries!".format(count))

正如你所看到的那样,我只是通过将它们与pident>保持一致来过滤我的序列。 95但是正如你所看到的,我将为所有这些序列获取与new_name相同的名称,但是我想在新名称的末尾添加一个数字。对于上面的例子,它将在fasta文件中给出:

>geneA_0035_0042_1
ATTGAC
>geneA_0035_0042_2
ATGAGCC
>geneB_0042_0035_1
AGCCAG
>geneB_0042_0035_2
AGCCAT
>geneB_0035_0042_1
AGCCATG

等等 而不是:

>geneA_0035_0042
ATTGAC
>geneA_0035_0042
ATGAGCC
>geneB_0042_0035
AGCCAG
>geneB_0042_0035
AGCCAT
>geneB_0035_0042
AGCCATG

正如我的脚本所做的那样

感谢您的帮助:)

问题:

我得到了:

>EOG090X0FA0_0042_0042_1
>EOG090X0FA0_0042_0035_2
>EOG090X0FA0_0035_0035_3
>EOG090X0FA0_0035_0042_4

但是因为它们都不同我应该得到:

>EOG090X0FA0_0042_0042_1
>EOG090X0FA0_0042_0035_1
>EOG090X0FA0_0035_0035_1
>EOG090X0FA0_0035_0042_1

1 个答案:

答案 0 :(得分:1)

在循环开始之前添加字典:

rec.name = rec.description = rec.id = x.iloc[0] + rec.id[rec.id.index("_"):]

现在执行

running = running_inds.get(x.iloc[0] + rec.id[rec.id.index("_"):], 1) # Get the running index for this sequence
running_inds[x.iloc[0] + rec.id[rec.id.index("_"):]] = running + 1

首先执行以下操作:

rec.name = rec.description = rec.id = x.iloc[0] + rec.id[rec.id.index("_"):] + '_' + str(running)

现在只需将其附加到名称:

HomeController