在r中的fasta文件中写一个包含属性的列表

时间:2014-01-03 20:40:12

标签: r attr fasta

我有一个ceratin列表,其头部看起来像下面的

> head(gg4)
[[1]]
  [1] "i" "a" "l" "t" "a" "l" "l" "g" "n" "m" "l" "v" "l" "w" "i" "v" "y" "k" "n" "h"
 [21] "t" "l" "r" "t" "i" "p" "n" "y" "f" "v" "i" "s" "l" "a" "c" "s" "d" "v" "v" "m"
 [41] "a" "a" "l" "a" "t" "p" "w" "s" "i" "a" "v" "l" "a" "v" "g" "k" "w" "p" "f" "d"


attr(,"name")
[1] "jgi|Nemve1|1499|gw.850.3.1"

我想用fasta格式文件写它..输出将是

> jgi|Nemve1|1499|gw.850.3.1

ialtallgnmlvlwivyknhtlrtipnyfvislacsdvvmaalatpwsiavlavgkwpfd
fvscqfqgfiviwaamaslqnlavmainrffrivkpahyrkffsasktrmyvllsyvyta

当我使用write.fasta(sequences = gg4, names = names(gg4), file.out = "mf3a.fasta")时的N.b 我得到了

>

ialtallgnmlvlwivyknhtlrtipnyfvislacsdvvmaalatpwsiavlavgkwpfd
fvscqfqgfiviwaamaslqnlavmainrffrivkpahyrkffsasktrmyvllsyvyta

2 个答案:

答案 0 :(得分:1)

我不熟悉write.fasta函数,但试试这个。

write.fasta(sequences = gg4, names = attr(gg4,"name"), file.out = "mf3a.fasta")

如果失败则采用冗长的方法:fasta格式是一个简单的文本文件。因此,任务归结为在创建文件后在文件开头添加一行。

保存文件,例如如上所述mf3a.fasta

write.fasta(sequences = gg4, names = names(gg4), file.out = "mf3a.fasta")

然后再次读入文件并在顶部添加您想要的行。 attr(gg4,"name")将为您提供>后面的第一行。

head <- paste0(">", attr(gg4,"name"))  # create header of ile
l <- readLines("mf3a.fasta")           # read fasta file without header
l <- c(head, l)                        # prepend header 
writeLines(l, "mf3a_2.fasta")          # write lines into new file

这会给你:

>jgi|Nemve1|1499|gw.850.3.1
ialtallgnmlvlwivyknhtlrtipnyfvislacsdvvmaalatpwsiavlavgkwpfd 
fvscqfqgfiviwaamaslqnlavmainrffrivkpahyrkffsasktrmyvllsyvyta

HTH

答案 1 :(得分:0)

以下函数将从gg4中提取属性(attr(,&#34; name&#34;))

attf<-function(x)
    {   l<-length(x)
        attv<-rep(NA,l)
            { for (i in 1:length(x))
                {   attv[i]<-attributes(x[[i]])$name}
            }
    return(attv)
    }

以下内容应该有效

write.fasta(sequences = gg4, names = attf(gg4), file.out = "mf3a.fasta")