study-management.component.ts 10.5 KB
Newer Older
rajkumar's avatar
rajkumar committed
1 2 3
import { Component, OnInit } from '@angular/core';
import { UtilService } from '../util/Util-services'
import { XmlService } from '../util/xml.service'
4
import { WebinRestService } from '../webin-rest.service';
rajkumar's avatar
rajkumar committed
5 6 7 8 9 10 11 12

import { MatTableDataSource, MAT_DATE_LOCALE } from '@angular/material';
import { Observable } from 'rxjs'
import { retry } from 'rxjs/operators';
import {DateAdapter, MAT_DATE_FORMATS} from '@angular/material';
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import * as _moment from 'moment';
import { ActivatedRoute } from '@angular/router';
13 14 15
import { HttpErrorResponse } from '@angular/common/http';
import { PopupMessageComponent } from '../popup-message/popup-message.component';
import { MatDialog } from '@angular/material/dialog';
16 17
import { environment } from '../../environments/environment';

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 43 44 45 46 47 48 49 50 51
  
  const moment =  _moment;
  export const CUSTOM_FORMATS = {
    parse: {
      dateInput: 'LL',
    },
    display: {
      dateInput: 'DD-MM-YYYY',
      monthYearLabel: 'YYYY',
      dateA11yLabel: 'LL',
      monthYearA11yLabel: 'YYYY',
    },
  };




@Component({
  selector: 'app-study-management',
  templateUrl: './study-management.component.html',
  styleUrls: ['./study-management.component.css'],
  providers: [
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},

    {provide: MAT_DATE_FORMATS, useValue: CUSTOM_FORMATS},
  ]
})
export class StudyManagementComponent implements OnInit {

  /*Page fields*/
  releaseDate:any;
  studyName: string;
  studyTitle: string;
  description:string;
rajkumar's avatar
rajkumar committed
52
  provideGenomeAnnotation:boolean;
rajkumar's avatar
rajkumar committed
53 54
  releaseStatus: string;
  xmlString: string;
rajkumar's avatar
rajkumar committed
55 56 57
  tag: string;
  tagValue: string;
  locustag: string;
rajkumar's avatar
rajkumar committed
58 59

  pubMedArray: [];
rajkumar's avatar
rajkumar committed
60 61
  attributeArray= [];
  locusTagArray=[];
rajkumar's avatar
rajkumar committed
62 63 64
  selectedPubMedArray= [];
  dataSource: MatTableDataSource<any>;
  displayedColumns: string[] = ['id', 'title','remove'];
rajkumar's avatar
rajkumar committed
65 66 67 68
  attributeDataSource: MatTableDataSource<any>;
  attributeDisplayColumn: string[] = ['tag','tagValue','remove']
  locusTagDataSource: MatTableDataSource<any>;
  locusTagDisplayColumn: string[] = ['locusTag','remove']
rajkumar's avatar
rajkumar committed
69
  showPubMedSearch = false;
rajkumar's avatar
rajkumar committed
70
  showDuplicatePubMedErr = false;
rajkumar's avatar
rajkumar committed
71 72
  showAttributeAdd = false;
  showLocusTagAdd = false;
73
  isProductionEnv=environment.production;
rajkumar's avatar
rajkumar committed
74

rajkumar's avatar
rajkumar committed
75 76 77 78 79
  pubMedSearch="";
  today=new Date();
  maxDate: any;
  id: any;
  action: string;
rajkumar's avatar
rajkumar committed
80
  showLoadingFlag=false;
rajkumar's avatar
rajkumar committed
81
  
82
  constructor(public dialog: MatDialog,private util: UtilService,private xmlUtil: XmlService,private activatedRoute: ActivatedRoute,private _webinRestService:WebinRestService,) { 
rajkumar's avatar
rajkumar committed
83 84
    var date=new Date();
    this.maxDate = new Date(date.getFullYear() + 2, date.getMonth(),date.getDate());
85
    
rajkumar's avatar
rajkumar committed
86 87 88 89 90 91 92 93 94 95 96 97
    
  }

  ngOnInit() {
    this.id=this.activatedRoute.snapshot.params.id;
    if(this.id){
      this.action="Edit";
      this.initEdit(this.id);
    }
    
  }

rajkumar's avatar
rajkumar committed
98 99 100
  getPubMed(searchVal){
    this.showLoading();
    this.util.getPubMed(searchVal).
rajkumar's avatar
rajkumar committed
101 102
      subscribe((data:any) => {
          this.pubMedArray=data.resultList.result;
rajkumar's avatar
rajkumar committed
103
          this.hideLoading();
rajkumar's avatar
rajkumar committed
104 105 106
  });
  }

rajkumar's avatar
rajkumar committed
107 108
  showExistingPubMed(id){
    (this.util.getPubMedById(id)).
rajkumar's avatar
rajkumar committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      subscribe((data:any) => {
        if(data.resultList.result.length>0){
          this.selectedPubMedArray.push(data.resultList.result[0]);
          this.dataSource = new MatTableDataSource<any>(this.selectedPubMedArray);
        }else{
          console.log("Invalid PubMed Id :'"+id+"' is ignored.")
        }
    });
  }

  getPubMedDisplayText(option){
    if(option){
    return option.id + " [ "+option.title+" ]";
    }
  }

  selectedPubMed(event) {
rajkumar's avatar
rajkumar committed
126 127 128 129 130 131 132 133 134 135
    var index = this.selectedPubMedArray.map(function(item) { return item.id; }).indexOf(event.option.value.id);
    if(index===-1){
      this.selectedPubMedArray.push(event.option.value);
      this.dataSource = new MatTableDataSource<any>(this.selectedPubMedArray);
      this.pubMedSearch="";
      this.showPubMedSearch=false;
      this.showDuplicatePubMedErr=false;
    }else{
      this.showDuplicatePubMedErr=true;
    }
rajkumar's avatar
rajkumar committed
136 137 138 139 140 141
  }

  addPubMed(){
    this.showPubMedSearch=true;
  }

rajkumar's avatar
rajkumar committed
142 143 144 145 146 147 148 149
  showAttributeAddPanel(){
    this.showAttributeAdd=true;
  }

  showLocusTagAddPanel(){
    this.showLocusTagAdd=true;
  }

rajkumar's avatar
rajkumar committed
150 151 152 153 154 155 156
  removePubMed(pubMedObj){
    var index = this.selectedPubMedArray.map(function(item) { return item.id; }).indexOf(pubMedObj.id);
    this.selectedPubMedArray.splice(index, 1);
    this.dataSource = new MatTableDataSource<any>(this.selectedPubMedArray);
  }

  submitStudy(form){
rajkumar's avatar
rajkumar committed
157
    this.showLoading()
158
    var observable: Observable<string>;
rajkumar's avatar
rajkumar committed
159
    if(this.action!="Edit"){
160
       observable=this.xmlUtil.generateStudyXml(form.value,this.selectedPubMedArray,this.attributeArray,this.locusTagArray);
rajkumar's avatar
rajkumar committed
161
    }else{
162
      observable=this.xmlUtil.updateProjectXml(this.xmlString,form.value,this.selectedPubMedArray,this.attributeArray,this.locusTagArray);
rajkumar's avatar
rajkumar committed
163
    }
164 165
    this.handleServerResponse(observable);
      
rajkumar's avatar
rajkumar committed
166 167
  }

rajkumar's avatar
rajkumar committed
168 169 170 171 172
  

  initEdit(id){
    this.showLoading();
    this.util.getProjectDetails(id).
rajkumar's avatar
rajkumar committed
173 174 175 176
    subscribe((data:any) => {
      this.setPageValuesfromReport(data[0].report);
    });

rajkumar's avatar
rajkumar committed
177
    this.util.getProjectXml(id).
rajkumar's avatar
rajkumar committed
178 179 180
    subscribe((xmlString:any) => {
      this.xmlString=xmlString;
      this.setPageValuesfromXml();
rajkumar's avatar
rajkumar committed
181
      this.hideLoading();
rajkumar's avatar
rajkumar committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    });
  }

  setPageValuesfromReport(data){
    this.releaseDate=new Date(data["holdDate"]);
    this.releaseStatus=data["releaseStatus"];
  }

  setPageValuesfromXml(){
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(this.xmlString, "text/xml");
    var nameTag=xmlDoc.getElementsByTagName("NAME")[0];
    var titleTag=xmlDoc.getElementsByTagName("TITLE")[0];
    var descriptionTag=xmlDoc.getElementsByTagName("DESCRIPTION")[0];
    this.studyName=nameTag.hasChildNodes() ? nameTag.childNodes[0].nodeValue : "";
    this.studyTitle=titleTag.hasChildNodes() ? titleTag.childNodes[0].nodeValue : "";
    this.description=descriptionTag.hasChildNodes() ? descriptionTag.childNodes[0].nodeValue : "";
    
    this.setPubMedDetails(xmlDoc);
rajkumar's avatar
rajkumar committed
201 202 203
    this.setAttributeDetails(xmlDoc);
    this.setLocusTagDetails(xmlDoc);
    
rajkumar's avatar
rajkumar committed
204 205 206
  }
  
  setPubMedDetails(xmlDoc){
rajkumar's avatar
rajkumar committed
207
    var projectLinks = xmlDoc.getElementsByTagName("PROJECT_LINKS")[0];
rajkumar's avatar
rajkumar committed
208 209 210 211 212 213 214 215 216 217 218 219
    if(projectLinks){
      var xRefLink=projectLinks.getElementsByTagName("XREF_LINK");
      length=xRefLink.length
      for(var i=0;i<length;i++){
        var dbTagValue=xRefLink[i].getElementsByTagName("DB")[0].childNodes[0].nodeValue; 
        if(dbTagValue==="PUBMED"){
          var pupMedId=xRefLink[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue; 
          this.showExistingPubMed(pupMedId);
        }
      }
    }
  }
rajkumar's avatar
rajkumar committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

  setAttributeDetails(xmlDoc){
    var projectAttributes=xmlDoc.getElementsByTagName("PROJECT_ATTRIBUTE");
    var projAttrLen=projectAttributes.length;
    for(var i=0;i<projAttrLen;i++){
      var tag=projectAttributes[i].getElementsByTagName("TAG")[0].childNodes[0].nodeValue; 
      var tagValue=projectAttributes[i].getElementsByTagName("VALUE")[0].childNodes[0].nodeValue;
      this.attributeArray.push({id:this.util.getId(),tag:tag,tagValue:tagValue});
    }
    if(this.attributeArray.length>0){
      this.attributeDataSource = new MatTableDataSource<any>(this.attributeArray);
    }
  }

  setLocusTagDetails(xmlDoc){
    var locusTagPrefix=xmlDoc.getElementsByTagName("LOCUS_TAG_PREFIX");
    var locusTagPrefixLen=locusTagPrefix.length;
    for(var i=0;i<locusTagPrefixLen;i++){
      var locusTag=locusTagPrefix[i].childNodes[0].nodeValue; 
      this.locusTagArray.push({id:this.util.getId(),locusTag:locusTag});
    }
    if(this.locusTagArray.length>0){
rajkumar's avatar
rajkumar committed
242
      this.provideGenomeAnnotation=true;
rajkumar's avatar
rajkumar committed
243 244 245
      this.locusTagDataSource = new MatTableDataSource<any>(this.locusTagArray);
    }
  }
rajkumar's avatar
rajkumar committed
246
  
rajkumar's avatar
rajkumar 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
  addAttribute(){
    this.attributeArray.push({id:this.util.getId(),tag:this.tag,tagValue:this.tagValue});
    this.attributeDataSource = new MatTableDataSource<any>(this.attributeArray);
    this.tag="";
    this.tagValue="";
    this.showAttributeAdd=false;
  }

  addlocusTag(){
    this.locusTagArray.push({id:this.util.getId,locusTag:this.locustag});
    this.locusTagDataSource = new MatTableDataSource<any>(this.locusTagArray);
    this.locustag="";
    this.showLocusTagAdd=false;
  }


  removeAttribute(attr){
    var index = this.attributeArray.map(function(item) { return item.id; }).indexOf(attr.id);
    this.attributeArray.splice(index, 1);
    this.attributeDataSource = new MatTableDataSource<any>(this.attributeArray);  
  }

  removeLocusTag(element){
    var index = this.attributeArray.map(function(item) { return item.id; }).indexOf(element.id);
    this.locusTagArray.splice(index, 1);
    this.locusTagDataSource = new MatTableDataSource<any>(this.locusTagArray);  
  }
rajkumar's avatar
rajkumar committed
274
 
rajkumar's avatar
rajkumar committed
275 276 277 278 279 280 281
  showLoading(){
    this.showLoadingFlag=true;
  }

  hideLoading(){
    this.showLoadingFlag=false;
  }
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

  handleServerResponse(observable){
    if (observable) {
      observable.pipe(
        retry(3)
      ).subscribe(
        data => {
            let result = this._webinRestService.parseResult(data);
            if (result.isError) {
              console.log(result.errors)
              let message=result.errors[0]["error"];
              this.showErrorPopup(message)
            } else {
              
              console.log(result.accessions);
              let message="Successfully subimited project with project identification : "+result.accessions[0]["accession"];
              this.showSuccessPopup(message);
              
            }
            this.hideLoading();
        },
        (err: HttpErrorResponse) => {
          console.error('** Webin submission service failed **', err);
          const message = 'Webin submission service failed. Please try again later. If the problem persists please contact the helpdesk.';
          this.showErrorPopup(message);
          this.hideLoading();
      }
    
    );
  
    }
   }

   showSuccessPopup(message){
    const dialogRef = this.dialog.open(PopupMessageComponent, {
      width: '500px',
      backdropClass: 'custom-dialog-backdrop-class',
      panelClass: 'custom-dialog-panel-class',
      data: {'action':'Success','message':message,'title':'Study Submission'}
    });
   }
rajkumar's avatar
rajkumar committed
323

324 325 326 327 328 329 330 331 332
    showErrorPopup(message){
      const dialogRef = this.dialog.open(PopupMessageComponent, {
        width: '500px',
        backdropClass: 'custom-dialog-backdrop-class',
        panelClass: 'custom-dialog-panel-class',
        data: {'action':'Error','message':message,'title':'Study Redistration Error'}
      });
  
   }
rajkumar's avatar
rajkumar committed
333 334 335 336 337 338 339

   setGenomeAnotation(event){
     if(!event.target.checked){
       this.locusTagArray=[];
       this.locusTagDataSource = new MatTableDataSource<any>(this.locusTagArray);
     }
   }
rajkumar's avatar
rajkumar committed
340
}