Sequence logo.pde

From dftwiki3
Revision as of 17:24, 1 August 2008 by Thiebaut (talk | contribs) (New page: <code><pre> // DNA_logo // D. Thiebaut // Creating a sequence logo //--------------------------------------------------------------------- // GEOMETRY //----------------------------------...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
// DNA_logo
// D. Thiebaut
// Creating a sequence logo

//---------------------------------------------------------------------
// GEOMETRY
//---------------------------------------------------------------------
int WIDTH        = 800;
int MIDWIDTH     = WIDTH/2;
int HEIGHT       = 600;
int BORDER       = 40;
int TITLELINE    = 20;
int ALINE        = HEIGHT/2;
PFont font;                          // the font used to display the symbols

int NOSEQS = 8;   // number of sequences
int A = 0;
int C = 1;
int G = 2;
int T = 3;

float Afreq[];
float Cfreq[];
float Gfreq[];
float Tfreq[];

float information[];

String seq[] = new String[NOSEQS];

//---------------------------------------------------------------------
// INITFONT: initialize the fonts (must be nonproportional ---
//---------------------------------------------------------------------
void initFont( ) {  
  font = loadFont( "GillSans-60.vlw" ); // ( "Monaco-60.vlw" );
  textFont( font );
}
//---------------------------------------------------------------------
// INITWINDOW: Draw the fixed text in the window
//---------------------------------------------------------------------
void initWindow( String title ) {
  textFont( font );
  color myColor = color( 99, 66, 204 );
  fill( myColor );
  textSize( 24 );
  text( title, BORDER, TITLELINE );
}

//---------------------------------------------------------------------
// SETUP: called once when app starts
//---------------------------------------------------------------------
void setup() {
  size( WIDTH, HEIGHT );
  background( 0, 0, 0 );
  initFont();
  initWindow( "Sequence Logo" );
  
  //---  initialize all 8 sequences ---
  seq[0] = "CCCATTGTTCTC";
  seq[1] = "TTTCTGGTTCTC";
  seq[2] = "TCAATTGTTTAG";
  seq[3] = "CTCATTGTTGTC";
  seq[4] = "TCCATTGTTCTC";
  seq[5] = "CCTATTGTTCTC";
  seq[6] = "TCCATTGTTCGT";
  seq[7] = "CCAATTGTTTTG";

  //--- generate arrays ---
  int noSymbols = seq[0].length( );
  Afreq  = new float[ noSymbols ];
  Cfreq  = new float[ noSymbols ];
  Gfreq  = new float[ noSymbols ];
  Tfreq  = new float[ noSymbols ];
  
  
  //--- compute information at each position of sequence ---
  findFreqsAndInformation();
  
  //--- display the logo ---
  displayLogo();
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
void generateACGTbitmaps() {
  textSize( 60 );
  fill( 255, 204, 0 );
  text( "ACGT", BORDER, BORDER+60 );
  fill( 132, 99, 0 );
  text( "ACGT", BORDER, BORDER+60*2 );
  fill( 99, 132, 204 );
  text( "ACGT", BORDER, BORDER+60*3 );
  fill( 99, 204,  33 );
  text( "ACGT", BORDER, BORDER+60*4 );
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
float xlog2x( float x ) {
  if ( x==0 ) return 0;
  return x * log(x)/log(2);
}




//---------------------------------------------------------------------
//---------------------------------------------------------------------
void findFreqsAndInformation() {
  //--- create frequency arrays for each symbol ---
  int noSymbols = seq[0].length( );
  int Acount[] = new int[noSymbols ];
  int Ccount[] = new int[noSymbols ];
  int Gcount[] = new int[noSymbols ];
  int Tcount[] = new int[noSymbols ];
  information = new float[ noSymbols ];  
  
  //--- compute frequency of A, C, G, and T in sequences ---
  for ( int i=0; i<seq[0].length(); i++ ) {
     Acount[i] = 0;
     Ccount[i] = 0;
     Gcount[i] = 0;
     Tcount[i] = 0;
     for ( int j=0; j<NOSEQS; j++ ) {
        if ( seq[j].charAt(i)=='A' ) Acount[i] += 1;
        if ( seq[j].charAt(i)=='C' ) Ccount[i] += 1;
        if ( seq[j].charAt(i)=='G' ) Gcount[i] += 1;
        if ( seq[j].charAt(i)=='T' ) Tcount[i] += 1;
     }
  }
  
  //--- compute information in each place ---
  for ( int i=0; i<seq[0].length(); i++ ) {
     Afreq[i] = Acount[i]*1.0/ NOSEQS;
     Cfreq[i] = Ccount[i]*1.0/ NOSEQS;
     Gfreq[i] = Gcount[i]*1.0/ NOSEQS;
     Tfreq[i] = Tcount[i]*1.0/ NOSEQS;
    
     information[i] = 2.0 + xlog2x(Afreq[i])
               +xlog2x( Cfreq[i] ) + xlog2x( Gfreq[i] )
               + xlog2x( Tfreq[i] );
     println( "information["+i+"] = "+information[i] );
  }
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
void displayLogo() {
  
  PImage a, c, g, t;
  
  a = loadImage( "a.png" );
  c = loadImage( "c.png" );
  g = loadImage( "g.png" );
  t = loadImage( "t.png" );
  //--- display seq[0], to test ---
  int noSymbols = seq[0].length();
  float charWidth = (WIDTH-BORDER*2)/noSymbols;
  char symbols[] = { 'A', 'C', 'G', 'T' };
  
  for ( int i=0; i< noSymbols; i++ ) {
    float charBase = ALINE;
    for ( int j=0; j<4; j++ ) {
        char sym = symbols[j];
        PImage img;
        float freq;
        img = a; freq = Afreq[i];
        if ( sym=='G' ) { img = g; freq = Gfreq[i]; }
        if ( sym=='C' ) { img = c; freq = Cfreq[i]; }
        if ( sym=='T' ) { img = t; freq = Tfreq[i]; }
        float charHeight = 50 * information[i] * freq;   
        image( img, BORDER + i*charWidth, charBase-charHeight, charWidth, charHeight );
        charBase = charBase - charHeight;
    }
  }  
}