Skip to content

Commit

Permalink
Merge pull request #11 from xgaia/dev
Browse files Browse the repository at this point in the history
Version 0.4: Bed files
  • Loading branch information
xgaia authored Jul 31, 2017
2 parents 32fe6cd + e9654cf commit 97f4157
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Installation

Clone the repository

git clone https://github.com/askomics/asko-cli.git
cd asko-cli
git clone https://github.com/askomics/askocli.git
cd askocli

Set up a virtual environment:
Set up and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate
Expand All @@ -31,7 +31,6 @@ Usage example

Integration

askocli integrate -a http://localhost -p 6543 -k mYap1Key path/to/file.csv
askocli integrate -a http://localhost -p 6543 -k mYap1Key path/to/file.csv
askocli integrate -a http://localhost -p 6543 -k mYap1Key -e gene transcript -t Arabidopsis_thaliana path/to/file.gff
askocli integrate -a http://localhost -p 6543 -k mYap1Key path/to/file.ttl
Expand All @@ -54,3 +53,7 @@ History
- Option `--disabled-columns` to don't integrate a columns of a TSV
- 0.3.2
- FIX error message
- 0.4: Bed files integration
- Integration of Bed files
- Option `--uri` to specify an uri for the entity
- Option `--headers` to set custom headers for TSV files
83 changes: 78 additions & 5 deletions libaskocli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from os.path import basename
import json
import requests
import csv
# import logging

# logging.getLogger('requests').setLevel(logging.CRITICAL)
Expand Down Expand Up @@ -47,6 +48,7 @@ def __init__(self, url, apikey, file_type):
self.type = file_type
self.public = False
self.disabled_columns = []
self.csv_headers = []

def set_cookie(self):
"""set the session cookie of user
Expand Down Expand Up @@ -172,27 +174,53 @@ def guess_col_types(self):
self.col_types = json.loads(response.text)['types']
self.col_types[0] = 'entity_start'

def set_headers(self, headers):
"""Set the headers of the CSV file"""

def integrate_data(self):
if headers is not None:
self.csv_headers = headers
return

headers = []
with open(self.path, 'r', encoding="utf-8", errors="ignore") as tabfile:

# Get dialect
contents = tabfile.readline()
dialect = csv.Sniffer().sniff(contents, delimiters=';,\t ')
tabfile.seek(0)

# Load the file with reader
tabreader = csv.reader(tabfile, dialect=dialect)

# first line is header
headers = next(tabreader)
headers = [h.strip() for h in headers]

self.csv_headers = headers


def integrate_data(self, uri):
"""Integrate the csv file into the triplestore
:returns: response text
:rtype: string
"""


url = self.url + '/load_data_into_graph'


json_dict = {
'file_name': basename(self.path),
'col_types': self.col_types,
'disabled_columns': self.disabled_columns,
'key_columns': self.key_columns,
'public': self.public,
'forced_type': self.type
'forced_type': self.type,
'headers': self.csv_headers
}

if uri is not None:
json_dict['uri'] = uri

response = requests.post(url, cookies=self.cookies, headers=self.headers, json=json_dict)

if response.status_code != 200:
Expand All @@ -204,13 +232,14 @@ def integrate_data(self):

return response.text

def integrate_gff(self, taxon, entities):
def integrate_gff(self, taxon, entities, uri):
"""Integrate a gff into the triplestore
:param taxon: taxon
:type taxon: string
:param entities: list of entities to integrate
:type entities: list
:param uri: a custom URI for this entity
:returns: response text
:rtype: string
"""
Expand All @@ -225,6 +254,9 @@ def integrate_gff(self, taxon, entities):
'forced_type': self.type
}

if uri is not None:
json_dict['uri'] = uri

response = requests.post(url, cookies=self.cookies, headers=self.headers, json=json_dict)

if response.status_code != 200:
Expand All @@ -236,6 +268,47 @@ def integrate_gff(self, taxon, entities):

return response.text

def integrate_bed(self, entity_name, taxon, uri):
"""Integrate a bed file into AskOmics
:param entity_name: The entityname, descibed in the bed file
:type entity_name: string
:param taxon: the taxon described into the bed file
:type taxon: string
:param uri: a custom URI for this entity
:type uri: string
:returns: response text
:rtype: string
"""

url = self.url + '/load_bed_into_graph'

if taxon is None:
taxon = ''
if entity_name is None:
entity_name = ''

json_dict = {
'file_name': basename(self.path),
'taxon': taxon,
'entity_name': entity_name,
'public': self.public
}

if uri is not None:
json_dict['uri'] = uri

response = requests.post(url, cookies=self.cookies, headers=self.headers, json=json_dict)

if response.status_code != 200:
raise Exception('Unexpected response from AskOmics when integrate bed: ' +
str(response.status_code) + '\n' + response.text)

if 'error' in json.loads(response.text):
raise Exception('AskOmics error: ' + str(json.loads(response.text)['error']))

return response.text

def integrate_ttl(self):
"""Integrate a ttl into the triplestore
Expand Down
29 changes: 21 additions & 8 deletions libaskocli/actions/integrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ def run(self, args):
:type args: Namespace
"""

parser = argparse.ArgumentParser(prog='askocli ' + self.__class__.__name__, description='Integrate data to a distant AskOmics')
parser = argparse.ArgumentParser(prog='askocli integrate', description='Integrate data to a distant AskOmics')
askomics_auth(parser)
askomics_url(parser)
parser.add_argument('file', nargs='?', type=str, action="store", help="file to integrate")
parser.add_argument('--file-type', help='The file type')
parser.add_argument('--public', action='store_true')
parser.add_argument('--uri', help='Custom URI')

parser.add_argument('--key-columns', nargs='*', help='List of the key columns index')
parser.add_argument('--disabled-columns', nargs='*', help='List of columns index to disable')
parser.add_argument('-c', '--columns', nargs='*', help='List of forced columns types')
parser.add_argument('-e', '--entities', nargs='*', help='List of entities to integrate')
parser.add_argument('-t', '--taxon', help='Taxon')
# CSV args
parser.add_argument('--headers', nargs='*', help='List of custom headers (csv)')
parser.add_argument('--key-columns', nargs='*', help='List of the key columns index (csv)')
parser.add_argument('--disabled-columns', nargs='*', help='List of columns index to disable (csv)')
parser.add_argument('-c', '--columns', nargs='*', help='List of forced columns types (csv)')

# GFF args
parser.add_argument('-e', '--entities', nargs='*', help='List of entities to integrate (gff)')

# GFF and BED args
parser.add_argument('-t', '--taxon', help='Taxon (gff and bed)')

# BED args
parser.add_argument('-n', '--entity-name', help='Entity name (bed)')

args = parser.parse_args(args)

Expand All @@ -48,9 +58,11 @@ def run(self, args):
ext = os.path.splitext(basename(args.file))[1].lower()

if ext in ('.gff', '.gff2', '.gff3') or args.file_type == 'gff':
api.integrate_gff(args.taxon, args.entities)
api.integrate_gff(args.taxon, args.entities, args.uri)
elif ext == '.ttl' or args.file_type == 'ttl':
api.integrate_ttl()
elif ext in ('.bed',) or args.file_type == 'bed':
api.integrate_bed(args.entity_name, args.taxon, args.uri)
else:
if args.key_columns:
api.set_key_columns(args.key_columns)
Expand All @@ -60,4 +72,5 @@ def run(self, args):
api.guess_col_types()
if args.disabled_columns:
api.set_disabled_columns(args.disabled_columns)
api.integrate_data()
api.set_headers(args.headers)
api.integrate_data(args.uri)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

setup(
name='askocli',
version='0.3.4',
version='0.4',
description='Command line interface for a distant AskOmics',
author='Xavier Garnier',
author_email='[email protected]',
url='https://github.com/askomics/askocli',
download_url='https://github.com/askomics/askocli/archive/0.3.4.git tar.gz',
download_url='https://github.com/askomics/askocli/archive/0.4.git tar.gz',
install_requires=['requests>=2.4.3'],
packages=find_packages(),
license='AGPL',
Expand Down

0 comments on commit 97f4157

Please sign in to comment.