<commit_before>/* 
 * Entrez JS
 * 
 */

function Entrez(options) {
	this.database = options.database;
	this.debug = options.debug;
    this.initialize(options);
}

$.extend(Entrez.prototype, {
    initialize: function(options) {

    },
    
    search: function(term) {
    	var self = this;
    	var url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";
    	return $.ajax({
    		type: 'GET',
    		url: url,
    		dataType: 'xml',
    		cache: false,
    		data: {
    			db: this.database,
    			term: term
    		}
    	}).then(function(xml) {
    		if (self.debug) console.log(xml);
    		if (xml) {
    			var $xml = $(xml),
			    	$id = $xml.find("Id");
    			
    			if ($id.length == 1) {
    				if (self.debug) console.log($id.text());
    				return $id.text();
    			}
    		}
    	});
    },
    
    fetch: function(id) {
    	var self = this;
    	var url ="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi";
    	return $.ajax({
    		type: 'GET',
    		url: url,
    		dataType: 'xml',
    		cache: false,
    		data: {
    			db: this.database,
    			id: id
    		}
    	}).then(function(xml) {
    		if (self.debug) console.log(xml);
    		if (xml) {
    			$xml = $(xml);
//    			var expXml = $xml.find("Item[Name='ExpXml']").text();
//    			var $expXml = $($.parseXML(expXml));
//    			var title = $expXml.find("Title").text();
    			var runs = $xml.find("Item[Name='Runs']").text();
    			var $runs = $($.parseXML(runs));
    			var $run = $runs.find("Run");
    			var accn = $run.attr("acc");
    			var size = $run.attr("total_bases");
    			return {
    				accn: accn,
    				size: size
    			}
    		}<commit_msg>Fix handling of invalid Entrez XML, COGE-778<commit_after>/* 
 * Entrez JS
 * 
 */

function Entrez(options) {
	this.database = options.database;
	this.debug = options.debug;
    this.initialize(options);
}

$.extend(Entrez.prototype, {
    initialize: function(options) {

    },
    
    search: function(term) {
    	var self = this;
    	var url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";
    	return $.ajax({
    		type: 'GET',
    		url: url,
    		dataType: 'xml',
    		cache: false,
    		data: {
    			db: this.database,
    			term: term
    		}
    	}).then(function(xml) {
    		if (self.debug) console.log(xml);
    		if (xml) {
    			var $xml = $(xml),
			    	$id = $xml.find("Id");
    			
    			if ($id.length == 1) {
    				if (self.debug) console.log($id.text());
    				return $id.text();
    			}
    		}
    	});
    },
    
    fetch: function(id) {
    	var self = this;
    	var url ="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi";
    	return $.ajax({
    		type: 'GET',
    		url: url,
    		dataType: 'xml',
    		cache: false,
    		data: {
    			db: this.database,
    			id: id
    		}
    	}).then(function(xml) {
    		if (self.debug) console.log(xml);
    		if (xml) {
    			var $xml = $(xml);
    			var runs = $xml.find("Item[Name='Runs']").text();
    			var $runs = $($.parseXML('<runs>'+runs+'</runs>')); // mdb added "runs" parent element, COGE-778
    			var $run = $runs.find("Run");
    			var accn = $run.attr("acc");
    			var size = $run.attr("total_bases");
    			return {
    				accn: accn,
    				size: size
    			}
    		}
{'fraction_non_alphanumeric': 0.18044202240387527, 'fraction_numerical': 0.0024220405691795337, 'mean_word_length': 1.8581314878892734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 29}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4188855', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1320, 'n_words': 234}