0

I need to reformat the kegg reconstruct pathway output, I have something like this in file1:

00550 Peptidoglycan biosynthesis (2)

K01000

K02563

00511 Other glycan degradation (8) K01190   K01191

K01192

K01201

K01227

K12309

I need some things like that in file2:

00550 Peptidoglycan biosynthesis (2)   K01000   K02563
00511 Other glycan degradation (6)   K01190   K01191   K01192   K01201   K01227   K12309

How could I reformat this in linux or python ?

Thanks

2
  • That (8) is what? And it is changed to (6) for what? The count of items to follow?
    – RudiC
    Oct 23, 2018 at 14:03
  • I am sorry this, in the case of this post, the number between the parenthesis is 6, since we have 6 K* Oct 23, 2018 at 15:05

1 Answer 1

0

How far would this get you:

awk '
!NF             {next                                                   # don"t process empty lines
                }
/^[0-9]+ /      {sub (/\([0-9]*\)/, "(" CNT ")", PRT)                   # for the "glycan" lines (leading numerical)
                                                                        # correct the count in parentheses
                 if (PRT) print PRT                                     # print the PRT buffer (NOT first line when empty)
                 PRT = ""                                               # empty it after print
                 CNT = gsub (/K[0-9]*/, "&") - 1                        # get this line"s "K..." count, corr.for later incr.
                }
                {PRT = sprintf ("%s%s%s", PRT, PRT?" ":"", $0)          # append this line to buffer
                 CNT++                                                  # increment "K..." count
                }
END             {sub (/\([0-9]*\)/, "(" CNT ")", PRT)                   # see above
                 print PRT
                }
' file
00550 Peptidoglycan biosynthesis (2) K01000 K02563
00511 Other glycan degradation (6) K01190   K01191 K01192 K01201 K01227 K12309
1
  • Thanks, it woks perfectly for my large dataset. This was helpful Oct 23, 2018 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .