Skip to content

Commit

Permalink
get basic version working
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jun 17, 2024
1 parent 02c2af5 commit 49b5681
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ pkg

# For rubinius:
#*.rbc

node_modules/**
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Option, program } from 'commander';
import { createInterface } from 'node:readline';

const VALID_FORMATS = ["fasta", "gff", "json", "rdf", "sequence", "xml"];

program
.version("1.0.0")
.summary("Command line interface to UniProt web services.")
.description(`Command line interface to UniProt web services.
The uniprot command fetches UniProt entries from the UniProt web services. The command expects a list of UniProt Accession Numbers that are passed
- as separate command line arguments
- to standard input
The command will give priority to the first way UniProt Accession Numbers are passed, in the order as listed above. The standard input should have one UniProt Accession Number per line.
The uniprot command yields just the protein sequences as a default, but can return several formats.`)
.argument("[accessions...]", "UniProt Accession Numbers")
.addOption(new Option("-f, --format <format>", `output format`).choices(VALID_FORMATS).default("sequence"));

program.parse(process.argv);
const format = program.opts().format;
const accessions = program.args;

if (accessions.length !== 0) {
accessions.forEach(processUniprotEntry);
} else {
for await (const line of createInterface({ input: process.stdin })) {
processUniprotEntry(line.trim());
};
}

async function processUniprotEntry(accession) {
process.stdout.write(await getUniprotEntry(accession, format) + "\n");
}

async function getUniprotEntry(accession, format) {
if (format === "sequence") {
return (await getUniprotEntry(accession, "fasta"))
.split("\n")
.slice(1)
.join("");
} else {
const r = await fetch(`https://rest.uniprot.org/uniprotkb/${accession}.${format}`);
if (r.ok) {
return r.text();
} else {
process.stderr.write(`Error fetching ${accession}: ${r.status} ${r.statusText}\n`);
return "";
}
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"repository": "[email protected]:unipept/unipept-cli.git",
"author": "Bart Mesuere <[email protected]>",
"license": "MIT",
"private": false
"private": false,
"type": "module",
"dependencies": {
"commander": "^12.1.0"
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


commander@^12.1.0:
version "12.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-12.1.0.tgz#01423b36f501259fdaac4d0e4d60c96c991585d3"
integrity sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==

0 comments on commit 49b5681

Please sign in to comment.