-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_swc.m
97 lines (92 loc) · 3.48 KB
/
load_swc.m
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
function [swc_data, name, color] = load_swc(swc_file_name)
%LOADSWC reads a JW formatted swc file and return color/header and data
%fields
%
% [OUTPUTARGS] = LOADSWC(INPUTARGS) Explain usage here
%
% Examples:
% [swcData,offset,color, header] = loadSWC(swcfile);
% swcData(:,3:5) = swcData(:,3:5) + ones(size(swcData,1),1)*offset;
% swcData(:,3:5) = swcData(:,3:5)*scale;
%
% Provide sample usage code here
%
% See also: List related files here
% $Author: base $ $Date: 2015/08/14 12:09:52 $ $Revision: 0.1 $
% Copyright: HHMI 2015
%% parse a swc file
offset = [0 0 0] ;
header = cell(1);
fid = fopen(swc_file_name);
this_line = fgets(fid);
skipline = 0;
header_line_count = 1;
centerpoint_count = 0 ;
was_name_specified = false ;
was_color_specified = false ;
while ischar(this_line)
% assumes that all header info is at the top of the swc file
if strcmp(this_line(1), '#')
skipline = skipline + 1;
header{header_line_count} = this_line;
header_line_count = header_line_count+1;
else
centerpoint_count = centerpoint_count + 1 ;
this_line = fgets(fid);
continue;
%break
end
% if get here, tline contains a header line
tokens = strsplit(deblank(this_line)) ;
token_count = length(tokens) ;
if token_count>=2 && isequal(tokens{2}, 'OFFSET') ,
tokens = strsplit(deblank(this_line)) ;
tokens_as_double = cellfun(@str2double, tokens) ;
numeric_tokens = tokens_as_double(~isnan(tokens_as_double)) ;
if isequal(size(numeric_tokens), [1 3]) ,
offset = numeric_tokens ;
else
error('Unable to parse OFFSET header line: "%s"', this_line) ;
end
elseif token_count>=2 && isequal(tokens{2}, 'COLOR') ,
indices_of_tag = strfind(this_line, 'COLOR') ;
start_of_tag_index = indices_of_tag(1) ;
just_after_tag_index = start_of_tag_index + length('COLOR') ;
rest_of_line = this_line(just_after_tag_index:end) ;
color = cellfun(@str2double,strsplit(deblank(rest_of_line),','));
was_color_specified = true ;
elseif token_count>=2 && isequal(tokens{2}, 'NAME') ,
indices_of_tag = strfind(this_line, 'NAME') ;
start_of_tag_index = indices_of_tag(1) ;
just_after_tag_index = start_of_tag_index + length('NAME') ;
rest_of_line = this_line(just_after_tag_index:end) ;
name = deblank(rest_of_line) ;
was_name_specified = ~isempty(name) ;
end
this_line = fgets(fid);
end
fclose(fid);
fid = fopen(swc_file_name);
for i=1:skipline
this_line = fgets(fid); %#ok<NASGU>
end
swc_data = zeros(centerpoint_count,7) ;
this_line = fgets(fid);
tl = 1;
while ischar(this_line)
swc_data(tl,:) = str2num(this_line); %#ok<ST2NM>
tl = tl+1;
this_line = fgets(fid);
end
fclose(fid);
raw_centerpoints = swc_data(:,3:5) ;
centerpoints = bsxfun(@plus, raw_centerpoints, offset) ;
swc_data(:,3:5) = centerpoints ;
if ~was_color_specified,
color = [ 1 0 1 ] ; % magenta. what the hell...
end
if ~was_name_specified,
random_number = floor(1e6 * rand(1)) ;
name = sprintf('unnamed-%06d', random_number) ;
end
end