Skip to content

Commit 3d202fd

Browse files
authored
Merge pull request #15 from aanil/master
Switch CouchDB with cloudant-python-sdk and get version from git release
2 parents b925793 + 4647f39 commit 3d202fd

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

multiqc_ngi/multiqc_ngi.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
from __future__ import print_function
66
from collections import OrderedDict
7-
from couchdb import Server
87
import logging
8+
from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1, ViewQuery
9+
from ibm_cloud_sdk_core import ApiException
910
import json
1011
import os
1112
import re
1213
import requests
13-
import socket
1414
import subprocess
1515
import yaml
1616

@@ -215,11 +215,11 @@ def get_ngi_project_metadata(self, pid):
215215
if self.couch is None:
216216
return None
217217
try:
218-
p_view = self.couch['projects'].view('project/summary')
219-
except socket.error:
218+
p_view = self.couch.post_view(db="projects", ddoc="project", view="summary").get_result()
219+
except ConnectionError:
220220
log.error('CouchDB Operation timed out')
221221
p_summary = None
222-
for row in p_view:
222+
for row in p_view['rows']:
223223
if row['key'][1] == pid:
224224
p_summary = row
225225

@@ -281,15 +281,15 @@ def get_ngi_samples_metadata(self, pid, s_names=None):
281281
""" Get project sample metadata from statusdb """
282282
if self.test_data is not None:
283283
report.ngi['sample_meta'] = self.test_data['samples']
284-
elif self.couch is not None:
285-
p_view = self.couch['projects'].view('project/samples')
286-
p_samples = p_view[pid]
287-
if not len(p_samples.rows) == 1:
288-
log.error(f"statusdb returned {len(p_samples.rows)} rows when querying {pid}")
284+
elif self.couch is not None:
285+
p_view_results = self.couch.post_view_queries(db="projects", ddoc="project", queries=[ViewQuery(key=pid)],
286+
view="samples").get_result()
287+
if not len(p_view_results['results'][0]['rows']) == 1:
288+
log.error(f"statusdb returned {len(p_view_results['results'][0]['rows'])} rows when querying {pid}")
289289
else:
290290
if 'sample_meta' not in report.ngi:
291291
report.ngi['sample_meta'] = dict()
292-
report.ngi['sample_meta'].update(p_samples.rows[0]['value'])
292+
report.ngi['sample_meta'].update(p_view_results['results'][0]['rows'][0]['value'])
293293

294294
if 'ngi_names' not in report.ngi:
295295
report.ngi['ngi_names'] = dict()
@@ -483,18 +483,17 @@ def push_statusdb_multiqc_data(self):
483483
if self.couch is None:
484484
return None
485485
try:
486-
db = self.couch['analysis']
487-
p_view = db.view('project/project_id')
488-
except socket.error:
486+
p_view_results = self.couch.post_view_queries(db="analysis", ddoc="project",
487+
queries=[ViewQuery(key=report.ngi['pid'])],
488+
view="project_id").get_result()
489+
except ConnectionError:
489490
log.error('CouchDB Operation timed out')
490491
return None
491492

492493
# Try to get an existing document if one exists
493494
doc = {}
494-
for row in p_view:
495-
if row['key'] == report.ngi['pid']:
496-
doc = row.value
497-
break
495+
if p_view_results['results'][0]['rows']:
496+
doc = p_view_results['results'][0]['rows'][0]['value']
498497

499498
# Start fresh unless the existing doc looks similar
500499
newdoc = {
@@ -530,12 +529,12 @@ def push_statusdb_multiqc_data(self):
530529

531530
# Save object to the database
532531
try:
533-
db.save(doc)
534-
except ValueError as e:
535-
if e.args[0] == 'Out of range float values are not JSON compliant':
536-
log.debug('Error saving to StatusDB: Out of range float values are not JSON compliant, might be NaNs, trying again...')
532+
self.couch.post_document(db="analysis", document=doc).get_result()
533+
except ApiException as e:
534+
if e.message[0] == 'bad_request: invalid UTF-8 JSON':
535+
log.debug('Error saving to StatusDB: bad_request: invalid UTF-8 JSON, might be NaNs, trying again...')
537536
doc = json.loads(utils.util_functions.dump_json(doc, filehandle=None))
538-
db.save(doc)
537+
self.couch.post_document(db="analysis", document=doc).get_result()
539538
log.debug('Saved to StatusDB after converting NaNs to nulls')
540539
else:
541540
log.error(f'Error saving to StatusDB: {e}')
@@ -576,7 +575,10 @@ def connect_statusdb(self):
576575
log.warning("Cannot contact statusdb - skipping NGI metadata stuff")
577576
return None
578577

579-
return Server(server_url)
578+
couch_server = cloudant_v1.CloudantV1(authenticator=CouchDbSessionAuthenticator(couch_user, password))
579+
couch_server.set_service_url(f"https://{couch_url}")
580+
581+
return couch_server
580582

581583

582584

setup.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@
44
specific to the National Genomics Infrastructure at the Science for Life
55
Laboratory in Stockholm, Sweden.
66
7+
Author: Phil Ewels
8+
79
For more information about NGI, see http://www.scilifelab.se/platforms/ngi/
810
For more information about MultiQC, see http://multiqc.info
911
"""
1012

1113
from setuptools import setup, find_packages
14+
import subprocess
15+
1216

13-
version = '0.8.0'
17+
def get_version():
18+
return subprocess.check_output(["git", "describe", "--tags"]).strip().decode("utf-8")
1419

1520
setup(
1621
name = 'multiqc_ngi',
17-
version = version,
18-
author = 'Phil Ewels',
19-
author_email = '[email protected]',
22+
version = get_version(),
2023
description = "MultiQC plugin for the National Genomics Infrastructure @ SciLifeLab Sweden",
2124
long_description = __doc__,
2225
keywords = 'bioinformatics',
23-
url = 'https://github.com/ewels/MultiQC_NGI',
24-
download_url = 'https://github.com/ewels/MultiQC_NGI/releases',
26+
url = 'https://github.com/NationalGenomicsInfrastructure/MultiQC_NGI',
27+
download_url = 'https://github.com/NationalGenomicsInfrastructure/MultiQC_NGI/releases',
2528
license = 'MIT',
2629
packages = find_packages(),
2730
include_package_data = True,
2831
install_requires = [
29-
'couchdb',
32+
'ibmcloudant>=0.9.1'
3033
'simplejson',
3134
'pyyaml',
3235
'requests',

0 commit comments

Comments
 (0)