3 -- Description: string split
4 function string:split(sep
, n
)
5 local a
, start
= {}, 1;
8 local b
, e
= self
:find(sep
, start
);
10 table.insert(a
, self
:sub(start
));
13 a
[#a
+1] = self
:sub(start
, b
- 1);
16 table.insert(a
, self
:sub(start
));
23 -- Description: intelligent file open
24 function io
.xopen(fn
, mode
)
26 if fn
== nil then return io
.stdin
;
27 elseif fn
== '-' then return (mode
== 'r' and io
.stdin
) or io
.stdout
;
28 elseif fn
:sub(-3) == '.gz' then return (mode
== 'r' and io
.popen('gzip -dc ' .. fn
, 'r')) or io
.popen('gzip > ' .. fn
, 'w');
29 elseif fn
:sub(-4) == '.bz2' then return (mode
== 'r' and io
.popen('bzip2 -dc ' .. fn
, 'r')) or io
.popen('bgzip2 > ' .. fn
, 'w');
30 else return io
.open(fn
, mode
) end
34 print("Usage: bc2rg.lua <barcode.list> <in.sam> <libname>");
41 local nuc
= {'A', 'C', 'G', 'T'};
42 local fp
= io
.xopen(arg
[1]);
43 for line
in fp
:lines() do
44 local bc
, sam
= line
:match("^(%S+)%s+(%S+)");
45 local rg
= sam
..'-'..arg
[3]..'-'..bc
;
47 table.insert(hdr
, "@RG\tID:"..rg
.."\tSM:"..sam
.."\tLB:"..rg
);
50 if nuc
[j
] ~= bc
:sub(i
, i
) then
51 local b
= bc
:sub(1, i
-1) .. nuc
[j
] .. bc
:sub(i
+1);
57 table.insert(hdr
, "@RG\tID:N/A\tSM:N/A\tLB:N/A");
60 -- process the SAM file
61 fp
= io
.xopen(arg
[2]);
63 for l
in fp
:lines() do
64 if l
:sub(1,1) == '@' then
68 print(table.concat(hdr
, "\n"));
71 local bc
= l
:match("BC:Z:(%S+)"):upper();
72 if bc
== nil or hash
[bc
] == nil then
75 print(l
, "RG:Z:" .. hash
[bc
]);