xml.service.ts 15.9 KB
Newer Older
rajkumar's avatar
rajkumar committed
1 2 3 4 5 6 7 8
import { Injectable } from '@angular/core';
import { FocusKeyManager } from '@angular/cdk/a11y';
import { WebinAuthenticationService } from '../webin-authentication.service';
import { WebinRestService } from '../webin-rest.service';
import { ReportType } from '../report-type.enum';
import { Observable } from 'rxjs';
import { retry } from 'rxjs/operators';
import { UtilService } from '../util/Util-services';
rajkumar's avatar
rajkumar committed
9
import { v4 as uuid } from 'uuid';
rajkumar's avatar
rajkumar committed
10 11 12 13 14 15 16 17


@Injectable({
  providedIn: 'root'
})
export class XmlService {

  constructor(
rajkumar's avatar
rajkumar committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    private _webinAuthenticationService: WebinAuthenticationService,
    private _webinRestService: WebinRestService,
    private util: UtilService) { }

  generateStudyXml(form, selectedPubMedArray, attributeArray, locusTagArray) {

    let pubMedXml = this.getPubMedXmlTags(selectedPubMedArray);
    let attributeXml = this.getAttributeXmlTags(attributeArray)
    let locusTagXml = this.getlocusTagXmlTags(locusTagArray);
    let alias = uuid();

    let projectXml = new Blob(['<?xml version = "1.0" encoding = "UTF-8"?>' +
      '<PROJECT_SET>' +
      '<PROJECT alias="' + alias + '">' +
      '<NAME>' + form.studyName + '</NAME>' +
      '<TITLE>' + form.studyTitle + '</TITLE>' +
      '<DESCRIPTION>' + form.description + '</DESCRIPTION>' +
      '<SUBMISSION_PROJECT>' +
      '<SEQUENCING_PROJECT>' +
      locusTagXml +
      '</SEQUENCING_PROJECT>' +
      '</SUBMISSION_PROJECT>' +
      pubMedXml +
      attributeXml +
      '</PROJECT>' +
rajkumar's avatar
rajkumar committed
43
      '</PROJECT_SET>'])
rajkumar's avatar
rajkumar committed
44 45
    var action = { name: "add" };
    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate));
Rajkumar-D's avatar
Rajkumar-D committed
46
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.projects, projectXml, 'Add', dateStr, form)
47
    return observable;
rajkumar's avatar
rajkumar committed
48 49
  }

Rajkumar-D's avatar
Rajkumar-D committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
  generateDacXml(form, contactArray) {

    let contactXml = this.getContactXmlTags(contactArray);
    let alias = uuid();

    let dacXml = new Blob(['<?xml version = "1.0" encoding = "UTF-8"?>' +
      '<DAC_SET>' +
      '<DAC alias="' + alias + '">' +
      '<TITLE>' + form.title + '</TITLE>' +
      contactXml +
      '</DAC>' +
      '</DAC_SET>'])
    var action = { name: "add" };
    //let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate));
Rajkumar-D's avatar
Rajkumar-D committed
64
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.dacs, dacXml, 'Add', form)
Rajkumar-D's avatar
Rajkumar-D committed
65 66
    return observable;
  }
rajkumar's avatar
rajkumar committed
67

Rajkumar-D's avatar
Rajkumar-D committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  generateDacPolicyXml(form) {

    let alias = uuid();
    let dacPolicyXml = new Blob(['<?xml version = "1.0" encoding = "UTF-8"?>' +
      '<POLICY_SET>' +
      '<POLICY alias="' + alias + '" center_name="" accession="" >' +
      '<TITLE>' + form.title + '</TITLE>' +
      '<DAC_REF accession="' + form.dacRef + '"/>' +
      '<POLICY_TEXT>' + form.policyText + '</POLICY_TEXT>' +
      '</POLICY>' +
      '</POLICY_SET>'])
    var action = { name: "add" };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.policies, dacPolicyXml, 'Add')
    return observable;
  }

  generateDacDatasetXml(form, typeArr, refArr) {
    let typeXml = this.getDatasetTypeXml(typeArr);
    let refXml = this.getRefXml(refArr);
    let alias = uuid();
    let dacDatasetXmlStr = '' +
      '<DATASETS>' +
      '<DATASET alias="' + alias + '" broker_name="EGA" >' +
      '<TITLE>' + form.title + '</TITLE>' +
      '<DESCRIPTION>' + form.description + '</DESCRIPTION>' +
      typeXml +
      refXml +
      '<POLICY_REF accession="' + form.policyRef + '"/>' +
      '</DATASET>' +
      '</DATASETS>';
    console.log(dacDatasetXmlStr)
    let dacDatasetXml = new Blob([dacDatasetXmlStr]);
    var action = { name: "add" };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.datasets, dacDatasetXml, 'Add')
    return observable;
  }

rajkumar's avatar
rajkumar committed
105

rajkumar's avatar
rajkumar committed
106 107 108 109 110 111 112 113 114 115 116 117
  getPubMedXmlTags(selectedPubMedArray) {
    let pubmedXml = "<PROJECT_LINKS>";
    selectedPubMedArray.forEach(element => {
      pubmedXml += '<PROJECT_LINK>' +
        '<XREF_LINK>' +
        '<DB>PUBMED</DB>' +
        '<ID>' + element.id + '</ID>' +
        '</XREF_LINK>' +
        '</PROJECT_LINK>';
    });
    pubmedXml += '</PROJECT_LINKS>';
    return pubmedXml;
118
  }
rajkumar's avatar
rajkumar committed
119 120 121 122 123 124 125 126 127 128 129

  getAttributeXmlTags(attributeArray) {
    let attributeXml = "<PROJECT_ATTRIBUTES>";
    attributeArray.forEach(element => {
      attributeXml += '<PROJECT_ATTRIBUTE>' +
        '<TAG>' + element.tag + '</TAG>' +
        '<VALUE>' + element.tagValue + '</VALUE>' +
        '</PROJECT_ATTRIBUTE>';
    });
    attributeXml += '</PROJECT_ATTRIBUTES>';
    return attributeXml;
130
  }
rajkumar's avatar
rajkumar committed
131 132 133 134 135 136 137 138 139 140

  getlocusTagXmlTags(locusTagArray) {
    let locusTagXml = "";
    locusTagArray.forEach(element => {
      locusTagXml += '<LOCUS_TAG_PREFIX>' +
        element.locusTag +
        '</LOCUS_TAG_PREFIX>';
    });

    return locusTagXml;
141
  }
rajkumar's avatar
rajkumar committed
142

Rajkumar-D's avatar
Rajkumar-D committed
143 144 145 146 147 148 149 150 151 152
  getContactXmlTags(contactArray) {
    let contactXml = "<CONTACTS>";
    contactArray.forEach(element => {
      //contactXml += '<CONTACT name="' + element.name + '" email="' + element.emailAddress + '" organisation="' + element.organization + '" telephone="' + element.telephone + '"/>';
      contactXml += '<CONTACT name="' + element.name + '" email="' + element.emailAddress + '" organisation="' + element.organization + '"/>';
    });
    contactXml += "</CONTACTS>"
    return contactXml;
  }

Rajkumar-D's avatar
Rajkumar-D committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  getDatasetTypeXml(typeArr) {
    let typeXml = "";
    typeArr.forEach(element => {
      typeXml += "<DATASET_TYPE>" + element + "</DATASET_TYPE>"
    });
    return typeXml;
  }

  getRefXml(refArr) {
    let refXml = "";
    let ref = ""
    refArr.forEach(val => {
      ref = "ANALYSIS_REF"
      if (val.trim().startsWith("EGAR")) {
        ref = "RUN_REF";
      }
      refXml += '<' + ref + ' accession="' + val + '"/>'
    });
    return refXml;
  }

Rajkumar-D's avatar
Rajkumar-D committed
174 175


rajkumar's avatar
rajkumar committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  updateProjectXml(orginalXml, form, pubMedArray, attributeArray, locusTagArray) {
    var pubMedXmlStr = this.getPubMedXmlTags(pubMedArray);
    var attributeXmlStr = this.getAttributeXmlTags(attributeArray);
    var locusTagXmlStr = '<LOCUS-TAGS>' + this.getlocusTagXmlTags(locusTagArray) + '</LOCUS-TAGS>';
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(orginalXml, "text/xml");
    var pubMedXml = parser.parseFromString(pubMedXmlStr, "text/xml");
    var attributeXml = parser.parseFromString(attributeXmlStr, "text/xml");
    var locusTagXml = parser.parseFromString(locusTagXmlStr, "text/xml");
    var nameTag = xmlDoc.getElementsByTagName("NAME")[0];
    var titleTag = xmlDoc.getElementsByTagName("TITLE")[0];
    var descriptionTag = xmlDoc.getElementsByTagName("DESCRIPTION")[0];

    /** Check if the tag is empty and set the values accordingly */
    if (nameTag) {
      nameTag.hasChildNodes() ? nameTag.childNodes[0].nodeValue = form.studyName : nameTag.appendChild(xmlDoc.createTextNode(form.studyName));
    }
    if (titleTag) {
      titleTag.hasChildNodes() ? titleTag.childNodes[0].nodeValue = form.studyTitle : titleTag.appendChild(xmlDoc.createTextNode(form.studyTitle));
rajkumar's avatar
rajkumar committed
195
    }
rajkumar's avatar
rajkumar committed
196 197
    if (descriptionTag) {
      descriptionTag.hasChildNodes() ? descriptionTag.childNodes[0].nodeValue = form.description : descriptionTag.appendChild(xmlDoc.createTextNode(form.description));
rajkumar's avatar
rajkumar committed
198
    }
rajkumar's avatar
rajkumar committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215



    /** Update PubMed Ids */
    this.updateProjectLinks(xmlDoc, pubMedXml);

    /** Update project attributes */
    this.updateProjectAttributes(xmlDoc, attributeXml);

    /** Update Locas tag Xml */
    this.updateProjectLocasTags(xmlDoc, locusTagXml);

    var xmlDocStr = new XMLSerializer().serializeToString(xmlDoc.documentElement);
    //console.log(xmlDocStr);
    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate))

    var action = { name: "Edit", id: form.id };
Rajkumar-D's avatar
Rajkumar-D committed
216
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.projects, new Blob([xmlDocStr]), action, dateStr, form)
rajkumar's avatar
rajkumar committed
217 218 219 220
    //this.handleServerResponse(observable);
    return observable;
  }

Rajkumar-D's avatar
Rajkumar-D committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  updateDacXml(orginalXml, form, contactArray) {
    var contactXmlStr = this.getContactXmlTags(contactArray);
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(orginalXml, "text/xml");
    var contactXml = parser.parseFromString(contactXmlStr, "text/xml");
    var titleTag = xmlDoc.getElementsByTagName("TITLE")[0];


    /** Check if the tag is empty and set the values accordingly */
    if (titleTag) {
      titleTag.hasChildNodes() ? titleTag.childNodes[0].nodeValue = form.title : titleTag.appendChild(xmlDoc.createTextNode(form.title));
    }

    /** Update project attributes */
    this.updateDacContacts(xmlDoc, contactXml);


    var xmlDocStr = new XMLSerializer().serializeToString(xmlDoc.documentElement);
    //console.log(xmlDocStr);
    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate))

    var action = { name: "Edit", id: form.id };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.dacs, new Blob([xmlDocStr]), action, dateStr)
    return observable;
  }

Rajkumar-D's avatar
Rajkumar-D committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  updateDacPolicyXml(orginalXml, form) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(orginalXml, "text/xml");
    var titleTag = xmlDoc.getElementsByTagName("TITLE")[0];
    var policyTextTag = xmlDoc.getElementsByTagName("POLICY_TEXT")[0];

    /** Check if the tag is empty and set the values accordingly */
    if (titleTag) {
      titleTag.hasChildNodes() ? titleTag.childNodes[0].nodeValue = form.title : titleTag.appendChild(xmlDoc.createTextNode(form.title));
    }

    /** Check if the tag is empty and set the values accordingly */
    if (policyTextTag) {
      policyTextTag.hasChildNodes() ? policyTextTag.childNodes[0].nodeValue = form.policyText : policyTextTag.appendChild(xmlDoc.createTextNode(form.policyText));
    }

    var xmlDocStr = new XMLSerializer().serializeToString(xmlDoc.documentElement);
    //console.log(xmlDocStr);
    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate))

    var action = { name: "Edit", id: form.id };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.policies, new Blob([xmlDocStr]), action, dateStr)
    return observable;
  }

  updateDacDatasetXml(orginalXml, form, typeArr, refArr) {

    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(orginalXml, "text/xml");
    let newTypeXml = parser.parseFromString('<DATASET_TYPES>' + this.getDatasetTypeXml(typeArr) + '</DATASET_TYPES>', "text/xml");
    let newRefXml = parser.parseFromString('<REFS>' + this.getRefXml(refArr) + '</REFS>', "text/xml");

    var titleTag = xmlDoc.getElementsByTagName("TITLE")[0];
    var descriptionTag = xmlDoc.getElementsByTagName("DESCRIPTION")[0];
    var datasetType = xmlDoc.getElementsByTagName("DATASET_TYPE");
    var runRef = xmlDoc.getElementsByTagName("RUN_REF");
    var analysisRef = xmlDoc.getElementsByTagName("ANALYSIS_REF");
    var datasetRef = xmlDoc.getElementsByTagName("DATASET")[0];

    /** Check if the tag is empty and set the values accordingly */
    if (titleTag) {
      titleTag.hasChildNodes() ? titleTag.childNodes[0].nodeValue = form.title : titleTag.appendChild(xmlDoc.createTextNode(form.title));
    }

    /** Check if the tag is empty and set the values accordingly */
    if (descriptionTag) {
      descriptionTag.hasChildNodes() ? descriptionTag.childNodes[0].nodeValue = form.description : descriptionTag.appendChild(xmlDoc.createTextNode(form.description));
    }

    /** Removing DATASET_TYPE, RUN_REF and ANALYSIS_REF */
    while (datasetType.length > 0) {
      xmlDoc.getElementsByTagName("DATASET_TYPE")[0].remove();
    }

    while (runRef.length > 0) {
      xmlDoc.getElementsByTagName("RUN_REF")[0].remove();
    }

    while (analysisRef.length > 0) {
      xmlDoc.getElementsByTagName("ANALYSIS_REF")[0].remove();
    }

    /** Adding DATASET_TYPE, RUN_REF and ANALYSIS_REF */
    var datasetTypes = newTypeXml.getElementsByTagName("DATASET_TYPES")[0];
    while (datasetTypes.hasChildNodes()) {
      datasetRef.append(datasetTypes.firstChild);
      datasetTypes.removeChild[0];
    }

    var refs = newRefXml.getElementsByTagName("REFS")[0];
    while (refs.hasChildNodes()) {
      datasetRef.append(refs.firstChild);
      refs.removeChild[0];
    }

    /** Removing POLICY_REF and appending to the end due to validation errors when uppended before other RUN_REF or ANALYSIS_REF */
    var policyRefClone = xmlDoc.getElementsByTagName("POLICY_REF")[0].cloneNode(true);
    xmlDoc.getElementsByTagName("POLICY_REF")[0].remove();
    datasetRef.append(policyRefClone);



    var xmlDocStr = new XMLSerializer().serializeToString(xmlDoc.documentElement);
    console.log(xmlDocStr);
    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate))

    var action = { name: "Edit", id: form.id };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.datasets, new Blob([xmlDocStr]), action, dateStr)
    return observable;
  }


rajkumar's avatar
rajkumar committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  updateProjectReleaseDate(orginalXml, form) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(orginalXml, "text/xml");
    var xmlDocStr = new XMLSerializer().serializeToString(xmlDoc.documentElement);

    let dateStr = this.getFormatedReleseDate(new Date(form.releaseDate))
    var action = { name: "Edit", id: form.id };
    const observable: Observable<string> = this._webinRestService.updateXml(ReportType.projects, new Blob([xmlDocStr]), action, dateStr)
    return observable;
  }

  updateProjectLinks(xmlDoc, pubMedXml) {
    if (xmlDoc.getElementsByTagName("PROJECT_LINKS")[0]) {
      var xmlDocProjectLinks = xmlDoc.getElementsByTagName("PROJECT_LINKS")[0];
      var xrefLinkCnt = xmlDocProjectLinks.getElementsByTagName("XREF_LINK").length;
      var newProjectLinks = pubMedXml.getElementsByTagName("PROJECT_LINKS")[0];
      var newXrefLinkCnt = newProjectLinks.getElementsByTagName("PROJECT_LINK").length;

      // Remove old XREF_LINK [ PubMed ] tag
      for (var i = 0; i < xrefLinkCnt; i++) {
        xmlDocProjectLinks.getElementsByTagName("XREF_LINK")[0].parentElement.remove();
      }

      // Add new XREF_LINK [ PubMed ] tag
      for (var j = 0; j < newXrefLinkCnt; j++) {
        /**  
         * The appendChild() method of DOM element moves the element from newProjectLinks to existingProjectLinks (it dose not copy )
         * that is why we are adding [0]th element always to the xml document.
         */
        xmlDocProjectLinks.appendChild(newProjectLinks.getElementsByTagName("PROJECT_LINK")[0])
      }
    } else {
rajkumar's avatar
rajkumar committed
371
      xmlDoc.getElementsByTagName("PROJECT")[0].append(pubMedXml.getElementsByTagName("PROJECT_LINKS")[0])
rajkumar's avatar
rajkumar committed
372 373
    }

rajkumar's avatar
rajkumar committed
374
  }
rajkumar's avatar
rajkumar committed
375 376 377 378 379

  updateProjectAttributes(xmlDoc, attributeXml) {
    if (xmlDoc.getElementsByTagName("PROJECT_ATTRIBUTES")[0]) {
      xmlDoc.getElementsByTagName("PROJECT_ATTRIBUTES")[0].replaceWith(attributeXml.getElementsByTagName("PROJECT_ATTRIBUTES")[0]);
    } else {
rajkumar's avatar
rajkumar committed
380
      xmlDoc.getElementsByTagName("PROJECT")[0].append(attributeXml.getElementsByTagName("PROJECT_ATTRIBUTES")[0])
rajkumar's avatar
rajkumar committed
381
    }
rajkumar's avatar
rajkumar committed
382
  }
rajkumar's avatar
rajkumar committed
383

Rajkumar-D's avatar
Rajkumar-D committed
384 385 386 387 388 389
  updateDacContacts(xmlDoc, contactsXml) {
    if (xmlDoc.getElementsByTagName("CONTACTS")[0]) {
      xmlDoc.getElementsByTagName("CONTACTS")[0].replaceWith(contactsXml.getElementsByTagName("CONTACTS")[0]);
    }
  }

rajkumar's avatar
rajkumar committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403
  updateProjectLocasTags(xmlDoc, locusTagXml) {

    var oldlocusTagPrefixLength = xmlDoc.getElementsByTagName("LOCUS_TAG_PREFIX").length;
    var newlocusTagPrefixLength = locusTagXml.getElementsByTagName("LOCUS_TAG_PREFIX").length

    // Remove old locus_taf_prefix tag
    for (var i = 0; i < oldlocusTagPrefixLength; i++) {
      xmlDoc.getElementsByTagName("LOCUS_TAG_PREFIX")[0].remove();
    }

    for (var j = 0; j < newlocusTagPrefixLength; j++) {
      xmlDoc.getElementsByTagName("SEQUENCING_PROJECT")[0].append(locusTagXml.getElementsByTagName("LOCUS_TAG_PREFIX")[0]);
    }

rajkumar's avatar
rajkumar committed
404 405
  }

rajkumar's avatar
rajkumar committed
406 407 408 409
  getDateString() {
    let currdate = new Date();
    return currdate.getDate() + "-" + currdate.getMonth() + "-" + currdate.getFullYear() + "-" + currdate.getHours() + "-" + currdate.getMinutes() + "-" + currdate.getSeconds() + "-" + currdate.getMilliseconds();
  }
rajkumar's avatar
rajkumar committed
410

rajkumar's avatar
rajkumar committed
411

rajkumar's avatar
rajkumar committed
412 413 414
  getFormatedReleseDate(date) {
    return date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2);
  }
rajkumar's avatar
rajkumar committed
415 416

}