Skip to content
Snippets Groups Projects
DatabaseDocument.java 93 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
        final ByteBuffer buff = new ByteBuffer();
    
    tjc's avatar
    tjc committed
        logger4j.debug("BUILD GENE GFF LINE");
    
    tjc's avatar
    tjc committed
        buildGffLineFromId(dao, chadoFeature.getFeatureId(), 
            id_store, parent.getUniqueName(), src_id, buff, chadoFeature);
        
    
        if(!readChildren)
        {
          logger4j.debug( new String(buff.getBytes()) );
          return buff;
        }
        
    
        // get children of gene
    
    tjc's avatar
    tjc committed
        List relations = new Vector(chadoFeature.getFeatureRelationshipsForObjectId());
    
        Set idsSeen = new HashSet();
    
        for(int i = 0; i < relations.size(); i++)
        {
    
    tjc's avatar
    tjc committed
          //Feature transcript = new Feature();
    
    tjc's avatar
    tjc committed
          int id = ((FeatureRelationship) relations.get(i)).getFeatureBySubjectId().getFeatureId();
    
          Integer idInt = new Integer(id);
          if(idsSeen.contains(idInt))
            continue;
          idsSeen.add(idInt);
    
    tjc's avatar
    tjc committed
          Feature transcript = buildGffLineFromId(dao, id, id_store, parent.getUniqueName(), 
                                                  src_id, buff, null);
    
          if( transcript == null || transcript.getCvTerm() == null ||
              transcript.getCvTerm().getName() == null || 
    
    tjc's avatar
    tjc committed
             (transcript.getCvTerm().getName().indexOf("RNA") < 0 &&
              transcript.getCvTerm().getName().indexOf("transcript") < 0 ) )
            continue;
    
          // get children of transcript - exons and pp
    
          logger4j.debug("GET CHILDREN OF "+transcript.getName());
    
    tjc's avatar
    tjc committed
          List transcipt_relations = new Vector(
              transcript.getFeatureRelationshipsForObjectId());
    
    
          for(int j = 0; j < transcipt_relations.size(); j++)
          {
    
    tjc's avatar
    tjc committed
            id = ((FeatureRelationship) transcipt_relations.get(j)).getFeatureBySubjectId().getFeatureId();
    
    tjc's avatar
    tjc committed
    
            buildGffLineFromId(dao, id, id_store, parent.getUniqueName(), 
                               src_id, buff, null);
    
    tjc's avatar
    tjc committed
        logger4j.debug( "GFF:\n"+new String(buff.getBytes()) );
    
    tjc's avatar
    tjc committed
    
    
        // now wait for cvterm to be loaded
        if(cvThread != null)
        {
          while(cvThread.isAlive())
            try
            {
              Thread.sleep(10);
            }
            catch(InterruptedException e)
            {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
        }
    
    tjc's avatar
    tjc committed
      
      /**
       * 
       * @param dao
       * @param featureId
       * @param id_store
       * @param parentName
       * @param srcFeatureId
       * @param this_buff
       * @param chadoFeature
       * @return
       */
    
    tjc's avatar
    tjc committed
      private Feature buildGffLineFromId(final GmodDAO dao, 
                                      final int featureId, 
                                      final Hashtable id_store,
                                      final String parentName,
                                      final int srcFeatureId,
                                      final ByteBuffer this_buff,
                                      Feature chadoFeature)
      {
        if(chadoFeature == null)
          chadoFeature = (Feature)dao.getFeatureById(featureId); 
    
        id_store.put(Integer.toString(chadoFeature.getFeatureId()), 
    
    tjc's avatar
    tjc committed
                     chadoFeature);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final FeatureLoc loc = getFeatureLoc(new Vector(
    
    tjc's avatar
    tjc committed
            chadoFeature.getFeatureLocsForFeatureId()), srcFeatureId);
    
    tjc's avatar
    tjc committed
        
        if(loc == null)
        {
          logger4j.debug("FEATURELOC NOT FOUND :: "+chadoFeature.getUniqueName());
          return null;
        }
    
    tjc's avatar
    tjc committed
        final Hashtable dbxrefs = IBatisDAO.mergeDbXRef(
    
    tjc's avatar
    tjc committed
            dao.getFeatureDbXRefsByFeatureUniquename(chadoFeature.getUniqueName()));
    
    tjc's avatar
    tjc committed
        final Hashtable synonym = getAllFeatureSynonyms( 
    
            dao.getFeatureSynonymsByFeatureUniquename(chadoFeature.getUniqueName()));
    
    tjc's avatar
    tjc committed
        
    
    tjc's avatar
    tjc committed
        final Hashtable featureCvTerms = getFeatureCvTermsByFeature(dao, 
    
    tjc's avatar
    tjc committed
                                      dao.getFeatureCvTermsByFeature(chadoFeature));
        
    
    tjc's avatar
    tjc committed
        final Hashtable featureCvTermDbXRefs = getFeatureCvTermDbXRef(dao, 
    
    tjc's avatar
    tjc committed
                                 dao.getFeatureCvTermDbXRefByFeature(chadoFeature));
        
    
    tjc's avatar
    tjc committed
        Hashtable featureCvTermPubs = null;
    
    tjc's avatar
    tjc committed
        
    
    tjc's avatar
    tjc committed
        try
        {
          featureCvTermPubs = getFeatureCvTermPub(dao,
                              dao.getFeatureCvTermPubByFeature(chadoFeature));
        }
        catch(RuntimeException re){re.printStackTrace();}
    
        final Hashtable featurePubs = getFeaturePubs(dao,
            dao.getFeaturePubsByFeature(chadoFeature));
        List pubDbXRefs= new Vector(); //dao.getPubDbXRef();
    
    tjc's avatar
    tjc committed
        chadoToGFF(chadoFeature, parentName, dbxrefs, synonym, featureCvTerms,
    
    tjc's avatar
    tjc committed
            pubDbXRefs, featureCvTermDbXRefs, featureCvTermPubs, featurePubs, 
            id_store, dao, loc, this_buff, gene_builder);  
    
    tjc's avatar
    tjc committed
        return chadoFeature;
      }
      
    
    tjc's avatar
    tjc committed
      /**
       * Convert the chado feature into a GFF line
       * @param feat           Chado feature
       * @param parentFeature  parent of this feature
       * @param dbxrefs        hashtable containing dbxrefs
       * @param synonym        hashtable containing synonynms
    
    tjc's avatar
    tjc committed
       * @param featureCvTerms
       * @param pubDbXRefs
       * @param featureCvTermDbXRefs
    
    tjc's avatar
    tjc committed
       * @param id_store       id store for looking up parent names
       * @param dao            chado data access
    
    tjc's avatar
    tjc committed
       * @param featureloc     feature location for this chado feature
    
    tjc's avatar
    tjc committed
       * @param this_buff      byte buffer of GFF line 
       */
    
    tjc's avatar
    tjc committed
      private static void chadoToGFF(final Feature feat,
    
                                     final String parentFeature,
                                     final Hashtable dbxrefs,
                                     final Hashtable synonym,
    
                                     final Hashtable featureCvTerms,
    
    tjc's avatar
    tjc committed
                                     final List pubDbXRefs,
    
    tjc's avatar
    tjc committed
                                     final Hashtable featureCvTermDbXRefs,
    
    tjc's avatar
    tjc committed
                                     final Hashtable featureCvTermPubs,
    
    tjc's avatar
    tjc committed
                                     final Hashtable featurePubs,
    
                                     final Hashtable id_store,
    
                                     final GmodDAO dao,
    
    tjc's avatar
    tjc committed
                                     final FeatureLoc featureloc,
    
                                     final ByteBuffer this_buff,
                                     final boolean gene_builder)
    
    tjc's avatar
    tjc committed
      {
        String gff_source = null;
        
    
        final int fmin          = featureloc.getFmin().intValue() + 1;
        final int fmax          = featureloc.getFmax().intValue();
        final int type_id       = feat.getCvTerm().getCvTermId();
        final Short strand      = featureloc.getStrand();
        final Integer phase     = featureloc.getPhase();
        final String name       = feat.getUniqueName();
    
        final String typeName   = getCvtermName(type_id, dao, gene_builder);
    
        final Integer featureId = new Integer(feat.getFeatureId());
    
        final String timelastmodified = Long.toString(feat.getTimeLastModified().getTime());
    
    tjc's avatar
    tjc committed
    
        String parent_id = null;
        String parent_relationship = null;
    
    tjc's avatar
    tjc committed
        int rank = -1;
    
    /*    if(feat.getFeatureRelationship() != null)
    
    tjc's avatar
    tjc committed
        {
    
          FeatureRelationship feat_relationship = feat.getFeatureRelationship();
    
    tjc's avatar
    tjc committed
          parent_id = Integer.toString(feat_relationship.getFeatureByObjectId().getFeatureId());
    
    tjc's avatar
    tjc committed
          long parent_type_id = feat_relationship.getCvTerm().getCvTermId();
    
    tjc's avatar
    tjc committed
          parent_relationship = feat_relationship.getCvTerm().getName();
    
    tjc's avatar
    tjc committed
          rank= feat_relationship.getRank();
    
    tjc's avatar
    tjc committed
          if(parent_relationship == null)
            parent_relationship = getCvtermName(parent_type_id, dao);
        }
    
    tjc's avatar
    tjc committed
        
        ByteBuffer clusterOrthoParalog = null;
    
        if(feat.getFeatureRelationshipsForSubjectId() != null)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          Collection relations = feat.getFeatureRelationshipsForSubjectId();
          Iterator it = relations.iterator();
          Set featureRelationshipIds = new HashSet();
          //Set duplicates = new HashSet();
          
          while(it.hasNext())
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            final FeatureRelationship fr = 
                                (FeatureRelationship)it.next();
            final Integer featureRelationShipId = new Integer( fr.getFeatureRelationshipId() );
    
            if(featureRelationshipIds.contains( featureRelationShipId ))
              continue;
    
    tjc's avatar
    tjc committed
            featureRelationshipIds.add(featureRelationShipId);
            final String cvTermName;
            if( fr.getCvTerm().getName() == null )
            {
              int parent_type_id = fr.getCvTerm().getCvTermId();
              cvTermName = getCvtermName(parent_type_id, dao, gene_builder);
            }
            else
              cvTermName = fr.getCvTerm().getName();
          
            if(cvTermName.equals("derives_from") || cvTermName.equals("part_of") ||
    
               cvTermName.equals("proper_part_of") || 
               cvTermName.equals("partof") || cvTermName.equals("producedby")) // flybase
    
    tjc's avatar
    tjc committed
              parent_relationship = cvTermName;
              parent_id = Integer.toString(fr.getFeatureByObjectId().getFeatureId());
              rank      = fr.getRank();
    
    tjc's avatar
    tjc committed
            {
              if(clusterOrthoParalog == null)
                clusterOrthoParalog = new ByteBuffer();
              // ortholog/paralog/cluster data
              int orthologueFeature = fr.getFeatureByObjectId().getFeatureId();
              clusterOrthoParalog.append(cvTermName+"="+
                  GFFStreamFeature.encode("object_id="+orthologueFeature+"; rank="+fr.getRank())+";");
            }
    
    tjc's avatar
    tjc committed
    
        // look up parent name
    
    tjc's avatar
    tjc committed
        if(parent_id != null && id_store != null &&  id_store.containsKey(parent_id))
    
    tjc's avatar
    tjc committed
          parent_id = ((Feature)id_store.get(parent_id)).getUniqueName();
    
    tjc's avatar
    tjc committed
     
        // make gff format
        
        Vector dbxref = null;
        // append dbxrefs
        if(dbxrefs != null &&
    
           dbxrefs.containsKey(featureId))
    
    tjc's avatar
    tjc committed
        {
    
          dbxref = (Vector)dbxrefs.get(featureId);
    
    tjc's avatar
    tjc committed
          for(int j=0; j<dbxref.size(); j++)
    
    tjc's avatar
    tjc committed
            if(((String)dbxref.get(j)).startsWith("GFF_source:"))
    
    tjc's avatar
    tjc committed
              gff_source = ((String)dbxref.get(j)).substring(11);
              dbxref.removeElementAt(j);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        this_buff.append(parentFeature + "\t"); // seqid
        
        if(gff_source != null)
          this_buff.append(gff_source+"\t");    // source
        else
    
          this_buff.append("chado\t");
        
        if(typeName.equals("exon"))
          this_buff.append(EXONMODEL + "\t");   // type
        else
          this_buff.append(typeName + "\t");    // type
    
    tjc's avatar
    tjc committed
        this_buff.append(fmin + "\t");          // start
        this_buff.append(fmax + "\t");          // end
        this_buff.append(".\t");                // score
    
        if(strand.equals( new Short((short)-1)) )                        // strand
    
    tjc's avatar
    tjc committed
          this_buff.append("-\t");
    
        else if(strand.equals( new Short((short)1)) )
    
    tjc's avatar
    tjc committed
          this_buff.append("+\t");
        else
          this_buff.append(".\t");
    
    
        if(phase == null)
    
    tjc's avatar
    tjc committed
          this_buff.append(".\t");               // phase
        else
          this_buff.append(phase+"\t"); 
    
        this_buff.append("ID=" + name + ";");
    
        this_buff.append("feature_id=" + featureId.toString() + ";");
    
        
        if(feat.getName() != null)
          this_buff.append("Name=" + feat.getName() + ";");
    
    tjc's avatar
    tjc committed
       
        if(parent_id != null && !parent_id.equals("0"))
        {
          if(parent_relationship.equals("derives_from"))
            this_buff.append("Derives_from=" + parent_id + ";");
    
    tjc's avatar
    tjc committed
          else
    
    tjc's avatar
    tjc committed
            this_buff.append("Parent=" + parent_id + ";");
        }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        this_buff.append("timelastmodified=" + timelastmodified + ";");
    
    tjc's avatar
    tjc committed
        this_buff.append("isObsolete=" + Boolean.toString(feat.isObsolete()) + ";");
    
    tjc's avatar
    tjc committed
        
        // this is the chado feature_relationship.rank used
    
    tjc's avatar
    tjc committed
        // to order joined features e.g. exons
    
    tjc's avatar
    tjc committed
        if(rank > -1)
    
    tjc's avatar
    tjc committed
          this_buff.append("feature_relationship_rank="+rank+";"); 
    
    tjc's avatar
    tjc committed
    
    
        //this_buff.append("feature_id="+feature_id+";");
        
    
    tjc's avatar
    tjc committed
        // attributes
    
        if(feat.getFeatureProps() != null &&
           feat.getFeatureProps().size() > 0)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          Collection featureprops = feat.getFeatureProps();
          Iterator it = featureprops.iterator();
          
          while(it.hasNext())
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            FeatureProp featprop = (FeatureProp)it.next();
    
            String qualifier_name = getCvtermName(featprop.getCvTerm().getCvTermId(), dao, gene_builder);
    
    tjc's avatar
    tjc committed
            if(qualifier_name == null)
              continue;
    
    tjc's avatar
    tjc committed
            if(featprop.getValue() != null)
    
    tjc's avatar
    tjc committed
              this_buff.append(GFFStreamFeature.encode(qualifier_name)+ "=" +
    
    tjc's avatar
    tjc committed
                               GFFStreamFeature.encode(featprop.getValue())+";");
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(clusterOrthoParalog != null)
          this_buff.append(clusterOrthoParalog);
        
    
    tjc's avatar
    tjc committed
        // append dbxrefs
    
    tjc's avatar
    tjc committed
        boolean foundPrimaryDbXRef = false;
        if(feat.getDbXRef() != null)
    
    tjc's avatar
    tjc committed
        {
          this_buff.append("Dbxref=");
    
    tjc's avatar
    tjc committed
          this_buff.append(GFFStreamFeature.encode(
              feat.getDbXRef().getDb().getName()+":"+feat.getDbXRef().getAccession()));
          foundPrimaryDbXRef = true;
          if(dbxref == null || dbxref.size() == 0)
            this_buff.append(";");
        }
        
        if(dbxref != null && dbxref.size() > 0)
        {
          if(foundPrimaryDbXRef)
            this_buff.append(",");
          else
            this_buff.append("Dbxref=");
    
    tjc's avatar
    tjc committed
          for(int j=0; j<dbxref.size(); j++)
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            this_buff.append(GFFStreamFeature.encode((String)dbxref.get(j)));
    
    tjc's avatar
    tjc committed
            if(j<dbxref.size()-1)
              this_buff.append(",");
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
          this_buff.append(";");
        }
        
        // append synonyms
        if(synonym != null &&
    
           synonym.containsKey(featureId))
    
    tjc's avatar
    tjc committed
        {   
    
    tjc's avatar
    tjc committed
          FeatureSynonym alias;
    
          Vector v_synonyms = (Vector)synonym.get(featureId);
    
    tjc's avatar
    tjc committed
          for(int j=0; j<v_synonyms.size(); j++)
          {
    
    tjc's avatar
    tjc committed
            alias = (FeatureSynonym)v_synonyms.get(j);
    
    tjc's avatar
    tjc committed
            
    
            this_buff.append( getCvtermName(alias.getSynonym().getCvTerm().getCvTermId(), dao, gene_builder) + "=" );
    
    tjc's avatar
    tjc committed
            //this_buff.append(alias.getSynonym().getCvterm().getName()+"=");
    
    tjc's avatar
    tjc committed
            this_buff.append(alias.getSynonym().getName());
            
    
            if(!alias.isCurrent())
              this_buff.append(GFFStreamFeature.encode(";current=false"));
            
    
    tjc's avatar
    tjc committed
            //if(j<v_synonyms.size()-1)
            this_buff.append(";");
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        // /literature
        if(featurePubs != null &&
           featurePubs.containsKey(featureId))
        {
          FeaturePub featurePub;
          Vector v_featurePubs = (Vector)featurePubs.get(featureId);
          for(int j=0; j<v_featurePubs.size(); j++)
          {
            featurePub = (FeaturePub)v_featurePubs.get(j);
            this_buff.append( "literature=" );
            this_buff.append(featurePub.getPub().getUniqueName());
            this_buff.append(";");
          }
        }
        
    
    tjc's avatar
    tjc committed
        // GO, controlled_curation, product
    
        if(featureCvTerms != null && 
    
           featureCvTerms.containsKey(featureId))
    
        {
          FeatureCvTerm feature_cvterm;
    
          Vector v_feature_cvterms = (Vector)featureCvTerms.get(featureId);
    
          for(int j=0; j<v_feature_cvterms.size(); j++)
          {
            feature_cvterm = (FeatureCvTerm)v_feature_cvterms.get(j);
            
    
    tjc's avatar
    tjc committed
            Integer featureCvTermId = new Integer( feature_cvterm.getFeatureCvTermId() );
            
    
    tjc's avatar
    tjc committed
            List featureCvTermDbXRefList = null;
    
    tjc's avatar
    tjc committed
            
    
    tjc's avatar
    tjc committed
            if(featureCvTermDbXRefs != null)
              featureCvTermDbXRefList = (List)featureCvTermDbXRefs.get(featureCvTermId);
            
            List featureCvTermPubList = null;
            
            if(featureCvTermPubs != null)
              featureCvTermPubList = (List)featureCvTermPubs.get(featureCvTermId);
    
    tjc's avatar
    tjc committed
            appendControlledVocabulary(this_buff, dao, feature_cvterm,
    
                                       featureCvTermDbXRefList,featureCvTermPubList, pubDbXRefs, gene_builder);
    
    tjc's avatar
    tjc committed
          }
          //System.out.println(new String(this_buff.getBytes()));
        }
          
        this_buff.append("\n");
      }
      
      /**
       * Appends controlled vocabulary terms to the buffer
       * @param attr_buff
       * @param dao
       * @param feature_cvterm
       * @param featureCvTermDbXRef
       */
      public static void appendControlledVocabulary(final ByteBuffer attr_buff,
                                              final GmodDAO dao,
                                              final FeatureCvTerm feature_cvterm,
                                              final List featureCvTermDbXRefs,
    
    tjc's avatar
    tjc committed
                                              final List featureCvTermPubs,
    
                                              final List pubDbXRefs,
                                              final boolean gene_builder)
    
    tjc's avatar
    tjc committed
      {
    
        CvTerm cvterm =  getCvTerm( feature_cvterm.getCvTerm().getCvTermId(), dao, gene_builder);
    
    tjc's avatar
    tjc committed
        DbXRef dbXRef = feature_cvterm.getCvTerm().getDbXRef();
        
        if(cvterm.getCv().getName().startsWith(DatabaseDocument.CONTROLLED_CURATION_TAG_CVNAME))
        {
          attr_buff.append("controlled_curation=");
          
    
          attr_buff.append("term="+
              GFFStreamFeature.encode(feature_cvterm.getCvTerm().getName())+"%3B");
          attr_buff.append("cv="+
              GFFStreamFeature.encode(feature_cvterm.getCvTerm().getCv().getName())+"%3B");   
    
    tjc's avatar
    tjc committed
          
          // N.B. the db_xref may be a FeatureCvTermDbXRef or a Pub for /controlled_curation
          int nfound_dbxref = 0;
          if(feature_cvterm.getPub().getUniqueName() != null &&
    
    tjc's avatar
    tjc committed
             !feature_cvterm.getPub().getUniqueName().equalsIgnoreCase("NULL"))
    
    tjc's avatar
    tjc committed
          {
            // PMID
            Pub pub = feature_cvterm.getPub();
            
            // internal check
            checkPubDbXRef(pubDbXRefs, pub.getPubId(), pub, feature_cvterm);
            
            attr_buff.append("db_xref="+ pub.getUniqueName());
            nfound_dbxref++;
          }
          
          if(featureCvTermDbXRefs != null &&
                  featureCvTermDbXRefs.size() > 0)
          {
            for(int i=0; i<featureCvTermDbXRefs.size(); i++)
    
    tjc's avatar
    tjc committed
              FeatureCvTermDbXRef featureCvTermDbXRef =
                (FeatureCvTermDbXRef)featureCvTermDbXRefs.get(i);
        
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermDbXRef.getFeatureCvTerm().getFeatureCvTermId())
                continue;
          
              if(nfound_dbxref == 0)
                attr_buff.append("db_xref=");
    
    tjc's avatar
    tjc committed
              else if(nfound_dbxref > 0)
    
    tjc's avatar
    tjc committed
                attr_buff.append("|");
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              DbXRef fc_dbXRef = featureCvTermDbXRef.getDbXRef();
              attr_buff.append(fc_dbXRef.getDb().getName()+":");
              attr_buff.append(fc_dbXRef.getAccession());
              nfound_dbxref++;
    
    tjc's avatar
    tjc committed
            }
    
    tjc's avatar
    tjc committed
          }
          
          if(nfound_dbxref > 0)
            attr_buff.append("%3B");
          
          List feature_cvtermprops = (List) feature_cvterm.getFeatureCvTermProps();
          for(int i = 0; i < feature_cvtermprops.size(); i++)
          {
            FeatureCvTermProp feature_cvtermprop = 
              (FeatureCvTermProp)feature_cvtermprops.get(i);
            attr_buff.append(getCvtermName(feature_cvtermprop.getCvTerm()
    
                .getCvTermId(), dao, gene_builder));
    
    tjc's avatar
    tjc committed
            attr_buff.append("=");
    
            attr_buff.append(GFFStreamFeature.encode(feature_cvtermprop.getValue()));
    
    tjc's avatar
    tjc committed
            if(i < feature_cvtermprops.size()-1)
              attr_buff.append("%3B");
          }
          
          attr_buff.append(";");
        }
        else if(cvterm.getCv().getName().equals(DatabaseDocument.PRODUCTS_TAG_CVNAME))
        {
          attr_buff.append("product=");
    
          attr_buff.append(GFFStreamFeature.encode(feature_cvterm.getCvTerm().getName())+";");
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        else if(cvterm.getCv().getName().equals(DatabaseDocument.RILEY_TAG_CVNAME))
        {
    
    tjc's avatar
    tjc committed
          // class include the cvTermId as a convenience for looking up the term
    
    tjc's avatar
    tjc committed
          attr_buff.append("class=");
    
    tjc's avatar
    tjc committed
          attr_buff.append(dbXRef.getAccession()+"::"+
                          feature_cvterm.getCvTerm().getCvTermId()+";");
    
    tjc's avatar
    tjc committed
        else
        {
          attr_buff.append("GO=");
    
    tjc's avatar
    tjc committed
          if(cvterm.getCv().getName().equals("molecular_function"))
            attr_buff.append("aspect=F%3B");
          else if(cvterm.getCv().getName().equals("cellular_component"))
            attr_buff.append("aspect=C%3B");
          else if(cvterm.getCv().getName().equals("biological_process"))
            attr_buff.append("aspect=P%3B");
    
    tjc's avatar
    tjc committed
          if(feature_cvterm.isNot())
            attr_buff.append("qualifier=NOT%3B");
    
    tjc's avatar
    tjc committed
          attr_buff.append("GOid="+dbXRef.getDb().getName() + ":"
              + dbXRef.getAccession() + "%3B");
          
    
    tjc's avatar
    tjc committed
          attr_buff.append("term="+
              GFFStreamFeature.encode(feature_cvterm.getCvTerm().getName())+"%3B");
    
    tjc's avatar
    tjc committed
          
          // PMID
    
    tjc's avatar
    tjc committed
          int nfound_pub = 0;
    
    tjc's avatar
    tjc committed
          if(feature_cvterm.getPub() != null &&
             feature_cvterm.getPub().getUniqueName() != null &&
    
    tjc's avatar
    tjc committed
             !feature_cvterm.getPub().getUniqueName().equalsIgnoreCase("NULL"))
    
    tjc's avatar
    tjc committed
          {
            Pub pub = feature_cvterm.getPub();
            attr_buff.append("db_xref="+
    
    tjc's avatar
    tjc committed
                pub.getUniqueName());
            nfound_pub++;
          }
          
          if(featureCvTermPubs != null &&
              featureCvTermPubs.size() > 0)
          {
            for(int i=0; i<featureCvTermPubs.size(); i++)
            {
              FeatureCvTermPub featureCvTermPub =
                (FeatureCvTermPub)featureCvTermPubs.get(i);
              
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermPub.getFeatureCvTerm().getFeatureCvTermId())
                continue;
              
              if(nfound_pub == 0)
                attr_buff.append("db_xref=");
    
    tjc's avatar
    tjc committed
              else if(nfound_pub > 0)
    
    tjc's avatar
    tjc committed
                attr_buff.append("|");
    
              attr_buff.append(featureCvTermPub.getPub().getUniqueName());
              nfound_pub++;
            }
    
    tjc's avatar
    tjc committed
          }
          
    
    tjc's avatar
    tjc committed
          if(nfound_pub > 0)
            attr_buff.append("%3B");
          
    
    tjc's avatar
    tjc committed
          if(featureCvTermDbXRefs != null &&
              featureCvTermDbXRefs.size() > 0 )
          {  
            int nfound = 0;
            for(int i=0; i<featureCvTermDbXRefs.size(); i++)
            {
              FeatureCvTermDbXRef featureCvTermDbXRef =
                (FeatureCvTermDbXRef)featureCvTermDbXRefs.get(i);
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermDbXRef.getFeatureCvTerm().getFeatureCvTermId())
    
    tjc's avatar
    tjc committed
              {
    
    tjc's avatar
    tjc committed
                continue;
    
    tjc's avatar
    tjc committed
              }
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              if(nfound == 0)
                attr_buff.append("with=");
              else if(nfound > 1)
                attr_buff.append("|");
              
              DbXRef fc_dbXRef = featureCvTermDbXRef.getDbXRef();
              attr_buff.append(fc_dbXRef.getDb().getName()+":");
              attr_buff.append(fc_dbXRef.getAccession());
              nfound++;
    
    tjc's avatar
    tjc committed
            
            if(nfound > 0)
              attr_buff.append("%3B");
    
    tjc's avatar
    tjc committed
          List feature_cvtermprops = (List)feature_cvterm
              .getFeatureCvTermProps();
          for(int i = 0; i < feature_cvtermprops.size(); i++)
          {
            FeatureCvTermProp feature_cvtermprop = 
              (FeatureCvTermProp)feature_cvtermprops.get(i);
    
            
            if(feature_cvtermprop.getValue() == null)
              continue;
            
    
    tjc's avatar
    tjc committed
            attr_buff.append(getCvtermName(feature_cvtermprop.getCvTerm()
    
                .getCvTermId(), dao, gene_builder));
    
    tjc's avatar
    tjc committed
            attr_buff.append("=");
    
            attr_buff.append(GFFStreamFeature.encode(feature_cvtermprop.getValue()));
    
    tjc's avatar
    tjc committed
            if(i < feature_cvtermprops.size()-1)
              attr_buff.append("%3B");
          }
          
          attr_buff.append(";");
        }
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      /**
       * Check the PubDbXref contains the Pub in FeatureCvTerm
       * @param pubDbXRefs
       * @param pubId
       * @param pub
       * @param feature_cvterm
       */
      private static void checkPubDbXRef(final List pubDbXRefs, final int pubId,
                                         final Pub pub, final FeatureCvTerm feature_cvterm)
      {
        PubDbXRef pubDbXRef = null;
        for(int i = 0; i < pubDbXRefs.size(); i++)
        {
          pubDbXRef = (PubDbXRef) pubDbXRefs.get(i);
          if(pubDbXRef.getPub().getPubId() == pubId)
          {
            DbXRef dbxref = pubDbXRef.getDbXRef();
            Splash.logger4j.debug("Checking PubDbXRef and found Pub "+dbxref.getDb().getName()+
                                  ":"+dbxref.getAccession());
            break;
          }
        }
        
        if(pubDbXRef == null || 
            !pub.getUniqueName().endsWith(pubDbXRef.getDbXRef().getAccession()))
         {
           Splash.logger4j.debug("Checking PubDbXRef and not found Pub "+
                               feature_cvterm.getPub().getUniqueName());
          
           JOptionPane.showMessageDialog(null, "Cannot find pub_dbxref for:\n"+
               feature_cvterm.getPub().getUniqueName(), 
               "Database Error",
               JOptionPane.ERROR_MESSAGE);
         }
      }
    
    tjc's avatar
    tjc committed
      /**
       * Look up the cvterm_id for a controlled vocabulary name.
       * @param name  
       * @return
       */
    
      public static Integer getCvtermID(final String name)
    
    tjc's avatar
    tjc committed
      {
    
        Enumeration enum_cvterm = cvterms.keys();
    
    tjc's avatar
    tjc committed
        while(enum_cvterm.hasMoreElements())
        {
    
    tjc's avatar
    tjc committed
          Integer key = (Integer)enum_cvterm.nextElement();
    
          if(name.equalsIgnoreCase( ((CvTerm)cvterms.get(key)).getName() ))
    
    tjc's avatar
    tjc committed
            return key;
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return null;
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Look up a cvterm name from the collection of cvterms.
       * @param id  a cvterm_id  
       * @return    the cvterm name
    
    tjc's avatar
    tjc committed
       */
    
      private static String getCvtermName(final int id, 
                                          final GmodDAO dao,
                                          final boolean gene_builder)
    
        if(gene_builder)
          return dao.getCvTermById(id).getName();
        
        return getCvTerm(id, dao, gene_builder).getName();
    
      private static CvTerm getCvTerm(final int id, 
                                      final GmodDAO dao,
                                      final boolean gene_builder)
    
    tjc's avatar
    tjc committed
      {
    
        if(gene_builder)
          return dao.getCvTermById(id);
    
        if(cvterms == null)
    
          getCvterms(dao);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        return (CvTerm)cvterms.get(new Integer(id));
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      
    
    tjc's avatar
    tjc committed
      /**
       * Use the CvTerm name to return a CvTerm.
       * @param cvTermId
       * @return
       */
    
      public static CvTerm getCvTermByCvTermName(final String cvterm_name)
    
    tjc's avatar
    tjc committed
      {
        Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
    
          if(cvterm_name.equalsIgnoreCase( cvterm.getName() ))
    
    tjc's avatar
    tjc committed
            return cvterm;
        }
        
        return null;
      }
    
    tjc's avatar
    tjc committed
      /**
       * Use the CvTermId to return a CvTerm.
       * @param cvTermId
       * @return
       */
    
      public static CvTerm getCvTermByCvTermId(final int cvTermId,
                                               final uk.ac.sanger.artemis.io.Feature feature)
    
        if(cvterms == null)
        {
          try
          {
            DatabaseDocument doc =
              (DatabaseDocument) ((DocumentEntry)feature.getEntry()).getDocument();
            return doc.getDAO().getCvTermById(cvTermId);
          }
          catch(ConnectException e)
          { 
            logger4j.warn(e.getMessage()); 
          }
          catch(SQLException e) 
          { 
            logger4j.warn(e.getMessage()); 
          }
        }
        
    
    tjc's avatar
    tjc committed
        Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
          if(cvterm.getCvTermId() == cvTermId)
            return cvterm;
        }
        
        return null;
      }
      
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Use the Cv name and CvTerm name to return a CvTerm.
    
    tjc's avatar
    tjc committed
       * @param cvterm_name
       * @param cvName
       * @return
       */
    
    tjc's avatar
    tjc committed
      public static CvTerm getCvTermByCvAndCvTerm(final String cvterm_name,
                                                  final String cvName)
      {
    
    tjc's avatar
    tjc committed
        final Enumeration enum_cvterm = cvterms.elements();
    
    tjc's avatar
    tjc committed
        while(enum_cvterm.hasMoreElements())
        {
    
    tjc's avatar
    tjc committed
          final CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
    
    tjc's avatar
    tjc committed
          if(cvName.equals( cvterm.getCv().getName() ) &&
             cvterm_name.equals( cvterm.getName() ))
            return cvterm;
        }
        return null;
      }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      /**
       * This is different from getCvTermByCvAndCvTerm as it makes sure
       * the Cv name part matches the name supplied to the function, i.e.
       * by matching just the start.
       * @param cvterm_name
       * @param cvName
       * @return
       */
      public static CvTerm getCvTermByCvPartAndCvTerm(final String cvterm_name,
                                                  final String cvName)
      {
        Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
          if(cvterm.getCv().getName().startsWith( cvName ) &&
             cvterm_name.equals( cvterm.getName() ))
            return cvterm;
        }
        return null;
      }
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Look up cvterms names and id and return in a hashtable.
    
    tjc's avatar
    tjc committed
       * @param dao the data access object
       * @return    the cvterm <code>Hashtable</code>
    
    tjc's avatar
    tjc committed
       */
    
      private static Hashtable getCvterms(final GmodDAO dao)
    
    tjc's avatar
    tjc committed
      {
        try
        {
    
    tjc's avatar
    tjc committed
          final List cvterm_list = dao.getCvTerms();
          final Iterator it = cvterm_list.iterator();
          cvterms = new Hashtable(cvterm_list.size());
    
    tjc's avatar
    tjc committed
    
          while(it.hasNext())
          {
    
    tjc's avatar
    tjc committed
            final CvTerm cvterm = (CvTerm)it.next();
    
    tjc's avatar
    tjc committed
            cvterms.put(new Integer(cvterm.getCvTermId()), cvterm);
    
    tjc's avatar
    tjc committed
          }
        }
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          System.err.println("SQLException retrieving CvTerms");
    
    tjc's avatar
    tjc committed
          System.err.println(sqle);
        }
    
    tjc's avatar
    tjc committed
        logger4j.debug("LOADED CvTerms");
        
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      
    
    tjc's avatar
    tjc committed
      /**
       * Get CvTerm's in a given CV
       * @param cvName
       * @return
       */
      public Vector getCvTermsByCvName(final String cvName)
      {
        if(cvterms == null)
    
        {
          logger4j.debug("getCvTermsByCvName LOADING CVTERMS");
    
    tjc's avatar
    tjc committed
          getCvterms(getDAOOnly());
    
    tjc's avatar
    tjc committed
        
        return getCvterms("", cvName);
      }
      
    
    tjc's avatar
    tjc committed
      public List getDatabaseNames()
      {
    
    tjc's avatar
    tjc committed
        GmodDAO dao = getDAOOnly();
        List dbs = dao.getDbs();
        List names = new Vector();
        Iterator it = dbs.iterator();
        while(it.hasNext())
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          Db db = (Db)it.next();
          names.add(db.getName());
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return names;
      }
      
      public List getOrganismNames()
      {
    
    tjc's avatar
    tjc committed
        if(organismNames != null && organismNames.size() > 0)
          return organismNames;
        
    
    tjc's avatar
    tjc committed
        GmodDAO dao = getDAOOnly();
        List organisms = dao.getOrganisms();
    
    tjc's avatar
    tjc committed
        organismNames = new Vector();
    
    tjc's avatar
    tjc committed
        Iterator it = organisms.iterator();
        while(it.hasNext())
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          Organism organism = (Organism)it.next();
          organismNames.add(organism.getCommonName());
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return organismNames;
    
    tjc's avatar
    tjc committed
      }
      
    
    tjc's avatar
    tjc committed
      public static Vector getCvterms(final String search_str, final String cv_name)
      {
        final Vector cvterm_match = new Vector();
        
        Enumeration enum_cvterm = cvterms.keys();
        while(enum_cvterm.hasMoreElements())
        {
          Integer key = (Integer)enum_cvterm.nextElement();
          CvTerm cvterm = (CvTerm)cvterms.get(key);
          
          if(cvterm.getCv().getName().startsWith(cv_name))
          {
            if(cvterm.getName().indexOf(search_str) > -1)
              cvterm_match.add(cvterm);
          }
        }
        
        return cvterm_match;
      }
    
      
      /**
       * Get a list of the CV names
       * @return
       */
      public static List getCvControledCurationNames()
      {
        if(cvControledCuratioNames != null)
          return cvControledCuratioNames;
        cvControledCuratioNames = new Vector();
        final Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          final CvTerm cvTerm = (CvTerm)enum_cvterm.nextElement();
          final String cvNameStr = cvTerm.getCv().getName();
          
          if(cvNameStr.startsWith(DatabaseDocument.CONTROLLED_CURATION_TAG_CVNAME) && 
             !cvControledCuratioNames.contains(cvNameStr))
            cvControledCuratioNames.add(cvNameStr);
        }
        
        return cvControledCuratioNames;
      }
    
    tjc's avatar
    tjc committed
    
    
      /**
       * Look up synonym type names e.g. synonym, systematic_id.
       * @return    the synonym tag names
       */
    
      public static String[] getSynonymTypeNames(final String cv_name, 
                                                 final GFFStreamFeature feature)
    
        if(cvterms == null)
        {
          DatabaseDocument doc = (DatabaseDocument)feature.getDocumentEntry().getDocument();
          try
          {
    
            Cv cv = new Cv();
            cv.setName(cv_name);
            List synonymCvTerms = doc.getDAO().getCvTermByNameInCv(null, cv);
            String synonymNames[] = new String[synonymCvTerms.size()];
            for(int i=0; i<synonymCvTerms.size(); i++)
              synonymNames[i] = ((CvTerm) synonymCvTerms.get(i)).getName();
    
            return synonymNames;
    
          }
          catch(ConnectException e){}
          catch(SQLException e){}   
        }
        
    
        Vector synonym_names = new Vector();
        Enumeration cvterm_enum = cvterms.elements();
        while(cvterm_enum.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)cvterm_enum.nextElement();
          if(cvterm.getCv().getName().equals(cv_name))
            synonym_names.add(cvterm.getName());
        }
        
        return (String[])synonym_names.toArray(
                           new String[synonym_names.size()]);
      }
      
    
    tjc's avatar
    tjc committed
      public void insertCvTerm(CvTerm cvTerm)
      {
        final GmodDAO dao = getDAOOnly();
        dao.persist(cvTerm);
        cvTerm = dao.getCvTermByNameAndCvName(cvTerm.getName(), cvTerm.getCv().getName());
        cvterms.put(new Integer(cvTerm.getCvTermId()), cvTerm);
      }
    
    tjc's avatar
    tjc committed
      /**
       * Get the sequence for a feature.
       * @param dao   the data access object
       * @param buff  the buffer to add the sequence to
       * @return      the resulting buffer
       * @throws java.sql.SQLException
       */
    
    tjc's avatar
    tjc committed
      private Feature getChadoSequence(GmodDAO dao, ByteBuffer buff)
    
    tjc's avatar
    tjc committed
      {
    
        Feature feature = dao.getFeatureById(Integer.parseInt(srcFeatureId));
    
    tjc's avatar
    tjc committed
        getChadoSequence(feature, buff);
        return feature;
      }
      
      
      /**
       * Get the sequence for a feature.
       * @param dao   the data access object
       * @param buff  the buffer to add the sequence to
       * @return      the resulting buffer
       * @throws java.sql.SQLException
       */
      private void getChadoSequence(final Feature feature, ByteBuffer buff)
      {
    
    tjc's avatar
    tjc committed
        buff.append("##FASTA\n>");
    
    tjc's avatar
    tjc committed
        buff.append(feature.getUniqueName());
    
    tjc's avatar
    tjc committed
        buff.append("\n");
    
    tjc's avatar
    tjc committed
        buff.append(feature.getResidues());
    
    tjc's avatar
    tjc committed
      }