使用 PowerShell 为多个 .faa 文件将文件名添加到以“>”开头的字符行

问题描述 投票:0回答:1

我有 100 个包含蛋白质序列的 FASTA 存储在一个目录中。我需要将它们各自的文件名添加到其中包含的每个 FASTA 标头(以“>”开头的字符串),然后将它们合并到一个 .faa 文件中。

我通过以下 PowerShell 命令获得了合并部分:

#Change extensions from .faa to .txt
gci -File | Rename-Item -NewName { $_.name -replace ".faa", ".txt" }

#Actual merging
Get-ChildItem $directory -include *.txt -rec | ForEach-Object {gc $_; ""} | out-file $directory

#Change encoding so I can process the file further in R
Get-Content .\test.txt | Set-Content -Encoding utf8 test-utf8.txt

之后,我将扩展名改回.faa。

每个文件存储多个蛋白质序列。每个标头应如下所示:

some_sequence -> >some_sequence file_name

第一次接触PowerShell,请问如何操作? 最好的问候!

powershell bioinformatics fasta
1个回答
0
投票

我假设您正在寻找类似以下内容的内容,它使用

switch
语句来处理单个文件并修改它们的标题:

Get-ChildItem $directory -Filter *.faa -Recurse | 
  ForEach-Object {
    $file = $_
    switch -Regex -File $file.FullName { # Process the file at hand.
      '^>' { $_ + ' ' + $file.Name  } # header line -> append file name
      default { $_ } # pass through
    }
    ''  # Empty line between the content from the indiv. files.
  } | 
  Set-Content -Encoding utf8 test-utf8.txt

注:

  • 无需先重命名
    .faa
    文件。
  • 无需修改标题的中间文件 - 最终输出文件的所有内容都可以直接流式传输到单个
    Set-Content
    调用。
© www.soinside.com 2019 - 2024. All rights reserved.