struct hash *getDuplicateNameHash(char *tableName)
/* return hash with names that occur more than once */
/* use a hash with all names to figure it out */
{
struct hash *nameHash = NULL;
struct hash *duplicateNameHash = NULL;
char query[512];
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr;
char **row;

nameHash = newHash(16);
duplicateNameHash = newHash(16);

verbose(1, "getDuplicateNameHash for %s...\n", tableName);

safef(query, sizeof(query), "select name from %s", tableName);
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    if (!addIfNew(nameHash, row[0]))
        addIfNew(duplicateNameHash, row[0]);
    }
sqlFreeResult(&sr);
return duplicateNameHash;
}
Example #2
0
struct hash *getHash(char *tableName)
/* read table, create hash */
{
struct hash *ret;
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr = NULL;
char query[64];
char **row = NULL;
struct hapmapSnps *loadItem = NULL;
struct hashEl *hel = NULL;

ret = newHash(18);

sqlSafef(query, sizeof(query), "select * from %s", tableName);
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    loadItem = hapmapSnpsLoad(row + 1);
    /* we want to capture all names in nameHash */
    addIfNew(loadItem->name);
    /* attempt to add to population hash */
    /* if failure, log multiple alignment */
    /* not bothering with error hash, so if something aligns 3 times it is logged twice, etc. */
    hel = hashLookup(ret, loadItem->name);
    if (hel)
        {
	fprintf(errorFileHandle, "multiple positions for %s\n", loadItem->name);
	continue;
	}
    hashAdd(ret, cloneString(loadItem->name), loadItem);
    }
sqlFreeResult(&sr);
hFreeConn(&conn);
return ret;
}
void addChunksToMatrix(mxArray *S, const char *buf, int bufsize, int numChannels) {
	int bufpos = 0;
	int numBlobs = 0;
	mxArray *blobs[MAX_NUM_BLOBS];
	mxArray *keyval = NULL;
	mxArray *A;
	int field;

	while (bufpos + sizeof(ft_chunkdef_t) <= bufsize) {
		ft_chunk_t *chunk = (ft_chunk_t *) (buf + bufpos);
				
		/* "chunk" now points to the right location, make sure it has a valid size definition */
		if (bufpos + sizeof(ft_chunkdef_t) + chunk->def.size > bufsize) {
			printf("Invalid chunk size (%i) in Fieldtrip header detected. Stopping to parse.\n", chunk->def.size);
			break;
		}
				
		switch (chunk->def.type) {
			case FT_CHUNK_CHANNEL_NAMES:
				field = addIfNew(S, "channel_names");
				if (field < 0) break;
				A  = channelNames2Cell(chunk->data, chunk->def.size, numChannels);
				mxSetFieldByNumber(S, 0, field, A);
				break;
			case FT_CHUNK_NIFTI1:
				if (chunk->def.size != SIZE_NIFTI_1) {
					mexWarnMsgTxt("Invalid NIFTI-1 chunk detected. Skipping.");
					break;
				}
				field = addIfNew(S, "nifti_1");
				if (field < 0) break;
				/* pass on as 348 bytes (uint8), should be decoded on Matlab level (?) */
				A  = mxCreateNumericMatrix(1, SIZE_NIFTI_1, mxUINT8_CLASS, mxREAL);
				memcpy(mxGetData(A), chunk->data, SIZE_NIFTI_1);
				mxSetFieldByNumber(S, 0, field, A);
				break;
			case FT_CHUNK_SIEMENS_AP:
				field = addIfNew(S, "siemensap");
				if (field < 0) break;
				/* pass on as uint8, should be decoded on Matlab level (?) */
				A  = mxCreateNumericMatrix(1, chunk->def.size, mxUINT8_CLASS, mxREAL);
				memcpy(mxGetData(A), chunk->data, chunk->def.size);
				mxSetFieldByNumber(S, 0, field, A);
				break;
			case FT_CHUNK_CTF_RES4:
				field = addIfNew(S, "ctf_res4");
				if (field < 0) break;
				/* pass on as uint8, should be decoded on Matlab level (?) */
				A  = mxCreateNumericMatrix(1, chunk->def.size, mxUINT8_CLASS, mxREAL);
				memcpy(mxGetData(A), chunk->data, chunk->def.size);
				mxSetFieldByNumber(S, 0, field, A);
				break;
			case FT_CHUNK_RESOLUTIONS:
				field = addIfNew(S, "resolutions");
				if (field >=0) {
					int nc = chunk->def.size / sizeof(double);
					/*  If the chunk is buggy and there are less resolution values present,
						we only fill in those we have. If there are more, we only fill in numChannels.
						So the returned 'resolutions' field will always match the number of channels in the buffer.
					*/
					if (nc>numChannels) nc = numChannels;
					A = mxCreateDoubleMatrix(numChannels, 1, mxREAL);
					memcpy(mxGetPr(A), chunk->data, nc*sizeof(double));
				}
				break;
			case FT_CHUNK_UNSPECIFIED:
			default:
				if (numBlobs < MAX_NUM_BLOBS) {
					/* pass on the binary(?) blob as an uint8 matrix */
					A = mxCreateNumericMatrix(chunk->def.size, (chunk->def.size>0)?1:0, mxUINT8_CLASS, mxREAL);
					memcpy(mxGetData(A), chunk->data, chunk->def.size);
					blobs[numBlobs++] = A;
				} else {
					mexWarnMsgTxt("Encountered too many unspecified chunks in header. Skipping this one.");
				}
		}
		/* jump to next chunk */
		bufpos += chunk->def.size + sizeof(ft_chunkdef_t);
	}
	
	if (numBlobs > 0) {
		int i;
		
		field = addIfNew(S, "unspecified_blob");
		if (field < 0) return;
		
		A = mxCreateCellMatrix(numBlobs,1);
		for (i=0;i<numBlobs;i++) {
			mxSetCell(A, i, blobs[i]);
		}
		mxSetFieldByNumber(S, 0, field, A);
	}
}