Project

General

Profile

1

    
2
import {Injectable} from '@angular/core';
3
import {Http, Response} from '@angular/http';
4
import {Observable}     from 'rxjs/Observable';
5
import {PublicationInfo} from '../../utils/entities/publicationInfo';
6
import 'rxjs/add/observable/of';
7
import 'rxjs/add/operator/do';
8
import 'rxjs/add/operator/share';
9
import 'rxjs/add/operator/map';
10
import{EnvProperties} from '../../utils/properties/env-properties';
11

    
12

    
13
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
14

    
15
@Injectable()
16
export class PublicationService {
17

    
18
  constructor(private http: Http ) {
19
    this.parsingFunctions = new ParsingFunctions();
20
  }
21

    
22
  public parsingFunctions: ParsingFunctions;
23
  publicationInfo: PublicationInfo;
24

    
25
  getPublicationInfo (id: string, properties:EnvProperties):any {
26
    console.info("getPublicationInfo in service");
27
    let url = properties.searchAPIURLLAst + 'publications/' +id+"?format=json";
28
    let key = url;
29

    
30
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
31
                .map(res => <any> res.json())
32
                .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity'], res])
33
                .map(res => [ res[1]['oaf:result'],
34
                              res[1]['oaf:result']['title'],
35
                              res[1]['oaf:result']['rels']['rel'],
36
                              res[1]['oaf:result']['children'],
37
                              res[1]['oaf:result']['pid'],
38
                              res[1]['oaf:result']['journal'],
39
                              res[1]['oaf:result']['language'],
40
                              res[1]['oaf:result']['subject'],
41
                              res[1]['oaf:result']['bestaccessright'],
42
                              res[1]['oaf:result']['collectedfrom'],
43
                              (res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null,
44
                              res[1]['oaf:result']['context'],
45
                              res[0],
46
                              res[1]['oaf:result']['creator'],
47
                              res[2],
48
                              res[1]['oaf:result']['country'],
49
                ])
50
                .map(res => this.parsePublicationInfo(res));
51
  }
52

    
53
  private handleError (error: Response) {
54
  // in a real world app, we may send the error to some remote logging infrastructure
55
  // instead of just logging it to the console
56
    console.log(error);
57
    return Observable.throw(error  || 'Server error');
58
  }
59

    
60
  parsePublicationInfo (data: any):any {
61
    this.publicationInfo = new PublicationInfo();
62
    this.publicationInfo.record = data[14];
63
    if(data[0] != null) {
64
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
65
      this.publicationInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
66
      this.publicationInfo.dateofacceptance = data[0].dateofacceptance;
67
      this.publicationInfo.publisher = data[0].publisher;
68
      if(!Array.isArray(data[0].description)) {
69
          this.publicationInfo.description = data[0].description;
70
      } else {
71
          this.publicationInfo.description = data[0].description[0];
72
      }
73
      this.publicationInfo.embargoEndDate = data[0].embargoenddate;
74
    }
75

    
76
/*
77
    this.publicationInfo.title = {"name": "", "url": "", "accessMode": ""};
78
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
79
      this.publicationInfo.title.accessMode = data[0]['bestaccessright'].classid;
80
    }
81

    
82
    if(data[1] != null) {
83
      if(Array.isArray(data[1])) {
84
        this.publicationInfo.title['name'] = data[1][0].content;
85
      } else {
86
        this.publicationInfo.title['name'] = data[1].content;
87
      }
88
    }
89
*/
90
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
91
      this.publicationInfo.accessMode = data[0]['bestaccessright'].classid;
92
    }
93
    if(data[1] != null) {
94
      if(Array.isArray(data[1])) {
95
        this.publicationInfo.title = data[1][0].content;
96
      } else {
97
        this.publicationInfo.title = data[1].content;
98
      }
99
    }
100

    
101
    if(data[2] != null) {
102
      let relation;
103
      let length = Array.isArray(data[2]) ? data[2].length : 1;
104

    
105
      for(let i=0; i<length; i++) {
106
        relation = Array.isArray(data[2]) ? data[2][i] : data[2];
107
        if(relation.hasOwnProperty("to")) {
108
          if(relation['to'].class == "isProducedBy") {
109
            this.publicationInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.publicationInfo.fundedByProjects, relation, this.publicationInfo.projectsProvenanceVocabulary);
110
          } else if(relation['to'].class == "isRelatedTo") {
111
            let provenanceAction: string;
112
            if(relation.provenanceaction in this.publicationInfo.researchResultsProvenanceVocabulary) {
113
              provenanceAction = this.publicationInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
114
            } else {
115
              provenanceAction = "Other";
116
            }
117

    
118
            this.publicationInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.publicationInfo.relatedResearchResults, relation, provenanceAction);
119
          } else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
120
            this.publicationInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.publicationInfo.similarResearchResults, relation);
121
          } else if(relation['to'].class == "hasAuthorInstitution") {
122
            this.publicationInfo.organizations = this.parseRelatedOrganizations(this.publicationInfo.organizations, relation);
123
          }
124
        }
125
      }
126
    }
127

    
128
    if(data[3] != null) {
129
      if(data[3].hasOwnProperty("instance")) {
130
        //this.publicationInfo.collectedFrom = new Array<{"name": string, "id": string}>();
131
        //this.publicationInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
132
        //this.publicationInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
133

    
134

    
135
        this.publicationInfo.hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
136

    
137

    
138
        this.publicationInfo.types = new Array<string>();
139
        let types = new Set<string>();
140

    
141
        let counter = 0;
142
        let instance;
143

    
144
        let length = Array.isArray(data[3]['instance']) ? data[3]['instance'].length : 1;
145

    
146
        for(let i=0; i<length; i++) {
147
          instance = Array.isArray(data[3]['instance']) ? data[3]['instance'][i] : data[3]['instance'];
148

    
149
          this.parsingFunctions.parseTypes(this.publicationInfo.types, types, instance);
150

    
151
          if(instance.hasOwnProperty("webresource")) {
152
            let url;
153
            if(!Array.isArray(instance['webresource'])) {
154
              url = instance['webresource'].url;
155
            } else{
156
              url = instance['webresource'][0].url;
157
            }
158

    
159
            /**********************************************************/
160
            if(instance.hasOwnProperty("hostedby")) {
161
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.publicationInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.publicationInfo.title*/, this.publicationInfo.accessMode);
162
            }
163
            /**********************************************************/
164
          }
165
        }
166
      }
167

    
168
      if(data[3].hasOwnProperty("externalreference")) {
169
        let externalResults: [Map<string, Map<string, string>>, { "name": string, "url": string}[]] = this.parseBioentitiesAndSoftware(data[3]);
170
        this.publicationInfo.bioentities = externalResults[0];
171
        this.publicationInfo.software = externalResults[1];
172
      }
173
    }
174

    
175
    if(data[4] != null) {
176
      this.publicationInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
177
    }
178

    
179
    if(data[5] != null) {
180
      this.publicationInfo.journal = {"journal": "", "issn": "", "lissn": "", "volume": "", "start_page": "", "end_page": ""}
181

    
182
      this.publicationInfo.journal['journal'] = data[5].content;
183
      this.publicationInfo.journal['issn'] = data[5].issn;
184
      this.publicationInfo.journal['lissn'] = data[5].lissn;
185
      this.publicationInfo.journal['volume'] = data[5].vol;
186
      this.publicationInfo.journal['start_page'] = data[5].sp;
187
      this.publicationInfo.journal['end_page'] = data[5].ep;
188
    }
189

    
190
    if(data[6] != null) {
191
      this.publicationInfo.languages = this.parsingFunctions.parseLanguages(data[6]);
192
    }
193
    if(data[15] != null) {
194
      this.publicationInfo.countries = this.parsingFunctions.parseCountries(data[15]);
195
    }
196

    
197
    if(data[7] != null) {
198
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[7]);
199
      this.publicationInfo.subjects = subjectResults[0];
200
      this.publicationInfo.otherSubjects = subjectResults[1];
201
      this.publicationInfo.classifiedSubjects = subjectResults[2];
202
    }
203

    
204
    // if(data[8] != null) {
205
    //     this.publicationInfo.bestaccessright = data[8].classid;
206
    // }
207

    
208
    // if(data[9] != null) {
209
    //   this.publicationInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(data[9]);
210
    // }
211

    
212
    this.publicationInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
213
                this.publicationInfo.hostedBy_collectedFrom, this.publicationInfo.publisher,
214
                this.publicationInfo.journal, this.publicationInfo.identifiers/*,
215
                this.publicationInfo.title*/);
216

    
217
    if(data[10] != null) {
218
      this.publicationInfo.references = this.parseReferences(data[10]);
219
    }
220

    
221
    if(data[11] != null) {
222
      this.publicationInfo.contexts = this.parsingFunctions.parseContexts(data[11]);
223
    }
224

    
225
    if(data[12] != null && data[12] == "under curation") {
226
      this.publicationInfo.underCurationMessage = true;
227
    } else {
228
      this.publicationInfo.underCurationMessage = false;
229
    }
230

    
231
    if(data[13] != null) {
232
      if(this.publicationInfo.authors == undefined) {
233
        this.publicationInfo.authors = new Array<string>();
234
      }
235

    
236
      let authors = data[13];
237
      let length = Array.isArray(authors) ? authors.length : 1;
238

    
239
      for(let i=0; i<length; i++) {
240
        let author = Array.isArray(authors) ? authors[i] : authors;
241
        this.publicationInfo.authors[author.rank-1] = author.content;
242
      }
243
      this.publicationInfo.authors = this.publicationInfo.authors.filter(function (item) {
244
        return (item != undefined);
245
      });
246
    }
247

    
248
    if(this.publicationInfo.relatedResearchResults) {
249
      let self = this;
250
      this.publicationInfo.relatedResearchResults.forEach(function (value, key, map) {
251
        self.publicationInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
252
      });
253
    }
254
    this.publicationInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.publicationInfo.similarResearchResults);
255

    
256
    return this.publicationInfo;
257
  }
258

    
259
  parseRelatedOrganizations(organizations: {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[], relation: any):
260
                  {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[] {
261
    if(organizations == undefined) {
262
      organizations = new Array<{
263
                  "name": string, "shortname": string,
264
                  "id": string, "websiteUrl": string,
265
                  "country": string, "trust": number}>();
266
    }
267

    
268
    let organization: { "name": string, "shortname": string,
269
                        "id": string, "websiteUrl": string,
270
                        "country": string, "trust": number
271
                      } = {
272
                        "name": "", "shortname": "",
273
                        "id": "", "websiteUrl": "",
274
                        "country": "", "trust": null
275
                      };
276

    
277
    organization.id = relation['to'].content;
278
    organization.name = relation.legalname;
279
    organization.shortname = relation.legalshortname;
280
    organization.websiteUrl = relation.websiteurl;
281
    if(relation.country) {
282
      organization.country = relation.country.classname;
283
    }
284
    if(relation.trust) {
285
      organization.trust = Math.round(relation.trust*100);
286
    }
287

    
288
    organizations.push(organization);
289
    return organizations;
290
  }
291

    
292
  parseBioentitiesAndSoftware(children: any) : [Map<string, Map<string, string>>, { "name": string, "url": string}[]] {
293
    let bioentities: Map<string, Map<string, string>>;
294
    let software: {"name": string, "url": string}[];
295

    
296
    let length = Array.isArray(children['externalreference']) ? children['externalreference'].length : 1;
297

    
298
    let externalreference;
299
    for(let i=0; i<length; i++) {
300
      externalreference = Array.isArray(children['externalreference']) ? children['externalreference'][i] : children['externalreference'];
301

    
302
      if(externalreference.hasOwnProperty("qualifier")) {
303
        if(externalreference['qualifier'].classid == "accessionNumber") {
304

    
305
          if(bioentities == undefined) {
306
            bioentities = new Map<string, Map<string, string>>();
307
          }
308

    
309
          if(!bioentities.has(externalreference.sitename)) {
310
            bioentities.set(externalreference.sitename, new Map<string, string>());
311
          }
312
          bioentities.get(externalreference.sitename).set(externalreference.refidentifier, externalreference.url);
313

    
314
        } else if(externalreference['qualifier'].classid == "software") {
315

    
316
          if(software == undefined) {
317
            software = new Array<{"name": string, "url": string}>();
318
          }
319

    
320
          software.push({"name": externalreference.sitename, "url": externalreference.url});
321
        }
322
      }
323
    }
324

    
325
    return [bioentities, software];
326
  }
327

    
328
  parseReferences(citations: any): {"name": string, "url": string}[] {
329
    let references = new Array<{"name": string, "url": string}>();
330

    
331
    let citation;
332
    let length = Array.isArray(citations) ? citations.length : 1;
333
    for(let i=0; i<length; i++) {
334
      citation = Array.isArray(citations) ? citations[i] : citations;
335

    
336
      let url;
337
      if(citation.hasOwnProperty("id")) {
338
        let citationId;
339
        let length1 = Array.isArray(citation['id']) ? citation['id'].length : 1;
340
        for(let j=0; j<length1; j++) {
341
          citationId = Array.isArray(citation['id']) ? citation['id'][j] : citation['id'];
342

    
343
          if(citationId.type == "pmid") {
344
            url = "http://www.ncbi.nlm.nih.gov/pubmed/"+citationId.value;
345
          }
346
        }
347
      }
348

    
349
      references[citation.position-1] = { "name": "", "url": ""};
350
      references[citation.position-1]['name'] = citation.rawText;
351
      references[citation.position-1]['url'] = url;
352
    }
353
    return references;
354
  }
355
}
(5-5/5)