Team:Freiburg software/Code/SequenceView.java

From 2009.igem.org

Revision as of 23:29, 21 October 2009 by JonasOnlyJonas (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 /*
     Copyright: synbiowave
     
     License: GPL
     
     Authors: Jörg Walossek
     
     Version: 0.1 
    
     DESCRIPTION:
    	This class can be used to create different visualizations of a DNA-Sequence
 */

 package org.synbiowave.sequence;

 import java.util.ArrayList;
 import java.util.List;

 import org.biojava.bio.seq.DNATools;
 import org.biojava.bio.seq.ProteinTools;
 import org.biojava.bio.seq.RNATools;
 import org.biojava.bio.symbol.IllegalSymbolException;

 import com.google.wave.api.Annotation;
 import com.google.wave.api.Range;

 // TODO: Auto-generated Javadoc
 /**
  * The Class SequenceView.
  */
 public class SequenceView {
	
	

	/** The a color code. */
	private static char[][] aColorCode={{'Y','F','A','I','L','M','V'},
										 {'W'},	
										 {'G'},	
										 {'P'},	
										 {'S','D','E','T'},
										 {'R','N','K'},
										 {'H'},	
										 {'Q'},	
										 {'C'}};
		
	/** The a colors. */
	private static String[] aColors=
                {"green","mediumturquoise","magenta","gold","red","blue","cornflowerblue","gray","saddlebrown"};
	
	/** The n color code. */
	private static char[][] nColorCode={{'U','T'},
										 {'A'},	
										 {'G'},
										 {'C'}};

	/** The n colors. */
	private static String[] nColors={"red","green","black","blue"};

	/**
	 * Instantiates a new sequence view.
	 */
	public SequenceView(){}
	
	/**
	 * Checks if is sequence.
	 * 
	 * @param sequence the sequence
	 * 
	 * @return the string
	 */
	public static String isSequence(String sequence){
		String type="DNA";
		try{DNATools.createDNA(sequence);}
		catch(IllegalSymbolException e){
			type="RNA";
			try{RNATools.createRNA(sequence);}
			catch(IllegalSymbolException se){
				type="PROTEIN";
				try{ProteinTools.createProtein(sequence);}
				catch(IllegalSymbolException ise){
					type="null";}}}
		return type;}
	
	/**
	 * Gets the color.
	 * 
	 * @param sequence the sequence
	 * @param type the type
	 * @param rShift the r shift
	 * 
	 * @return the color
	 */
	public static List<Annotation> getColor(String sequence,String type,int rShift){
		List<Annotation> cAnnotations=new ArrayList<Annotation>();
		return getColor(sequence, type, rShift, cAnnotations);}

	/**
	 * Gets the color.
	 * 
	 * @param sequence the sequence
	 * @param type the type
	 * @param rShift the r shift
	 * @param cAnnotations the c annotations
	 * 
	 * @return the color
	 */
	public static List<Annotation> getColor(String sequence, String type, int rShift,List<Annotation> cAnnotations) {
		char[][] colorCode=SequenceView.nColorCode;
		String[] colors=SequenceView.nColors;
		if(type.contains("PROTEIN")){
			colorCode=SequenceView.aColorCode;
			colors=SequenceView.aColors;}
		boolean na=true;
		int position=rShift;
		char[] sa=sequence.toUpperCase().toCharArray();
		for(char sc:sa){
			if(na){
				for(int r=0;r<colorCode.length;r++){
					for(int c=0;c<colorCode[r].length;c++){
						if(colorCode[r][c]==sc){
							cAnnotations.add(new Annotation("style/color",colors[r],
                                                                 new Range(position,position+1)));
							na=false;}}}
				if(na){cAnnotations.add(new Annotation("style/color","black",new Range(position,position+1)));}
				else{na=true;}
				position++;}}
		return cAnnotations;}
	
	/**
	 * Gets the scale.
	 * 
	 * @param length the length
	 * 
	 * @return the scale
	 */
	public static ArrayList<String> getScale(int length){
		ArrayList<String> sScale=new ArrayList<String>();
		StringBuffer scale=new StringBuffer();
		StringBuffer numbers=new StringBuffer();
		int wait=0;
		for(int i=1;i<length+1;i++){
			if((i%5)==0){
				scale.append('\u23AA');
				numbers.append(i);
				wait=String.valueOf(i).length()-1;}
			else{
				scale.append('\'');
				if(wait==0){numbers.append(' ');}
				else{wait--;}}
			if((i%50)==0){
				sScale.add(scale.append('\n').append(numbers.append('\n')).toString());
				scale=new StringBuffer();
				numbers=new StringBuffer();
				wait=0;}}
		sScale.add(scale.append('\n').append(numbers.append('\n')).toString());
		return sScale;}
	
	/**
	 * Gets the hTML view.
	 * 
	 * @param sequence the sequence
	 * @param type the type
	 * 
	 * @return the hTML view
	 */
	public static String getHTMLView(String sequence,String type){
		StringBuffer scale=new StringBuffer();
		StringBuffer numbers=new StringBuffer();
		int wait=0;
		for(int i=1;i<sequence.length()+1;i++){
			if((i%5)==0){
				scale.append('|');
				numbers.append(i);
				wait=String.valueOf(i).length()-1;}
			else{
				scale.append('\'');
				if(wait==0){numbers.append(" ");}
				else{wait--;}}}
		String htmlView="(br /)"+scale+"(br /)"+numbers+"(br /)";
		char[][] colorCode=SequenceView.nColorCode;
		String[] colors=SequenceView.nColors;
		if(type.contains("PROTEIN")){
			colorCode=SequenceView.aColorCode;
			colors=SequenceView.aColors;}
		char[] sa=sequence.toUpperCase().toCharArray();
		for(char sc:sa){
			for(int r=0;r<colorCode.length;r++){
				for(int c=0;c<colorCode[r].length;c++){
					if(colorCode[r][c]==sc){
						htmlView+="(span style='color:"+colors[r]+";')"+sc+"(/span)";}}}}
		return htmlView+"(br /)(br /)";}
	
	/**
	 * Gets the hTML view.
	 * 
	 * @param sequences the sequences
	 * 
	 * @return the hTML view
	 */
	public static String getHTMLView(ArrayList<String> sequences){
		int maxLength=0;
		for(String sequence:sequences){if(sequence.length()>maxLength){maxLength=sequence.length();}}
		StringBuffer scale=new StringBuffer();
		StringBuffer numbers=new StringBuffer();
		int wait=0;
		for(int i=1;i<maxLength+1;i++){
			if((i%5)==0){
				scale.append('|');
				numbers.append(i);
				wait=String.valueOf(i).length()-1;}
			else{
				scale.append('\'');
				if(wait==0){numbers.append(" ");}
				else{wait--;}}}
		String htmlView="(br /)"+scale+"(br /)"+numbers+"(br /)";
		for(String sequence:sequences){
			String type=isSequence(sequence);
			char[][] colorCode=SequenceView.nColorCode;
			String[] colors=SequenceView.nColors;
			if(type.contains("PROTEIN")){
				colorCode=SequenceView.aColorCode;
				colors=SequenceView.aColors;}
			char[] sa=sequence.toUpperCase().toCharArray();
			for(char sc:sa){
				for(int r=0;r<colorCode.length;r++){
					for(int c=0;c<colorCode[r].length;c++){
						if(colorCode[r][c]==sc){
							htmlView+="(span style='color:"+colors[r]+";')"+sc+"(/span)";}}}}
			htmlView+="(br /)";}
		return htmlView+"(br /)";}
	
 }