Beispiel #1
0
Vector *HomologyAdaptor_fetchHomologuesOfGene(HomologyAdaptor *ha, char *sp, char *gene) {
  char qStr[1024];
  char *species;
  int nRelationship;
  IDType *relationshipIds;
  Vector *genes;
  int i;

  species = StrUtil_copyString(&species,sp,0);
  species = StrUtil_strReplChr(species,'_',' ');

  sprintf(qStr,
            "select grm.gene_relationship_id "
            " from   gene_relationship_member grm, "
            "        genome_db gd "
            " where  gd.genome_db_id = grm.genome_db_id "
            " and    gd.name = '%s' "
            " and    grm.member_stable_id = '%s' "
            " group by grm.gene_relationship_id", species, gene);

  nRelationship = HomologyAdaptor_getRelationships(ha, qStr,&relationshipIds);

  genes = Vector_new();
  for (i=0;i<nRelationship;i++) {
    Vector *homols;
    sprintf(qStr,
               "select   grm.member_stable_id,"
               "         gd.name,"
               "         grm.chromosome,"
               "         grm.chrom_start,"
               "         grm.chrom_end"
               " from    gene_relationship_member grm,"
               "         genome_db gd"
               " where   grm.gene_relationship_id = " IDFMTSTR 
               " and     grm.genome_db_id = gd.genome_db_id "
               " and NOT (grm.member_stable_id = '%s')", relationshipIds[i], gene);

    homols = HomologyAdaptor_getHomologues(ha, qStr);
    Vector_append(genes,homols);
    Vector_free(homols);
  }

  free(relationshipIds);
  free(species);

  return genes;
}
Beispiel #2
0
static bool read_piref(Streader* sr, int32_t index, void* userdata)
{
    rassert(sr != NULL);
    rassert(userdata != NULL);

    Order_list* ol = userdata;

    // Read the Pattern instance reference
    Pat_inst_ref* p = PAT_INST_REF_AUTO;
    if (!Streader_read_piref(sr, p))
        return false;

    // Check if the Pattern instance is already used
    Index_mapping* key = INDEX_MAPPING_AUTO;
    key->p = *p;
    key->ol_index = index;
    if (AAtree_contains(ol->index_map, key))
    {
        Streader_set_error(
                sr,
                "Duplicate occurrence of pattern instance"
                    " [%" PRId16 ", %" PRId16 "]",
                p->pat, p->inst);
        return false;
    }

    // Add the reference to our containers
    if (!Vector_append(ol->pat_insts, p))
    {
        Streader_set_memory_error(
                sr, "Could not allocate memory for order list");
        return false;
    }

    Index_mapping* im = new_Index_mapping(key);
    if (im == NULL || !AAtree_ins(ol->index_map, im))
    {
        memory_free(im);
        Streader_set_memory_error(
                sr, "Could not allocate memory for order list");
        return false;
    }

    return true;
}
Beispiel #3
0
Vector *HomologyAdaptor_fetchHomologuesOfGeneInSpecies(HomologyAdaptor *ha, 
                          char *sp, char *gene, char *hSp) {
  char qStr[1024];
  Vector *genes;
  IDType *relationshipIds;
  int nRelationship;
  int i;
  char *hSpecies;
  char *species;

  species = StrUtil_copyString(&species,sp,0);
  species = StrUtil_strReplChr(species,'_',' ');

  hSpecies = StrUtil_copyString(&hSpecies,hSp,0);
  hSpecies = StrUtil_strReplChr(hSpecies,'_',' ');

  sprintf(qStr,
           "select grm.gene_relationship_id "
           " from   gene_relationship_member grm, "
           "        genome_db gd "
           " where  gd.genome_db_id = grm.genome_db_id "
           " and    gd.name = '%s' "
           " and    grm.member_stable_id = '%s' "
           " group by grm.gene_relationship_id", species, gene);

  nRelationship = HomologyAdaptor_getRelationships(ha,qStr,&relationshipIds);

  genes = Vector_new();
  for (i=0;i<nRelationship;i++) {
    Vector *homols = HomologyAdaptor_fetchHomologuesBySpeciesRelationshipId(ha,hSpecies,relationshipIds[i]);
    Vector_append(genes, homols);
    Vector_free(homols);
  }

  free(relationshipIds);
  free(species);
  free(hSpecies);

  return genes;
}
Beispiel #4
0
// Test your vector here
int main() { 
	Vector* temp = Vector_create(); //create a vector
	
	Vector_append( temp, "a");//1
	Vector_append( temp, "b");//2
	Vector_append( temp, "c");//3
	Vector_append( temp, "d");//4
	Vector_append( temp, NULL);//5
	Vector_append( temp, "f");//6
	Vector_append( temp, "g");//7
	Vector_append( temp, NULL);//8
	Vector_append( temp, "");//9
	Vector_append( temp, "h");//10
	Vector_append( temp, "i");//11
	Vector_append( temp, NULL);//12
	
	Vector_resize( temp, 20 );
	
	if( Vector_size(temp) != 20 || strcmp( Vector_get( temp, 10 ), "i" ) || Vector_get( temp, 15 ) != NULL )
		perror( "something wrong.\n");
	
	//done for append, resize, get, size
	
	Vector_set( temp, 19, "caibi" );
	Vector_insert( temp, 20, "niubi" );
	Vector_insert( temp, 30, "wori" );
	
	if( Vector_size(temp) != 31 || strcmp( Vector_get( temp, 19 ), "caibi" ) || strcmp( Vector_get( temp, 20 ), "niubi" ) ||  Vector_get( temp, 15 ) != NULL || strcmp( Vector_get( temp, 30 ), "wori" ) )
		perror( "something wrong.\n");
	
	Vector_delete( temp, 11 );
	Vector_delete( temp, 27 );
	Vector_delete( temp, 1 );
	Vector_delete( temp, 18 );
	
	if( Vector_size(temp) != 27 || strcmp( Vector_get( temp, 4 ), "f" ) || strcmp( Vector_get( temp, 26 ), "wori") || Vector_get( temp, 18 ) !=NULL || strcmp( Vector_get( temp, 17 ), "caibi") )
		perror( "something wrong.\n");
	
	
	Vector_destroy( temp );

	return 0; 
}
Beispiel #5
0
int
Stack_push(Stack s, void *value)
{
	return Vector_append(&s->vector, 1, value);
}
Beispiel #6
0
Vector *GenomicAlignAdaptor_fetchAllByDNAFragGenomeDB(GenomicAlignAdaptor *gaa,
               DNAFrag *dnaFrag, GenomeDB *targetGenome, int *startP, int *endP, 
               char *alignmentType) {

  Vector *result = NULL;
  GenomeDB *genomeCons;
  IDType methodLinkId;
  GenomeDB *genomeQuery;
  Vector *mergedAligns;
  int ok = 1;

  if (!dnaFrag) {
    fprintf(stderr, "Error: dnaFrag argument must be non NULL\n");
    ok = 0;
  }

  if (ok) {
    methodLinkId = GenomicAlignAdaptor_methodLinkIdByAlignmentType(gaa, alignmentType);

    genomeCons = DNAFrag_getGenomeDB(dnaFrag);
    genomeQuery = targetGenome;
  
    // direct or indirect ??
    if (GenomeDB_hasConsensus(genomeCons, genomeQuery, methodLinkId) ||
        GenomeDB_hasQuery(genomeCons, genomeQuery, methodLinkId)) {
      result = GenomicAlignAdaptor_fetchAllByDNAFragGenomeDBDirect(gaa, 
                                                                   dnaFrag, targetGenome, startP, endP, methodLinkId);
    } else {
      // indirect checks
      Vector *linkedCons  = GenomeDB_linkedGenomesByMethodLinkId(genomeCons, methodLinkId);
      Vector *linkedQuery = GenomeDB_linkedGenomesByMethodLinkId(genomeQuery, methodLinkId);
    
      // there are not many genomes, square effort is cheap
      Vector *linked = Vector_new();
      Vector *set1 = Vector_new();
      mergedAligns = Vector_new();
      int i;

      for (i=0; i<Vector_getNumElement(linkedCons); i++) {
        int j;
        GenomeDB *g1 = Vector_getElementAt(linkedCons, i);
        
        for (j=0; j<Vector_getNumElement(linkedQuery); j++) {
          GenomeDB *g2 = Vector_getElementAt(linkedQuery, i);
          if (g1 == g2) {
            Vector_addElement(linked, g1);
          }
        }
      }
      Vector_free(linkedCons);
      Vector_free(linkedQuery);

      // collect GenomicAligns from all linked genomes
      for (i=0; i<Vector_getNumElement(linked); i++) {
        GenomeDB *g = Vector_getElementAt(linked, i);

        Vector *gres = GenomicAlignAdaptor_fetchAllByDNAFragGenomeDBDirect(gaa, 
                                                                           dnaFrag, g, startP, endP, methodLinkId);
        Vector_append(set1, gres);
        
        Vector_free(gres);
      }

      // go from each dnafrag in the result set to target_genome
      // there is room for improvement here: create start end
      // my %frags = map { $_->query_dnafrag->dbID => $_->query_dnafrag } @$set1;
    

      for (i=0; i<Vector_getNumElement(set1); i++) {
        GenomicAlign *alignA = Vector_getElementAt(set1,i);
        DNAFrag *frag = GenomicAlign_getQueryDNAFrag(alignA);
        int qStart = GenomicAlign_getQueryStart(alignA);
        int qEnd   = GenomicAlign_getQueryEnd(alignA);

        Vector *dres = GenomicAlignAdaptor_fetchAllByDNAFragGenomeDBDirect(gaa,
                                                                           frag, genomeQuery, &qStart, &qEnd, methodLinkId);
        int j;

        for (j=0; j<Vector_getNumElement(dres); j++) {
          GenomicAlign *alignB = Vector_getElementAt(dres,j);
          GenomicAlignAdaptor_addDerivedAlignments(gaa,  mergedAligns, alignA, alignB);
        } 
        Vector_free(dres);
      }
      // NIY freeing
      result = mergedAligns;
    }
  }

  return result;
}
Beispiel #7
0
Vector *GenomicAlignAdaptor_fetchAllByDNAFragGenomeDBDirect( GenomicAlignAdaptor *gaa, 
     DNAFrag *dnaFrag, GenomeDB *targetGenome, int *startP, int *endP, IDType methodLinkId) {
  IDType dnaFragId;
  GenomeDB *genomeDB;
  char *qStr = NULL;
  char tmpStr[512];
  Vector *results;
  StatementHandle *sth;
  int ok = 0;

  if (!dnaFrag) {
    fprintf(stderr, "Error: Input dnafrag must not be NULL\n");
    ok = 0;
  }

  if (ok) {
    // formatting the dnafrag
    dnaFragId = DNAFrag_getDbID(dnaFrag);

    genomeDB = DNAFrag_getGenomeDB(dnaFrag);

    StrUtil_copyString(&qStr,
                       "SELECT gab.consensus_dnafrag_id,"
                       "       gab.consensus_start," 
                       "       gab.consensus_end,"
                       "       gab.query_dnafrag_id," 
                       "       gab.query_start," 
                       "       gab.query_end,"
                       "       gab.query_strand,"
                       "       gab.method_link_id,"
                       "       gab.score,"
                       "       gab.perc_id," 
                       "       gab.cigar_line"
                       " FROM genomic_align_block gab ",0);

    if (targetGenome) {
      qStr = StrUtil_appendString(qStr,", dnafrag d");
    }
    sprintf(tmpStr," WHERE gab.method_link_id = " IDFMTSTR, methodLinkId);
    qStr = StrUtil_appendString(qStr,tmpStr);

    results = Vector_new();

    if (!targetGenome ||
        GenomeDB_hasQuery(genomeDB, targetGenome, methodLinkId)) {
      Vector *qres;

      sprintf(tmpStr," AND gab.consensus_dnafrag_id = " IDFMTSTR, dnaFragId);
      qStr = StrUtil_appendString(qStr, tmpStr);

      if (startP && endP) {
        int lowerBound = *startP - gaa->maxAlignmentLength;
        sprintf(tmpStr,
                " AND gab.consensus_start <= %d"
                " AND gab.consensus_start >= %d"
                " AND gab.consensus_end >= %d", *endP, lowerBound, *startP ) ;
        qStr = StrUtil_appendString(qStr, tmpStr);
      }

      if (targetGenome) {
        sprintf(tmpStr,
                " AND gab.query_dnafrag_id = d.dnafrag_id"
                " AND d.genome_db_id = " IDFMTSTR, GenomeDB_getDbID(targetGenome));
        qStr = StrUtil_appendString(qStr, tmpStr);
      }

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

      qres = GenomicAlignAdaptor_objectsFromStatementHandle(gaa, sth, 0);
      Vector_append(results,qres);
      Vector_free(qres);

      sth->finish(sth);
    }

    if (!targetGenome ||
        GenomeDB_hasConsensus(genomeDB, targetGenome, methodLinkId)) {
      Vector *cres;

      sprintf(tmpStr," AND gab.query_dnafrag_id = " IDFMTSTR, dnaFragId);
      qStr = StrUtil_appendString(qStr, tmpStr);

      if (startP && endP) {
        int lowerBound = *startP - gaa->maxAlignmentLength;
        sprintf(tmpStr,
                " AND gab.query_start <= %d"
                " AND gab.query_start >= %d"
                " AND gab.query_end >= %d", *endP, lowerBound, *startP ) ;
        qStr = StrUtil_appendString(qStr, tmpStr);
      }
      if (targetGenome) {
        sprintf(tmpStr,
                " AND gab.consensus_dnafrag_id = d.dnafrag_id"
                " AND d.genome_db_id = " IDFMTSTR, GenomeDB_getDbID(targetGenome));
        qStr = StrUtil_appendString(qStr, tmpStr);
      }
      sth = gaa->prepare((BaseAdaptor *)gaa, qStr, strlen(qStr));
      sth->execute(sth);

      cres = GenomicAlignAdaptor_objectsFromStatementHandle(gaa, sth, 1);
      Vector_append(results,cres);
      Vector_free(cres);

      sth->finish(sth);
    }
  }

  if (qStr)
    free(qStr);

  return results;
}
Beispiel #8
0
// Note I didn't implement the stable id fetching uggliness here. I'll probably make a separate method for that
// if necessary
Vector *BaseAdaptor_uncachedFetchAllByDbIDList(BaseAdaptor *ba, Vector *idList, Slice *slice) {
  if ( idList == NULL) {
    fprintf(stderr, "id_list list reference argument is required - bye!");
    return NULL;
  }
  char constraintPref[1024];
  

  if (!Vector_getNumElement(idList)) {
    return Vector_new();
  }

  NameTableType *tables = ba->getTables();
  char **t = (*tables)[0];

  sprintf(constraintPref, "%s.%s_id ", t[SYN], t[NAME] ); 

  // Ensure that we do not exceed MySQL's max_allowed_packet (defaults to
  // 1 MB) splitting large queries into smaller queries of at most 256 KB.
  // Assuming a (generous) average dbID string
  // length of 16, this means 16384 dbIDs in each query.
  int maxSize = 16384;

  // Uniquify the list
  IDHash *idListHash = IDHash_new(IDHASH_MEDIUM);

  int i;
  for (i=0; i<Vector_getNumElement(idList); i++) {
    IDType id = *(IDType *)(Vector_getElementAt(idList, i));
    if (!IDHash_contains(idListHash, id)) {
      IDHash_add(idListHash, id, &trueVal);
    }
  }

  IDType *uniqueIds = IDHash_getKeys(idListHash);
  int nUniqueId = IDHash_getNumValues(idListHash);

  IDHash_free(idListHash, NULL);

  Vector *out = Vector_new();

  int lenNum;
  for (i=0; i<nUniqueId; i+=maxSize) {
    char *constraint = NULL;

    if ((constraint = (char *)calloc(655500,sizeof(char))) == NULL) {
      fprintf(stderr,"Failed allocating constraint\n");
      return out;
    }

    strcpy(constraint, constraintPref);
  
    // Special case for one remaining Id
    if (i == nUniqueId-1) {
      sprintf(constraint, "%s = "IDFMTSTR, constraint, uniqueIds[i]);
    } else {
      char tmpStr[1024];
      int endPoint = sprintf(constraint, "%s IN (", constraint);
      int j;
      for (j=0; j<maxSize && j+i<nUniqueId; j++) {
        if (j!=0) {
          constraint[endPoint++] = ',';
          constraint[endPoint++] = ' ';
        }
        lenNum = sprintf(tmpStr, IDFMTSTR, uniqueIds[i+j]);
        memcpy(&(constraint[endPoint]), tmpStr, lenNum);
        endPoint+=lenNum;
      }
      constraint[endPoint++] = ')';
      constraint[endPoint] = '\0';
    }

    Vector *resChunk = BaseAdaptor_genericFetch(ba, constraint, NULL, slice);

    Vector_append(out, resChunk);

    Vector_free(resChunk);
    free(constraint);
  }
  free(uniqueIds);

  return out;
}