繁体   English   中英

将 SED 命令转换为 linux 命令

[英]Converting SED command to linux command

我有一个 sed 命令,它应该在 linux 上的 python 代码中运行(使用 os.system() )或转换为 Z6523EEEB439A3BDD5 代码。 但我不知道这个 sed 命令到底是做什么的。 如果您给我代码或帮助我如何在 python 中使用 os.system 实现它,我们将不胜感激,因为我在使用 os.system 时遇到了很多错误。

sed -n '1~4s/^@/>/p;2~4p' file1.fastq > file1.fasta

顺便说一句,输入和 output 文件应该在我的 python 代码中动态定义:

seq_file1 = '6448.fastq'
input_file1 = os.path.join(sys.path[0],seq_file1)
os.system(os.path.join("sed -n '1~4s/^@/>/p;2~4p' "+ seq_file1 + ' > ' + os.path.splitext(os.path.basename(input_file1))[0]+".fasta") , shell = True)

这个 sed 命令到底有什么作用?

sed命令在此文件中同时运行两个不同的操作。

-n : 抑制整个文件的 output。 仅打印应用指令p的行。

1~4 :从第 1 行开始,每 4 行应用下一条指令。

s/^@/>/p :用>替换每个前导@并打印结果。 由于上述指令,这个指令从第 1 行开始每 4 行应用一次。

; 操作分隔符。

2~4 :从第 2 行开始每 4 行应用下一条指令。

p :打印一行。

这意味着什么:“在从 #1 开始的每 4 行中用>替换前导@并从 #2 开始每 4 行打印一次”

例子:

file1.fastq的内容:

@ line 1
@ line 2
@ line 3
@ line 4
@ line 5
@ line 6
@ line 7
@ line 8
@ line 9
@ line 10
@ line 11
@ line 12

运行sed -n '1~4s/^@/>/p;2~4p' file1.fastq > file1.fasta

file1.fasta的内容

> line 1
@ line 2
> line 5
@ line 6
> line 9
@ line 10

一个很好的参考是: http://www.gnu.org/software/sed/manual/sed.html

如何在 Python 中做同样的事情?

以下代码片段旨在进行教学,因此我避免使用许多 Python 语言资源,可以应用这些资源来改进算法。

我测试了几次,它对我有用。

# import Regular Expressions module
import re

output = []

# Open the input file in read mode
with open('file1.fastq', 'r') as file_in:
    replace_step = 1 # replacement starts in line #1
    print_step = 0   # print function starts in line #2 so it bypass one step
    for i, line in enumerate(file_in):
        if replace_step == 1:
            output.append(re.sub('^@', '>', line))                        
        if replace_step >= 4:
            replace_step = 1
        else:
            replace_step += 1            

        if print_step == 1:
            output.append(line)
        if print_step >= 4:
            print_step = 1
        else:   
            print_step +=1

    print("".join(output))
    

# Open the output file in write mode
with open('file1.fasta', 'w') as file_out:
    file_out.write("".join(output))

您还可以使用subprocess.run

import subprocess
 
seq_file_in = '6448.fastq'
seq_file_out = '6448_out.fastq'
with open(seq_file_out, 'w') as fw:
    subprocess.run(["sed", r"1~4s/^@/>/p;2~4p", seq_file_in], stdout=fw)

在这种情况下,当sed命令如此简短时, subprocess.run可能会变得非常方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM