Create Files From Double For Cycle
1
0
Entering edit mode
10.7 years ago
filipzembol ▴ 180

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:

 #!/bin/bash 
 for h in {1..22..1} 
 do 
 for i in {0..249480000..60000}
 do 
 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

 done  
 done

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

output bash awk • 2.7k views
ADD COMMENT
1
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode
10.7 years ago

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 COMMENT

Login before adding your answer.

Traffic: 2264 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