summaryrefslogtreecommitdiff
path: root/Gene Ontology.js
blob: 75b22ea83dd466faab397b205e162c119f5cf14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
	"translatorID": "cee0cca2-e82a-4618-b6cf-16327970169d",
	"label": "Gene Ontology",
	"creator": "Amelia Ireland",
	"target": "^https?://.*\\.geneontology\\.org",
	"minVersion": "2.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcv",
	"lastUpdated": "2014-01-05 11:26:46"
}

/*
	Gene Ontology website translator
	Copyright (C) 2010-2011 girlwithglasses, amelia.ireland@gmail.com

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	This translator works on cited PubMed references on the Gene Ontology website.

	It makes use of the code of the existing PubMed translator; thanks to the
	authors of that translator for their premium quality code.
*/


var items = {};
var selectArray = {};	
function detectWeb(doc, url) {
	var xPath = '//cite//*[@class="pmid"] | //cite//a[contains (@href, "pubmed")]';
	var cites = doc.evaluate(xPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();

	if (cites)
	{	Zotero.debug("Found some cites!");
		return "multiple";
	}
}

function doWeb(doc, url) {
	var PMIDs = getPMIDs(doc);
	retrievePMIDs(PMIDs, doc);
}


function getPMIDs(doc){
	var myPMID = '//cite//*[@class="pmid"] | //cite//a[contains (@href, "pubmed")]';
	var pmids = doc.evaluate(myPMID, doc, null, XPathResult.ANY_TYPE, null);
	var pmid_list = new Array();
	var unknown_list = new Array();
	var x;
	while (x = pmids.iterateNext()) {
		if (x.href && x.href.match('pubmed')) {
			// get the number
			var n = x.href.lastIndexOf("/");
			n++;
			pmid_list.push(x.href.substr(n));
//			Zotero.debug("Got a pubmed href! " + x.href.substr(n));
		}
		else {
			unknown_list.push(x);
		}
	}
	if (unknown_list.length > 0) {
//		Zotero.debug("Couldn't work out what to do with these refs: " + unknown_list.join("\n"));
	}
	if (pmid_list.length > 0) {
		Zotero.debug( "Found " + pmid_list.length + " PMIDs!" );
	}
	return pmid_list;
}

function retrievePMIDs(PMIDs, doc){
	_numPMIDs = PMIDs.length;
	for (var i=0; i<_numPMIDs; i++){
		(function(doc, PMID) {
			var translate = Zotero.loadTranslator("search");
			translate.setTranslator("fcf41bed-0cbc-3704-85c7-8062a0068a7a");
	
			var item = {"itemType":"journalArticle", "PMID":PMID};
			translate.setSearch(item);
	
			// don't save when item is done
			translate.setHandler("itemDone", function(translate, item) {
				item.repository = "CrossRef";
				items[PMID] = item;
				selectArray[PMID] = item.title;
			});
	
			translate.setHandler("done", function(translate) {
				_numPMIDs--;
				if(_numPMIDs <= 0) {
					completePMIDs(doc);
				}
			});
	
			// Don't throw on error
			translate.setHandler("error", function() {});
	
			translate.translate();
		})(doc, PMIDs[i]);	
	}
}

function completePMIDs(doc) {
	// all PMIDs retrieved now
	// check to see if there is more than one DOI
	var numPMIDs = 0;
	for(var PMID in selectArray) {
		numPMIDs++;
		if(numPMIDs == 1) break;
	}
	if(numPMIDs == 0) {
		throw "Could not find PMID";
	}  else {
		Zotero.selectItems(selectArray, function(selectedPMIDs) {
			if(!selectedPMIDs) return true;

			for(var PMID in selectedPMIDs) {
				items[PMID].complete();
			}
		});
	}
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.geneontology.org/GO.cite.shtml",
		"items": "multiple"
	}
]
/** END TEST CASES **/