如何将不同目录中类似的命名序列文件放入单个大型fasta文件中



我正在努力完成以下工作。我有大约40个不同物种的目录,每个目录都有100个包含同源序列的序列文件。序列文件的名称与每个物种目录的名称相似。我想把40个物种目录中同名的文件连接成一个同名的序列文件。

我的数据如下,例如:

directories: Species1 Species2 Species3 
Within directory (similar for all): sequenceA.fasta sequenceB.fasta sequenceC.fasta
I want to get single files named: sequenceA.fasta sequenceB.fasta sequenceC.fasta 
where the content of the different files from the different species is concatenated.

我试图用一个循环来解决这个问题(但这对我来说从来都不是一个好结果!(:

ls . | while read FILE; do cat ./*/"$FILE" >> ./final/"$FILE"; done

这导致了空文件和错误。我确实试图在其他地方找到解决方案,例如:(https://www.unix.com/unix-for-dummies-questions-and-answers/249952-cat-multiple-files-according-file-name.html,https://unix.stackexchange.com/questions/424204/how-to-combine-multiple-files-with-similar-names-in-different-folders-by-using-u)但我一直无法将它们编辑到我的案例中。

有人能帮我一下吗?谢谢

在物种目录所在的根目录中,您应该运行以下程序:

$ mkdir output
$ find Species* -type f -name "*.fasta" -exec sh -c 'cat {} >> output/`basename {}`' ;

它递归遍历所有文件,并将具有相同基本名称的文件的内容合并到输出目录下的文件中。

编辑:尽管这是一个公认的答案,但OP在一条评论中提到,真实目录与原始问题中显示的常见模式Species*不匹配。在这种情况下,你可以使用这个:

$ find -type f -not -path "./output/*" -name "*.fasta" -exec sh -c 'cat {} >> output/`basename {}`' ;

通过这种方式,我们不指定搜索模式,而是明确省略output目录,以避免重复已处理的数据。

最新更新