<commit_before>var debug = require('debug')('p3api-server:media');
var nodeExcel = require('excel-export');
module.exports=function(req,res,next){
	var rpcTypes = ["application/jsonrpc.result+json", "application/jsonrpc+json"];

	if (rpcTypes.some(function(t){
		return req.is(t);
	})){
		debug("RPC Request");
	}

	var fields;

	if (req.call_collection=="genome_feature"){
		fields = ["genome_name", "accession", "seed_id", "refseq_locus_tag", "alt_locus_tag", "feature_id",
				"annotation", "feature_type", "start", "end", "na_length", "strand", "protein_id", "aa_length", "gene", "product"
		];
	}else if (req.call_collection =="genome") {
		fields = ["genome_id", "genome_name", "organism_name", "taxon_id", "genome_status",
			"strain", "serovar", "biovar", "pathovar", "mlst", "other_typing",
			"culture_collection", "type_strain",
			"completion_date", "publication",
			"bioproject_accession", "biosample_accession", "assembly_accession", "genbank_accessions",
			"refseq_accessions",
			"sequencing_centers", "sequencing_status", "sequencing_platform", "sequencing_depth", "assembly_method",
			"chromosomes", "plasmids", "contigs", "sequences", "genome_length", "gc_content",
			"patric_cds", "brc1_cds", "refseq_cds",
			"isolation_site", "isolation_source", "isolation_comments", "collection_date",
			"isolation_country", "geographic_location", "latitude", "longitude", "altitude", "depth", "other_environmental",
			"host_name", "host_gender", "host_age", "host_health", "body_sample_site", "body_sample_subsite", "other_clinical",
			"antimicrobial_resistance", "antimicrobial_resistance_evidence",
			"gram_stain", "cell_shape", "motility", "sporulation", "temperature_range", "optimal_temperature", "salinity", "oxygen_requirement",
			"habitat",
			"disease", "comments", "additional_metadata"
		]
	}



	res.format({
		"application/dna+fasta": function(){
			debug("application/fna handler")
			if (req.isDownload){
				req.set("content-disposition", "attachment; filename=patric_genomes.fasta");
			}

			//console.log("res.results: ", res.results);

			if (res.results && res.results.response && res.results.response.docs) {
				res.results.response.docs.forEach(function(o){
					var row = ">" + o.seed_id + "|"+o.feature_id+ " " + o.product + "\n" + o.na_sequence + "\n"; 
					res.write(row);
				});	
			}

			res.end();

		},

		"application/protein+fasta": function(){
			debug("application/dna+fasta handler")
			if (req.isDownload){
				req.set("content-disposition", "attachment; filename=patric_proteins.fasta");
			}

			//console.log("res.results: ", res.results);
			if (res.results && res.results.response && res.results.response.docs) {
				res.results.response.docs.forEach(function(o){
					var fasta_id;
					if (o.feature_type=="source") { return; }
					if (o.annotation == "PATRIC") {
						fasta_id = o.seed_id + "|"+(o.refseq_locus_tag?(o.refseq_locus_tag+"|"):"") + (o.alt_locus_tag?(o.alt_locus_tag+"|"):"");<commit_msg>add dna+fasta exporter for genome_sequence core<commit_after>var debug = require('debug')('p3api-server:media');
var nodeExcel = require('excel-export');
module.exports=function(req,res,next){
	var rpcTypes = ["application/jsonrpc.result+json", "application/jsonrpc+json"];

	if (rpcTypes.some(function(t){
		return req.is(t);
	})){
		debug("RPC Request");
	}

	var fields;

	if (req.call_collection=="genome_feature"){
		fields = ["genome_name", "accession", "seed_id", "refseq_locus_tag", "alt_locus_tag", "feature_id",
				"annotation", "feature_type", "start", "end", "na_length", "strand", "protein_id", "aa_length", "gene", "product"
		];
	}else if (req.call_collection =="genome") {
		fields = ["genome_id", "genome_name", "organism_name", "taxon_id", "genome_status",
			"strain", "serovar", "biovar", "pathovar", "mlst", "other_typing",
			"culture_collection", "type_strain",
			"completion_date", "publication",
			"bioproject_accession", "biosample_accession", "assembly_accession", "genbank_accessions",
			"refseq_accessions",
			"sequencing_centers", "sequencing_status", "sequencing_platform", "sequencing_depth", "assembly_method",
			"chromosomes", "plasmids", "contigs", "sequences", "genome_length", "gc_content",
			"patric_cds", "brc1_cds", "refseq_cds",
			"isolation_site", "isolation_source", "isolation_comments", "collection_date",
			"isolation_country", "geographic_location", "latitude", "longitude", "altitude", "depth", "other_environmental",
			"host_name", "host_gender", "host_age", "host_health", "body_sample_site", "body_sample_subsite", "other_clinical",
			"antimicrobial_resistance", "antimicrobial_resistance_evidence",
			"gram_stain", "cell_shape", "motility", "sporulation", "temperature_range", "optimal_temperature", "salinity", "oxygen_requirement",
			"habitat",
			"disease", "comments", "additional_metadata"
		]
	}



	res.format({
		"application/dna+fasta": function(){
			debug("application/dna+fastahandler")
			if (req.isDownload){
				req.set("content-disposition", "attachment; filename=patric_genomes.fasta");
			}

			//console.log("res.results: ", res.results);

			if (res.results && res.results.response && res.results.response.docs) {
				res.results.response.docs.forEach(function(o){
					if (req.call_collection=="genome_feature"){
						var row = ">" + o.seed_id + "|"+o.feature_id+ " " + o.product + "\n" + o.na_sequence + "\n"; 
					}else if (req.call_collection="genome_sequence") {
						var row = ">accn|" + o.accession + "   " + o.description + "   " + "["+(o.genome_name|| o.genome_id) +"]\n" + o.sequence;
					}else{
						throw Error("Cannot query for application/dna+fasta from this data collection");
					}
					res.write(row);
				});	
			}

			res.end();

		},

		"application/protein+fasta": function(){
			debug("application/dna+fasta handler")
			if (req.isDownload){
				req.set("content-disposition", "attachment; filename=patric_proteins.fasta");
			}

			//console.log("res.results: ", res.results);
			if (res.results && res.results.response && res.results.response.docs) {
				res.results.response.docs.forEach(function(o){
					var fasta_id;
					if (o.feature_type=="source") { return; }
					if (o.annotation == "PATRIC") {
						fasta_id = o.seed_id + "|"+(o.refseq_locus_tag?(o.refseq_locus_tag+"|"):"") + (o.alt_locus_tag?(o.alt_locus_tag+"|"):"");
{'fraction_non_alphanumeric': 0.18812351543942993, 'fraction_numerical': 0.0006334125098970705, 'mean_word_length': 5.405679513184584, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7348863', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2207, 'n_words': 355}