# Coloring of specific characters in a String

**URL:** <https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 19, 2021, 9:28pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468 "2021-05-19T21:28:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Post date:** [May 19, 2021, 9:28pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/1 "2021-05-19T21:28:19Z")

</div>

I work with RNA and DNA sequences and am interested in determining how to change a particular character in a string to a different color.

Specifically, I’d like to

```julia
sequence = "AUCGCGCA"

sequence[3] = #Blue copy of C

```

Any suggestions are welcome?

Jim

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [May 19, 2021, 9:47pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/2 "2021-05-19T21:47:33Z")

</div>

There’s `printstyled()`, and Crayons.jl for terminals, but it would be nice to have a StyledString type that could be rendered in any display.

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [May 19, 2021, 10:44pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/3 "2021-05-19T22:44:33Z")

</div>

This is not a full solution, but if colours are problematic you can use a different font for special letters. Julia has full unicode, co there are few quite distinct fonts which could serve as letter highlighting.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 19, 2021, 11:09pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/4 "2021-05-19T23:09:13Z")

</div>

Can you give a bit more information about just what you are trying to accomplish?

Do you only need to display ASCII characters?  
Is this only to display a string from your own code, annotated with colors?

Are there only a few colors that you need to use?

One possibilty would be to create a vector for the colors for each character you wish to display,  
and write a function that displays a string with the colors.

The API could be something like the following:

```julia
using ColoredSequences
cs = ColoredSequence(sequence)
setcolor!(cs, 3, CS_BLUE)
println(cs)

```

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 19, 2021, 11:59pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/5 "2021-05-19T23:59:10Z")

</div>

```julia
module ColoredSequences

export ColoredSequence, setcolor!

const CS_NONE = 0x0
const CS_RED = 0x1
const CS_GREEN = 0x2
const CS_BLUE = 0x3

export CS_NONE, CS_RED, CS_GREEN, CS_BLUE

struct ColoredSequence{T<:AbstractString}
    s::T
    cv::Vector{UInt8}
end

ColoredSequence(s) = ColoredSequence(s, fill(CS_NONE, length(s)))

setcolor!(cs, loc, color) = (cs.cv[loc] = color)

const colors = [:normal, :red, :green, :blue]

function findnextneq(vec, loc, val)
    len = length(vec)
    while loc <= len
        vec[loc] != val && return loc
        loc += 1
    end
    loc
end

function Base.show(io::IO, cs::ColoredSequence)
    str = cs.s
    cv = cs.cv
    len = sizeof(cv)
    prv = 1
    while prv < len
        prevc = cv[prv]
        loc = findnextneq(cv, prv, prevc)
        printstyled(io, SubString(str, prv, loc-1), color = colors[prevc+1])
        prv = loc
    end
end

end # module ColoredSequences

```

This is a quick-and-dirty implementation, it assumes that you are only using strings that do not have any multi-codeunit characters in it (i.e. all ASCII, for String, and `UTF8Str`, for example, or all \< 65536 for `UTF16Str`)  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Post date:** [May 20, 2021, 12:05am UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/6 "2021-05-20T00:05:31Z")

</div>

Thanks for asking. So mostly I want to be able to clearly visualize differences between sequences. A sequence can be a rather long combination of ‘T’, ‘C’, ‘G’, and ‘A’ characters. So it can be hard to pick out which character is different when comparing two sequences. I was hoping I could use a color to highlight the individual characters that are different. Maybe a different color for each of the four nucleotides noted above. I’d use this in a jupyter or Pluto notebook mostly.

---

<div class="post-metadata">

**Author:** ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Post date:** [May 20, 2021, 12:10am UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/7 "2021-05-20T00:10:03Z")

</div>

Sweet. Let me give this a go. Thanks so much!

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 20, 2021, 12:20am UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/8 "2021-05-20T00:20:24Z")

</div>

I’m happy to help!  
I noticed that it wasn’t all that easy to see the color for letters (at least, on my monitor, with a white background).  
It might be better to cause the highlighted letters to be inverted (i.e. colored background, with black or white text), to stand out more.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 20, 2021, 1:01am UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/9 "2021-05-20T01:01:26Z")

</div>

```julia
module ColoredSequences

export ColoredSequence

const CS_NONE = 0x0
const CS_RED = 0x1
const CS_GREEN = 0x2
const CS_BLUE = 0x3
const CS_CYAN = 0x4
const CS_MAGENTA = 0x5
const CS_YELLOW = 0x6

export CS_NONE, CS_RED, CS_GREEN, CS_BLUE, CS_CYAN, CS_MAGENTA, CS_YELLOW

struct ColoredSequence{T<:AbstractString}
    s::T
    cv::Vector{UInt8}
end

ColoredSequence(s) = ColoredSequence(s, fill(CS_NONE, length(s)))

Base.setindex!(cs, color, ind) = (cs.cv[ind] = color)

const colors = [:normal, :red, :green, :blue, :cyan, :magenta, :yellow]

function findnextneq(vec, loc, val)
    len = length(vec)
    while loc <= len
        vec[loc] != val && return loc
        loc += 1
    end
    loc
end

function Base.show(io::IO, cs::ColoredSequence)
    str = cs.s
    cv = cs.cv
    len = sizeof(cv)
    prv = 1
    while prv <= len
        prevc = cv[prv]
        loc = findnextneq(cv, prv, prevc)
        printstyled(io, SubString(str, prv, loc-1);
                    color = colors[prevc + 1], reverse = (prevc != CS_NONE))
        prv = loc
    end
end

end # module ColoredSequences

```

I changed it to use reverse for displaying colors, it’s much more visible, fixed an off-by-one bug, and added a few more colors

---

<div class="post-metadata">

**Author:** ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Post date:** [May 20, 2021, 1:24pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/10 "2021-05-20T13:24:03Z")

</div>

Nice, I might try to extend this a little - make a couple options for background or character color, and maybe font size or typeface.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [May 20, 2021, 6:14pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/11 "2021-05-20T18:14:45Z")

</div>

Check out the [CSS font properties](https://developer.mozilla.org/en-US/docs/Web/CSS/font) for reference.

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [May 21, 2021, 3:16pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/12 "2021-05-21T15:16:50Z")

</div>

This is a side note, but as you are working with DNA I would highly recommend using package `BioSequences`. You get containers for various biological sequences with many tools for analysis and manipulation. I don’t think there is colour highlighting there, but you can add it the same way as shown above.

---

<div class="post-metadata">

**Author:** ![Jdbeck66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdbeck66/32/19399_2.png) [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Post date:** [May 21, 2021, 4:52pm UTC](https://discourse.julialang.org/t/coloring-of-specific-characters-in-a-string/61468/13 "2021-05-21T16:52:46Z")

</div>

Yeah, I’ve looked at that some. Thanks for the direction

JB
