Skip to content

Commit

Permalink
Merge pull request #2146 from antgonza/analysis-refactor-minor-fixes
Browse files Browse the repository at this point in the history
Analysis refactor minor fixes
  • Loading branch information
josenavas committed Jun 16, 2017
2 parents 64c3c96 + 24905ff commit 4caaec7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 68 deletions.
8 changes: 5 additions & 3 deletions qiita_db/support_files/patches/python_patches/54.py
Expand Up @@ -96,9 +96,11 @@ def create_non_rarefied_biom_artifact(analysis, biom_data, rarefied_table):
biom_table = load_table(biom_fp)
samples = set(samples).intersection(biom_table.ids())
biom_table.filter(samples, axis='sample', inplace=True)
new_table = new_table.merge(biom_table)
ids_map.update({sid: "%d.%s" % (a_id, sid)
for sid in biom_table.ids()})
# we need to check if the table has samples left before merging
if biom_table.shape[0] != 0 and biom_table.shape[1] != 0:
new_table = new_table.merge(biom_table)
ids_map.update({sid: "%d.%s" % (a_id, sid)
for sid in biom_table.ids()})

# Check if we need to rename the sample ids in the biom table
new_table_ids = set(new_table.ids())
Expand Down
10 changes: 0 additions & 10 deletions qiita_db/test/test_util.py
Expand Up @@ -446,16 +446,6 @@ def test_move_filepaths_to_upload_folder(self):

self.files_to_remove.append(new_fp)

def test_get_filepath_id(self):
_, base = qdb.util.get_mountpoint("raw_data")[0]
fp = join(base, '1_s_G1_L001_sequences.fastq.gz')
obs = qdb.util.get_filepath_id("raw_data", fp)
self.assertEqual(obs, 1)

def test_get_filepath_id_error(self):
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.util.get_filepath_id("raw_data", "Not_a_path")

def test_get_mountpoint(self):
exp = [(5, join(qdb.util.get_db_files_base_dir(), 'raw_data'))]
obs = qdb.util.get_mountpoint("raw_data")
Expand Down
40 changes: 1 addition & 39 deletions qiita_db/util.py
Expand Up @@ -48,7 +48,7 @@
from binascii import crc32
from bcrypt import hashpw, gensalt
from functools import partial
from os.path import join, basename, isdir, relpath, exists
from os.path import join, basename, isdir, exists
from os import walk, remove, listdir, makedirs, rename
from shutil import move, rmtree, copy as shutil_copy
from json import dumps
Expand Down Expand Up @@ -841,44 +841,6 @@ def move_filepaths_to_upload_folder(study_id, filepaths):
qdb.sql_connection.TRN.execute()


def get_filepath_id(table, fp):
"""Return the filepath_id of fp
Parameters
----------
table : str
The table type so we can search on this one
fp : str
The full filepath
Returns
-------
int
The filepath id forthe given filepath
Raises
------
QiitaDBError
If fp is not stored in the DB.
"""
with qdb.sql_connection.TRN:
_, mp = get_mountpoint(table)[0]
base_fp = join(get_db_files_base_dir(), mp)

sql = "SELECT filepath_id FROM qiita.filepath WHERE filepath=%s"
qdb.sql_connection.TRN.add(sql, [relpath(fp, base_fp)])
fp_id = qdb.sql_connection.TRN.execute_fetchindex()

# check if the query has actually returned something
if not fp_id:
raise qdb.exceptions.QiitaDBError(
"Filepath not stored in the database")

# If there was a result it was a single row and and single value,
# hence access to [0][0]
return fp_id[0][0]


def filepath_id_to_rel_path(filepath_id):
"""Gets the relative to the base directory of filepath_id
Expand Down
26 changes: 10 additions & 16 deletions qiita_pet/handlers/analysis_handlers/listing_handlers.py
Expand Up @@ -19,7 +19,7 @@
from qiita_pet.handlers.util import download_link_or_path
from qiita_pet.handlers.analysis_handlers import check_analysis_access
from qiita_pet.util import is_localhost
from qiita_db.util import get_filepath_id
from qiita_db.util import retrieve_filepaths
from qiita_db.analysis import Analysis
from qiita_db.logger import LogEntry
from qiita_db.reference import Reference
Expand All @@ -37,27 +37,21 @@ def get(self):
analyses = user.shared_analyses | user.private_analyses

is_local_request = is_localhost(self.request.headers['host'])
gfi = partial(get_filepath_id, 'analysis')
dlop = partial(download_link_or_path, is_local_request)
mappings = {}
bioms = {}
tgzs = {}
for analysis in analyses:
_id = analysis.id
# getting mapping file
mapping = analysis.mapping_file
if mapping is not None:
mappings[_id] = dlop(mapping, gfi(mapping), 'mapping file')
else:
mappings[_id] = ''

bioms[_id] = ''
# getting tgz file
tgz = analysis.tgz
if tgz is not None:
tgzs[_id] = dlop(tgz, gfi(tgz), 'tgz file')
else:
tgzs[_id] = ''
mappings[_id], bioms[_id], tgzs[_id] = '', '', ''
for fid, fp, fpt in retrieve_filepaths('analysis_filepath',
'analysis_id', _id):
if fpt == 'plain_text':
mappings[_id] = dlop(fp, fid, 'mapping file')
if fpt == 'biom':
bioms[_id] = dlop(fp, fid, 'biom file')
if fpt == 'tgz':
tgzs[_id] = dlop(fp, fid, 'tgz file')

self.render("list_analyses.html", analyses=analyses, message=message,
level=level, is_local_request=is_local_request,
Expand Down

0 comments on commit 4caaec7

Please sign in to comment.