Question: Create Files From Double For Cycle
gravatar for filipzembol
5.4 years ago by
filipzembol100 wrote:

Dear all, I create a code like this, which solve my problem to separate bam file to each segments, but still time I have one problem and I cannot finish it. I would like to create many outputs (files), which has name, same like variable $h. Than I want one think, if the for cycle for "h" finished, the code create a text fie with same name like $h.txt . I tried this, but it doesnt work:

 for h in {1..22..1} 
 for i in {0..249480000..60000}
 u=$i let "u +=60000"

 echo $i"-"$u | samtools view /home/filip/Desktop/Posledni\ NIFTY/019/odfiltrovany019.bam  chr$h:$i-$u | awk '{ n=length($10);print gsub(/[GCCgcs]/,"",$10)/n;}'| awk '{s+=$1}END{print NR,s/NR}' > ${h%}.txt


OTUPUT will be 1.txt, 2.txt, 3.txt, ......., 22.txt. This file will contain information from loop cycle for variable i. Thank you.

bash output awk • 1.4k views
ADD COMMENTlink modified 5.4 years ago by Giovanni M Dall'Olio26k • written 5.4 years ago by filipzembol100

maybe I am slow this morning but I do not fully understand the question plus the title is somewhat misleading. Could you put an example of the type of output you want ?

ADD REPLYlink written 5.4 years ago by Gabriel R.2.6k

I do not know, how could I use > ${h%}.txt , because I want to receive on output txt files, which have name same like variable h. The code i OK, but I do not know how use the output syntax, after each loop (for variable h) I recieve one file with same name (1..22) and it will be filled by loop (for i).

ADD REPLYlink written 5.4 years ago by filipzembol100

This question is hardly bioinformatics related, apart from the samtools command. Also, I still don't understand what you're asking.

ADD REPLYlink written 5.4 years ago by David Westergaard1.4k
gravatar for Giovanni M Dall'Olio
5.4 years ago by
London, UK
Giovanni M Dall'Olio26k wrote:

I would do it using GNU/parallel. See Gnu Parallel - Parallelize Serial Command Line Programs Without Changing Them.

Basically, once you have installed GNU/parallel (not the parallel tool that comes with some linux distributions), you can convert your loop to something like the following:

$: parallel 'chromosome={1}; start={2}; end=$(echo "$start"+60000|bc); echo $chromosome $start $end' ::: $(seq 1 22) ::: $(seq 0 60000 249480000)
1 0 60000
1 60000 120000
1 120000 180000
1 180000 240000
1 240000 300000
1 300000 360000
1 360000 420000
1 420000 480000
1 480000 540000
1 540000 600000
1 600000 660000
1 660000 720000
1 720000 780000
1 780000 840000

Now, you can modify your original command, substituting $h, $i and $u with $chromosome, $start, and $end (if I interpreted your code correctly)

ADD COMMENTlink written 5.4 years ago by Giovanni M Dall'Olio26k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1517 users visited in the last hour