Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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或R中连接多个DNA序列文本文件?_Python_R_Concatenation_Dna Sequence - Fatal编程技术网

在Python或R中连接多个DNA序列文本文件?

在Python或R中连接多个DNA序列文本文件?,python,r,concatenation,dna-sequence,Python,R,Concatenation,Dna Sequence,我想知道如何使用Python或R连接exon/DNA fasta文件 示例文件: 到目前为止,我非常喜欢在cbind方法中使用R-ape包,这完全是因为fill.with.gaps=TRUE属性。当一个物种缺少一个外显子时,我真的需要插入缺口 我的代码: ex1 <- read.dna("exon1.txt", format="fasta") ex2 <- read.dna("exon2.txt", format="fasta") output <- cbind(ex1, ex

我想知道如何使用Python或R连接exon/DNA fasta文件

示例文件: 到目前为止,我非常喜欢在cbind方法中使用R-ape包,这完全是因为
fill.with.gaps=TRUE
属性。当一个物种缺少一个外显子时,我真的需要插入缺口

我的代码:

ex1 <- read.dna("exon1.txt", format="fasta")
ex2 <- read.dna("exon2.txt", format="fasta")
output <- cbind(ex1, ex2, fill.with.gaps=TRUE)
write.dna(output, "Output.txt", format="fasta")
exon2.txt

>sp1
AGG-G
>sp2
CTGAT
>sp3
CTTTT
输出文件:

>sp1
AAAAAGG-G
>sp2
CCCCCTGAT
>sp3
----CTTTT
到目前为止,当我有多个外显子文件(试图找出一个循环来打开并执行目录中以.fa结尾的所有文件的cbind方法)时,我在尝试应用此技术时遇到了困难,有时并非所有文件的外显子长度都相同-因此DNAbin停止工作

到目前为止,我已经:

file_list <- list.files(pattern=".fa") 

myFunc <- function(x) {
   for (file in file_list) {
     x <- read.dna(file, format="fasta")
     out <- cbind(x, fill.with.gaps=TRUE)
     write.dna(out, "Output.txt", format="fasta")
   }
}

file\u list我刚刚用Python 3给出了这个答案:

def read_fasta(fasta): #Function that reads the files
  output = {}
  for line in fasta.split("\n"):
    line = line.strip()
    if not line:
      continue
    if line.startswith(">"):
      active_sequence_name = line[1:]
      if active_sequence_name not in output:
        output[active_sequence_name] = []
      continue
    sequence = line
    output[active_sequence_name].append(sequence)
  return output

with open("exon1.txt", 'r') as file: # read exon1.txt
  file1 = read_fasta(file.read())
with open("exon2.txt", 'r') as file: # read exon2.txt
  file2 = read_fasta(file.read())

finaldict = {}                                     #Concatenate the
for i in list(file1.keys()) + list(file2.keys()):  #both files content
  if i not in file1.keys():
    file1[i] = ["-" * len(file2[i][0])]
  if i not in file2.keys():
    file2[i] = ["-" * len(file1[i][0])]
  finaldict[i] = file1[i] + file2[i]

with open("output.txt", 'w') as file:  # output that in file 
  for k, i in finaldict.items():       # named output.txt
    file.write(">{}\n{}\n".format(k, "".join(i))) #proper formatting
很难对它进行完整的评论和解释,这可能对你没有帮助,但这总比什么都没有好:p


我使用了从answer到的Łukasz Rogalski的代码。

如果您更喜欢使用Linux一行程序,请使用您现有的

      cat exon1.txt exon2.txt > outfile
如果您只需要outfile中的唯一记录,请使用

      awk '/^>/{f=!d[$1];d[$1]=1}f' outfile > sorted_outfile

你能解释一下它应该做什么吗?简言之,它连接了两个文件的DNA字符串。它连接了两个fasta文件,因为现在我已经把基因分解成外显子文件,但我需要将它们组合起来运行某个程序。谢谢!我将尝试这段代码,并对其进行一些调整,以将某个目录中的所有文件连接起来,因为对20多个外显子文件使用“with open”会很乏味。我也只知道Python 2.7,但再次感谢您对我的帮助!我很感激!我认为它在Python2中可以工作,但我没有测试它:PIt在Python2.7中工作。再次感谢你。你和拉克希米的评论都帮助我想出了一些代码来简化我的工作。干杯我确实试过了,但由于某种原因,它弄乱了我的Fasta格式,我真的不知道为什么。我的文件最后的>名称也以随机行连接在一起……前面的注释对数据进行了排序,但没有保持fasta格式的完整性。很抱歉,提供了错误的命令。我已经编辑了第二条命令,这可以解决你的问题@DNAngel谢谢!这帮了大忙:)
      awk '/^>/{f=!d[$1];d[$1]=1}f' outfile > sorted_outfile