-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtwoBit.c
1354 lines (1205 loc) · 32.8 KB
/
twoBit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2014 The Regents of the University of California
* See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
#include "common.h"
#include "hash.h"
#include "dnaseq.h"
#include "dnautil.h"
#include "sig.h"
#include "localmem.h"
#include "linefile.h"
#include "obscure.h"
#include "bPlusTree.h"
#include "twoBit.h"
#include "udc.h"
#include "net.h"
#include "portable.h"
#include <limits.h>
/* following are the wrap functions for the UDC and stdio functoins
* that read twoBit files. All of these are to get around the C compiler
* complaining about the automatic cast of a void * to FILE * or
* struct udcFile *.
*/
/* first the UDC wrappers */
static void udcSeekCurWrap(void *file, bits64 offset)
{
udcSeekCur((struct udcFile *)file, offset);
}
static void udcSeekWrap(void *file, bits64 offset)
{
udcSeek((struct udcFile *)file, offset);
}
static bits64 udcTellWrap(void *file)
{
return udcTell((struct udcFile *)file);
}
static void udcMustReadWrap(void *file, void *buf, size_t size)
{
udcMustRead((struct udcFile *)file, buf, size);
}
static void udcFileCloseWrap(void *pFile)
{
udcFileClose((struct udcFile **)pFile);
}
static bits32 udcReadBits32Wrap(void *f, boolean isSwapped)
{
return udcReadBits32((struct udcFile *)f, isSwapped);
}
static bits64 udcReadBits64Wrap(void *f, boolean isSwapped)
{
return udcReadBits64((struct udcFile *)f, isSwapped);
}
static boolean udcFastReadStringWrap(void *f, char buf[256])
{
return udcFastReadString((struct udcFile *)f, buf);
}
/* now the stdio wrappers */
static void seekCurWrap(void *file, bits64 offset)
{
fseek((FILE *)file, offset, SEEK_CUR);
}
static void seekWrap(void *file, bits64 offset)
{
fseek((FILE *)file, offset, SEEK_SET);
}
static bits64 tellWrap(void *file)
{
return ftell((FILE *)file);
}
static void mustReadWrap(void *file, void *buf, size_t size)
{
mustRead((FILE *)file, buf, size);
}
static void fileCloseWrap(void *pFile)
{
carefulClose((FILE **)pFile);
}
static bits32 readBits32Wrap(void *f, boolean isSwapped)
{
return readBits32((FILE *)f, isSwapped);
}
static bits64 readBits64Wrap(void *f, boolean isSwapped)
{
return readBits64((FILE *)f, isSwapped);
}
static boolean fastReadStringWrap(void *f, char buf[256])
{
return fastReadString((FILE *)f, buf);
}
static void setFileFuncs( struct twoBitFile *tbf, boolean useUdc)
/* choose the proper function pointers depending on whether
* this open twoBit is using stdio or UDC
*/
{
if (useUdc)
{
tbf->ourSeekCur = udcSeekCurWrap;
tbf->ourSeek = udcSeekWrap;
tbf->ourTell = udcTellWrap;
tbf->ourReadBits32 = udcReadBits32Wrap;
tbf->ourReadBits64 = udcReadBits64Wrap;
tbf->ourFastReadString = udcFastReadStringWrap;
tbf->ourClose = udcFileCloseWrap;
tbf->ourMustRead = udcMustReadWrap;
}
else
{
tbf->ourSeekCur = seekCurWrap;
tbf->ourSeek = seekWrap;
tbf->ourTell = tellWrap;
tbf->ourReadBits32 = readBits32Wrap;
tbf->ourReadBits64 = readBits64Wrap;
tbf->ourFastReadString = fastReadStringWrap;
tbf->ourClose = fileCloseWrap;
tbf->ourMustRead = mustReadWrap;
}
}
static int countBlocksOfN(char *s, int size)
/* Count number of blocks of N's (or n's) in s. */
{
int i;
boolean isN, lastIsN = FALSE;
char c;
int blockCount = 0;
for (i=0; i<size; ++i)
{
c = s[i];
isN = (c == 'n' || c == 'N');
if (isN && !lastIsN)
++blockCount;
lastIsN = isN;
}
return blockCount;
}
static int countBlocksOfLower(char *s, int size)
/* Count number of blocks of lower case letters. */
{
int i;
boolean isLower, lastIsLower = FALSE;
int blockCount = 0;
for (i=0; i<size; ++i)
{
isLower = islower(s[i]);
if (isLower && !lastIsLower)
++blockCount;
lastIsLower = isLower;
}
return blockCount;
}
static void storeBlocksOfN(char *s, int size, bits32 *starts, bits32 *sizes)
/* Store starts and sizes of blocks of N's. */
{
int i;
boolean isN, lastIsN = FALSE;
int startN = 0;
char c;
for (i=0; i<size; ++i)
{
c = s[i];
isN = (c == 'n' || c == 'N');
if (isN)
{
if (!lastIsN)
startN = i;
}
else
{
if (lastIsN)
{
*starts++ = startN;
*sizes++ = i - startN;
}
}
lastIsN = isN;
}
if (lastIsN)
{
*starts = startN;
*sizes = i - startN;
}
}
static void storeBlocksOfLower(char *s, int size, bits32 *starts, bits32 *sizes)
/* Store starts and sizes of blocks of lower case letters. */
{
int i;
boolean isLower, lastIsLower = FALSE;
int startLower = 0;
for (i=0; i<size; ++i)
{
isLower = islower(s[i]);
if (isLower)
{
if (!lastIsLower)
startLower = i;
}
else
{
if (lastIsLower)
{
*starts++ = startLower;
*sizes++ = i - startLower;
}
}
lastIsLower = isLower;
}
if (lastIsLower)
{
*starts = startLower;
*sizes = i - startLower;
}
}
static int packedSize(int unpackedSize)
/* Return size when packed, rounding up. */
{
return ((unpackedSize + 3) >> 2);
}
struct twoBit *twoBitFromDnaSeq(struct dnaSeq *seq, boolean doMask)
/* Convert dnaSeq representation in memory to twoBit representation.
* If doMask is true interpret lower-case letters as masked. */
{
int ubyteSize = packedSize(seq->size);
UBYTE *pt;
struct twoBit *twoBit;
DNA last4[4]; /* Holds few bases. */
DNA *dna;
int i, end;
/* Allocate structure and fill in name. */
AllocVar(twoBit);
pt = AllocArray(twoBit->data, ubyteSize);
twoBit->name = cloneString(seq->name);
twoBit->size = seq->size;
/* Convert to 4-bases per byte representation. */
dna = seq->dna;
end = seq->size - 4;
for (i=0; i<end; i += 4)
{
*pt++ = packDna4(dna+i);
}
/* Take care of conversion of last few bases. */
last4[0] = last4[1] = last4[2] = last4[3] = 'T';
memcpy(last4, dna+i, seq->size-i);
*pt = packDna4(last4);
/* Deal with blocks of N. */
twoBit->nBlockCount = countBlocksOfN(dna, seq->size);
if (twoBit->nBlockCount > 0)
{
AllocArray(twoBit->nStarts, twoBit->nBlockCount);
AllocArray(twoBit->nSizes, twoBit->nBlockCount);
storeBlocksOfN(dna, seq->size, twoBit->nStarts, twoBit->nSizes);
}
/* Deal with masking */
if (doMask)
{
twoBit->maskBlockCount = countBlocksOfLower(dna, seq->size);
if (twoBit->maskBlockCount > 0)
{
AllocArray(twoBit->maskStarts, twoBit->maskBlockCount);
AllocArray(twoBit->maskSizes, twoBit->maskBlockCount);
storeBlocksOfLower(dna, seq->size,
twoBit->maskStarts, twoBit->maskSizes);
}
}
return twoBit;
}
static int twoBitSizeInFile(struct twoBit *twoBit)
/* Figure out size structure will take in file. */
{
return packedSize(twoBit->size)
+ sizeof(twoBit->size)
+ sizeof(twoBit->nBlockCount)
+ sizeof(twoBit->nStarts[0]) * twoBit->nBlockCount
+ sizeof(twoBit->nSizes[0]) * twoBit->nBlockCount
+ sizeof(twoBit->maskBlockCount)
+ sizeof(twoBit->maskStarts[0]) * twoBit->maskBlockCount
+ sizeof(twoBit->maskSizes[0]) * twoBit->maskBlockCount
+ sizeof(twoBit->reserved);
}
void twoBitWriteOne(struct twoBit *twoBit, FILE *f)
/* Write out one twoBit sequence to binary file.
* Note this does not include the name, which is
* stored only in index. */
{
writeOne(f, twoBit->size);
writeOne(f, twoBit->nBlockCount);
if (twoBit->nBlockCount > 0)
{
fwrite(twoBit->nStarts, sizeof(twoBit->nStarts[0]),
twoBit->nBlockCount, f);
fwrite(twoBit->nSizes, sizeof(twoBit->nSizes[0]),
twoBit->nBlockCount, f);
}
writeOne(f, twoBit->maskBlockCount);
if (twoBit->maskBlockCount > 0)
{
fwrite(twoBit->maskStarts, sizeof(twoBit->maskStarts[0]),
twoBit->maskBlockCount, f);
fwrite(twoBit->maskSizes, sizeof(twoBit->maskSizes[0]),
twoBit->maskBlockCount, f);
}
writeOne(f, twoBit->reserved);
mustWrite(f, twoBit->data, packedSize(twoBit->size));
}
void twoBitWriteHeaderExt(struct twoBit *twoBitList, FILE *f, boolean useLong)
/* Write out header portion of twoBit file, including initial
* index. If useLong is True, use 64 bit quantities for the index offsets to support >4Gb assemblies */
{
bits32 sig = twoBitSig;
bits32 version = 0;
if (useLong)
version = 1;
bits32 seqCount = slCount(twoBitList);
bits32 reserved = 0;
bits32 offset = 0;
bits64 longOffset = 0;
struct twoBit *twoBit;
long long counter = 0; /* check for 32 bit overflow */
/* Write out fixed parts of header. */
writeOne(f, sig);
writeOne(f, version);
writeOne(f, seqCount);
writeOne(f, reserved);
/* Figure out location of first byte past index.
* Each index entry contains 4 bytes of offset information
* and the name of the sequence, which is variable length. */
longOffset = offset = sizeof(sig) + sizeof(version) + sizeof(seqCount) + sizeof(reserved);
for (twoBit = twoBitList; twoBit != NULL; twoBit = twoBit->next)
{
int nameLen = strlen(twoBit->name);
if (nameLen > 255)
errAbort("name %s too long", twoBit->name);
if (useLong)
longOffset += nameLen + 1 + sizeof(bits64);
else
offset += nameLen + 1 + sizeof(bits32);
}
/* Write out index. */
for (twoBit = twoBitList; twoBit != NULL; twoBit = twoBit->next)
{
int size = twoBitSizeInFile(twoBit);
writeString(f, twoBit->name);
if (useLong)
{
writeOne(f, longOffset);
longOffset += size;
}
else
{
writeOne(f, offset);
offset += size;
}
counter += (long long)size;
if (!useLong && (counter > UINT_MAX ))
errAbort("Error in faToTwoBit, index overflow at %s. The 2bit format "
"does not support indexes larger than %dGb, \n"
"please split up into smaller files, or use -long option.\n",
twoBit->name, UINT_MAX/1000000000);
}
}
void twoBitWriteHeader(struct twoBit *twoBitList, FILE *f)
/* Write out header portion of twoBit file, including initial
* index */
{
twoBitWriteHeaderExt(twoBitList, f, FALSE);
}
void twoBitClose(struct twoBitFile **pTbf)
/* Free up resources associated with twoBitFile. */
{
struct twoBitFile *tbf = *pTbf;
if (tbf != NULL)
{
twoBitFree(&tbf->seqCache);
freez(&tbf->fileName);
(*tbf->ourClose)(&tbf->f);
hashFree(&tbf->hash);
/* The indexList is allocated out of the hash's memory pool. */
bptFileClose(&tbf->bpt);
freez(pTbf);
}
}
boolean twoBitSigRead(struct twoBitFile *tbf, boolean *isSwapped)
/* read twoBit signature, return FALSE if not good
* set isSwapped to TRUE if twoBit file is byte swapped */
{
bits32 sig;
*isSwapped = FALSE;
(*tbf->ourMustRead)(tbf->f, &sig, sizeof(sig));
if (sig == twoBitSwapSig)
*isSwapped = TRUE;
else if (sig != twoBitSig)
return FALSE;
return TRUE;
}
static struct twoBitFile *getTbfAndOpen(char *fileName, boolean useUdc)
{
struct twoBitFile *tbf;
AllocVar(tbf);
setFileFuncs(tbf, useUdc);
if (useUdc)
tbf->f = udcFileOpen(fileName, NULL);
else
tbf->f = mustOpen(fileName, "rb");
return tbf;
}
static struct twoBitFile *twoBitOpenReadHeader(char *fileName, boolean useUdc)
/* Open file, read in header but not index.
* Squawk and die if there is a problem. */
{
struct twoBitFile *tbf;
boolean isSwapped = FALSE;
tbf = getTbfAndOpen(fileName, useUdc);
/* Allocate header verify signature, and read in
* the constant-length bits. */
if (!twoBitSigRead(tbf, &isSwapped))
errAbort("%s doesn't have a valid twoBitSig", fileName);
tbf->isSwapped = isSwapped;
tbf->fileName = cloneString(fileName);
tbf->version = (*tbf->ourReadBits32)(tbf->f, isSwapped);
if ((tbf->version != 0) && (tbf->version != 1))
{
errAbort("Can only handle version 0 or version 1 of this file. This is version %d",
(int)tbf->version);
}
tbf->seqCount = (*tbf->ourReadBits32)(tbf->f, isSwapped);
tbf->reserved = (*tbf->ourReadBits32)(tbf->f, isSwapped);
return tbf;
}
struct twoBitFile *twoBitOpen(char *fileName)
/* Open file, read in header and index.
* Squawk and die if there is a problem. */
{
boolean useUdc = FALSE;
if (hasProtocol(fileName))
useUdc = TRUE;
struct twoBitFile *tbf = twoBitOpenReadHeader(fileName, useUdc);
struct twoBitIndex *index;
boolean isSwapped = tbf->isSwapped;
int i;
struct hash *hash;
void *f = tbf->f;
/* Read in index. */
hash = tbf->hash = hashNew(digitsBaseTwo(tbf->seqCount));
for (i=0; i<tbf->seqCount; ++i)
{
char name[256];
if (!(*tbf->ourFastReadString)(f, name))
errAbort("%s is truncated", fileName);
lmAllocVar(hash->lm, index);
if (tbf->version == 1)
index->offset = (*tbf->ourReadBits64)(f, isSwapped);
else
index->offset = (*tbf->ourReadBits32)(f, isSwapped);
hashAddSaveName(hash, name, index, &index->name);
slAddHead(&tbf->indexList, index);
}
slReverse(&tbf->indexList);
return tbf;
}
struct twoBitFile *twoBitOpenExternalBptIndex(char *twoBitName, char *bptName)
/* Open file, read in header, but not regular index. Instead use
* bpt index. Beware if you use this the indexList field will be NULL
* as will the hash. */
{
boolean useUdc = FALSE;
if (hasProtocol(twoBitName))
useUdc = TRUE;
struct twoBitFile *tbf = twoBitOpenReadHeader(twoBitName, useUdc);
tbf->bpt = bptFileOpen(bptName);
if (tbf->seqCount != tbf->bpt->itemCount)
errAbort("%s and %s don't have same number of sequences!", twoBitName, bptName);
return tbf;
}
static int findGreatestLowerBound(int blockCount, bits32 *pos,
int val)
/* Find index of greatest element in posArray that is less
* than or equal to val using a binary search. */
{
int startIx=0, endIx=blockCount-1, midIx;
int posVal;
for (;;)
{
if (startIx == endIx)
{
posVal = pos[startIx];
if (posVal <= val || startIx == 0)
return startIx;
else
return startIx-1;
}
midIx = ((startIx + endIx)>>1);
posVal = pos[midIx];
if (posVal < val)
startIx = midIx+1;
else
endIx = midIx;
}
}
boolean twoBitHasSeq(struct twoBitFile *tbf, char *name)
/* Return TRUE if sequence of given name exists in two bit file */
{
if (tbf->bpt)
{
bits64 offset;
return bptFileFind(tbf->bpt, name, strlen(name), &offset, sizeof(offset));
}
else
{
struct twoBitIndex *index = hashFindVal(tbf->hash, name);
return index != NULL;
}
}
static void twoBitSeekTo(struct twoBitFile *tbf, char *name)
/* Seek to start of named record. Abort if can't find it. */
{
if (tbf->bpt)
{
bits64 offset;
if (!bptFileFind(tbf->bpt, name, strlen(name), &offset, sizeof(offset)))
errAbort("%s is not in %s", name, tbf->bpt->fileName);
(*tbf->ourSeek)(tbf->f, offset);
}
else
{
struct twoBitIndex *index = hashFindVal(tbf->hash, name);
if (index == NULL)
errAbort("%s is not in %s", name, tbf->fileName);
(*tbf->ourSeek)(tbf->f, index->offset);
}
}
static void readBlockCoords(struct twoBitFile *tbf, boolean isSwapped, bits32 *retBlockCount,
bits32 **retBlockStarts, bits32 **retBlockSizes)
/* Read in blockCount, starts and sizes from file. (Same structure used for
* both blocks of N's and masked blocks.) */
{
bits32 blkCount = (*tbf->ourReadBits32)(tbf->f, isSwapped);
*retBlockCount = blkCount;
if (blkCount == 0)
{
*retBlockStarts = NULL;
*retBlockSizes = NULL;
}
else
{
bits32 *nStarts, *nSizes;
AllocArray(nStarts, blkCount);
AllocArray(nSizes, blkCount);
(*tbf->ourMustRead)(tbf->f, nStarts, sizeof(nStarts[0]) * blkCount);
(*tbf->ourMustRead)(tbf->f, nSizes, sizeof(nSizes[0]) * blkCount);
if (isSwapped)
{
int i;
for (i=0; i<blkCount; ++i)
{
nStarts[i] = byteSwap32(nStarts[i]);
nSizes[i] = byteSwap32(nSizes[i]);
}
}
*retBlockStarts = nStarts;
*retBlockSizes = nSizes;
}
}
static struct twoBit *readTwoBitSeqHeader(struct twoBitFile *tbf, char *name)
/* read a sequence header, nBlocks and maskBlocks from a twoBit file,
* leaving file pointer at data block */
{
boolean isSwapped = tbf->isSwapped;
struct twoBit *twoBit;
AllocVar(twoBit);
twoBit->name = cloneString(name);
void *f = tbf->f;
/* Find offset in index and seek to it */
twoBitSeekTo(tbf, name);
/* Read in seqSize. */
twoBit->size = (*tbf->ourReadBits32)(f, isSwapped);
/* Read in blocks of N. */
readBlockCoords(tbf, isSwapped, &(twoBit->nBlockCount),
&(twoBit->nStarts), &(twoBit->nSizes));
/* Read in masked blocks. */
readBlockCoords(tbf, isSwapped, &(twoBit->maskBlockCount),
&(twoBit->maskStarts), &(twoBit->maskSizes));
/* Reserved word. */
twoBit->reserved = (*tbf->ourReadBits32)(f, isSwapped);
return twoBit;
}
struct twoBit *twoBitOneFromFile(struct twoBitFile *tbf, char *name)
/* Get single sequence as two bit. */
{
struct twoBit *twoBit = readTwoBitSeqHeader(tbf, name);
bits32 packByteCount;
void *f = tbf->f;
/* Read in data. */
packByteCount = packedSize(twoBit->size);
twoBit->data = needLargeMem(packByteCount);
(*tbf->ourMustRead)(f, twoBit->data, packByteCount);
return twoBit;
}
struct twoBit *twoBitFromOpenFile(struct twoBitFile *tbf)
/* Get twoBit list of all sequences in twoBit file. */
{
struct twoBitIndex *index;
struct twoBit *twoBitList = NULL;
for (index = tbf->indexList; index != NULL; index = index->next)
{
struct twoBit *twoBit = twoBitOneFromFile(tbf, index->name);
slAddHead(&twoBitList, twoBit);
}
twoBitClose(&tbf);
slReverse(&twoBitList);
return twoBitList;
}
struct twoBit *twoBitFromFile(char *fileName)
/* Get twoBit list of all sequences in already open twoBit file. */
{
struct twoBitFile *tbf = twoBitOpen(fileName);
return twoBitFromOpenFile(tbf);
}
void twoBitFree(struct twoBit **pTwoBit)
/* Free up a two bit structure. */
{
struct twoBit *twoBit = *pTwoBit;
if (twoBit != NULL)
{
freeMem(twoBit->nStarts);
freeMem(twoBit->nSizes);
freeMem(twoBit->maskStarts);
freeMem(twoBit->maskSizes);
freeMem(twoBit->data);
freez(pTwoBit);
}
}
void twoBitFreeList(struct twoBit **pList)
/* Free a list of dynamically allocated twoBit's */
{
struct twoBit *el, *next;
for (el = *pList; el != NULL; el = next)
{
next = el->next;
twoBitFree(&el);
}
*pList = NULL;
}
static struct twoBit *getTwoBitSeqHeader(struct twoBitFile *tbf, char *name)
/* get the sequence header information using the cache. Position file
* right at data. */
{
if ((tbf->seqCache != NULL) && (sameString(tbf->seqCache->name, name)))
{
// use cached
(*tbf->ourSeek)(tbf->f, tbf->dataOffsetCache);
}
else
{
// fetch new and cache
twoBitFree(&tbf->seqCache);
tbf->seqCache = readTwoBitSeqHeader(tbf, name);
tbf->dataOffsetCache = (*tbf->ourTell)(tbf->f);
}
return tbf->seqCache;
}
struct dnaSeq *twoBitReadSeqFragExt(struct twoBitFile *tbf, char *name,
int fragStart, int fragEnd, boolean doMask, int *retFullSize)
/* Read part of sequence from .2bit file. To read full
* sequence call with start=end=0. Sequence will be lower
* case if doMask is false, mixed case (repeats in lower)
* if doMask is true. */
{
struct dnaSeq *seq;
void *f = tbf->f;
int i;
int packByteCount, packedStart, packedEnd, remainder, midStart, midEnd;
int outSize;
UBYTE *packed, *packedAlloc;
DNA *dna;
/* get sequence header information, which is cached */
dnaUtilOpen();
struct twoBit *twoBit = getTwoBitSeqHeader(tbf, name);
/* validate range. */
if (fragEnd == 0)
fragEnd = twoBit->size;
if (fragEnd > twoBit->size)
errAbort("twoBitReadSeqFrag in %s end (%d) >= seqSize (%d)", name, fragEnd, twoBit->size);
outSize = fragEnd - fragStart;
if (outSize < 1)
errAbort("twoBitReadSeqFrag in %s start (%d) >= end (%d)", name, fragStart, fragEnd);
/* Allocate dnaSeq, and fill in zero tag at end of sequence. */
AllocVar(seq);
if (outSize == twoBit->size)
seq->name = cloneString(name);
else
{
char buf[256*2];
safef(buf, sizeof(buf), "%s:%d-%d", name, fragStart, fragEnd);
seq->name = cloneString(buf);
}
seq->size = outSize;
dna = seq->dna = needLargeMem(outSize+1);
seq->dna[outSize] = 0;
/* Skip to bits we need and read them in. */
packedStart = (fragStart>>2);
packedEnd = ((fragEnd+3)>>2);
packByteCount = packedEnd - packedStart;
packed = packedAlloc = needLargeMem(packByteCount);
(*tbf->ourSeekCur)(f, packedStart);
(*tbf->ourMustRead)(f, packed, packByteCount);
/* Handle case where everything is in one packed byte */
if (packByteCount == 1)
{
int pOff = (packedStart<<2);
int pStart = fragStart - pOff;
int pEnd = fragEnd - pOff;
UBYTE partial = *packed;
assert(pEnd <= 4);
assert(pStart >= 0);
for (i=pStart; i<pEnd; ++i)
*dna++ = valToNt[(partial >> (6-i-i)) & 3];
}
else
{
/* Handle partial first packed byte. */
midStart = fragStart;
remainder = (fragStart&3);
if (remainder > 0)
{
UBYTE partial = *packed++;
int partCount = 4 - remainder;
for (i=partCount-1; i>=0; --i)
{
dna[i] = valToNt[partial&3];
partial >>= 2;
}
midStart += partCount;
dna += partCount;
}
/* Handle middle bytes. */
remainder = fragEnd&3;
midEnd = fragEnd - remainder;
for (i=midStart; i<midEnd; i += 4)
{
UBYTE b = *packed++;
dna[3] = valToNt[b&3];
b >>= 2;
dna[2] = valToNt[b&3];
b >>= 2;
dna[1] = valToNt[b&3];
b >>= 2;
dna[0] = valToNt[b&3];
dna += 4;
}
if (remainder >0)
{
UBYTE part = *packed;
part >>= (8-remainder-remainder);
for (i=remainder-1; i>=0; --i)
{
dna[i] = valToNt[part&3];
part >>= 2;
}
}
}
freez(&packedAlloc);
if (twoBit->nBlockCount > 0)
{
int startIx = findGreatestLowerBound(twoBit->nBlockCount, twoBit->nStarts, fragStart);
for (i=startIx; i<twoBit->nBlockCount; ++i)
{
int s = twoBit->nStarts[i];
int e = s + twoBit->nSizes[i];
if (s >= fragEnd)
break;
if (s < fragStart)
s = fragStart;
if (e > fragEnd)
e = fragEnd;
if (s < e)
memset(seq->dna + s - fragStart, 'n', e - s);
}
}
if (doMask)
{
toUpperN(seq->dna, seq->size);
if (twoBit->maskBlockCount > 0)
{
int startIx = findGreatestLowerBound(twoBit->maskBlockCount, twoBit->maskStarts,
fragStart);
for (i=startIx; i<twoBit->maskBlockCount; ++i)
{
int s = twoBit->maskStarts[i];
int e = s + twoBit->maskSizes[i];
if (s >= fragEnd)
break;
if (s < fragStart)
s = fragStart;
if (e > fragEnd)
e = fragEnd;
if (s < e)
toLowerN(seq->dna + s - fragStart, e - s);
}
}
}
if (retFullSize != NULL)
*retFullSize = twoBit->size;
return seq;
}
struct dnaSeq *twoBitReadSeqFrag(struct twoBitFile *tbf, char *name,
int fragStart, int fragEnd)
/* Read part of sequence from .2bit file. To read full
* sequence call with start=end=0. Note that sequence will
* be mixed case, with repeats in lower case and rest in
* upper case. */
{
return twoBitReadSeqFragExt(tbf, name, fragStart, fragEnd, TRUE, NULL);
}
struct dnaSeq *twoBitReadSeqFragLower(struct twoBitFile *tbf, char *name,
int fragStart, int fragEnd)
/* Same as twoBitReadSeqFrag, but sequence is returned in lower case. */
{
return twoBitReadSeqFragExt(tbf, name, fragStart, fragEnd, FALSE, NULL);
}
int twoBitSeqSize(struct twoBitFile *tbf, char *name)
/* Return size of sequence in two bit file in bases. */
{
twoBitSeekTo(tbf, name);
return (*tbf->ourReadBits32)(tbf->f, tbf->isSwapped);
}
long long twoBitTotalSize(struct twoBitFile *tbf)
/* Return total size of all sequences in two bit file. */
{
struct twoBitIndex *index;
long long totalSize = 0;
for (index = tbf->indexList; index != NULL; index = index->next)
{
(*tbf->ourSeek)(tbf->f, index->offset);
totalSize += (*tbf->ourReadBits32)(tbf->f, tbf->isSwapped);
}
return totalSize;
}
struct dnaSeq *twoBitLoadAll(char *spec)
/* Return list of all sequences matching spec, which is in
* the form:
*
* file/path/input.2bit[:seqSpec1][,seqSpec2,...]
*
* where seqSpec is either
* seqName
* or
* seqName:start-end */
{
struct twoBitSpec *tbs = twoBitSpecNew(spec);
struct twoBitFile *tbf = twoBitOpen(tbs->fileName);
struct dnaSeq *list = NULL;
if (tbs->seqs != NULL)
{
struct twoBitSeqSpec *tbss;
for (tbss = tbs->seqs; tbss != NULL; tbss = tbss->next)
slSafeAddHead(&list, twoBitReadSeqFrag(tbf, tbss->name,
tbss->start, tbss->end));
}
else
{
struct twoBitIndex *index;
for (index = tbf->indexList; index != NULL; index = index->next)
slSafeAddHead(&list, twoBitReadSeqFrag(tbf, index->name, 0, 0));
}
slReverse(&list);
twoBitClose(&tbf);
twoBitSpecFree(&tbs);
return list;
}
struct slName *twoBitSeqNames(char *fileName)
/* Get list of all sequences in twoBit file. */
{
struct twoBitFile *tbf = twoBitOpen(fileName);
struct twoBitIndex *index;
struct slName *name, *list = NULL;
for (index = tbf->indexList; index != NULL; index = index->next)
{
name = slNameNew(index->name);
slAddHead(&list, name);
}
twoBitClose(&tbf);
slReverse(&list);
return list;
}
boolean twoBitIsFile(char *fileName)
/* Return TRUE if file is in .2bit format. */
{
boolean useUdc = FALSE;
if (hasProtocol(fileName))
useUdc = TRUE;
else if (!isRegularFile(fileName))
return FALSE;
struct twoBitFile *tbf = getTbfAndOpen(fileName, useUdc);
boolean isSwapped;
boolean isTwoBit = twoBitSigRead(tbf, &isSwapped);
(*tbf->ourClose)(&tbf->f);
return isTwoBit;
}