我有 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,请问如何操作? 最好的问候!
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
调用。