void appendToClustering(DBReader* currSeqDbr, std::string BIndexFile, std::string BA_base, unsigned int* id2rep, cluster_t* clusters, std::string Brest_indexFile){ DBReader* BADbr = new DBReader(BA_base.c_str(), (BA_base + ".index").c_str()); BADbr->open(DBReader::NOSORT); ffindex_index_t* Bindex = openIndex(BIndexFile.c_str()); FILE* Brest_index_file = fopen(Brest_indexFile.c_str(), "w"); seqsWithMatches = 0; seqsWithoutMatches = 0; char* buf = new char[1000000]; for (unsigned int i = 0; i < BADbr->getSize(); i++){ char* qKey = BADbr->getDbKey(i); unsigned int qId = currSeqDbr->getId(qKey); // find out which cluster the sequence belongs to char* alnData = BADbr->getData(i); strcpy(buf, alnData); char* tKey = strtok(buf, "\t"); if (tKey != 0){ unsigned int tId = currSeqDbr->getId(tKey); if (tId == UINT_MAX){ std::cerr << "ERROR: DB key " << tKey << " is in the B->A alignment lists, but not in the new database!\n"; exit(EXIT_FAILURE); } // find out the representative sequence of the cluster of the hit int repId = id2rep[tId]; if(repId == -1){ std::cout << "ERROR: database sequence " << tKey << " is not in the clustering!\n"; std::cout << "Query from B: " << qKey << " matched this sequence.\n"; continue; } // append new member to the cluster clu_entry_t* curr = new clu_entry_t; curr->id = qId; curr->next = 0; clusters[repId].last->next = curr; clusters[repId].last = curr; clusters[repId].clu_size++; seqsWithMatches++; } else{ ffindex_entry_t* e = ffindex_get_entry_by_name(Bindex, qKey); fprintf(Brest_index_file, "%s\t%zd\t%zd\n", e->name, e->offset, e->length); seqsWithoutMatches++; } } BADbr->close(); fclose(Brest_index_file); }
void Prefilter::init_selected(FFindexDatabase* cs219_database, std::vector<std::string> templates, std::vector<std::pair<int, std::string> >& prefiltered_entries) { ffindex_index_t* db_index = cs219_database->db_index; for (size_t n = 0; n < templates.size(); n++) { ffindex_entry_t* entry = ffindex_get_entry_by_name(db_index, const_cast<char *>(templates[n].c_str())); prefiltered_entries.push_back( std::make_pair<int, std::string>(entry->length, std::string(entry->name))); } }
VALUE method_ffindex_get_data_by_name(VALUE self, VALUE key) { Check_Type(key, T_STRING); char * name = calloc(RSTRING_LEN(key) + 1, sizeof(char)); memcpy(name, StringValuePtr(key), RSTRING_LEN(key)); ffindex_db_t * ffindex_db; Data_Get_Struct(self, ffindex_db_t, ffindex_db); ffindex_entry_t * entry = ffindex_get_entry_by_name(ffindex_db->ffindex, name); if(entry) { char * data = ffindex_get_data_by_entry(ffindex_db->ffdata, entry); return rb_str_new2(data); } else return Qnil; }
int main(int argn, char **argv) { int by_index = 0; static struct option long_options[] = { { "byindex", no_argument, NULL, 'n' }, { NULL, 0, NULL, 0 } }; int opt; while (1) { int option_index = 0; opt = getopt_long(argn, argv, "n", long_options, &option_index); if (opt == -1) break; switch (opt) { case 'n': by_index = 1; break; default: usage(argv[0]); return EXIT_FAILURE; } } if(argn < 3) { usage(argv[0]); return EXIT_FAILURE; } char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; FILE *data_file = fopen(data_filename, "r"); FILE *index_file = fopen(index_filename, "r"); if( data_file == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_get", data_filename); exit(EXIT_FAILURE); } if(index_file == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_get", index_filename); exit(EXIT_FAILURE); } size_t data_size; char *data = ffindex_mmap_data(data_file, &data_size); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); exit(EXIT_FAILURE); } if(by_index) { for(int i = optind; i < argn; i++) { size_t index_n = atol(argv[i]) - 1; // offset from 0 but specify from 1 ffindex_entry_t* entry = ffindex_get_entry_by_index(index, index_n); if(entry == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]); } else { char *filedata = ffindex_get_data_by_entry(data, entry); if(filedata == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]); } else fwrite(filedata, entry->length - 1, 1, stdout); } } } else // by name { for(int i = optind; i < argn; i++) { char *filename = argv[i]; ffindex_entry_t* entry = ffindex_get_entry_by_name(index, filename); if(entry == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename); } else { char *filedata = ffindex_get_data_by_entry(data, entry); if(filedata == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename); } else fwrite(filedata, entry->length - 1, 1, stdout); } } /* Alternative code using (slower) ffindex_fopen */ /* FILE *file = ffindex_fopen(data, index, filename); if(file == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_fopen file not found in index", filename); } else { char line[LINE_MAX]; while(fgets(line, LINE_MAX, file) != NULL) printf("%s", line); } */ } return 0; }