Team:Freiburg software/Code/Util.java

From 2009.igem.org

 /*
     Copyright: synbiowave
     
     License: GPL
     
     Authors: Jörg Walossek
     
     Version: 0.1 
    
     DESCRIPTION:
    	This class provides the static methods used in the file up- and download.
 */

 package org.synbiowave.biojava;

 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;

 import org.biojava.bio.BioException;
 import org.biojava.bio.seq.DNATools;
 import org.biojava.bio.seq.ProteinTools;
 import org.biojava.bio.seq.RNATools;
 import org.biojavax.Namespace;
 import org.biojavax.RichObjectFactory;
 import org.biojavax.bio.seq.RichSequence;
 import org.biojavax.bio.seq.RichSequenceIterator;
 import org.synbiowave.database.Sequence;

 // TODO: Auto-generated Javadoc
 /**
  * The Class Util.
  */
 public class Util {
	
	/** The Constant ns. */
	private static final Namespace ns=RichObjectFactory.getDefaultNamespace();

	/**
	 * Instantiates a new util.
	 */
	public Util() {}
	
	/**
	 * Read.
	 * 
	 * @param in the in
	 * 
	 * @return the rich sequence iterator
	 * 
	 * @throws ClassNotFoundException the class not found exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static RichSequenceIterator read(InputStream in) throws ClassNotFoundException,IOException{
		RichSequence.IOTools.registerFormat(org.biojavax.bio.seq.io.FastaFormat.class);
		RichSequence.IOTools.registerFormat(org.biojavax.bio.seq.io.EMBLFormat.class);
		RichSequence.IOTools.registerFormat(org.biojavax.bio.seq.io.EMBLxmlFormat.class);
		RichSequence.IOTools.registerFormat(org.biojavax.bio.seq.io.GenbankFormat.class);
		RichSequence.IOTools.registerFormat(org.biojavax.bio.seq.io.INSDseqFormat.class);
		return RichSequence.IOTools.readStream(new BufferedInputStream(in),ns);}

	/**
	 * Write.
	 * 
	 * @param out the out
	 * @param sequences the sequences
	 * @param format the format
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static void write(OutputStream out,RichSequenceIterator sequences,String format) throws IOException{
		if(format.equalsIgnoreCase("fasta")){
			RichSequence.IOTools.writeFasta(out,sequences,ns);}
		else if(format.equalsIgnoreCase("embl")){
			RichSequence.IOTools.writeEMBL(out,sequences,ns);}
		else if(format.equalsIgnoreCase("genbank")){
			RichSequence.IOTools.writeGenbank(out,sequences,ns);}}
	
	/**
	 * Convert to rich sequence.
	 * 
	 * @param sequence the sequence
	 * 
	 * @return the org.biojava.bio.seq. sequence
	 * 
	 * @throws BioException the bio exception
	 * @throws ClassNotFoundException the class not found exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static org.biojava.bio.seq.Sequence convertToRichSequence(Sequence sequence) throws    
                        BioException,ClassNotFoundException,IOException{
		org.biojava.bio.seq.Sequence s=null;
		if(sequence.getPlain()!=null){s=Util.read(new ByteArrayInputStream(sequence.getPlain().getBytes())).nextSequence();}
		else{
			if(sequence.getType().equals("DNA")){s=DNATools.createDNASequence(sequence.getSequence(),sequence.getName());}
			else if(sequence.getType().equals("RNA")){
                                 s=RNATools.createRNASequence(sequence.getSequence(),sequence.getName());}
			else{s=ProteinTools.createProteinSequence(sequence.getSequence(),sequence.getName());}}
 //			StrandedFeature.Template temp = new StrandedFeature.Template();
 //			temp.annotation = org.biojava.bio.Annotation.EMPTY_ANNOTATION;
 //			temp.location = new RangeLocation(10,25);
 //			temp.source = "";
 //			temp.strand = StrandedFeature.POSITIVE;
 //			temp.type = "";
 //	 
 //			Feature f = s.createFeature(temp);
 //			temp = (StrandedFeature.Template)f.makeTemplate();
 //			temp.location = new RangeLocation(30,35);
 //			temp.strand = StrandedFeature.NEGATIVE;
 //			s.createFeature(temp);
 //			}
 //         rs=Tools.createRichSequence(sequence.getName(),sequence.getSequence(),AlphabetManager.alphabetForName(sequence.getType()));
 //			rs.setDescription(sequence.getName());}
 //			ArrayList<Annotation> annotations=sequence.getAnnotations();
 //			Set<Feature> features=new HashSet<Feature>();
 //			for(Annotation a:annotations){
 //				if(a.getName().equals("feature")){
 //					Feature f=RichFeature.Tools.makeEmptyFeature();
 //					f.setType(a.getValue());
 //					f.setLocation(LocationTools.makeLocation(a.getRange().getStart(),a.getRange().getEnd()));
 //					features.add(f);}}}
		return s;}
 }