Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

Executing bash for loop in a python Jupyter notebook cell

I'm trying to run a bash for loop in a cell of my Jupyter notebook. I can re-write the command on one line and prefix it with ! and it runs fine, but I like the format for readability (teaching notebook). Using the %%bash magic command doesn't seem to work.

%%bash
for sample in $(cat samples)
do
    echo "On sample: $sample" 
    cutadapt 
    -g ^GTGYCAGCMGCCGCGGTAA 
    -G ^CCGYCAATTYMTTTRAGTTT 
    -m 209 -M 255 
    --too-short-output untrimmed_fastq/${sample}_R1_too-short.fastq.gz 
    --too-long-output untrimmed_fastq/${sample}_R1_too-long.fastq.gz 
    --untrimmed-output untrimmed_fastq/${sample}_R1_untrimmed.fastq.gz 
    --too-short-paired-output untrimmed_fastq/${sample}_R2_too-short.fastq.gz 
    --too-long-paired-output untrimmed_fastq/${sample}_R2_too-long.fastq.gz 
    --untrimmed-paired-output untrimmed_fastq/${sample}_R2_untrimmed.fastq.gz 
    -o trimmed_fastq/${sample}_R1_001.fastq.gz -p trimmed_fastq/${sample}_R2_001.fastq.gz 
    fastqfiles/${sample}_R1_001.fastq.gz fastqfiles/${sample}_R2_001.fastq.gz 
    >> cutadapt_stats.txt 2>&1
done

Yields the syntax error:

File "<ipython-input-3-592002da22aa>", line 3
    for sample in $(cat samples)
                  ^
SyntaxError: invalid syntax

I'm fairly new to using magic commands and subprocesses. Thank you for any tips or insight

question from:https://stackoverflow.com/questions/65894547/executing-bash-for-loop-in-a-python-jupyter-notebook-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...