From 7d37107bc146f68cd5d12ead18efd3fcf7b75acd Mon Sep 17 00:00:00 2001
From: gv1 <gv1@ee4ac58c-ac51-4696-9907-e4b3aa274f04>
Date: Thu, 13 Aug 2009 14:24:12 +0000
Subject: [PATCH] added translation for polypeptide features (with Tim's help!)

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@11533 ee4ac58c-ac51-4696-9907-e4b3aa274f04
---
 uk/ac/sanger/artemis/io/GFFStreamFeature.java | 94 +++++++++++++++++--
 1 file changed, 84 insertions(+), 10 deletions(-)

diff --git a/uk/ac/sanger/artemis/io/GFFStreamFeature.java b/uk/ac/sanger/artemis/io/GFFStreamFeature.java
index 12004f71e..1072cb558 100644
--- a/uk/ac/sanger/artemis/io/GFFStreamFeature.java
+++ b/uk/ac/sanger/artemis/io/GFFStreamFeature.java
@@ -20,15 +20,19 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
- * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/io/GFFStreamFeature.java,v 1.68 2009-07-21 17:25:42 gv1 Exp $
+ * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/io/GFFStreamFeature.java,v 1.69 2009-08-13 14:24:12 gv1 Exp $
  */
 
 package uk.ac.sanger.artemis.io;
 
 
+
+
+
 import java.util.Hashtable;
 import java.util.Enumeration;
 import java.util.List;
+
 import java.util.StringTokenizer;
 import java.util.Vector;
 import java.io.IOException;
@@ -38,6 +42,7 @@ import java.text.SimpleDateFormat;
 
 import uk.ac.sanger.artemis.Options;
 import uk.ac.sanger.artemis.chado.ClusterLazyQualifierValue;
+import uk.ac.sanger.artemis.components.genebuilder.GeneUtils;
 import uk.ac.sanger.artemis.components.genebuilder.ProteinMapPanel;
 import uk.ac.sanger.artemis.components.genebuilder.ortholog.MatchPanel;
 import uk.ac.sanger.artemis.util.LinePushBackReader;
@@ -50,7 +55,7 @@ import uk.ac.sanger.artemis.util.StringVector;
  *  A StreamFeature that thinks it is a GFF feature.
  *
  *  @author Kim Rutherford
- *  @version $Id: GFFStreamFeature.java,v 1.68 2009-07-21 17:25:42 gv1 Exp $
+ *  @version $Id: GFFStreamFeature.java,v 1.69 2009-08-13 14:24:12 gv1 Exp $
  **/
 
 public class GFFStreamFeature extends SimpleDocumentFeature
@@ -93,6 +98,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
   private org.gmod.schema.sequence.Feature chadoLazyFeature;
   private boolean readOnlyFeature = false;
   
+  
 
   private static String MAP_DECODE[][] = {
     { " ",  "%20" },  // white space
@@ -188,6 +194,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
   public GFFStreamFeature(final Feature feature)
   {
     this(feature, false);
+    
   }
   
   /**
@@ -200,6 +207,8 @@ public class GFFStreamFeature extends SimpleDocumentFeature
   {
     this(feature.getKey(), feature.getLocation(), feature.getQualifiers());
     
+
+    
     if(feature instanceof GFFStreamFeature)
     {
       if(((GFFStreamFeature)feature).id_range_store != null)
@@ -280,6 +289,9 @@ public class GFFStreamFeature extends SimpleDocumentFeature
         }
         catch(ReadOnlyException e){}
         catch(EntryInformationException e){}
+      } else
+      {
+    	  chadoGene = ((GFFStreamFeature)feature).chadoGene;
       }
     }
   }
@@ -727,6 +739,8 @@ public class GFFStreamFeature extends SimpleDocumentFeature
   }
 
   protected static Hashtable contig_ranges;
+  private Range this_range;
+  private String frame;
 
   /**
    *  Write this Feature to the given stream.
@@ -739,12 +753,12 @@ public class GFFStreamFeature extends SimpleDocumentFeature
   {
     final RangeVector ranges = getLocation().getRanges();
     final int ranges_size = ranges.size();
-
-//  final Hashtable contig_ranges = SimpleDocumentEntry.getContigRanges();
+    
+    //  final Hashtable contig_ranges = SimpleDocumentEntry.getContigRanges();
 
     for(int i = 0; i < ranges_size; ++i) 
     {
-      Range this_range = (Range)ranges.elementAt(i);
+      this_range = (Range)ranges.elementAt(i);
 
       String seqname = getGffSeqName();
       String source  = getGffSource();
@@ -790,7 +804,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
           group = gene;
       }
 
-      String frame = ".";
+      frame = ".";
 
       final Qualifier codon_start = getQualifierByName("codon_start");
 
@@ -876,6 +890,8 @@ public class GFFStreamFeature extends SimpleDocumentFeature
                              "Derives_from",
                              "Target", "Gap", "Note", 
                              "Dbxref", "Ontology_term" };
+    
+    
     int count = 0;
     Qualifier this_qualifier;
     final int names_length = names.length;
@@ -894,9 +910,16 @@ public class GFFStreamFeature extends SimpleDocumentFeature
       if(this_qualifier == null) 
         continue;
       
+      if(this_qualifier.getValues().toString().length() <= 2)
+    	  continue;
+      
       // GSV :: see new getQualifierString signature
       // this qualifier is one of the reserved qualifiers 
       final String this_qualifier_str = getQualifierString(this_qualifier, true);
+      
+      if (this_qualifier_str.length() == 0)
+    	  continue;
+      
       if(this_qualifier_str == null)
         continue;
 
@@ -906,12 +929,14 @@ public class GFFStreamFeature extends SimpleDocumentFeature
       count++;
     }
     
+    addTranslation();
+	
     boolean lname;
     final int qualifiers_size = qualifiers.size();
     for(int i = 0; i < qualifiers_size; i++) 
     {
       this_qualifier = (Qualifier)qualifiers.elementAt(i);
-
+      
       lname = false;
       for(int j=0; j<names_length; j++)
         if(this_qualifier.getName().equals(names[j]))
@@ -921,7 +946,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
         continue;
       
       // GSV :: see new getQualifierString signature
-      // this qualifier is NOT one of the reserved qualifiers 
+      // this qualifier is NOT one of the reserved qualifiers
       String this_qualifier_str = getQualifierString(this_qualifier, false);
       
       if(this_qualifier_str == null)
@@ -934,7 +959,56 @@ public class GFFStreamFeature extends SimpleDocumentFeature
 
     return buffer.toString();
   }
-
+  
+  
+  /*
+   * 
+   * Adds translations to polypeptide features.
+   */
+  private void addTranslation() 
+  {
+	if (! getKey().getKeyString().equals("polypeptide"))
+		return;
+	
+	ChadoCanonicalGene ccg = this.chadoGene;
+  
+	String id = this.getQualifierByName("ID").getValues().toString() ;
+	if (ccg != null)
+	{
+	  @SuppressWarnings("unused")
+	  uk.ac.sanger.artemis.Feature f = new uk.ac.sanger.artemis.Feature(this);
+	  // the above line constructs the appropriate userData within this current GFFStreamFeature object, 
+	  // which is required by the following GeneUtils.deriveResidues()
+	  
+	  String residues = GeneUtils.deriveResidues(this);
+	  if (residues != null)
+	  {
+		  Qualifier translation = new Qualifier("translation");
+		  translation.addValue(residues);
+		  try {
+			  setQualifier(translation);
+		  } catch (Exception e) {
+			  UI.warn(e.getMessage(), e.getClass().toString());
+		  }
+	  } else
+	  {
+		  UI.warn(id + " has no translatable residues, is pseudogene? " + ccg.getGene().getKey().getKeyString(), "empty residues");
+	  }
+	} else
+	{
+	  UI.warn(id + " has no ChadoCanonicalGene ", "no canonical gene");
+  
+	  Qualifier comment = new Qualifier("autogenerated_comment");
+	  comment.addValue( "this polypeptide has no parent gene associated, probably unknown transcript"  );
+	  try {
+		  setQualifier(comment);
+	  } catch (Exception e) {
+		  UI.warn(e.getMessage(), e.getClass().toString());
+	  }
+	}
+  }
+  
+  
   /**
    * Used to write out the GFF attributes.
    * @param q the qualifier to represent as a <code>String</code>
@@ -970,7 +1044,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
      */
     String nameToBuffer = encode(name);
     if (! reserved)
-    	nameToBuffer = Character.toLowerCase(nameToBuffer.charAt(0)) + nameToBuffer.substring(1);
+    	nameToBuffer = nameToBuffer.toLowerCase(); //Character.toLowerCase(nameToBuffer.charAt(0)) + nameToBuffer.substring(1);
     buffer.append(nameToBuffer);
     
     if(values != null)
-- 
GitLab