-7

Here is my file:

>ref
AAAAAAA
>seq1
BBBBBBB
>ref
AAAAAAA
>seq2
CCCCCCC
>ref
AAAAAAA
>seq3
DDDDDDD
...

Here is what I'd like to get:

>seq1
AAAAAAA
>ref
BBBBBBB
>seq2
AAAAAAA
>ref
CCCCCCC
>seq3
AAAAAAA
>ref
DDDDDDD
...

So, swap line 1 with line 3, line 5 with line 7, line 9 with line 11, etc. Any suggestion on how I could do it (in bash, perl or python) will be much appreciated! :)

tripleee
  • 175,061
  • 34
  • 275
  • 318
tlorin
  • 1,100
  • 6
  • 17
  • 30
  • 1
    1) Pick a language 2) Show your attempt so far – Cory Kramer Apr 23 '15 at 11:55
  • You have to provide a minimal working example for us to work on. Also this helps us to know about your efforts in solving the problem. :) – ρss Apr 23 '15 at 11:57
  • Use `awk` it's a piece of cake with it. In pseudocode you could use `awk 'if line contains ">ref" {store line header; next line; store line content; next line; next line; print ">ref"; print current line; print stored line header; print stored line content}'` – ShellFish Apr 23 '15 at 12:07
  • Wowowoh, sorry guys, I did not say what I had tried so far! I opened my file with Vim, and then did `:1m3|1m2|5m7|5m6|9m11|9m10` but for a long file it's a bit of a nightmare to do this! So that's why I thought of another script. @ShellFish, I get the idea, however I'm not familiar enough with awk to eat this "piece of cake" ;) – tlorin Apr 23 '15 at 13:41
  • 1
    Record a macro in vim and execute it as many times as you want. Google for macro vim, it's a life saver – ShellFish Apr 23 '15 at 13:54

2 Answers2

1

Using perl from command line,

perl -ne 'push @r, $_; print(@r[2,1,0,3]), @r=() if @r==4 or eof' file
mpapec
  • 50,217
  • 8
  • 67
  • 127
1

This is python 2.7

Here you go through the input file line by line. If the line is ref it replaces it with seq with a running number. Other lines stay untouched.

fo = open("input.txt", "r")
calc = 1
ref = "ref"
seq = "seq"

for lines in fo:
    lines = lines.strip()
    if "ref" in lines:
        lines = seq + str(calc)
        calc = int(calc) +1 
        print lines
    elif "seq" in lines:
        print ref
    else:
         print lines

output:

seq1
AAAAAAA
ref
BBBBBBB
seq2
AAAAAAA
ref
CCCCCCC
seq3
AAAAAAA
ref
DDDDDDD
jester112358
  • 465
  • 3
  • 17