0

I have a fasta file like:

grep -A 7 -B 4 COSN229024 wrong.fasta:

>COSN9627597
CGCTGGGCTCGCCTCCAGCCTGGCCTGCATTCCCAAATCTA
>COSN8175610
CAAGAGAGAAATTCTGACACCTCCTAAGTCTACCAAGCTTT
>COSN229024
CACTATAAAAATATTAAGAGA
>COSN18183003
TGTGTTTGTGATTGATGT
>COSN18487588
TGCTTACCCCTTAAATGCAACTTATTTACTTTTACCACTGT
>COSN1681903|COSN1178783
CTTCCCAACTCATGAGTTCTGAATTCCAATACGTCTCCATT

I observed that in wrong.fasta approximately half of the sequence of >COSN229024 breaks up and forms a new sequence >COSN18183003. In the end, the whole order of fasta header mess up after breakup. So, I want to put the break part of >COSN229024 back into the >COSN229024 in wrong.fasta and than transfer / replace the header. I used grep -A 7 -B 4 COSN229024 wrong.fasta. My desired output would be like this :

>COSN9627597
CGCTGGGCTCGCCTCCAGCCTGGCCTGCATTCCCAAATCTA
>COSN8175610
CAAGAGAGAAATTCTGACACCTCCTAAGTCTACCAAGCTTT
>COSN229024
CACTATAAAAATATTAAGAGATGTGTTTGTGATTGATGT
>COSN18183003
TGCTTACCCCTTAAATGCAACTTATTTACTTTTACCACTGT
>COSN18487588
CTTCCCAACTCATGAGTTCTGAATTCCAATACGTCTCCATT
>COSN1681903|COSN1178783
........so on and so forth
5
  • 1
    It's not clear what you're asking. grep doesn't rearrange your lines, so if there's a breakup in some lines, it comes from your file, not from grep. Maybe try showing the relevant 11 lines from wrong.fasta? If it's not a question about grep but about joining lines together, you'll have to explain more precisely how lines should be rearranged. Apr 27 '16 at 9:32
  • The way I understand it you want to merge two lines and shift everything below up by one. Does that mean the last two lines in the file don't have >? Or the last > will go empty? Apr 27 '16 at 9:41
  • @frostschutz: yeah you are right. As after >COSN229024, everything would be shifted below up by one. In the end of new file, I would have > which is empty. Apr 27 '16 at 9:46
  • wouldn't it make more sense to figure out what produced the broken fasta file and why, then fix that so it doesn't do it again? i.e. fixing it at the source is better than trying to patch it up later.
    – cas
    Apr 27 '16 at 11:47
  • BTW, you can check if it's more than just COSN229024 that is broken/incomplete. assuming that all sequence lines are supposed to have 41 characters, try grep -E -v -B1 '^>|^[CGTA]{41}$' wrong.fasta - or remove the $ in the regexp if they're supposed to have at least 41 chars.
    – cas
    Apr 27 '16 at 11:54
1

You can fix the file using awk like this:

awk '/^>/{if(x)t=$0;else print"\n"$0} !/^>/{printf"%s",$0;if(t)print"\n"t} /^>COSN229024/{x=1}' < wrong.fasta > good.fasta

After this you will probably also need to fix the beginning and the end of the file manually.

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.