简体   繁体   English

将 SED 命令转换为 linux 命令

[英]Converting SED command to linux command

I have a sed command which should be run in a python code on linux (using os.system() ) or converted to a python code.我有一个 sed 命令,它应该在 linux 上的 python 代码中运行(使用 os.system() )或转换为 Z6523EEEB439A3BDD5 代码。 but I don't know what exactly this sed command do.但我不知道这个 sed 命令到底是做什么的。 It's appreciated If you give me the code or help me how to implement it using os.system in python, because I faced lots of errors when using os.system.如果您给我代码或帮助我如何在 python 中使用 os.system 实现它,我们将不胜感激,因为我在使用 os.system 时遇到了很多错误。

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

by the way, the input and output files should be defined dynamically in my python code:顺便说一句,输入和 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)

What does exactly this sed command do?这个 sed 命令到底有什么作用?

This sed command is running two different operations at once in this file.sed命令在此文件中同时运行两个不同的操作。

-n : Suppress the output of whole file. -n : 抑制整个文件的 output。 Print only lines where the instruction p is applied to.仅打印应用指令p的行。

1~4 : apply the next instruction in every 4 lines starting in the line #1. 1~4 :从第 1 行开始,每 4 行应用下一条指令。

s/^@/>/p : replace every leading @ by a > and print the result. s/^@/>/p :用>替换每个前导@并打印结果。 Because of the above instruction, this one is applied in every 4 lines starting in the line #1.由于上述指令,这个指令从第 1 行开始每 4 行应用一次。

; operation separator.操作分隔符。

2~4 : apply the next instruction every 4 lines starting in the line #2. 2~4 :从第 2 行开始每 4 行应用下一条指令。

p : print a line. p :打印一行。

What this means: "Replace leading @ by a > in every 4 lines starting at #1 and print every 4 lines starting at #2"这意味着什么:“在从 #1 开始的每 4 行中用>替换前导@并从 #2 开始每 4 行打印一次”

Example:例子:

Content of file1.fastq : 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

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

Content of file1.fasta file1.fasta的内容

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

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

How to do the same in Python?如何在 Python 中做同样的事情?

The below code snippet aims to be didactic, so I avoided using many Python language resources, which can be applied in order to refine the algorithm.以下代码片段旨在进行教学,因此我避免使用许多 Python 语言资源,可以应用这些资源来改进算法。

I tested it a few times and it worked for me.我测试了几次,它对我有用。

# 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))

You can also use subprocess.run :您还可以使用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)

In cases like this, when the sed command is that short and succint, subprocess.run might turn out really handy.在这种情况下,当sed命令如此简短时, subprocess.run可能会变得非常方便。

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

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