Skip to content

Commit 2c34ec7

Browse files
committed
results: Allow downloading ClusterBlast and CompaRiPPson results as TSV
Signed-off-by: Kai Blin <kblin@biosustain.dtu.dk>
1 parent 033e400 commit 2c34ec7

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/components/common/ClusterblastResults.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
<script setup lang="ts">
2+
import { downloadTsv } from "@/utils/tsv";
3+
24
const props = defineProps<{
35
hits: any[];
46
}>();
57
68
function getAreaLink(hit: any) {
79
return `/area?record=${hit.s_acc}&start=${hit.s_rec_start}&end=${hit.s_rec_end}`;
810
}
11+
12+
function triggerDownload(){
13+
downloadTsv(props.hits, 'clusterblast_results.tsv');
14+
}
915
</script>
1016

1117
<template>
1218
<div v-if="props.hits.length > 0">
19+
<button class="dl-button" @click="triggerDownload">Download TSV</button>
1320
<table>
1421
<thead>
1522
<tr>
@@ -38,4 +45,7 @@ function getAreaLink(hit: any) {
3845
.identity {
3946
text-align: right;
4047
}
48+
.dl-button {
49+
float: right;
50+
}
4151
</style>

src/components/common/ComparippsonResults.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
<script setup lang="ts">
2+
import { downloadTsv } from "@/utils/tsv";
3+
24
const props = defineProps<{
35
hits: any[];
46
}>();
57
68
function getAreaLink(hit: any) {
79
return `/area?record=${hit.s_acc}&start=${hit.s_rec_start}&end=${hit.s_rec_end}`;
810
}
11+
12+
function triggerDownload(){
13+
downloadTsv(props.hits, 'clusterblast_results.tsv');
14+
}
915
</script>
1016

1117
<template>
1218
<div v-if="props.hits.length > 0">
19+
<button class="dl-button" @click="triggerDownload">Download TSV</button>
1320
<table>
1421
<thead>
1522
<tr>
@@ -40,4 +47,7 @@ function getAreaLink(hit: any) {
4047
.identity {
4148
text-align: right;
4249
}
50+
.dl-button {
51+
float: right;
52+
}
4353
</style>

src/utils/tsv.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export const downloadTsv = (
2+
data: object[],
3+
filename: string,
4+
headers?: string[],
5+
separator?: string,
6+
) => {
7+
if (!data || !data.length) {
8+
return;
9+
}
10+
if (separator === undefined) {
11+
separator = "\t";
12+
}
13+
14+
const keys: string[] = Object.keys(data[0]);
15+
16+
let columnHeaders: string[];
17+
18+
if (headers) {
19+
columnHeaders = headers;
20+
} else {
21+
columnHeaders = keys;
22+
}
23+
24+
const contentRows = data.map(dataItem => {
25+
return keys.map(key => {
26+
if (dataItem[key] === null || dataItem[key] === undefined) {
27+
return "";
28+
}
29+
return String(dataItem[key]).replace(/\t/g, " ");
30+
}).join(separator);
31+
});
32+
33+
const tsvData = [columnHeaders.join(separator), ...contentRows].join("\n")
34+
35+
const blob = new Blob([tsvData], { type: "text/csv;charset=utf-8" });
36+
const link = document.createElement("a");
37+
link.href = window.URL.createObjectURL(blob);
38+
link.download = filename;
39+
link.click();
40+
};
41+
42+

0 commit comments

Comments
 (0)