lib/nibTwo.c

Go to the documentation of this file.
00001 /* nibTwo - Something to let you transparently access either
00002  * .2bit or .nib files. */
00003 
00004 #include "common.h"
00005 #include "hash.h"
00006 #include "dnaseq.h"
00007 #include "nib.h"
00008 #include "twoBit.h"
00009 #include "nibTwo.h"
00010 
00011 static char const rcsid[] = "$Id: nibTwo.c,v 1.5 2006/03/10 17:43:37 angie Exp $";
00012 
00013 struct nibTwoCache *nibTwoCacheNew(char *pathName)
00014 /* Get something that will more or less transparently get sequence from 
00015  * nib files or .2bit. */ 
00016 {
00017 struct nibTwoCache *ntc;
00018 AllocVar(ntc);
00019 ntc->pathName = cloneString(pathName);
00020 ntc->isTwoBit = twoBitIsFile(pathName);
00021 if (ntc->isTwoBit)
00022     ntc->tbf = twoBitOpen(pathName);
00023 else
00024     ntc->nibHash = newHash(10);
00025 return ntc;
00026 }
00027 
00028 void nibTwoCacheFree(struct nibTwoCache **pNtc)
00029 /* Free up resources associated with nibTwoCache. */
00030 {
00031 struct nibTwoCache *ntc = *pNtc;
00032 if (ntc != NULL)
00033     {
00034     freez(&ntc->pathName);
00035     if (ntc->isTwoBit)
00036         twoBitClose(&ntc->tbf);
00037     else
00038         {
00039         struct hashEl *el, *list = hashElListHash(ntc->nibHash);
00040         struct nibInfo *nib;
00041         for (el = list; el != NULL; el = el->next)
00042              {
00043              nib = el->val;
00044              nibInfoFree(&nib);
00045              }
00046         hashElFreeList(&list);
00047         hashFree(&ntc->nibHash);
00048         }
00049     freez(pNtc);
00050     }
00051 }
00052 
00053 struct dnaSeq *nibTwoCacheSeq(struct nibTwoCache *ntc, char *seqName)
00054 /* Return all of sequence. This will have repeats in lower case. */
00055 {
00056 if (ntc->isTwoBit)
00057     return twoBitReadSeqFrag(ntc->tbf, seqName, 0, 0);
00058 else
00059     {
00060     struct nibInfo *nib = nibInfoFromCache(ntc->nibHash, ntc->pathName, seqName);
00061     return nibLdPart(nib->fileName, nib->f, nib->size, 0, nib->size);
00062     }
00063 }
00064 
00065 struct dnaSeq *nibTwoCacheSeqPartExt(struct nibTwoCache *ntc, char *seqName, int start, int size,
00066                                      boolean doMask, int *retFullSeqSize)
00067 /* Return part of sequence. If *retFullSeqSize is non-null then return full
00068  * size of sequence (not just loaded part) there.   Sequence will be lower
00069  * case if doMask is false, mixed case (repeats in lower)
00070  * if doMask is true. */
00071 {
00072 if (ntc->isTwoBit)
00073     {
00074     return twoBitReadSeqFragExt(ntc->tbf, seqName, start, start+size,
00075                                 doMask, retFullSeqSize);
00076     }
00077 else
00078     {
00079     struct nibInfo *nib = nibInfoFromCache(ntc->nibHash, ntc->pathName, seqName);
00080     int opts = (doMask ? NIB_MASK_MIXED : 0);
00081     if (retFullSeqSize != NULL)
00082         *retFullSeqSize = nib->size;
00083     return nibLdPartMasked(opts, nib->fileName, nib->f, nib->size, start, size);
00084     }
00085 }
00086 
00087 struct dnaSeq *nibTwoCacheSeqPart(struct nibTwoCache *ntc, char *seqName, int start, int size,
00088         int *retFullSeqSize)
00089 /* Return part of sequence. If *retFullSeqSize is non-null then return full size of
00090  * sequence (not just loaded part) there. This will have repeats in lower case. */
00091 {
00092 return nibTwoCacheSeqPartExt(ntc, seqName, start, size, TRUE, retFullSeqSize);
00093 }
00094 
00095 struct dnaSeq *nibTwoLoadOne(char *pathName, char *seqName)
00096 /* Return sequence from a directory full of nibs or a .2bit file. 
00097  * The sequence will have repeats in lower case. */
00098 {
00099 struct dnaSeq *seq;
00100 if (twoBitIsFile(pathName))
00101     {
00102     struct twoBitFile *tbf = twoBitOpen(pathName);
00103     seq = twoBitReadSeqFrag(tbf, seqName, 0, 0);
00104     twoBitClose(&tbf);
00105     }
00106 else
00107     {
00108     char path[512];
00109     safef(path, sizeof(path), "%s/%s.nib", pathName, seqName);
00110     seq = nibLoadAllMasked(NIB_MASK_MIXED, path);
00111     }
00112 return seq;
00113 }
00114 

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2