Skip to content

Commit 17da9c2

Browse files
committed
Merge pull request #845 from squirrelo/issue-835
Issue 835
2 parents 559556e + 5a38e03 commit 17da9c2

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

qiita_db/analysis.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# -----------------------------------------------------------------------------
1818
from __future__ import division
1919
from collections import defaultdict
20-
from binascii import crc32
2120
from os.path import join
2221

2322
from future.utils import viewitems
@@ -31,7 +30,7 @@
3130
from .study import Study
3231
from .exceptions import QiitaDBStatusError # QiitaDBNotImplementedError
3332
from .util import (convert_to_id, get_work_base_dir,
34-
get_mountpoint, get_table_cols)
33+
get_mountpoint, get_table_cols, insert_filepaths)
3534

3635

3736
class Analysis(QiitaStatusObject):
@@ -738,24 +737,11 @@ def _add_file(self, filename, filetype, data_type=None, conn_handler=None):
738737
conn_handler = conn_handler if conn_handler is not None \
739738
else SQLConnectionHandler()
740739

741-
# get required bookkeeping data for DB
742-
_, base_fp = get_mountpoint(self._table, conn_handler=conn_handler)[0]
743-
fptypeid = convert_to_id(filetype, "filepath_type", conn_handler)
744-
fullpath = join(base_fp, filename)
745-
with open(fullpath, 'rb') as f:
746-
checksum = crc32(f.read()) & 0xffffffff
747-
748-
analysis_dd_id, _ = get_mountpoint("analysis",
749-
conn_handler=conn_handler)[0]
750-
751-
# add file to analysis
752-
sql = ("INSERT INTO qiita.filepath (filepath, filepath_type_id, "
753-
"checksum, checksum_algorithm_id, data_directory_id) VALUES "
754-
"(%s, %s, %s, %s, %s) RETURNING filepath_id")
755-
# magic number 1 is for crc32 checksum algorithm
756-
fpid = conn_handler.execute_fetchone(sql, (fullpath, fptypeid,
757-
checksum, 1,
758-
analysis_dd_id))[0]
740+
filetype_id = convert_to_id(filetype, 'filepath_type', conn_handler)
741+
_, mp = get_mountpoint('analysis', conn_handler)[0]
742+
fpid = insert_filepaths([
743+
(join(mp, filename), filetype_id)], -1, 'analysis', 'filepath',
744+
conn_handler, move_files=False)[0]
759745

760746
col = ""
761747
dtid = ""

qiita_db/support_files/patches/14.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
--Feb 11, 2015
2+
--Placeholder for python to run
13
-- we don't really need an SQL patch, but having nothing here breaks the build
24
select 42;

qiita_db/support_files/patches/15.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--Feb 11, 2015
2+
--Placeholder for python to run
3+
-- we don't really need an SQL patch, but having nothing here breaks the build
4+
select 42;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Feb 11, 2015
2+
# This changes all analysis files to be relative path instead of absolute
3+
4+
from os.path import basename, dirname
5+
6+
from qiita_db.util import get_mountpoint
7+
from qiita_db.sql_connection import SQLConnectionHandler
8+
9+
conn_handler = SQLConnectionHandler()
10+
11+
filepaths = conn_handler.execute_fetchall(
12+
'SELECT f.* from qiita.filepath f JOIN qiita.analysis_filepath afp ON '
13+
'f.filepath_id = afp.filepath_id')
14+
15+
# retrieve relative filepaths as dictionary for matching
16+
mountpoints = {m[1].rstrip('/\\'): m[0] for m in get_mountpoint(
17+
'analysis', conn_handler=conn_handler, retrieve_all=True)}
18+
19+
for filepath in filepaths:
20+
filename = basename(filepath['filepath'])
21+
# find the ID of the analysis filepath used
22+
mp_id = mountpoints[dirname(filepath['filepath']).rstrip('/\\')]
23+
conn_handler.execute(
24+
'UPDATE qiita.filepath SET filepath = %s, data_directory_id = %s WHERE'
25+
' filepath_id = %s',
26+
[filename, mp_id, filepath['filepath_id']])

qiita_db/test/test_analysis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest import TestCase, main
2+
from os import remove
23
from os.path import exists, join
34
from datetime import datetime
45
from shutil import move
@@ -37,6 +38,10 @@ def tearDown(self):
3738
with open(self.map_fp, 'w') as f:
3839
f.write("")
3940

41+
fp = join(get_mountpoint('analysis')[0][1], 'testfile.txt')
42+
if exists(fp):
43+
remove(fp)
44+
4045
mp = get_mountpoint("processed_data")[0][1]
4146
study2fp = join(mp, "2_2_study_1001_closed_reference_otu_table.biom")
4247
if exists(study2fp):
@@ -427,6 +432,22 @@ def test_build_files_raises_value_error(self):
427432
with self.assertRaises(ValueError):
428433
self.analysis.build_files(-10)
429434

435+
def test_add_file(self):
436+
fp = join(get_mountpoint('analysis')[0][1], 'testfile.txt')
437+
with open(fp, 'w') as f:
438+
f.write('testfile!')
439+
self.analysis._add_file('testfile.txt', 'plain_text', '18S')
440+
441+
obs = self.conn_handler.execute_fetchall(
442+
'SELECT * FROM qiita.filepath WHERE filepath_id = 19')
443+
exp = [[19, 'testfile.txt', 9, '3675007573', 1, 1]]
444+
self.assertEqual(obs, exp)
445+
446+
obs = self.conn_handler.execute_fetchall(
447+
'SELECT * FROM qiita.analysis_filepath WHERE filepath_id = 19')
448+
exp = [[1, 19, 2]]
449+
self.assertEqual(obs, exp)
450+
430451

431452
if __name__ == "__main__":
432453
main()

qiita_db/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ def get_mountpoint(mount_type, conn_handler=None, retrieve_all=False):
608608
"SELECT data_directory_id, mountpoint, subdirectory FROM "
609609
"qiita.data_directory WHERE data_type='%s' and active=true"
610610
% mount_type)]
611-
612-
return [(d, join(get_db_files_base_dir(), m, s)) for d, m, s in result]
611+
basedir = get_db_files_base_dir()
612+
return [(d, join(basedir, m, s)) for d, m, s in result]
613613

614614

615615
def insert_filepaths(filepaths, obj_id, table, filepath_table, conn_handler,
@@ -627,7 +627,7 @@ def insert_filepaths(filepaths, obj_id, table, filepath_table, conn_handler,
627627
Id of the object calling the functions. Disregarded if move_files
628628
is False
629629
table : str
630-
Table that holds the file data. Disregarded if move_files is False
630+
Table that holds the file data.
631631
filepath_table : str
632632
Table that holds the filepath information
633633
conn_handler : SQLConnectionHandler

0 commit comments

Comments
 (0)