index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/taxonomic_divisions/uniprot_sprot_human.xml.gz
  2. import fs from 'fs'
  3. import readline from 'readline'
  4. import { XMLParser } from 'fast-xml-parser'
  5. import jsonata from 'jsonata'
  6. import { text } from 'stream/consumers'
  7. const line$ = (path: string) => readline.createInterface({
  8. input: fs.createReadStream(path),
  9. crlfDelay: Infinity
  10. })
  11. const makeIndex = async (filePath: string, indexPath?: string) => {
  12. indexPath = indexPath || filePath + '.jsi'
  13. let byteAcc = 0
  14. const fromSel = new RegExp("^<entry")
  15. const toSel = new RegExp("^</entry>")
  16. const valSel = new RegExp('<accession>')
  17. let tmp = {values:[]} as any
  18. for await (const line of line$(filePath)) {
  19. if(fromSel.test(line)) tmp['from'] = byteAcc
  20. byteAcc += (line.length + 1)
  21. if (valSel.test(line)) tmp['values'].push(line.match("<accession>(.*?)</accession>")![1]) // 'uck
  22. if(toSel.test(line)) {
  23. await fs.promises.appendFile(indexPath, tmp.values.join(';') + '\t' + tmp.from + '\t' + byteAcc + '\n')
  24. tmp = {values:[]}
  25. }
  26. }
  27. }
  28. const readOffset = (path: string, from:number, to:number) => {
  29. return new Promise<string>(async (resolve, reject) => {
  30. const size = to - from
  31. const buffer = Buffer.alloc(size);
  32. let filehandle
  33. try {
  34. filehandle = await fs.promises.open(path, 'r+');
  35. await filehandle.read(buffer, 0, buffer.length, from);
  36. } finally {
  37. if (filehandle) {
  38. await filehandle.close()
  39. resolve(buffer.toString())
  40. }
  41. }
  42. })
  43. }
  44. const getEntryOffset = async (dbPath:string, accession:string): Promise<number[]> => {
  45. const indexPath = dbPath + '.jsi'
  46. if (!fs.existsSync(indexPath)) await makeIndex(dbPath)
  47. const lineSel = new RegExp(accession)
  48. for await (const line of line$(indexPath)) {
  49. if (lineSel.test(line)) return [Number(line.split('\t')[1]),Number(line.split('\t')[2])]
  50. }
  51. return [0, 0]
  52. }
  53. const getEntry = async (dbPath:string, accession:string) => {
  54. const parser = new XMLParser({
  55. ignoreAttributes: false,
  56. alwaysCreateTextNode: false,
  57. attributeNamePrefix: "",
  58. textNodeName: "value",
  59. allowBooleanAttributes: true,
  60. })
  61. const offsets = await getEntryOffset(dbPath, accession)
  62. return parser.parse(await readOffset(dbPath, offsets[0], offsets[1]))
  63. }
  64. const getEntryFromGeneName = async (idmappingPath: string, dbPath:string, geneName:string) => {
  65. const accessions = await getAccessFromGene(idmappingPath, geneName)
  66. return await getEntry(dbPath, accessions[0]) // seems to be always the first with entry
  67. }
  68. const getAccessFromGene = async (idmappingPath: string, geneName:string) => {
  69. const sel = new RegExp('Gene_Name\t' + geneName)
  70. let accessions: any[] = []
  71. for await (const line of line$(idmappingPath)) {
  72. if(sel.test(line)) accessions.push(line.split('\t')[0])
  73. }
  74. return accessions
  75. }
  76. const getInteractionsFromEntry = async (json:any) => {
  77. const blaskList =
  78. ['DNA', 'PHOSPHOSERINE', 'MOTIFS', 'INFECTION', 'PROTEIN', 'PROTEINS', 'GAMMA-SECRETASE', 'CALCIUM',
  79. 'MICROBIAL', 'VIRUS', 'HEPATITIS', 'HERPES', 'SIMPLEX', 'RELATED', 'AND', 'CLATHRIN', 'WORTMANNIN']
  80. const uniprotIDs = Array.isArray(json.entry.accession) ? json.entry.accession : [json.entry.accession]
  81. // geneName
  82. const gnT = Array.isArray(json.entry.gene.name) ? json.entry.gene.name : [json.entry.gene.name]
  83. const geneName = gnT.filter((e:any)=> e.type === 'primary').map((e:any)=> e.value)[0]
  84. // Interactants
  85. const interactants = json.entry.comment
  86. .filter((e:any)=> e?.type === 'interaction')
  87. .flatMap((e:any) => ({
  88. type : 'interactant',
  89. fromProductId: e.interactant[0].id,
  90. toProductId : e.interactant[1].id,
  91. to : e.interactant[1].label,
  92. nExperiments : Number(e.experiments)
  93. }))
  94. const regExp = new RegExp('INTERACTION WITH |Interacts with |complex with ', 'i')
  95. const geneRegExp = new RegExp(/[A-Z]{1}[A-Z|0-9]{2,}$/)
  96. // uniprot_comment_text_value
  97. const commentInteractsWith = json.entry.comment
  98. .filter((e:any) => e?.text?.value)
  99. .filter((e:any) => regExp.test(e.text.value))
  100. .map((e:any) => ({
  101. to : e.text.value
  102. .split(/\.|;/)
  103. .flatMap((ee:any) => ee.replace(/ *\([^)]*\) */g, ' '))
  104. .filter((ee:any) => regExp.test(ee))
  105. .flatMap((ee:any) => ee.trim().split(regExp))
  106. .flatMap((ee:any) => ee.split(/,| and | /))
  107. .filter((_:any) => _)
  108. .filter((ee:any) => geneRegExp.test(ee))
  109. .filter((ee:any) => !blaskList.includes(ee) && ee !== geneName)
  110. .map((ee:any) => ee.trim()),
  111. text: e.text.value,
  112. //evidences: e.text.evidence.split(' ')//.map((ee:string)=> json.entry.reference.filter((eee:any)=> eee.key === ee)) // Doesnt work with ref key
  113. }))
  114. .flatMap((e:any)=> e.to.flatMap((ee:any) => ({
  115. type : 'uniprot_comment_text_value',
  116. to : ee,
  117. text : e.text
  118. })))
  119. // uniprot_reference_scope
  120. const referenceInteract = json.entry.reference
  121. .map((e:any)=> ({...e, scope : Array.isArray(e.scope) ? e.scope : [e.scope]}))
  122. .filter((e:any) => regExp.test(e.scope.join('')))
  123. .map((e:any)=> ({
  124. to: e.scope
  125. //.split(/\.|;/)
  126. .flatMap((ee:any) => regExp.test(ee) ? [ee] : [])
  127. .filter((_:any) => _)
  128. .flatMap((ee:any) => ee.replace(/ *\([^)]*\) */g, ' '))
  129. .filter((ee:any) => regExp.test(ee))
  130. .flatMap((ee:any) => ee.trim().split(regExp)[1])
  131. .flatMap((ee:any) => ee.split(/,| and | /i))
  132. .filter((_:any) => _)
  133. .filter((ee:any) => geneRegExp.test(ee))
  134. .filter((ee:any) => !blaskList.includes(ee) && ee !== geneName)
  135. .map((ee:any) => ee.trim()),
  136. ...e,
  137. }))
  138. .flatMap((e:any)=> e.to.flatMap((ee:any) => ({
  139. type : 'reference_scope',
  140. to : ee,
  141. scope : e.scope,
  142. //citation: e.citation
  143. })))
  144. // Group
  145. const byTo = {} as {[key:string]: any}
  146. [...interactants, ...referenceInteract, ...commentInteractsWith]
  147. //.map((e:any)=> byTo[e.to] = byTo[e.to] ? [e, ...byTo[e.to]] : [e] )
  148. .map((e:any)=> byTo[e.to] = byTo[e.to] ? {...e, ...byTo[e.to]} : {...e} )
  149. const results = Object.keys(byTo).map((e:any) => {
  150. delete byTo[e]?.to
  151. return {
  152. from: geneName,
  153. to: e,
  154. data: byTo[e]
  155. }
  156. })
  157. .filter((e:any) => !blaskList.includes(e.to) && e.to !== geneName)
  158. await fs.promises.writeFile('test/tmp.json', JSON.stringify(results.map((e:any)=>e.to), null,4))
  159. return results
  160. }
  161. // const findDistance = async (idmappingPath: string, dbPath:string, geneNameA:string, geneNameB:string, maxDistance = 6) => {
  162. // let rounds = [[geneNameA]]
  163. // let tree = {[geneNameA]: {}} as {[key:string]:any}
  164. // let run = true
  165. // let a = tree
  166. // Object.keys(a).map((gene) => )
  167. // let nIter = 0
  168. // while(nIter <= maxDistance && run) {
  169. // for (const gA of rounds[nIter]) {
  170. // console.log(nIter,gA);
  171. // const tmp = await getInteractionsFromEntry(await getEntryFromGeneName(idmappingPath, dbPath, gA))
  172. // if (tmp.includes(geneNameB)) { run = false; break }
  173. // rounds.push(tmp)
  174. // }
  175. // nIter++
  176. // }
  177. // //console.log(rounds);
  178. // return nIter
  179. // }
  180. export { makeIndex, readOffset, getEntry as getEnrty, getEntryFromGeneName, getInteractionsFromEntry }