<resource schema="species" resdir=".">
	<meta name="creationDate">2022-08-02T12:18:57Z</meta>
	<meta name="schema-rank">500</meta>

	<meta name="title">Atomic and Molecular Species Names</meta>
	<meta name="description">
		A TAP-queriable database of common names of molecules and their
		InChIs.  This is an experimental copy of http://species.vamdc.org
		created in the context of the LineTAP effort, augmented with names
		found in the CASA line list.  You will probably want
		to query this using SQL wildcards (% and _) and the ILIKE operator.
	</meta>
	<meta name="subject">astrochemistry</meta>
	<meta name="subject">virtual-observatories</meta>

	<meta name="creator">The VAMDC consortium; Brogan, C.; Remijan, A. J. ;
		Markwick-Kemper, A.</meta>

	<meta name="source">http://species.vamdc.org</meta>
	<meta name="contentLevel">Research</meta>
	<meta name="type">Catalog</meta>

	<table id="main" onDisk="True" adql="True">
		<meta name="utype">ivo://ivoa.net/std/linetap#species-1.0</meta>
		<index columns="inchikey"/>
		<index columns="name"/>
		<index columns="formula"/>
		<index columns="name" method="gin"
			>lower(name) gin_trgm_ops</index>
		<index columns="formula" method="gin"
			>lower(formula)</index>

		<column name="inchikey" type="text"
			description="InChIKey of this species"/>
		<column name="inchi" type="text"
			description="InChI of this species"/>
		<column name="name" type="unicode"
			description="A common name of this species"/>
		<column name="formula" type="text"
			description="Chemical formula of this species in some free-ish notation"/>
		<column name="source_id" type="text"
			description="VAMDC identifier of the origin of this mapping"/>
	</table>

	<data id="import">
		<sources pattern="data/*.xls"/>

		<unionGrammar>
			<handles pattern="\.xls$">
				<embeddedGrammar>
					<iterator>
						<setup imports="xlrd"/>
						<code>
							book = xlrd.open_workbook(self.sourceToken)
							sheet = book.sheet_by_index(0)
							assert sheet.name=="speciesList"

							# row 0 is the column names
							keys = [cell.value.lower().replace(" ", "_")
								for cell in sheet.row(0)]
							# we want 'stoichiometricformula' for formula
							# and drop formula.
							keys[keys.index("formula")] = "odd-formula"
							keys[keys.index("stoichiometricformula")] = "formula"

							inchiIndex = keys.index("inchikey")

							for rowInd in range(1, sheet.nrows):
								row = sheet.row(rowInd)
								if row[inchiIndex].value:
									yield dict((key, cell.value)
										for key, cell in zip(keys, row))
						</code>
					</iterator>
				</embeddedGrammar>
			</handles>
			<handles pattern="\.csv$">
				<csvGrammar/>
			</handles>
		</unionGrammar>

		<make table="main">
			<rowmaker idmaps="*">
				<apply>
					<code>
						if @inchi.startswith("1S"):
							@inchi = "InChI="+@inchi
					</code>
				</apply>
				<simplemaps>
					source_id: node_identifier
				</simplemaps>
			</rowmaker>
		</make>
	</data>

	<service id="web">
		<publish render="form" sets="local,ivo_managed"/>
		<meta name="shortName">species web</meta>
		<meta name="description">A form to search the VAMDC species
		list published on the TAP service at http://dc.g-vo.org/tap.
		Enter parts of species names or chemical formulae here and get
		back matching names, inchis and inchikeys.</meta>

		<dbCore queriedTable="main">
			<condDesc>
				<inputKey name="parts" type="text"
					description="Particles to match in name or formula
						(case-insensitively)"/>
				<phraseMaker>
					<code>
					pat = '%'+inPars[inputKeys[0].name].replace(
						"%", r"\\%").replace("_", r"\\_"
						)+'%'
					yield "name like %({})s or formula like %({})s".format(
						base.getSQLKey("name", pat, outPars),
						base.getSQLKey("formula", pat, outPars))
					</code>
				</phraseMaker>
			</condDesc>
		</dbCore>
	</service>

	<regSuite title="species test">
		<regTest title="Species TAP table has data">
			<url parSet="TAP" QUERY="
				select inchikey, name, formula 
				from species.main
				where name ilike '%oxacycl%' and inchi like '%i3+2'
			">/tap/sync</url>
			<code>
				rows = self.getVOTableRows()
				self.assertEqual(len(rows), 2)
				self.assertEqual(rows[0]["inchikey"],
					"IAYPIBMASNFSPL-YZRHJBSPSA-N")
				self.assertEqual(rows[0]["inchikey"],
					rows[1]["inchikey"])
			</code>
		</regTest>

		<regTest title="Species form service returns something">
			<url parSet="form" parts="Oxacycl">web/form</url>
			<code>
				self.assertHasStrings(
					"Oxacyclopentadiene",
					"C2H4O",
					"AHHWIHXENZJRFG-UHFFFAOYSA-N")
			</code>
		</regTest>
	</regSuite>
</resource>
