Error while using Seqkit tool in jupyter notebook
1
0
Entering edit mode
2.9 years ago

Dear all I have a problem while using seqkit tool command in python jupytar notebook? I was trying to convert some fastq files to fasta files with the simple command:

seqkit fq2fa file_name.fastq -o file_name.fa

or the command:

seqtk seq -a file_name.fastq > output.fasta

But unfortunately in both cases I got the error:

File "<ipython-input-1-88778dbee2d4>", line 1
    seqkit fq2fa file_name.fastq -o file_name.fa
           ^
SyntaxError: invalid syntax

but these commands worked on a bash terminal properly...!

Is there is any way to loop over multiple fastq files in a directory and convert them to fasta files in python jupyter nootbook?

jupyter-notebook seqkit • 1.5k views
ADD COMMENT
2
Entering edit mode
2.9 years ago

try !seqkit

ADD COMMENT
0
Entering edit mode

Many thanks actually applying your suggestion worked when I tried to apply it on a single file but when I tried to put it in a loop to convert all files at once I got an error,

rootdir = '/home/phoebemagdy/workdir/PhD/1st_PhD_Trial_Code/Ecto_Data/'
fastq_extension = ".fastq"
for file in os.listdir(rootdir):
    if file.endswith(fastq_extension): # check for ".fastq" extension
        #print(file)
        !seqkit fq2fa file -o file.fa

Any suggestions for the loop to work properly..??

Many thanks in advance

ADD REPLY
2
Entering edit mode

Please post error next time.

Try this:

!find /home/phoebemagdy/workdir/PhD/1st_PhD_Trial_Code/Ecto_Data/ -type f -name "*.fastq" | while read line; do echo seqkit fq2fa $line -o ${line/.fastq/}_new.fastq; done

You don't need to declare directory or extension outside shell. There are three issues here. file is a python variable, not a shell variable. Second issue would be after passing python variable to shell (using {}), it would still print file name only, not file path, due to which command would fail again. Thirdly, you would not have control over extension and renaming unless you write more python code, to remove file extension and add extra text and extension. Hence it is better to do entire operation in shell as I mentioned above.

If you want to continue above way (OP), try this:

import os
rootdir = '/home/phoebemagdy/workdir/PhD/1st_PhD_Trial_Code/Ecto_Data/'
destdir = '/home/phoebemagdy/workdir/PhD/1st_PhD_Trial_Code/Ecto_Data/test/' ## make sure that this (test) directory exists or write a script to 
file=[rootdir+file for file in os.listdir(rootdir) if file.endswith(".fastq")]
for i in file:
    new_file=os.path.basename(i).replace("fastq","fa")
    !seqkit fq2fa {i} -o {destdir}{new_file}

(beware of tabs while copy/pasting)

ADD REPLY
0
Entering edit mode

Many thanks your suggestions worked very properly and converted all my fastq files to fasta files. Really appreciate your help, and I'm highly grateful for the tips you wrote it really clarified a lot of mistakes I was falling in, once again thanks alot

ADD REPLY

Login before adding your answer.

Traffic: 2001 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6