Esempio n. 1
0
IDType SyntenyRegionAdaptor_store(SyntenyRegionAdaptor *sra, IDType clusterId, SyntenyRegion *region) {
  IDType regionId = 0;
  StatementHandle *sth;
  char qStr[1024];
  
  if (!region) {
    fprintf(stderr, "Error: region is not a SyntenyRegion\n");
  } else {
    sprintf(qStr, "insert into synteny_region (synteny_cluster_id,dnafrag_id,seq_start,seq_end)"
            " VALUES (" IDFMTSTR ", " IDFMTSTR ", %d, %d)", 
            clusterId,
            SyntenyRegion_getDNAFragId(region),
            SyntenyRegion_getSeqStart(region),
            SyntenyRegion_getSeqEnd(region));
    sth = sra->prepare((BaseAdaptor *)sra, qStr, strlen(qStr));
   
    sth->execute(sth);

    regionId = sth->getInsertId(sth);
   
    SyntenyRegion_setDbID(region, regionId);
    SyntenyRegion_setAdaptor(region, sra);
  }
   
  return regionId;
}
// This method was a mess - tidied and rearranged
IDType IntronSupportingEvidenceAdaptor_store(IntronSupportingEvidenceAdaptor *isea, IntronSupportingEvidence *sf)  {
  if (sf == NULL) {
    fprintf(stderr,"sf is NULL in IntronSupportingEvidenceAdaptor_store\n");
    exit(1);
  }

  Class_assertType(CLASS_INTRONSUPPORTINGEVIDENCE, sf->objectType);
  
  DBAdaptor *db = isea->dba;
  AnalysisAdaptor *analysisAdaptor = DBAdaptor_getAnalysisAdaptor(db);
  
  if (IntronSupportingEvidence_isStored(sf, db)) {
    fprintf(stderr,"ISE already stored\n");
    return IntronSupportingEvidence_getDbID(sf);
  }
  
  Analysis *analysis = IntronSupportingEvidence_getAnalysis(sf);

  if (!Analysis_isStored(analysis, db)) {
    AnalysisAdaptor_store(analysisAdaptor, analysis);
  }
  IDType analysisId = Analysis_getDbID(analysis);

  // Think the above is equivalent to this horror
  //my $analysis_id = $analysis->is_stored($db) ? $analysis->dbID() : $db->get_AnalysisAdaptor()->store($analysis);
  
/* No transfer (see GeneAdaptor for why)
  my $seq_region_id;
  ($sf, $seq_region_id) = $self->_pre_store($sf);
*/

  IDType seqRegionId = BaseFeatureAdaptor_preStore((BaseFeatureAdaptor *)isea, (SeqFeature*)sf);

  char qStr[1024];
  sprintf(qStr, "INSERT IGNORE INTO intron_supporting_evidence "
                "(analysis_id, seq_region_id, seq_region_start, seq_region_end, seq_region_strand, hit_name, score, score_type, is_splice_canonical) "
                "VALUES ("IDFMTSTR","IDFMTSTR",%ld,%ld,%d,'%s',%f,'%s',%d)", 
                analysisId,
                seqRegionId,
          IntronSupportingEvidence_getSeqRegionStart((SeqFeature*)sf),
          IntronSupportingEvidence_getSeqRegionEnd((SeqFeature*)sf),
          IntronSupportingEvidence_getSeqRegionStrand((SeqFeature*)sf),
                IntronSupportingEvidence_getHitName(sf),
                IntronSupportingEvidence_getScore(sf),
                IntronSupportingEvidence_getScoreType(sf),
                IntronSupportingEvidence_getIsSpliceCanonical(sf));

  StatementHandle *sth = isea->prepare((BaseAdaptor *)isea,qStr,strlen(qStr));
  
  sth->execute(sth);
  IDType sfId = sth->getInsertId(sth);
  sth->finish(sth);

  if (!sfId) {
    sprintf(qStr,"SELECT intron_supporting_evidence_id "
                   "FROM intron_supporting_evidence "
                  "WHERE analysis_id = "IDFMTSTR
                   " AND seq_region_id = "IDFMTSTR
                   " AND seq_region_start = %ld"
                   " AND seq_region_end = %ld" 
                   " AND seq_region_strand = %d"
                   " AND hit_name = '%s'",
                analysisId,
                seqRegionId,
            IntronSupportingEvidence_getSeqRegionStart((SeqFeature*)sf),
            IntronSupportingEvidence_getSeqRegionEnd((SeqFeature*)sf),
            IntronSupportingEvidence_getSeqRegionStrand((SeqFeature*)sf),
                IntronSupportingEvidence_getHitName(sf));

    sth = isea->prepare((BaseAdaptor *)isea,qStr,strlen(qStr));
    sth->execute(sth);
    if (sth->numRows(sth) > 0) {
      ResultRow *row = sth->fetchRow(sth);
      sfId = row->getLongLongAt(row, 0);
    }
    sth->finish(sth);
  }
  
  IntronSupportingEvidence_setAdaptor((SeqFeature*)sf, (BaseAdaptor*)isea);
  IntronSupportingEvidence_setDbID(sf, sfId);

  return IntronSupportingEvidence_getDbID(sf);
}
Esempio n. 3
0
IDType DBEntryAdaptor_store(DBEntryAdaptor *dbea, DBEntry *exObj, 
                            IDType ensObject, char *ensType, int ignoreRelease) {
  fprintf(stderr,"DBEntryAdaptor_store does not implement ignoreRelease functionality yet\n");

  char qStr[512];
  StatementHandle *sth;
  ResultRow *row;
  IDType dbRef;
  IDType dbX;

  //
  // Check for the existance of the external_db, throw if it does not exist
  //
  sprintf(qStr,
     "SELECT external_db_id"
     "  FROM external_db"
     " WHERE db_name = '%s'"
     "   AND db_release = %s",
     DBEntry_getDbName(exObj),
     DBEntry_getRelease(exObj));

  sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));
  sth->execute(sth);
    
  row = sth->fetchRow(sth);
  if( row == NULL ) {
    sth->finish(sth);
    fprintf(stderr,"Error: external_db [%s] release [%s] does not exist\n", 
            DBEntry_getDbName(exObj), DBEntry_getRelease(exObj));
    exit(1);
  }

  dbRef =  row->getLongLongAt(row,0);
  sth->finish(sth);
    
  //
  // Check for the existance of the external reference, add it if not present
  //
  sprintf(qStr,
       "SELECT xref_id"
       "  FROM xref"
       " WHERE external_db_id = " IDFMTSTR
       "   AND dbprimary_acc = '%s'"
       "   AND version = %s",
      dbRef,
      DBEntry_getPrimaryId(exObj),
      DBEntry_getVersion(exObj));

  sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));
  sth->execute(sth);

  row = sth->fetchRow(sth);
    
  if (row != NULL) {
    dbX =  row->getLongLongAt(row,0);
    sth->finish(sth);
  } else {
    //
    // store the new xref
    //

    // First finish the old sth
    sth->finish(sth);

// NIY Handling NULL values
    sprintf(qStr,
       "INSERT ignore INTO xref"
       " SET dbprimary_acc = '%s',"
       "    display_label = '%s',"
       "    version = %s,"
       "    description = '%s',"
       "    external_db_id = " IDFMTSTR,
       DBEntry_getPrimaryId(exObj),
       DBEntry_getDisplayId(exObj),
       DBEntry_getVersion(exObj),
       DBEntry_getDescription(exObj),
       dbRef
      );
    sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));

    sth->execute(sth);
    dbX = sth->getInsertId(sth);

    sth->finish(sth);
	
    //
    // store the synonyms for the new xref
    // 
    if (DBEntry_getAllSynonyms(exObj)) {
      StatementHandle *checkSth;
      StatementHandle *storeSth;
      int i;
      Vector *synonyms;

      sprintf(qStr,
              "SELECT xref_id, synonym"
              " FROM external_synonym"
              " WHERE xref_id = %" IDFMTSTR
              " AND synonym = '%%s'");

      checkSth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));

      sprintf(qStr,
        "INSERT ignore INTO external_synonym"
        " SET xref_id = %" IDFMTSTR ", synonym = '%%s'");     

      storeSth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));

      synonyms = DBEntry_getAllSynonyms(exObj);

      for (i=0;i<Vector_getNumElement(synonyms); i++) {	    
        char *syn = Vector_getElementAt(synonyms,i);
        checkSth->execute(checkSth, dbX, syn);
        row = checkSth->fetchRow(checkSth);
        if (!row) {
          storeSth->execute(storeSth, dbX, syn);
        }
      }
  	
      checkSth->finish(checkSth);
      storeSth->finish(storeSth);
    }
  }

  //
  // check if the object mapping was already stored
  //
  sprintf(qStr,
           "SELECT xref_id"
           " FROM object_xref"
           " WHERE xref_id = " IDFMTSTR
           " AND   ensembl_object_type = '%s'"
           " AND   ensembl_id = " IDFMTSTR,
         dbX, ensType, ensObject);

  sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));

  sth->execute(sth);

  row = sth->fetchRow(sth);
// NOTE row will be invalid after this call but will still
//      indicate whether something was found
  sth->finish(sth);
    
  if (!row) {
    IDType Xidt;

    //
    // Store the reference to the internal ensembl object
    //
    sprintf(qStr,
         "INSERT ignore INTO object_xref"
         " SET xref_id = " IDFMTSTR ","
         "     ensembl_object_type = '%s',"
         "     ensembl_id = " IDFMTSTR,
        dbX, ensType, ensObject);

    sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));
	
    sth->execute(sth);
    DBEntry_setDbID(exObj, dbX);
    DBEntry_setAdaptor(exObj, (BaseAdaptor *)dbea);
      
    Xidt = sth->getInsertId(sth);

    //
    // If this is an IdentityXref need to store in that table too
    //
    if (DBEntry_getIdentityXref(exObj)) {
      IdentityXref *idx = DBEntry_getIdentityXref(exObj);
      sprintf(qStr,
             "INSERT ignore INTO identity_xref"
             " SET object_xref_id = " IDFMTSTR ","
             "     query_identity = %f,"
             "     target_identity = %f",
             Xidt, 
             IdentityXref_getQueryIdentity(idx),
             IdentityXref_getTargetIdentity(idx));

      sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr));
      sth->execute(sth);
      sth->finish(sth);
    }
  } 
  return dbX;    
}