diff --git a/README.md b/README.md index c7fd002..70c7f3e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,11 @@ This command depends these commands(`head|tail|cut|sort|awk`) on bash. ``` colc [column] [file.csv|tsv|txt] + +*1 +When you set [column,column] on the column parameter, +you'll get the correlation coefficient of two columns. +(thie feature don't support below options) ``` ## Option @@ -48,7 +53,7 @@ you can download a binary release ```sh # Install with wget or curl ## set the latest version on releases. -VERSION=v1.0.14 +VERSION=v1.0.15 ## case you use wget wget https://github.com/solaoi/colc/releases/download/$VERSION/colc_linux_amd64.tar.gz ## case you use curl diff --git a/colc.ts b/colc.ts index 4142006..ada3995 100644 --- a/colc.ts +++ b/colc.ts @@ -9,7 +9,18 @@ import { parse } from "https://deno.land/std@0.66.0/flags/mod.ts"; const { _, binsize, b, filter, f, check, c } = parse(Deno.args); const [column, filename] = _; -if (typeof column !== "number" || typeof filename !== "string") { +const hasTwoColumn = (() => { + if (typeof column !== "string") return false; + if (column.indexOf(",") === -1) return false; + const columns = column.split(","); + if (columns.length !== 2) return false; + return Number.isInteger(parseInt(columns[0])) && + Number.isInteger(parseInt(columns[1])); +})(); + +if ( + (typeof column !== "number" && !hasTwoColumn) || typeof filename !== "string" +) { console.log("Usage:\n colc [column] [file.csv|tsv|txt]"); Deno.exit(1); } @@ -35,6 +46,36 @@ const headerName = await runner.run( ); const hasHeader = headerName !== ""; +if (hasTwoColumn) { + const statsCommand = (() => { + const bash = []; + if (hasHeader) { + bash.push( + `tail -n +2 ${filename} | cut -f${column} ${isCsv ? "-d, " : ""}`, + ); + } else { + bash.push( + `cut -f${column} ${isCsv ? "-d, " : ""}${filename} `, + ); + } + bash.push("| awk"); + bash.push( + `'BEGIN{OFMT="%.6f"}{split($1,col,",");asum+=col[1];a[NR]=col[1];bsum+=col[2];b[NR]=col[2]}END{amean=asum/NR;bmean=bsum/NR;for(i in a){as+=(a[i]-amean)^2;bs+=(b[i]-bmean)^2;sum+=(a[i]-amean)*(b[i]-bmean)};astddev=sqrt(as/NR);bstddev=sqrt(bs/NR);print sum/NR/astddev/bstddev}'`, + ); + return bash.join(" "); + })(); + const correlationCoefficient = await runner + .run(statsCommand); + const { println, showHeader, hr } = formatter( + 24, + correlationCoefficient.length, + ); + hasHeader ? showHeader(headerName) : hr(); + println("correlation-coefficient", correlationCoefficient); + hr(); + Deno.exit(0); +} + if (hasCheck) { const checkCommand = (() => { const bash = [];