2

Lets say I have these many rows

chr1    66999638    66999638    SGIP1   1   +
chr1    66999251    66999251    SGIP1   1   +
chr1    33545778    33549778    AZIN2   1   +
chr1    8376144 8380144 SLC45A1 1   +
chr1    16765166    16769166    NECAP2  1   +
chr1    33544713    33548713    AZIN2   1   +
chr1    25069759    25073759    CLIC4   1   +
chr1    33544729    33548729    AZIN2   1   +
chr1    50487626    50491626    AGBL4   1   -
chr1    92349836    92353836    TGFBR3  1   -

Expected output:

chr1    66999638    66999638    SGIP1   1   +
chr1    66999251    66999251    SGIP1_1 1   +
chr1    33545778    33549778    AZIN2   1   +
chr1    83761448380144  SLC45A1 1   +
chr1    16765166    16769166    NECAP2  1   +
chr1    33544713    33548713    AZIN2_1 1   +
chr1    25069759    25073759    CLIC4   1   +
chr1    33544729    33548729    AZIN2_2 1   +
chr1    50487626    50491626    AGBL4   1   -
chr1    92349836    92353836    TGFBR3  1   -

I would like a way to alter the second row (and 3rd, 4th, etc.) which is duplicated in column $4. I simply want to add a "_1" string so it reads "SGIP_1" or "WhateverGeneName_1".

Preferably an awk or sed solution would be best. Thanks so much in advance.

7
  • 1
    I assume you want the 3rd occurrence to have _2 added, the 4th to be _3? I also assume there will be A, A, B, A, B, C, C, D such that you want A, A_1, B, A_2, B_1, C, C_1, D as the output? Jan 12, 2016 at 14:29
  • All of these assumptions are correct.
    – System
    Jan 12, 2016 at 14:34
  • You might want to add more description to the question then, because the example is too vague / too light on detail. Jan 12, 2016 at 14:35
  • Would you want more row examples or more description on what the rows actually contain?
    – System
    Jan 12, 2016 at 14:36
  • 3
    Please edit your question and show your desired output. Examples are much easier to understand than descriptions. Also make sure you include an example of a gene name that only occurs once in the gff file and the output you would like for that. Should it still be foo_1 or should only those names that occur more than once be modified?
    – terdon
    Jan 12, 2016 at 14:38

2 Answers 2

2

In the simplest case where you want all gene names to have _N appended even if they only appear once, you can do:

$ awk '$4=$4"_"++a[$4];' file.gff 
chr1 66999638 66999638 SGIP1_1 1 +
chr1 66999251 66999251 SGIP1_2 1 +
chr1 33545778 33549778 AZIN2_1 1 +
chr1 8376144 8380144 SLC45A1_1 1 +
chr1 16765166 16769166 NECAP2_1 1 +
chr1 33544713 33548713 AZIN2_2 1 +
chr1 25069759 25073759 CLIC4_1 1 +
chr1 33544729 33548729 AZIN2_3 1 +
chr1 50487626 50491626 AGBL4_1 1 -
chr1 92349836 92353836 TGFBR3_1 1 -

Note that that will change the field separator to a single space. To keep it tab separated (as GFF files should be), use:

$ awk -vOFS="\t" '$4=$4"_"++a[$4];' file.gff 
chr1    66999638    66999638    SGIP1_1 1   +
chr1    66999251    66999251    SGIP1_2 1   +
chr1    33545778    33549778    AZIN2_1 1   +
chr1    8376144 8380144 SLC45A1_1   1   +
chr1    16765166    16769166    NECAP2_1    1   +
chr1    33544713    33548713    AZIN2_2 1   +
chr1    25069759    25073759    CLIC4_1 1   +
chr1    33544729    33548729    AZIN2_3 1   +
chr1    50487626    50491626    AGBL4_1 1   -
chr1    92349836    92353836    TGFBR3_1    1   -

If you only want to modify the names of those genes that appear more than once, it gets a little bit more complex:

$ awk -vOFS="\t" '(++a[$4]>1){$4=$4"_"a[$4]-1}1;' file.gff
chr1    66999638    66999638    SGIP1   1   +
chr1    66999251    66999251    SGIP1_1 1   +
chr1    33545778    33549778    AZIN2   1   +
chr1    8376144 8380144 SLC45A1 1   +
chr1    16765166    16769166    NECAP2  1   +
chr1    33544713    33548713    AZIN2_1 1   +
chr1    25069759    25073759    CLIC4   1   +
chr1    33544729    33548729    AZIN2_2 1   +
chr1    50487626    50491626    AGBL4   1   -
chr1    92349836    92353836    TGFBR3  1   -
1
  • The third answer seems to have worked as well. I'll keep looking at the 30k+ gene list for both answers, but they look good so far. I appreciate it!
    – System
    Jan 12, 2016 at 15:00
1

Using awk

awk -vOFS="\t" '{$4=a[$4]++?$4"_"a[$4]-1:$4}1' file

chr1    66999638        66999638        SGIP1   1       +
chr1    66999251        66999251        SGIP1_1 1       +
chr1    33545778        33549778        AZIN2   1       +
chr1    8376144 8380144 SLC45A1 1       +
chr1    16765166        16769166        NECAP2  1       +
chr1    33544713        33548713        AZIN2_1 1       +
chr1    25069759        25073759        CLIC4   1       +
chr1    33544729        33548729        AZIN2_2 1       +
chr1    50487626        50491626        AGBL4   1       -
chr1    92349836        92353836        TGFBR3  1       -

Sets $4 to equal itself if only one occurence has been seen or adds _ then the number of occurences seen minus 1.

1
  • This answer seems to have worked well, i'll keep looking through the 30k+ gene list and keep you updated. Thanks a ton!
    – System
    Jan 12, 2016 at 14:59

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.