FCS (.fcs, .lmd)
背景
-
- FCS 分子生物学格式.
- 存储和交换流式细胞术检测数据的标准格式.
- 混合式 ASCII/二进制格式.
- 把细胞事件的信号强度存储为一个数组.
- 关于数据采集的元信息存储在文件中.
Import 与 Export
- Import["file.fcs"] 返回一个表示存储在文件中的细胞事件的数组.
- Export["file.fcs",{paramnames,events}] 把一个参数名称列表和事件数组导出为 FCS 格式.
- Export["file.fcs",{paramnames,paramreagents,events}] 把一个参数名称列表、参数试剂列表和事件数组导出为 FCS 格式.
- Import["file.fcs",elem] 从一个 FCS 文件中导入指定的参数.
- Import["file.fcs",{{elem1,elem2,…}}] 导入多个参数.
- 导入格式可用 Import["file","FCS"] 或 Import["file",{"FCS",elem,…}] 指定.
- Export["file.fcs",{expr1,expr2,…},{{elem1,elem2,…}}] 把每一个 expri 指定为相应的 elemi.
- Export["file.fcs",expr,opt1->val1,…] 导出具有指定值的指定选项参数的 expr.
- Export["file.fcs",{elem1->expr1,elem2->expr2,…},"Rules"] 使用规则指定要导出的参数.
- 请到以下参考页面了解完整的基本信息:
-
Import, Export 从文件导入或导出到文件 CloudImport, CloudExport 从云对象导入或导出到云对象 ImportString, ExportString 从字符串导入或导出到字符串 ImportByteArray, ExportByteArray 从字节数组导入或导出到字节数组
参数
- Import 的通用参数:
-
"Elements" 该文件可用的参数和选项列表 "Summary" 文件摘要 "Rules" 所有可用参数的规则列表 - 文件元数据:
-
"Header" 以规则列表形式给出的原文件标头 - 表示数据的参数:
-
"Events" 以一个数组表示的细胞事件 "ParameterName" 以一个规则列表(导入)或列表(导出)表示的参数名称 "ParameterReagent" 以一个规则列表(导入)或列表(导出)表示的参数试剂 - 其他数据参数:
-
"Data" 以一个数组表示的细胞事件 "LabeledData" 存储在文件中的数据规则列表 - 默认情况下,对于 FCS 格式,Import 使用 "Events" 参数.
- Export 至少需要一个参数名称列表和一个事件数组.
- Export 可有选择地接受参数试剂列表. 其他可选关键字必须通过 "OptionalKeys" 选项指定.
选项
- 高级 Export 选项:
-
"DataType" Automatic 用于编码事件的数据类型 "OptionalKeys" {} 包含在标头中的可选关键字,以规则列表表示 - Wolfram 语言尝试识别编码事件的最紧凑格式. 自动数据类型识别可通过 "DataType" 选项被重写.
- "DataType" 的有效设置为:
-
"UnsignedInteger8" 8位无符号整数 "UnsignedInteger16" 16位无符号整数 "UnsignedInteger24" 24位无符号整数 "UnsignedInteger32" 32位无符号整数 "Real32" IEEE 单精度实数 "Real64" IEEE d双精度实数 - Wolfram 语言会自动重写某些在 "OptionalKeys" 中指定的被保护的关键字(例如, "$BEGINDATA" 和 "$BYTEORD"). 因此,如果你重新导出一个已导入的 FCS 文件,你可以直接设置 "OptionalKeys" 为导入的 FCS 文件的 "Header" 参数值. 参数试剂("$PnS" 关键字)会从 "OptionalKeys" 中删除仅当参数试剂是在主要的 Export 表达式中提供.
范例
打开所有单元 关闭所有单元基本范例 (7)
Import["ExampleData/5k.fcs", {"FCS", "Header"}]//Short[#, 3]&Import["ExampleData/5k.fcs", {"FCS", {"ParameterReagent", "ParameterName"}}]{events, params} = Import["ExampleData/5k.fcs", {"FCS", {"Events", "ParameterName"}}];
TableForm[Transpose[events[[ ;; 5]]], TableHeadings -> {params[[All, 2]], None}]ExportString[{{"FSC-A", "SSC-A"}, {{1, 2}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}}, "FCS"]ExportString[{{"FSC-A", "SSC-A"}, {{1, 2}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}}, "FCS", "DataType" -> "UnsignedInteger16"]ExportString[{{"FSC-A", "SSC-A"}, {"P1", "P2"}, {{1, 2}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}}, "FCS"]导出一个具有同样标头的数据文件版本,但是带有事件的更小子集:
{pns, evs, head} = Import["ExampleData/5k.fcs", {"FCS", {"ParameterName", "Events", "Header"}}];ExportString[{"ParameterName" -> pns[[All, 2]], "Events" -> evs[[ ;; 5]]}, "FCS", "OptionalKeys" -> head]范围 (6)
events = Import["ExampleData/5k.fcs", {"FCS", "Events"}];
DensityHistogram[events[[All, {2, 1}]], 100, AspectRatio -> 1, AxesOrigin -> {-200, -200}, PlotRange -> Full, FrameLabel -> {"FSC-W", "FSC-A"}]将具有 ArcSinh 变换轴的事件绘制为阴影等值线图:
FACSplot[events_, xparam_, yparam_, cofactors_, opts___] := Block[{asinh, ticks, range, majorTicks},
asinh[x_] := ArcSinh[x / cofactors];
majorTicks = {asinh[#], #, Scaled[0.00005]}& /@ (10 ^ Range[Log10[1], Log10[100000]]);
ticks = Join[
(*Major labeled ticks*)
majorTicks, -majorTicks,
(*Minor unlabeled ticks*){asinh[#], ""}& /@ Flatten@Outer[Times, 10 ^ Range[0, 4], Join[Range[-9, -1], Range[9]]]
];
range = {ArcSinh[-200 / cofactors], ArcSinh[10000 / cofactors]};
SmoothDensityHistogram[ArcSinh[# / cofactors]& /@ events[[ ;; 5000, {xparam, yparam}]],
PlotRange -> {range, range},
ColorFunction -> ColorData["Rainbow"],
Mesh -> 10,
FrameTicks -> {{ticks, None}, {ticks, None}},
RegionFunction -> (#3 > 0.01&),
opts
]
]events = Import["ExampleData/5k.fcs", {"FCS", "Events"}];
FACSplot[events, 6, 9, 150, FrameLabel -> Text /@ Style /@ {"B220", "CD90"}, ImageSize -> 325]使用 FindClusters 自动检测细胞的类似分组:
events = Import["ExampleData/5k.fcs", {"FCS", "Events"}];
xticks = Join[
(*Major labeled ticks*)
{ArcSinh[# / 150], #}& /@ (10 ^ Range[Log10[1], Log10[100000]]),
-{ArcSinh[# / 150], #}& /@ (10 ^ Range[Log10[1], Log10[100000]]),
(*Minor unlabeled ticks*)
{ArcSinh[# / 150], ""}& /@
Flatten@Outer[Times, 10 ^ Range[0, 4], Join[Range[-9, -1], Range[9]]]
];
ListPlot[FindClusters[ArcSinh[# / 150]& /@ events[[All, {6, 9}]], 3], AspectRatio -> 1, AxesOrigin -> {ArcSinh[-200 / 150], ArcSinh[-200 / 150]}, PlotRange -> Full, AxesLabel -> {Text@Style@"B220", Text@Style@"CD90"}, Ticks -> {xticks, xticks}]{params, events} = Import["ExampleData/5k.fcs", {"FCS", {"ParameterReagent", "Events"}}];(*Global plot range*)
pr = Round[ArcSinh[# / 150]& /@ {Min[events[[All, 4 ;; 8]]], Max[events[[All, 4 ;; 8]]]}, 0.25];
labels = Text[Style[#]]& /@ params[[4 ;; 8, 2]];
Grid[
Join[Partition[{Null}~Join~(Rotate[#, π / 2]& /@ labels), 1],
Join[{labels},
Table[ListPlot[ArcSinh[# / 150]& /@ events[[All, {y, x}]], AspectRatio -> 1, ImageSize -> {80, 80}, FrameTicks -> None, Frame -> True, PlotRange -> {pr, pr}, AxesOrigin -> {-0.5, -0.5}], {x, 4, 8}, {y, 4, 8}]], 2]]{strings, params, events} = Import["ExampleData/5k.fcs", {"FCS", {"ParameterReagent", "ParameterName", "Events"}}];(*Ray casting algorithm*)
PointInPolygonQ[{x_, y_}, poly_] := PointInPolygonQi[x, y, poly];
PointInPolygonQi = Compile[{{ptx, _Real}, {pty, _Real}, {poly, _Real, 2}}, Module[{i, j, c = False, vertx, verty},
{vertx, verty} = Transpose@poly;
For[i = 1;j = Length[vertx], i ≤ Length[vertx], j = i++,
If[(((verty[[i]] > pty) ≠ (verty[[j]] > pty)) && (ptx < (vertx[[j]] - vertx[[i]]) * (pty - verty[[i]]) / (verty[[j]] - verty[[i]]) + vertx[[i]])), c = !c];
];
Return[c]
]];(*Use reagent names when available*)
params = Table[strings[[i, 2]] /. " " -> params[[i, 2]], {i, Length@strings}];is = ImageSize -> {300, 300};
twoDopts = {PlotRange -> {{-.5, 7}, {-.5, 7}}, is, AspectRatio -> 1, AxesOrigin -> {-0.5, -0.5}};
Manipulate[
Column[
{
Labeled[Overlay[{
ListPlot[Map[ArcSinh[# / 150]&, events[[All, {Position[params, x1][[1, 1]], Position[params, y1][[1, 1]]}]], {2}],
twoDopts
],
Graphics[{EdgeForm[Thick], Opacity[0.5], Red, Polygon[pt]}, Frame -> True, twoDopts]
(*Note that we're showing the frame, ticks and labels for both parts of the overlay to provide visual verification that they're overlaid properly*)
}, is], {x1, y1}, {Bottom, Left}, RotateLabel -> True],
subpop = Pick[events[[All, {Position[params, x2][[1, 1]], Position[params, y2][[1, 1]]}]], Map[PointInPolygonQ[#, pt]&, Map[ArcSinh[# / 150]&, events[[All, {Position[params, x1][[1, 1]], Position[params, y1][[1, 1]]}]], {2}]]];
If[SecondDisplay == "2D",
If[Length@subpop > 0,
Labeled[
ListPlot[Map[ArcSinh[# / 150]&, subpop, {2}], twoDopts], {x2, y2}, {Bottom, Left}, RotateLabel -> True], Graphics[{White, Rectangle[]}, is]],
Labeled[SmoothHistogram[Map[ArcSinh[# / 150]&, subpop[[All, 1]]], AspectRatio -> 1, is], x2, Bottom]
]
}
]
,
{{pt, {{-.25, 1.55}, {-.25, 3.25}, {0.5, 3.75}, {1.1, 3.25}, {1.1, 1.75}, {.5, 1.5}}}, Locator, LocatorAutoCreate -> True},
{{x1, params[[6]]}, params, ControlType -> PopupMenu},
{{y1, params[[9]]}, params, ControlType -> PopupMenu},
{{x2, params[[11]]}, params, ControlType -> PopupMenu},
{{y2, params[[7]]}, params, ControlType -> PopupMenu},
{{SecondDisplay, "1D", "Secondary Plot Type"}, {"2D", "1D"}},
ControlPlacement -> {Automatic, Top, Top, Bottom, Bottom, Bottom},
SaveDefinitions -> True
]ExportString[{"ParameterName" -> {"FSC-A", "SSC-A"}, "Events" -> {{1, -2}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}}, "FCS"]ExportString[{"ParameterName" -> {"FSC-A", "SSC-A"}, "Events" -> {{1, -2}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}}, "FCS", "DataType" -> "Real32"]历史
2012年引入 (9.0)