UNPKG

@plantinformatics/vcf-genotype-brapi

Version:

Client and server functions to access genotype data from VCF via a custom web API and BrAPI

117 lines (102 loc) 3.53 kB
//------------------------------------------------------------------------------ /** Base of web API endpoint URLs of IPK PanBARLEX */ const baseUrl = 'https://panbarlex.ipk-gatersleben.de'; //------------------------------------------------------------------------------ /** * Represents an interval selected ("brushed") on a chromosome axis by the user. * @typedef {object} Interval * @property {string} genotype - The genotype to fetch data for. * @property {string} contig - The contig to fetch data for. * @property {number} start - The starting position of the data range. * @property {number} end - The ending position of the data range. */ /** * Fetches dot plot data from the web API. * * Explanation : * This function accepts parameters for `genotype`, `contig`, `start`, and `end`, allowing you to fetch different data sets based on user input or other dynamic values. * It constructs the request body based on these parameters and performs the fetch operation. * It checks if the response is OK and then parses the JSON data if it is. * * Error Handling : * The function includes error handling to log any issues with the fetch operation. * * The first version of this function was partially generated by ChatGPT based * on /dotplot network request recorded by Web Inspector `Copy as Fetch`. * * @param {Interval[]} intervals - An array of objects each containing the parameters * - {genotype, contig, start, end} * * @returns {Promise<Object>} The JSON response from the API containing the dot plot data. * @throws {Error} Will throw an error if the fetch operation fails or the response is not OK. */ export async function fetchDotPlotData(intervals) { const fnName = 'fetchDotPlotData', url = baseUrl + '/assemblies/dotplot', entries = intervals.slice(0, 2) .map((interval, i) => [["1st", "2nd"][i] , interval]), params = Object.fromEntries(entries), body = JSON.stringify(params); try { const response = await fetch(url, { method: "POST", // Method is required body: body, // Request body mode: "cors" // CORS mode }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching dot plot data:", error); throw error; // Rethrow so the caller can handle it } } //------------------------------------------------------------------------------ // test data for Dotplot Types : const Self_Alignment = [ { genotype: "Akashinriki", contig: "chr1H", start: 0, end: 200 }], Pairwise_Alignment = [ { contig : "chr1H", end : 100, genotype : "RGT_Planet", start : 0, }, { contig : "chr1H", end : 100, genotype : "OUN333", start : 0, }]; /** * Example function to retrieve dot plot data and log it to the console. * This demonstrates how to call `fetchDotPlotData` with sample parameters and handle the result. * * Usage : // Call the example function * getDotPlot(); */ async function getDotPlot() { try { const params = Self_Alignment, // params = Pairwise_Alignment, result = await fetchDotPlotData(params); console.log("Dot plot data:", result); // Process the result as needed } catch (error) { console.error("Failed to get dot plot data:", error); } } //------------------------------------------------------------------------------