static void addUngappedBlock(struct psl* psl, int* pslSpace, struct block* blk, unsigned flags) /* add the next ungapped block to a psl */ { unsigned newIBlk = psl->blockCount; unsigned blkSize = blk->qEnd - blk->qStart; // uses query size so protein psl is right if (newIBlk >= *pslSpace) pslGrow(psl, pslSpace); psl->qStarts[newIBlk] = blk->qCoordMult * blk->qStart; psl->tStarts[newIBlk] = blk->tCoordMult * blk->tStart; psl->blockSizes[newIBlk] = blk->qCoordMult * blkSize; /* keep bounds current */ psl->qStart = psl->qStarts[0]; psl->qEnd = psl->qStarts[newIBlk] + (blk->qCoordMult * blkSize); if (psl->strand[0] == '-') reverseIntRange(&psl->qStart, &psl->qEnd, psl->qSize); psl->tStart = psl->tStarts[0]; psl->tEnd = psl->tStarts[newIBlk] + (blk->q2tBlkSizeMult * blkSize); if (psl->strand[1] == '-') reverseIntRange(&psl->tStart, &psl->tEnd, psl->tSize); if (flags & bldPslx) { psl->qSequence[newIBlk] = cloneStringZ(blk->qAln + blk->alnStart, blkSize); psl->tSequence[newIBlk] = cloneStringZ(blk->tAln + blk->alnStart, blkSize); } psl->blockCount++; }
char *checkAndFetchBacDna(struct dnaSeq *chromSeq, struct bac *bac) /* fetch bac dna return string to be freed later. Reports if segment exceeds ends of chromosome. */ { if (bac->chromStart < 0) { errAbort("bac error chromStart < 0 : %s %u %u %s %d \n", bac->chrom, bac->chromStart, bac->chromEnd, bac->strand, bac->probe ); return NULL; } if (bac->chromEnd > chromSeq->size) { errAbort("bac error chromEnd > chromSeq->size (%lu) : %s %u %u %s %d \n", (unsigned long) chromSeq->size, bac->chrom, bac->chromStart, bac->chromEnd, bac->strand, bac->probe ); return NULL; } return cloneStringZ(chromSeq->dna + bac->chromStart, bac->chromEnd - bac->chromStart); }
char *checkAndFetchNib(struct dnaSeq *chromSeq, struct snpMap *snp, int lf, int ls) /* fetch nib return string to be freed later. Reports if segment exceends ends of chromosome. */ { if ((snp->chromStart - lf) < 0) { printf("snpMap error (chromStart - offset) < 0 : %s %s %u %u %d \n", snp->name, snp->chrom, snp->chromStart, snp->chromEnd, lf ); return NULL; } if ((snp->chromStart - lf + ls) > chromSeq->size) { printf("bed error chromStart - leftFlank + seqlen > chromSeq->size : %s %s %u %u chr-size: %u \n", snp->name, snp->chrom, snp->chromStart, snp->chromEnd, chromSeq->size ); return NULL; } return cloneStringZ(chromSeq->dna + snp->chromStart - lf, ls); }
struct hashEl *hashAddN(struct hash *hash, char *name, int nameSize, void *val) /* Add name of given size to hash (no need to be zero terminated) */ { struct hashEl *el; if (hash->lm) el = lmAlloc(hash->lm, sizeof(*el)); else AllocVar(el); el->hashVal = hashString(name); int hashVal = el->hashVal & hash->mask; if (hash->lm) { el->name = lmAlloc(hash->lm, nameSize+1); memcpy(el->name, name, nameSize); } else el->name = cloneStringZ(name, nameSize); el->val = val; el->next = hash->table[hashVal]; hash->table[hashVal] = el; hash->elCount += 1; if (hash->autoExpand && hash->elCount > (int)(hash->size * hash->expansionFactor)) { /* double the size */ hashResize(hash, digitsBaseTwo(hash->size)); } return el; }
static char *parseResponse(int sd, char **pResponseCode) /* parse the http response */ { struct dyString *dy = netSlurpFile(sd); close(sd); char *protocol = "HTTP/1.1 "; if (!startsWith(protocol, dy->string)) errAbort("GenomeSpace: Expected response to start with [%s], got [%s]", protocol, dy->string); if (pResponseCode) { char *rc = dy->string + strlen(protocol); char *rcEndString = "\r\n"; char *rcEnd = strstr(dy->string, rcEndString); *pResponseCode = cloneStringZ(rc, rcEnd - rc); } char *headerEndString = "\r\n\r\n"; char *headerEnd = strstr(dy->string, headerEndString); if (!headerEnd) errAbort("header end not found in response"); char *gsResponse = cloneString(headerEnd+strlen(headerEndString)); dyStringFree(&dy); return gsResponse; }
struct htmlStatus *htmlStatusParse(char **pText) /* Read in status from first line. Update pText to point to next line. * Note unlike many routines here, this does not insert zeros into text. */ { char *text = *pText; char *end = strchr(text, '\n'); struct htmlStatus *status; if (end != NULL) *pText = end+1; else *pText = text + strlen(text); end = skipToSpaces(text); if (end == NULL) { warn("Short status line."); return NULL; } AllocVar(status); status->version = cloneStringZ(text, end-text); end = skipLeadingSpaces(end); if (!isdigit(end[0])) { warn("Not a number in status field"); return NULL; } status->status = atoi(end); return status; }
char *cloneString(char *s) /* Make copy of string in dynamic memory */ { if (s == NULL) return NULL; else return cloneStringZ(s, strlen(s)); }
void oldAns01MetaOut(FILE *f, char *midString) /* Version of function used for Anshul's TFBS uniform peak calling ENCODE Jan 2011 freeze. */ { /* Truncate at _VS_ */ char *pattern = "AlnRep0_VS_"; char *patPos = stringIn(pattern, midString); if (patPos == NULL) errAbort("Can't find %s in %s\n", pattern, midString); *patPos = 0; /* Get prefix from known list. */ static char *knownPrefixes[] = {"BroadHistone", "HaibTfbs", "OpenChromChip", "SydhTfbs", "UchicagoTfbs", "UwTfbs", NULL}; char *prefix = findKnownPrefix(midString, knownPrefixes); if (prefix == NULL) errAbort("Don't recognize composite prefix for %s", midString); char *cellStart = midString + strlen(prefix); /* Rely on camelCasing to find antibody */ char *abStart = firstUpper(cellStart+1); if (abStart == NULL) errAbort("Can't find antibody in %s", cellStart); char *cell = cloneStringZ(cellStart, abStart - cellStart); /* Rely on camelCasing to find treatment */ char *treatStart = firstUpper(abStart+1); char *ab = NULL; if (treatStart == NULL) { if (sameString(prefix, "OpenChromChip")) { treatStart = "Std"; ab = cloneString(abStart); } else errAbort("Can't find treatment in %s", midString); } else ab = cloneStringZ(abStart, treatStart - abStart); fprintf(f, "\t%s", cell); fprintf(f, "\t%s", ab); fprintf(f, "\t%s", treatStart); fprintf(f, "\t%s", prefix); }
static char *flyTxToGene(char *tx) /* Convert transcript to gene (by cutting off '-') */ { char *e = strchr(tx, '-'); if (e == NULL) return cloneString(tx); else return cloneStringZ(tx, e-tx); }
struct joinerDtf *joinerDtfFromDottedTriple(char *triple) /* Get joinerDtf from something in db.table.field format. */ { char *s, *e; struct joinerDtf *dtf; AllocVar(dtf); s = triple; e = strchr(s, '.'); if (e == NULL) notTriple(triple); dtf->database = cloneStringZ(s, e-s); s = e+1; e = strchr(s, '.'); if (e == NULL) notTriple(triple); dtf->table = cloneStringZ(s, e-s); dtf->field = cloneString(e+1); return dtf; }
static char *boldTerm(char *target, char *term, int offset, enum dbDbMatchType type) /* Return a string with <b>term</b> swapped in for term at offset. * If offset is negative and type is ddmtSciName, treat term as an abbreviated species * name (term = "G. species" vs. target = "Genus species"): bold the first letter of the * genus and the matching portion of the species. */ { int termLen = strlen(term); int targetLen = strlen(target); if (offset + termLen > targetLen) errAbort("boldTerm: invalid offset (%d) for term '%s' (length %d) in target '%s' (length %d)", offset, term, termLen, target, targetLen); else if (offset < 0 && type != ddmtSciName) errAbort("boldTerm: negative offset (%d) given for type %d", offset, type); // Allocate enough to have two bolded chunks: int resultSize = targetLen + 2*strlen("<b></b>") + 1; char result[resultSize]; char *p = result; int size = sizeof(result); if (offset >= 0) { // The part of target before the term: safeAddN(&p, &size, target, offset); // The bolded term: safeAdd(&p, &size, "<b>"); safeAddN(&p, &size, target+offset, termLen); safeAdd(&p, &size, "</b>"); // The rest of the target after the term: safeAdd(&p, &size, target+offset+termLen); // Accounting tweak -- we allocate enough for two bolded chunks, but use only one here: size -= strlen("<b></b>"); } else { // Term is abbreviated scientific name -- bold the first letter of the genus: safeAdd(&p, &size, "<b>"); safeAddN(&p, &size, target, 1); safeAdd(&p, &size, "</b>"); // add the rest of the genus: char *targetSpecies = skipLeadingSpaces(skipToSpaces(target)); int targetOffset = targetSpecies - target; safeAddN(&p, &size, target+1, targetOffset-1); // bold the matching portion of the species: char *termSpecies = skipLeadingSpaces(skipToSpaces(term)); termLen = strlen(termSpecies); safeAdd(&p, &size, "<b>"); safeAddN(&p, &size, targetSpecies, termLen); safeAdd(&p, &size, "</b>"); // add the rest of the species: safeAdd(&p, &size, targetSpecies+termLen); } if (*p != '\0' || size != 1) errAbort("boldTerm: bad arithmetic (size is %d, *p is '%c')", size, *p); return cloneStringZ(result, resultSize); }
static struct refSeqVerInfo *refSeqVerInfoNewFile(char *acc) /* construct a refSeqVerInfo object from a file */ { struct refSeqVerInfo *rsvi; AllocVar(rsvi); char *dot = strchr(acc, '.'); rsvi->acc = cloneStringZ(acc, ((dot != NULL) ? (dot - acc) : strlen(acc))); if (dot != NULL) rsvi->requestVer = sqlUnsigned(dot+1); return rsvi; }
struct dnaSeq *stickyEnd(struct cutter *enz) /* Return the sticky end sequence of an enzyme. Strand is unspecified. Free this. */ { struct dnaSeq *ret = NULL; if (!enz) return NULL; if (enz->overhang > 0) { char *seq = cloneStringZ(enz->seq + enz->cut, enz->overhang); ret = newDnaSeq(seq, enz->overhang, "sticky"); } else if (enz->overhang < 0) { int size = intAbs(enz->overhang); char *seq = cloneStringZ(enz->seq + enz->cut - size, size); complement(seq, strlen(seq)); ret = newDnaSeq(seq, strlen(seq), "sticky"); } return ret; }
char *hmacSha1(char *key, char *data) /* Calculate a openssl SHA1 keyed-hash message authentication code (HMAC) */ { unsigned char* digest; digest=HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), NULL, NULL); char hmacStr[40]; int i; for(i = 0; i < 20; i++) sprintf(&hmacStr[i*2], "%02x", (unsigned int)digest[i]); return cloneStringZ(hmacStr, sizeof(hmacStr)); }
char *getSrcDir() /* Get the kent/src/ dir */ { char *pwd = getCurrentDir(); char *s = strstr(pwd, "/src/"); if (!s) s = strstr(pwd, "/src"); if (!s) errAbort("unable to locate src/ directory, must run in your sandbox"); s += strlen("/src"); return cloneStringZ(pwd, s - pwd); }
struct wabAli *wabAliLoad(char **row) /* Load a wabAli from row fetched with select * from wabAli * from database. Dispose of this with wabAliFree(). */ { struct wabAli *ret; AllocVar(ret); ret->query = cloneString(row[0]); ret->qStart = sqlUnsigned(row[1]); ret->qEnd = sqlUnsigned(row[2]); strcpy(ret->strand, row[3]); ret->chrom = cloneString(row[4]); ret->chromStart = sqlUnsigned(row[5]); ret->chromEnd = sqlUnsigned(row[6]); ret->milliScore = sqlUnsigned(row[7]); ret->symCount = sqlUnsigned(row[8]); ret->qSym = cloneStringZ(row[9], ret->symCount); ret->tSym = cloneStringZ(row[10], ret->symCount); ret->hSym = cloneStringZ(row[11], ret->symCount); return ret; }
char *eatHrefTag(char *line) /* Remove HrefTag from line and return it. */ { char *startTag = strchr(line, '<'); char *endTag = strchr(startTag, '>'); char *hrefTag = cloneStringZ(startTag, endTag-startTag+1); strcpy(startTag, endTag+1); startTag = strchr(startTag, '<'); endTag = strchr(startTag, '>'); strcpy(startTag, endTag+1); return hrefTag; }
char *hmacMd5(char *key, char *data) /* Calculate a openssl MD5 keyed-hash message authentication code (HMAC) */ { unsigned char* digest; digest=HMAC(EVP_md5(), key, strlen(key), (unsigned char*)data, strlen(data), NULL, NULL); //printf("Raw mdr digest: %s\n", digest); char hmacStr[32]; int i; for(i = 0; i < 16; i++) sprintf(&hmacStr[i*2], "%02x", (unsigned int)digest[i]); return cloneStringZ(hmacStr, sizeof(hmacStr)); }
boolean isAllFloat(char *s) /* Return true if it looks like an floating point */ { char *point = strchr(s,'.'); if (!point) return isAllInt(s); if (!isAllUInt(point+1)) return FALSE; char *temp=cloneStringZ(s,point-s); boolean result = isAllInt(temp); freeMem(temp); return result; }
static boolean parseMetaBigFileName(char* original, char** pRemotePart, char** pFullFilePart, char** pJustFilePart, char** pSectionPart) /* split the filename string into up to three pieces: remote/file/sections parts */ { boolean remote = FALSE; char* baseFileName = NULL; char* colonInFileName = NULL; if (original == NULL) errAbort("NULL filename error, something went wrong"); if (strstr(original, "tp://") || strstr(original, "https://")) remote = TRUE; /* find right-most '/' */ baseFileName = strrchr(original, '/'); if (baseFileName == NULL) { baseFileName = original; *pRemotePart = NULL; } else if (remote && ((baseFileName - original == 7) || (baseFileName - original == 6))) errAbort("malformed URL"); else if (remote) { *pRemotePart = cloneStringZ(original, baseFileName + 1 - original); baseFileName += 1; } else baseFileName += 1; colonInFileName = strchr(baseFileName, ':'); if (colonInFileName != NULL) /* this means there is a section portion of the filename */ { *pFullFilePart = cloneStringZ(original, colonInFileName - original); *pJustFilePart = cloneStringZ(baseFileName, colonInFileName - baseFileName); if (colonInFileName + 1 == NULL) errAbort("Sectioning operator \':\' used after filename but left empty"); *pSectionPart = cloneString(colonInFileName + 1); } else { *pFullFilePart = cloneString(original); *pJustFilePart = cloneString(baseFileName); *pSectionPart = NULL; } return remote; }
static struct rqlParse *rqlParseAtom(struct tokenizer *tkz) /* Return low level (symbol or literal) */ { char *tok = tokenizerMustHaveNext(tkz); struct rqlParse *p; AllocVar(p); char c = tok[0]; if (c == '\'' || c == '"') { p->op = rqlOpLiteral; p->type = rqlTypeString; int len = strlen(tok+1); p->val.s = cloneStringZ(tok+1, len-1); } else if (isalpha(c) || c == '_') { p->op = rqlOpSymbol; p->type = rqlTypeString; /* String until promoted at least. */ p->val.s = cloneString(tok); } else if (isdigit(c)) { p->op = rqlOpLiteral; p->type = rqlTypeInt; p->val.i = sqlUnsigned(tok); if ((tok = tokenizerNext(tkz)) != NULL) { if (tok[0] == '.') { char buf[32]; tok = tokenizerMustHaveNext(tkz); safef(buf, sizeof(buf), "%d.%s", p->val.i, tok); p->type = rqlTypeDouble; p->val.x = sqlDouble(buf); } else tokenizerReuse(tkz); } } else if (c == '(') { p = rqlParseExpression(tkz); skipOverRequired(tkz, ")"); } else { errAbort("Unexpected %s line %d of %s", tok, tkz->lf->lineIx, tkz->lf->fileName); } return p; }
struct gdfGene *newGdfGene(char *name, int nameSize, int exonCount, char strand, UBYTE chromIx) /* Return a new gene. */ { struct gdfGene *gene = needMem(sizeof *gene); gene->name = cloneStringZ(name, nameSize); gene->dataCount = exonCount*2; if (exonCount > 0) { gene->dataPoints = needMem(gene->dataCount * sizeof(gene->dataPoints[0])); } gene->strand = strand; gene->chromIx = chromIx; return gene; }
static char *getGsPersonalDirectory(char *gsToken) /* Get User's default directory from GenomeSpace DM * Returns a url like [https://identity.genomespace.org/datamanager/files/users/<user>] */ { // DEFAULT DIRECTORY // old1 char *defaultDirectoryUrl = "https://identity.genomespace.org/datamanager/defaultdirectory"; // old2 char *defaultDirectoryUrl = "https://dmtest.genomespace.org:8444/datamanager/defaultdirectory"; // old3 char *defaultDirectoryUrl = "https://dm.genomespace.org/datamanager/v1.0/defaultdirectory"; // NOTE the defaultdirectory method got renamed to personaldirectory // old4 char *personalDirectoryUrl = "https://dm.genomespace.org/datamanager/v1.0/personaldirectory"; char *dmSvr = getGenomeSpaceConfig("dmServer"); char personalDirectoryUrl[1024]; safef(personalDirectoryUrl, sizeof personalDirectoryUrl, "%s/v1.0/personaldirectory", dmSvr); struct dyString *reqExtra = newDyString(256); dyStringPrintf(reqExtra, "Cookie: gs-token=%s\r\n", gsToken); int sd = netOpenHttpExt(personalDirectoryUrl, "GET", reqExtra->string); if (sd < 0) errAbort("failed to open socket for [%s]", personalDirectoryUrl); struct dyString *dy = netSlurpFile(sd); close(sd); char *personalDirectory = NULL; if (strstr(dy->string, "HTTP/1.1 303 See Other")) { char *valStart = strstr(dy->string, "Location: "); if (valStart) { valStart += strlen("Location: "); char *valEnd = strstr(valStart, "\r\n"); if (!valEnd) errAbort("location not found in response headers"); personalDirectory = cloneStringZ(valStart, valEnd - valStart); } } dyStringFree(&dy); dyStringFree(&reqExtra); return personalDirectory; }
struct galleryEntry *galLoad(char **row) /* Load a session entry from a row. A row consists of: * 0. gbMembers.realName * 1. namedSessionDb.userName * 2. gbMembers.idx * 3. namedSessionDb.sessionName * 4. namedSessionDb.useCount * 5. namedSessionDb.settings * 6. namedSessionDb.contents * 7. namedSessionDb.firstUse */ { char *dbIdx, *dbEnd; struct galleryEntry *ret; AllocVar(ret); ret->realName = cloneString(row[0]); ret->userName = cloneString(row[1]); cgiDecodeFull(ret->userName, ret->userName, strlen(ret->userName)); ret->sessionName = cloneString(row[3]); cgiDecodeFull(ret->sessionName, ret->sessionName, strlen(ret->sessionName)); ret->sessionUrl = dyStringCreate("hgS_doOtherUser=submit&hgS_otherUserName=%s&hgS_otherUserSessionName=%s", row[1], row[3]); ret->imgPath = sessionThumbnailFilePath(row[2], row[3], row[7]); if (fileExists(ret->imgPath)) ret->imgUri = sessionThumbnailFileUri(row[2], row[3], row[7]); else ret->imgUri = NULL; ret->useCount = sqlUnsignedLong(row[4]); ret->settings = cloneString(row[5]); if (startsWith("db=", row[6])) dbIdx = row[6] + 3; else dbIdx = strstr(row[6], "&db=") + 4; if (dbIdx != NULL) { dbEnd = strchr(dbIdx, '&'); if (dbEnd != NULL) ret->db = cloneStringZ(dbIdx, dbEnd-dbIdx); else ret->db = cloneString(dbIdx); } else ret->db = cloneString("n/a"); ret->firstUse = cloneString(row[7]); char *spacePt = strchr(ret->firstUse, ' '); if (spacePt != NULL) *spacePt = '\0'; return ret; }
static char *getSimplifiedAllele(char *geneName, char *allele) /* If allele is of form geneName<varient> then return just * varient. Else return allele. Do a freeMem on result when * done. */ { int len = strlen(geneName); if (startsWith(geneName, allele) && allele[len] == '<') { char *s = allele + len + 1; char *e = strrchr(s, '>'); if (e == NULL) e = s + strlen(s); return cloneStringZ(s, e - s); } else return cloneString(allele); }
static void chromNameCallback(void *context, void *key, int keySize, void *val, int valSize) /* Callback that captures chromInfo from bPlusTree. */ { struct chromNameCallbackContext *c = context; struct bbiChromInfo *info; struct bbiChromIdSize *idSize = val; assert(valSize == sizeof(*idSize)); AllocVar(info); info->name = cloneStringZ(key, keySize); info->id = idSize->chromId; info->size = idSize->chromSize; if (c->isSwapped) { info->id = byteSwap32(info->id); info->size = byteSwap32(info->size); } slAddHead(&c->list, info); }
static struct dnaSeq *seqReaderTblGet(struct seqReader *seqReader, char *seqName, int start, int end, int *retFullSeqSize) /* get a sequence from the seqTbl */ { struct dnaSeq *fullSeq = hashFindVal(seqReader->seqTbl, seqName); if (fullSeq == NULL) errAbort("can't find sequence %s in %s", seqName, seqReader->spec); if (end > fullSeq->size) errAbort("range %d-%d longer than sequence %s length %d in %s", start, end, seqName, fullSeq->size, seqReader->spec); if (retFullSeqSize != NULL) *retFullSeqSize = fullSeq->size; int len = (end-start); struct dnaSeq *seq = newDnaSeq(cloneStringZ(fullSeq->dna+start, len), len, seqName); if (!gMasked) tolowers(seq->dna); return seq; }
void uw02MetaOut(FILE *f, char *midString) /* uw02MetaOut - Version of function used for UW dnase on hg19. */ { /* At this point midString has format CellPkRep## - parse out Cell and ## */ char *pattern = "PkRep"; int patLen = strlen(pattern); char *patPos = stringIn(pattern, midString); if (patPos == NULL) errAbort("Can't find %s in %s\n", pattern, midString); char *cell = cloneStringZ(midString, patPos - midString); char *rep = cloneString(patPos + patLen); /* Sometimes rep will have a 'V1' or 'V2' after it. Chop it off. */ char *v = strchr(rep, 'V'); if (v != NULL) *v = 0; fprintf(f, "\t%s\t%s", cell, rep); }
char *insertUserPasswordIntoUrl(char *url, char *user, char *password) /* Insert cgi-encoded user and password into url after protocol. Free returned string when done. */ { char resultUrl[1024]; char *encUser = cgiEncode(user); char *encPassword = cgiEncode(password); char *rest = stringIn("://", url); if (!rest) errAbort("expected url [%s] to have ://", url); char *protocol = cloneStringZ(url, rest - url); rest += strlen("://"); safef(resultUrl, sizeof resultUrl, "%s://%s:%s@%s", protocol, encUser, encPassword, rest); freeMem(protocol); freeMem(encUser); freeMem(encPassword); return cloneString(resultUrl); }
int nearCountUniqAccRows(struct htmlPage *page) /* Count number of unique rows in table containing just hyperlinked * accessions. */ { char *s, *e, *row, *acc; int count = 0; struct hash *uniqHash = hashNew(0); if (page == NULL) return -1; /* Set s to first row. */ s = stringIn(nearStartTablePat, page->htmlText); if (s == NULL) return -1; s += strlen(nearStartTablePat); for (;;) { e = stringIn(nearEndRowPat, s); if (e == NULL) break; row = cloneStringZ(s, e-s); acc = qaStringBetween(row, ">", "</a>"); if (acc == NULL) { warn("Can't find acc text between > and </a> while counting uniq row %s", row); freez(&row); break; } if (!hashLookup(uniqHash, acc)) { hashAdd(uniqHash, acc, NULL); ++count; } freez(&row); freez(&acc); s = e + strlen(nearEndRowPat); } hashFree(&uniqHash); return count; }