0

I have a python command that runs as follows:

python script.py -file 1000G_EUR_Phase3_plink/1000G.NUMBER --out GTEx_Cortex.chrNUMBER

I would like to replace the NUMBER variable with the numbers 1:20. So if I replace NUMBER with 1 it would look like this:

python script.py -file 1000G_EUR_Phase3_plink/1000G.1 --out GTEx_Cortex.chr1

and this on the second iteration (if I replace it with 2):

python script.py -file 1000G_EUR_Phase3_plink/1000G.2 --out GTEx_Cortex.chr2

But I don't want to keep manually changing NUMBER 20 times. I want to automate the entire thing.

How can I do this in the command prompt? Should this be done in VIM or is there another way in python?

Thanks!

2 Answers 2

2
for i in `seq 1 20`;do python script.py -file 1000G_EUR_Phase3_plink/1000G.${i} --annot GTEx_Cortex_chr1.annot.gz --out GTEx_Cortex.chr${i};done
Sign up to request clarification or add additional context in comments.

1 Comment

1000G_EUR_Phase3_plink/1000G.${i}
2

If you are doing this frequently you could also write a bash script.

Create a file run_stuff that loops through commands. It could be analogous to this:

#!/bin/bash
n=$1
i=1
while (( i <= n )); do
    python prog${i}.py
    (( i = i + 1 ))
done

The above script runs prog1.py, then prog2.py, and so on. For your code, just replace the 5th line with the analogous line you want.

Then in the terminal you would do:

chmod u+x run_stuff
./run_stuff 20

The chmod command just changes the permissions of the file so you can execute it.

3 Comments

Silly question, what is in prog1.py, for example? Would it be the command I stated in the question?
No, the idea was just to show how to call commands with arguments that iterate. Substituting the line python prog${i}.py with python script.py -file 1000G_EUR_Phase3_plink/1000G.${i} --annot GTEx_Cortex_chr1.annot.gz --out GTEx_Cortex.chr${i} should do exactly what you want.
I just answered the question with the python prog${i}.py part since it is easily reproducible by anyone with python (including me so I can actually make sure it works :) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.