Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
djcomlab committed Mar 21, 2017
2 parents a3ea9f9 + 545615a commit 6fb3d89
Show file tree
Hide file tree
Showing 31 changed files with 1,404 additions and 653 deletions.
37 changes: 37 additions & 0 deletions docs/exportdata.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
###############################
Exporting data from ISA formats
###############################

We have provided a number of modules that allow you to export data from ISA formats to formats for well-known
databases or services.

----------------------------------
Exporting ISA content to SampleTab
----------------------------------
To export metadata from SampleTab files (e.g. for EBI BioSamples database), you can do the following to export a
ISA-Tab to SampleTab:

.. code-block:: python
from isatools.convert import isatab2sampletab
with open('your/path/to/i_investigation.txt', 'r') as input_investigation_file:
with open('your/path/to/sampletab.txt', 'w') as output_sampletab_file:
isatab2sampletab.convert(input_investigation_file, output_sampletab_file)
To export an ISA JSON file to SampleTab, you can do:

.. code-block:: python
from isatools.convert import isatab2sampletab
with open('your/path/to/i_investigation.txt', 'r') as input_investigation_file:
with open('your/path/to/sampletab.txt', 'w') as output_sampletab_file:
isatab2sampletab.convert(input_investigation_file, output_sampletab_file)
You can also dump SampleTab content directly from ISA Python objects:

.. code-block:: python
from isatools import sampletab
with open('your/path/to/sampletab.txt', 'w') as output_sampletab:
# Note: ISA would be a previously loaded or constructed root Investigation object
sampletab.dump(ISA, output_sampletab)
32 changes: 31 additions & 1 deletion docs/importdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,34 @@ tool from https://github.com/ISA-tools/mzml2isa and can be run as follows:
.. code-block:: python
from isatools.convert import mzml2isa
mzml2isa.convert('your/path/to/mzml/files/', 'tmp/', "My Study ID")
mzml2isa.convert('your/path/to/mzml/files/', 'tmp/', "My Study ID")
--------------------------
Importing SampleTab to ISA
--------------------------
To import metadata from SampleTab files (e.g. from EBI BioSamples database), you can do the following to import a
SampleTab to ISA-Tab:

.. code-block:: python
from isatools.convert import sampletab2isatab
with open('your/path/to/sampletab.txt', 'r') as input_sampletab:
sampletab2isatab.convert(input_sampletab, 'tmp/')
To import a SampleTab to ISA JSON, you can do:

.. code-block:: python
from isatools.convert import sampletab2json
with open('your/path/to/sampletab.txt', 'r') as input_sampletab:
with open('your/path/to/myjson.json', 'w') as output_json:'
sampletab2json.convert(input_sampletab, output_json)
You can also load SampleTab content directly into ISA Python objects:

.. code-block:: python
from isatools import sampletab
with open('your/path/to/sampletab.txt', 'r') as input_sampletab:
ISA = sampletab.load(input_sampletab)
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ flexible way to build and use ISA content, as well as provide utility functions
#. :doc:`Downloading files stored in Github </github>`
#. :doc:`Validating ISA-Tab and ISA JSON </validation>`
#. :doc:`Importing data in ISA formats </importdata>`
#. :doc:`Exporting data in ISA formats </exportdata>`
#. :doc:`Known Issues </knownissues>`

License
Expand All @@ -43,4 +44,5 @@ This code is licensed under the `CPAL License <https://raw.githubusercontent.com
Downloading files stored in Github <github>
Validating ISA-Tab and ISA JSON <validation>
Importing data in ISA formats <importdata>
Exporting data from ISA formats <exportdata>
Known Issues <knownissues>
5 changes: 5 additions & 0 deletions docs/knownissues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Known issues
############

isatools v0.7 package
---------------------
- Issues #101, #153 are still outstanding, as per below
- SRA/ENA importer and Biocrates importer relies on XSLT2 processing only available with SAXON and requires .jar file to run

isatools v0.6 package
---------------------
- Issues #146, #101, #153 are still outstanding, as per below
Expand Down
29 changes: 16 additions & 13 deletions isatools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ def main(argv=None):
)

p.add_argument('-c', dest='cmd', help='isatools API command to run', required=True,
choices=['isatab2json', 'json2isatab', 'isatab2sra', 'json2sra', 'zipisatab'])
p.add_argument('-i', dest='in_path', help='in folder (files will be read from here)', required=True)
p.add_argument('-o', dest='out_path', help='out folder (a new directory will be created here)', required=True)
p.add_argument('--version', action='version', version='isatools {}'.format("0.6"))
choices=['isatab2json', 'json2isatab', 'sampletab2isatab', 'sampletab2json'])
p.add_argument('-i', dest='in_path', help='in (files or directory will be read from here)', required=True)
p.add_argument('-o', dest='out_path', help='out (file will be written out here or written to directory if ISA-Tab '
'archive out)', required=True)
p.add_argument('--version', action='version', version='isatools {}'.format("0.7"))
p.add_argument('-v', dest='verbose', help="show more output", action='store_true', default=False)

args = p.parse_args(argv or sys.argv[1:])

if args.verbose:
print("{} input path: {}".format(os.linesep, args.in_path))
print("output path: {}".format(args.out_path))
print("{} input: {}".format(os.linesep, args.in_path))
print("output: {}".format(args.out_path))

if args.cmd == 'isatab2json':
from isatools.convert import isatab2json
Expand All @@ -41,14 +42,16 @@ def main(argv=None):
with open(args.in_path) as in_fp:
json2isatab.convert(in_fp, args.out_path)

elif args.cmd == 'isatab2sra':
raise NotImplementedError # TODO: Finish this

elif args.cmd == 'json2sra':
raise NotImplementedError # TODO: Finish this
elif args.cmd == 'sampletab2isatab':
from isatools.convert import sampletab2isatab
with open(args.in_path) as in_fp:
sampletab2isatab.convert(in_fp, args.out_path)

elif args.cmd == 'zipisatab':
raise NotImplementedError # TODO: Finish this
elif args.cmd == 'sampletab2json':
from isatools.convert import sampletab2json
with open(args.in_path) as in_fp:
with open(args.out_path) as out_fp:
sampletab2json.convert(in_fp, out_fp)

if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion isatools/config/json/default/schemas/data_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"Post Translational Modification Assignment File",
"Derived Array Data Matrix File",
"Free Induction Decay Data File",
"Metabolite Assignment File"
"Metabolite Assignment File",
"Array Data Matrix File"
]
},
"comments" : {
Expand Down
14 changes: 14 additions & 0 deletions isatools/convert/isatab2sampletab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from isatools import isatab, sampletab
import logging

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def convert(source_inv_fp, target_fp):
""" Converter for ISA-Tab to SampleTab.
:param source_inv_fp: File descriptor of input investigation file
:param target_fp: File descriptor to write output SampleTab to (must be writeable)
"""
ISA = isatab.load(source_inv_fp)
sampletab.dump(ISA, target_fp)
4 changes: 2 additions & 2 deletions isatools/convert/isatab2sra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import json
from io import StringIO
from isatools.convert import isatab2json, json2sra


def zipdir(path, zip_file):
Expand All @@ -20,11 +21,10 @@ def zipdir(path, zip_file):


def convert(source_path, dest_path, sra_settings=None, validate_first=True):
from isatools.convert import isatab2json, json2sra
isa_json = isatab2json.convert(source_path, validate_first=validate_first)
isa_json_fp = StringIO(json.dumps(isa_json))
isa_json_fp.name = "BII-S-3.json"
json2sra.convert2(isa_json_fp, dest_path, sra_settings=sra_settings, validate_first=False)
json2sra.convert(isa_json_fp, dest_path, sra_settings=sra_settings, validate_first=False)
logging.info("Conversion complete...")
buffer = BytesIO()
if os.path.isdir(dest_path):
Expand Down
14 changes: 14 additions & 0 deletions isatools/convert/json2sampletab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from isatools import isajson, sampletab
import logging

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def convert(source_json_fp, target_fp):
""" Converter for ISA-JSON to SampleTab.
:param source_json_fp: File descriptor of input ISA JSON file
:param target_fp: File descriptor to write output SampleTab to (must be writeable)
"""
ISA = isajson.load(source_json_fp)
sampletab.dump(ISA, target_fp)
20 changes: 2 additions & 18 deletions isatools/convert/json2sra.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
from isatools import isajson, sra

import glob
import os
import logging

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def convert(json_fp, path, config_dir=None):
""" Converter for ISA JSON to SRA.
:param json_fp: File pointer to ISA JSON input
:param path: Directory for output to be written
:param config_dir: path to JSON configuration
"""
from isatools.convert import json2isatab, isatab2sra
json2isatab.convert(json_fp=json_fp, path=path, config_dir=config_dir)
isatab2sra.create_sra(path, path)
for f in glob.iglob(os.path.join(path, '*.txt')): # remove generated isatab files
os.remove(f)


def convert2(json_fp, path, config_dir=None, sra_settings=None, datafilehashes=None, validate_first=True):
def convert(json_fp, path, config_dir=None, sra_settings=None, datafilehashes=None, validate_first=True):
""" (New) Converter for ISA JSON to SRA.
:param json_fp: File pointer to ISA JSON input
:param path: Directory for output to be written
Expand Down Expand Up @@ -53,5 +37,5 @@ def convert2(json_fp, path, config_dir=None, sra_settings=None, datafilehashes=N
If files in filehashes dict don't map 1:1 to files found in ISA JSON content, raise Exception
json2sra.convert2(json_fp=open('/Users/dj/PycharmProjects/isa-api/copo.json'), path='/Users/dj/PycharmProjects/isa-api/tmp', sra_settings=)
json2sra.convert(json_fp=open('/Users/dj/PycharmProjects/isa-api/copo.json'), path='/Users/dj/PycharmProjects/isa-api/tmp', sra_settings=)
"""
3 changes: 2 additions & 1 deletion isatools/convert/mw2isa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ def mw2isa_convert(**kwargs):
each_record.insert(1, taxonid)

# Building the Investigation Object and its elements:
investigation = Investigation()
investigation = Investigation(identifier=studyid)
investigation.comments.append(Comment(name="Primary Database", value="NIH Metabolomics Workbench"))
investigation.comments.append(Comment(name="conversion date", value=str(date.today())))
investigation.comments.append(Comment(name="conversion software", value="MW2ISA version 1.0"))
Expand Down Expand Up @@ -1923,6 +1923,7 @@ def mw2isa_convert(**kwargs):
# ATTEMPTING TO WRITE INVESTIGATION FILE:
try:
print("writing 'investigation information' to file...")
print(isatab.dumps(investigation))
isatab.dump(investigation, outputpath)
except IOError:
print("Error: in main() method can\'t open file or write data")
Expand Down
16 changes: 16 additions & 0 deletions isatools/convert/sampletab2isatab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from isatools import sampletab, isatab
import logging

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def convert(source_sampletab_fp, target_dir):
""" Converter for ISA-JSON to SampleTab.
:param source_sampletab_fp: File descriptor of input SampleTab file
:param target_dir: Path to write out ISA-Tab files to
"""
ISA = sampletab.load(source_sampletab_fp)
isatab.dump(ISA, target_dir)


16 changes: 16 additions & 0 deletions isatools/convert/sampletab2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from isatools import sampletab
from isatools.isajson import ISAJSONEncoder
import json
import logging

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)


def convert(source_sampletab_fp, target_json_fp):
""" Converter for ISA-JSON to SampleTab.
:param source_sampletab_fp: File descriptor of input SampleTab file
:param target_json_fp: File descriptor to write output ISA JSON (must be writeable)
"""
ISA = sampletab.load(source_sampletab_fp)
json.dump(ISA, fp=target_json_fp, cls=ISAJSONEncoder)
4 changes: 2 additions & 2 deletions isatools/examples/validateISAtab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Inspired by validateSBML.py example from libSBML Python API

from isatools.isatab import validate2
from isatools.isatab import validate
import sys
import os

Expand All @@ -29,7 +29,7 @@ def main(args):
numfiles += 1
else:
with open(args[i]) as fp:
report = validate2(fp)
report = validate(fp)
numerrors = len(report['errors'])
numwarnings = len(report['warnings'])
if numerrors > 0:
Expand Down
Loading

0 comments on commit 6fb3d89

Please sign in to comment.