0

I've got a .fasta file, which is strictly a formatted text containing some informations about DNA. Here's its common structure:

>NODE_18_length_75451_cov_83.3021
TGAACCGCTTGCCAAATATTTTCCGTCCGGACTTACGGCAACGGAAAGGAC
>NODE_3_length_175235_cov_84.0427
ACATGCAATGTTTATAGTCCTTGTATCAGAGACTCTATCAACGCTCTCGG

On even lines you've got the DNA sequence, and on odd lines you've got infos about the sequence. This scheme is repeated for at least 10k lines, into a single text file. I need to find a way to get only the value after "cov_" for every lines, to multiplicate it for 2 and print into a new file. The new file needs to have this scheme (for all the lines):

>NODE_18 cov_166.60
DNA seq: TGAACCGCTTGCCAAATATTTTCCGTCCGGACTTACGGCAACGGAAAGGAC
>NODE_3 cov_168.04
DNA seq: ACATGCAATGTTTATAGTCCTTGTATCAGAGACTCTATCAACGCTCTCGG  
  • Did you already tried something? Please post! – F. Hauri Feb 13 '18 at 15:49
  • @JigglyNaga answer actually did the work. Tryin' to improve for doing the real task needed: when fixed, I'll update the quest with the code. – Shred Feb 13 '18 at 16:09
0

If you really want to use the shell for this, you can hand the arithmetic over to another command, like bc:

while read odd ; do
    echo -n "cov_" ; echo "2*${odd##*_}" | bc -q
    read even
    echo "DNA seq: $even"
done < input.fasta
  • It doesn't print actually what I'm looking for. "cov." value where cutted off in the output. – Shred Feb 13 '18 at 14:17
  • Sorry, I slipped up while renaming variables. Fixed. – JigglyNaga Feb 13 '18 at 14:40
  • can i grep letters from each even lines to calculate the % of G and C of every even line inside the loop? Tried as here, ( pastebin.com/Gb2w13dJ ) don't know why it doesn't work. Could you please give a look? – Shred Feb 13 '18 at 15:24
  • The shell runs this loop once for each pair of input lines; it can't backtrack, or read the same line again. If you add a grep (reading from stdin) into the loop, that will consume the whole of the rest of the file on the first run. You'd have to find some other way to count characters (which could be eg. echo $even | grep). – JigglyNaga Feb 13 '18 at 15:39
  • Updated. Seems to work, except for "Dna cont" : it stamps for every line "50%" which is, obviously, not correct. Could you give a look? pastebin.com/ZYcCwUVU – Shred Feb 13 '18 at 16:07
0

With bash? don't go there, it's not a text processing language. With awk:

awk -F_ '/^>/ {printf "%s_%s cov_%.2f\n", $1, $2, $6 * 2; next} {print "DNA seq:", $0}' file.fasta 
>NODE_18 cov_166.60
DNA seq: TGAACCGCTTGCCAAATATTTTCCGTCCGGACTTACGGCAACGGAAAGGAC
>NODE_3 cov_168.09
DNA seq: ACATGCAATGTTTATAGTCCTTGTATCAGAGACTCTATCAACGCTCTCGG
  • Is it possible to do more complex arithmetic operations into the printf argument? If I need to multiplicate and divide the $6 for other variables, did I need to do hand arithmetic over a "while..done" ? – Shred Feb 13 '18 at 14:08
  • @Shred printf arguments can be any valid expression AFAIK - beyond that, I'm unclear exactly what you're asking – steeldriver Feb 13 '18 at 14:13
  • In my case, for the cov. value i don't need to do just a *2 , but an operation where this value where divided for another variable (called, for example, K_MER). Could I do an operation like $6 / $K_MER into the printf argument? How can I use parenthesis to do that? – Shred Feb 13 '18 at 14:16
  • @Shred Arithmetic in awk follows the usual rules of operator precedence however $K_MER would refer to the field whose number is equal to the value of variable K_MER - for the value itself, drop the $ – steeldriver Feb 13 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.