cli.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import chalk from 'chalk';
  2. import { getFromAcc } from '.'
  3. import blessed from 'blessed'
  4. import figlet from 'figlet'
  5. import fs from 'fs'
  6. const formatSeq = (seq:string) => {
  7. return seq.split('').map(c => {
  8. let r
  9. switch (c) {
  10. case 'A':
  11. r = chalk.green(c)
  12. break;
  13. case 'T':
  14. r = chalk.red(c)
  15. break;
  16. case 'C':
  17. r = chalk.blue(c)
  18. break;
  19. case 'G':
  20. r = chalk.yellow(c)
  21. break;
  22. case '.':
  23. //r = chalk.hex('#FF0000').bgBlue(' ')
  24. // r = chalk.bgHex('#DEADED').underline(' ')
  25. // break;
  26. r = c
  27. default:
  28. r = c
  29. break;
  30. }
  31. return r
  32. }).join('')
  33. }
  34. const combineSTr = (strings: string[], lineN:number) => {
  35. const splittedSts: RegExpMatchArray[] = []
  36. const r = new RegExp(".{1," + lineN + "}","g");
  37. for (const s of strings) {
  38. const tmpLines = s.match(r) || []
  39. const resLines = []
  40. for (const line of tmpLines) resLines.push(line.length < lineN ? line + ' '.repeat(lineN - line.length) : line)
  41. splittedSts.push(resLines)
  42. }
  43. const firstLines = splittedSts.shift() || []
  44. return firstLines.map((e,i) => formatSeq(e.toUpperCase()) + formatSeq(splittedSts.map(ee => ee[i]).join(''))).join('')
  45. }
  46. (async()=>{
  47. const arg = process.argv.slice(2)
  48. console.log(chalk.blue('Welcome to gbffParser!'))
  49. if (arg.length === 0 || arg.length > 1) {
  50. console.log(chalk.red('Usage : cli.js <Accession>'))
  51. } else {
  52. const screen = blessed.screen({
  53. smartCSR: true,
  54. dockBorders: true,
  55. autoPadding: true
  56. })
  57. screen.title = 'gbffParser'
  58. const mainBox = blessed.box({
  59. top: '15%',
  60. left: '10%',
  61. width: '90%',
  62. height: '85%',
  63. content: '',
  64. tags: true,
  65. border: {
  66. type: 'line'
  67. },
  68. scrollable: true,
  69. scrollbar: {
  70. style: {bg: 'blue'},
  71. },
  72. keys: true,
  73. mouse: false,
  74. style: {
  75. fg: 'white',
  76. border: {
  77. fg: '#f0f0f0'
  78. }
  79. }
  80. })
  81. const topBox = blessed.box({
  82. top: 0,
  83. left: 0,
  84. width: '100%',
  85. height: '15%',
  86. content: 'top',
  87. tags: true,
  88. border: {
  89. type: 'line'
  90. },
  91. scrollable: false,
  92. keys: true,
  93. mouse: false,
  94. style: {
  95. fg: 'white',
  96. border: {
  97. fg: '#f0f0f0'
  98. }
  99. }
  100. })
  101. const leftBox = blessed.box({
  102. top: '15%',
  103. left: 0,
  104. width: '10%',
  105. height: '85%',
  106. content: 'left',
  107. tags: true,
  108. border: {
  109. type: 'line'
  110. },
  111. scrollable: false,
  112. keys: true,
  113. mouse: false,
  114. style: {
  115. fg: 'white',
  116. border: {
  117. fg: '#f0f0f0'
  118. }
  119. }
  120. })
  121. screen.append(topBox)
  122. screen.append(leftBox)
  123. screen.append(mainBox)
  124. screen.key(['escape', 'q', 'C-c'], (ch, key) => process.exit(0))
  125. const dbPath = [1,2,3,4,5,6,7,8,9,10].map(n => '/home/thomas/NGS/ref/ncbi/RNA/human.' + n + '.rna.gbff')
  126. const result = await getFromAcc(arg[0], dbPath)
  127. if (result) {
  128. await fs.promises.writeFile('tmp.json', JSON.stringify(result))
  129. const title = result.features.filter(feature => feature.type === 'gene')[0]?.name || arg[0]
  130. topBox.setContent(figlet.textSync(title))
  131. mainBox.setContent(result.sequence)
  132. mainBox.focus();
  133. const lineN = mainBox.getScreenLines()[0].length
  134. mainBox.setContent(combineSTr([result.sequence, result.sequence.split('').map(e => '.').join('')], lineN))
  135. screen.render();
  136. }
  137. }
  138. })()