parser.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ReaderResult as Result } from '../result';
  7. import { Task } from '../../../mol-task';
  8. import { parseCsv } from '../csv/parser';
  9. import { Column, Table } from '../../../mol-data/db';
  10. import { toTable } from '../cif/schema';
  11. import Schema = Column.Schema
  12. import { CsvTable } from '../csv/data-model';
  13. export const Schema3DG = {
  14. /** Chromosome name */
  15. chromosome: Schema.str,
  16. /** Base position */
  17. position: Schema.int,
  18. /** X coordinate */
  19. x: Schema.float,
  20. /** Y coordinate */
  21. y: Schema.float,
  22. /** Z coordinate */
  23. z: Schema.float,
  24. };
  25. export type Schema3DG = typeof Schema3DG
  26. export interface File3DG {
  27. table: Table<Schema3DG>
  28. }
  29. const FieldNames = [ 'chromosome', 'position', 'x', 'y', 'z' ];
  30. function categoryFromTable(name: string, table: CsvTable) {
  31. return {
  32. name,
  33. rowCount: table.rowCount,
  34. fieldNames: FieldNames,
  35. getField: (name: string) => {
  36. return table.getColumn(FieldNames.indexOf(name).toString());
  37. }
  38. };
  39. }
  40. export function parse3DG(data: string) {
  41. return Task.create<Result<File3DG>>('Parse 3DG', async ctx => {
  42. const opts = { quote: '', comment: '#', delimiter: '\t', noColumnNames: true };
  43. const csvFile = await parseCsv(data, opts).runInContext(ctx);
  44. if (csvFile.isError) return Result.error(csvFile.message, csvFile.line);
  45. const category = categoryFromTable('3dg', csvFile.result.table);
  46. const table = toTable(Schema3DG, category);
  47. return Result.success({ table });
  48. });
  49. }