# Renaming IDs in a GFF3

**URL:** https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687
**Category:** Biology, Health, and Medicine
**Created:** [January 23, 2020, 8:15am UTC](https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687 "2020-01-23T08:15:13Z")
**Posts on this page:** 3
**Page:** 3

<div class="post-metadata">

### Author: ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)
#### Post date: [January 27, 2020, 8:45am UTC](https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687/41 "2020-01-27T08:45:14Z")

</div>

Thank you @kevbonham, I agree! I’ll happily answer any questions about GenomicAnnotations, but it’s very difficult when the target keeps changing, or when the questions aren’t actually about the package.

@mictadlo, if you want to change the name of each chromosome you can iterate over the chromosomes:

```julia
for (i, chr) in enumerate(chrs)
    chr.name = string(i)
end

```

Obviously, this assumes that `chrs` is an `Array{Chromosome}`, and that the chromosomes are stored in the order that they should be numbered, which you will have to verify yourself. Iterating over the chromosomes is one way to solve your problem with `currentID`, as well.  
Regarding the documentation, I think adding an example section is a good idea so I’ll look into that.

> [@Ward9250](#):
>
> But I know how to do this with Automa if a FSM adaptation of GenomicAnnotations parser is desirable.

If it increases performance it would certainly be welcome. I haven’t encountered any problems with the current implementation in some time now, though, so I don’t think it has high priority.

---

<div class="post-metadata">

### Author: ![mictadlo](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mictadlo](https://discourse.julialang.org/u/mictadlo)
#### Post date: [January 28, 2020, 1:03am UTC](https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687/42 "2020-01-28T01:03:00Z")

</div>

Thank you for your solution. I updated the code and it does what I wanted. However, I just wonder whether there would be better to do it:

```julia
using GenomicAnnotations
chrs = readgff("/Users/lorencm/chr1-2.augustus.hints_utr.gff3")
currentID = 0
currentChr = ""
for gene in @genes(chrs)
    if currentChr != parent(gene).name
        global currentChr = parent(gene).name
        global currentID = 0
    end
    
    if feature(gene) == :gene
        global currentID += 1
    end

    newID = lpad(currentID, 6, '0')
    chrID = SubString(parent(gene).name, 7)
    if !ismissing(gene.ID)
        geneIDextension = ""
        splitCurrentID = split(gene.ID,".",limit=2)
        if length(splitCurrentID) == 2
            println(splitCurrentID)
            geneIDextension="."*splitCurrentID[2]
            println(geneIDextension)
        end
        gene.ID = "$chrID$newID$geneIDextension"
        println(gene.ID)
    end
    if !ismissing(gene.Parent)
        println("HeLLO")
        println(gene.Parent)
        println(parent(gene).name)
        geneIDextension = ""
        splitCurrentID = split(gene.Parent,".",limit=2)
        if length(splitCurrentID) == 2
            println(splitCurrentID)
            geneIDextension="."*splitCurrentID[2]
            println(geneIDextension)
        end
        println("$(parent(gene).name)$newID")
        gene.Parent = "$chrID$newID$geneIDextension"
        println(gene.Parent)
        
        
        #println("!!Parent",parent(gene),parent(gene).name)
    end
end
open("updated.gbk", "w") do f
    printgff(f, chrs)
end

```

Thank you in advance

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [January 28, 2020, 3:51pm UTC](https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687/43 "2020-01-28T15:51:28Z")

</div>

> [@mictadlo](#):
>
> I updated the code and it does what I wanted. However, I just wonder whether there would be better to do it:

“Better” is quite subjective, and there are a lot of ways to think about it. Do you mean more performant, more clear, more idiomatic? Sometimes there are style things that just come down to personal preference.

For performance reasons, I think it would be better to wrap that all in a function and avoid all the `global`s.

Style-wise, I prefer using logging functionality (like `@info`) instead of print statements. I also like using short-circuit boolean operators instead of short `if` statements, eg

```julia
feature(gene) == :gene && (global currentID += 1)

```

instead of

```julia
if feature(gene) == :gene
    global currentID += 1
end

```

and use `continue` instead of a large indented `if` block, eg

```julia
ismissing(gene.Parent) && continue
# stuff...

```

instead of

```julia
if !ismissing(gene.Parent)
    # stuff....
end

```

though your way is arguably clearer. Keep trying different stuff, and see what you like. In the majority of code that I write, clarity and correctness is far more important than performance or some notion of “proper” code.

[Previous page](https://discourse.julialang.org/t/renaming-ids-in-a-gff3/33687.md?page=2)
