LookIntoEast
LookIntoEast

Reputation: 8798

How could I set color for different letters?

In bash terminal:

cat AGTC.txt
AGCTGGCCAGGTGCCCAGGTCCCC

This is basically four DNA nucleotides. How could I color each of them, say A colored as Green; G colored as Brown; C colored as Blue; T colored as Red

In bash or Python, how could we do that?

tHX

Upvotes: 2

Views: 377

Answers (3)

AP.
AP.

Reputation: 8921

You can use the echo command like this with eval (-e flag) (with bash colors):

# Set all Bash color vars
A_COLOR="\e[32mA"
G_COLOR="\e[33mG"
C_COLOR="\e[34mC"
T_COLOR="\e[31mT"
NO_COL="\e[0m"

# Set DNA, and perform colored replacements
DNA="AGCTGGCCAGGTGCCCAGGTCCCC"
DNA=${DNA//A/${A_COLOR}}
DNA=${DNA//G/${G_COLOR}}
DNA=${DNA//C/${C_COLOR}}
DNA=${DNA//T/${T_COLOR}}$NO_COL

# Print DNA!
echo -e $DNA

Color reference for bash.

Upvotes: 3

SsJVasto
SsJVasto

Reputation: 496

If you want to colorize output using BASH, check out the color codes and replace letters appropriately

http://misc.flogisoft.com/bash/tip_colors_and_formatting

A --> [92mA
G --> [33mG
C --> [34mC
T --> [31mT

With Python (running under BASH), you could use the same syntax as above to produce color in your terminal.

echo -e "\e[92mA\e[33mG\e[34mC\e[31mT\e[33mG\e[33mG\e[34mC\e[34mC\e[92mA\e[33mG\e[33mG\e[31mT\e[33mG\e[34mC\e[34mC\e[34mC\e[92mA\e[33mG\e[33mG\e[31mT\e[34mC\e[34mC\e[34mC\e[34mC"

The above echo produces the right output in BASH

Upvotes: 0

Buzz
Buzz

Reputation: 1907

You can use a python library called termcolor. With it you can set the output text of strings with colors. like this:

from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')

the output of this in terminal would be the word "hello" in red text and the work "world" in green text

Upvotes: 1

Related Questions