-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccp4.ts
159 lines (137 loc) · 5.03 KB
/
ccp4.ts
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
/*
* Copyright (c) 2016 - now, David Sehnal, licensed under Apache 2.0, See LICENSE file for more info.
*/
import * as File from '../common/file'
import * as DataFormat from '../common/data-format'
export const enum Mode { Int8 = 0, Int16 = 1, Float32 = 2 }
export interface Header {
name: string,
mode: Mode,
grid: number[], // grid is converted to the axis order!!
axisOrder: number[],
extent: number[],
origin: number[],
spacegroupNumber: number,
cellSize: number[],
cellAngles: number[],
littleEndian: boolean,
dataOffset: number
}
/** Represents a circular buffer for 2 * blockSize layers */
export interface SliceBuffer {
buffer: File.TypedArrayBufferContext,
sliceCapacity: number,
slicesRead: number,
values: DataFormat.ValueArray,
sliceCount: number,
/** Have all the input slice been read? */
isFinished: boolean
}
export interface Data {
header: Header,
file: number,
slices: SliceBuffer
}
export function getValueType(header: Header) {
if (header.mode === Mode.Float32) return DataFormat.ValueType.Float32;
if (header.mode === Mode.Int16) return DataFormat.ValueType.Int16;
return DataFormat.ValueType.Int8;
}
export function assignSliceBuffer(data: Data, blockSize: number) {
const { extent } = data.header;
const valueType = getValueType(data.header);
const sliceSize = extent[0] * extent[1] * DataFormat.getValueByteSize(valueType);
const sliceCapacity = Math.max(1, Math.floor(Math.min(64 * 1024 * 1024, sliceSize * extent[2]) / sliceSize));
const buffer = File.createTypedArrayBufferContext(sliceCapacity * extent[0] * extent[1], valueType);
data.slices = {
buffer,
sliceCapacity,
slicesRead: 0,
values: buffer.values,
sliceCount: 0,
isFinished: false
};
}
function compareProp(a: any, b: any) {
if (a instanceof Array && b instanceof Array) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
return a === b;
}
export function compareHeaders(a: Header, b: Header) {
for (const p of ['grid', 'axisOrder', 'extent', 'origin', 'spacegroupNumber', 'cellSize', 'cellAngles', 'mode']) {
if (!compareProp((a as any)[p], (b as any)[p])) return false;
}
return true;
}
function getArray(r: (offset: number) => number, offset: number, count: number) {
const ret: number[] = [];
for (let i = 0; i < count; i++) {
ret[i] = r(offset + i);
}
return ret;
}
async function readHeader(name: string, file: number) {
const headerSize = 1024;
const { buffer: data } = await File.readBuffer(file, 0, headerSize);
let littleEndian = true;
let mode = data.readInt32LE(3 * 4);
if (mode < 0 || mode > 2) {
littleEndian = false;
mode = data.readInt32BE(3 * 4, true);
if (mode < 0 || mode > 2) {
throw Error('Only CCP4 modes 0, 1, and 2 are supported.');
}
}
const readInt = littleEndian ? (o: number) => data.readInt32LE(o * 4) : (o: number) => data.readInt32BE(o * 4);
const readFloat = littleEndian ? (o: number) => data.readFloatLE(o * 4) : (o: number) => data.readFloatBE(o * 4);
const origin2k = getArray(readFloat, 49, 3);
const nxyzStart = getArray(readInt, 4, 3);
const header: Header = {
name,
mode,
grid: getArray(readInt, 7, 3),
axisOrder: getArray(readInt, 16, 3).map(i => i - 1),
extent: getArray(readInt, 0, 3),
origin: origin2k[0] === 0.0 && origin2k[1] === 0.0 && origin2k[2] === 0.0 ? nxyzStart : origin2k,
spacegroupNumber: readInt(22),
cellSize: getArray(readFloat, 10, 3),
cellAngles: getArray(readFloat, 13, 3),
// mean: readFloat(21),
littleEndian,
dataOffset: headerSize + readInt(23) /* symBytes */
};
// "normalize" the grid axis order
header.grid = [header.grid[header.axisOrder[0]], header.grid[header.axisOrder[1]], header.grid[header.axisOrder[2]]];
return header;
}
export async function readSlices(data: Data) {
const { slices, header } = data;
if (slices.isFinished) {
return;
}
const { extent } = header;
const sliceSize = extent[0] * extent[1];
const sliceByteOffset = slices.buffer.elementByteSize * sliceSize * slices.slicesRead;
const sliceCount = Math.min(slices.sliceCapacity, extent[2] - slices.slicesRead);
const sliceByteCount = sliceCount * sliceSize;
await File.readTypedArray(slices.buffer, data.file, header.dataOffset + sliceByteOffset, sliceByteCount, 0, header.littleEndian);
slices.slicesRead += sliceCount;
slices.sliceCount = sliceCount;
if (slices.slicesRead >= extent[2]) {
slices.isFinished = true;
}
}
export async function open(name: string, filename: string): Promise<Data> {
const file = await File.openRead(filename);
const header = await readHeader(name, file);
return {
header,
file,
slices: void 0 as any
};
}