From a718159507142bbdd7b38a48ecfb268ab9646a3e Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 11:24:48 +1000 Subject: [PATCH 01/10] Added code licence --- LICENSE | 1 + 1 file changed, 1 insertion(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bbab3d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +Educational Community License Version 2.0, April 2007 The Educational Community License version 2.0 ("ECL") consists of the Apache 2.0 license, modified to change the scope of the patent grant in section 3 to be specific to the needs of the education communities using this license. The original Apache 2.0 license can be found at: http://www.apache.org/licenses/LICENSE-2.0 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. Any patent license granted hereby with respect to contributions by an individual employed by an institution or organization is limited to patent claims where the individual that is the author of the Work is also the inventor of the patent claims licensed, and where the organization or institution has the right to grant such license under applicable grant and research funding agreements. No other express or implied licenses are granted. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Educational Community License to your work To apply the Educational Community License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. From 9a0e4f23f49c24172fce6f29206eacaa3e18ecaf Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 11:25:26 +1000 Subject: [PATCH 02/10] Added initial script - references licenece and top level docstring --- data_fetcher/data_fetcher.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 data_fetcher/data_fetcher.py diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py new file mode 100644 index 0000000..f70f998 --- /dev/null +++ b/data_fetcher/data_fetcher.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +# Copyright 2014 Beatson Laboratory Licensed under the +# Educational Community License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may +# obtain a copy of the License at +# +# http://www.osedu.org/licenses/ECL-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an "AS IS" +# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing +# permissions and limitations under the License. + +""" +A tool to programatically mass download data from the ENA +""" + + From 802049b0c263e27ff1d9da630a8132cb6837f6f7 Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 11:40:31 +1000 Subject: [PATCH 03/10] Added basic argument parsing --- data_fetcher/data_fetcher.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index f70f998..4831aee 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -17,4 +17,26 @@ A tool to programatically mass download data from the ENA """ +import sys, argparse + +def core(args): + """ + """ + print "I'm working" + print args + + + +if __name__ == '__main__': + try: + parser = argparse.ArgumentParser(description= + 'Bulk downloader given SRA/ENA study accession', + epilog='Written by the Beatson Lab. http://www.beatsonlab.com') + parser.add_argument("study_id", help="A valid SRA/ENA accession") + parser.set_defaults(func=core) + args = parser.parse_args() + args.func(args) + sys.exit(0) + except: + pass From 04fee9b75189b4916cf1ed0949258a3f863c9a87 Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 11:44:51 +1000 Subject: [PATCH 04/10] Mock/dummy methods have been added --- data_fetcher/data_fetcher.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index 4831aee..f8c27c5 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -20,12 +20,40 @@ import sys, argparse -def core(args): +def get_required_metadata(): + """ """ + pass + +def parse_meta_data(): """ - print "I'm working" - print args + """ + pass + + +def print_data_stats(): + """ + """ + pass + +def download_files(): + """ + """ + pass + + +def core(args): + """ + The core function (accepts argparse and calls required) + + :param args: an argparse object + """ + # Download the required metadata + # Parse the metadata + # Print some statistics + # Download the fastq + pass if __name__ == '__main__': From fd6374ee44a97a05b5cd6773cafd484d84c693a6 Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 13:19:40 +1000 Subject: [PATCH 05/10] Added function to pulldown metadata --- data_fetcher/data_fetcher.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index f8c27c5..442f893 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -17,13 +17,29 @@ A tool to programatically mass download data from the ENA """ -import sys, argparse +import sys, argparse, urllib2 -def get_required_metadata(): +def get_required_metadata(study_id): """ + Fetch a txt document containing useful data Given a valid SRA/ENA study id + + :param study_id: a valid SRA/ENA study id + :type study_id: string + + :returns: the txt formatted metadata info as a string """ - pass + BASE = ("http://www.ebi.ac.uk/ena/data/warehouse/filereport?accession=" + "CURRENT&result=read_run&fields=study_accession," + "secondary_study_accession,sample_accession," + "secondary_sample_accession,experiment_accession,run_accession" + ",scientific_name,instrument_model,library_layout,fastq_ftp" + ",fastq_galaxy,submitted_ftp,submitted_galaxy,col_tax_id," + "col_scientific_name,sra_ftp,sra_galaxy") + BASE = BASE.replace('CURRENT', study_id) + response = urllib2.urlopen(BASE) + # Turn into a list + return response.read().split('\n') def parse_meta_data(): """ @@ -49,11 +65,11 @@ def core(args): :param args: an argparse object """ - # Download the required metadata # Parse the metadata # Print some statistics # Download the fastq - pass + metadata_file = get_required_metadata(args.study_id) + print metadata_file if __name__ == '__main__': From 72a037019dce40a1f8f23132edbc6881c32b146a Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 13:51:35 +1000 Subject: [PATCH 06/10] Added parser code --- data_fetcher/data_fetcher.py | 50 ++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index 442f893..99c32f7 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -27,7 +27,7 @@ def get_required_metadata(study_id): :param study_id: a valid SRA/ENA study id :type study_id: string - :returns: the txt formatted metadata info as a string + :returns: the txt formatted metadata info as a list """ BASE = ("http://www.ebi.ac.uk/ena/data/warehouse/filereport?accession=" "CURRENT&result=read_run&fields=study_accession," @@ -41,10 +41,48 @@ def get_required_metadata(study_id): # Turn into a list return response.read().split('\n') -def parse_meta_data(): + +def parse_meta_data(metadata_list): """ + Extract out strain ID & ftp_links from a metadata list + + :param metadata: a metadata list as returned by + get_required_metadata(study_id): + :type metadata_list: list + + :returns: 2 lists. First is a Banzai renaming file, 2 is a list of FTP + files to download """ - pass + ftp_urls = [] + rename = [] + # strip off header, skip blank last line + metadata_list = metadata_list[1:-1] + for ele in metadata_list: + cur = ele.split('\t') + # Get the strain ID - might be specific to this dataset. Please check + sid = cur[6].split()[-1] + if cur[8] == 'PAIRED': + # Get the read ftp URLs + r1, r2 = cur[9].split(';') + if r1.find("_1.fastq.gz") != -1: + tmp = sid+"_1.fastq.gz,"+r1.split('/')[-1] + rename.append(tmp) + else: + print "Read naming does not follow expected pattern" + sys.exit(1) + if r2.find("_2.fastq.gz") != -1: + tmp = sid+"_2.fastq.gz,"+r2.split('/')[-1] + rename.append(tmp) + else: + print "Read naming does not follow expected pattern" + sys.exit(1) + ftp_urls.append(r1) + ftp_urls.append(r2) + else: + print "Needs paired read files" + sys.exit(1) + return rename, ftp_urls + def print_data_stats(): @@ -68,8 +106,10 @@ def core(args): # Parse the metadata # Print some statistics # Download the fastq - metadata_file = get_required_metadata(args.study_id) - print metadata_file + metadata = get_required_metadata(args.study_id) + rename, urls = parse_meta_data(metadata) + print rename + print urls if __name__ == '__main__': From 69b44ebc1c8901402018eb3361b497a1678f5009 Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 13:57:30 +1000 Subject: [PATCH 07/10] Added simple statistics method --- data_fetcher/data_fetcher.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index 99c32f7..68661a2 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -82,13 +82,16 @@ def parse_meta_data(metadata_list): print "Needs paired read files" sys.exit(1) return rename, ftp_urls - -def print_data_stats(): +def print_data_stats(urls): """ + Calculate number of read *pairs*that will be donwloaded + + :param urls: list of ftp urls """ - pass + # Assume paired + print "Will download paired reads for %i strains" % (len(urls)/2) def download_files(): @@ -103,13 +106,10 @@ def core(args): :param args: an argparse object """ - # Parse the metadata - # Print some statistics # Download the fastq metadata = get_required_metadata(args.study_id) rename, urls = parse_meta_data(metadata) - print rename - print urls + print_data_stats(urls) if __name__ == '__main__': From 0e840738e675a01f229f186e35ca8a6da2b3cb3d Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 14:13:57 +1000 Subject: [PATCH 08/10] Added basic (but not functioning) FTP support --- data_fetcher/data_fetcher.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index 68661a2..fbcb3f0 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -17,7 +17,7 @@ A tool to programatically mass download data from the ENA """ -import sys, argparse, urllib2 +import sys, argparse, urllib2, ftplib def get_required_metadata(study_id): @@ -94,10 +94,17 @@ def print_data_stats(urls): print "Will download paired reads for %i strains" % (len(urls)/2) -def download_files(): +def download_files(ftp_data): """ + Use FTP to connect and download all files in given list """ - pass + tmp = ftp_data[0].split('/') + server = tmp[0] + location = '/'.join(tmp[1:]) + ftp = ftplib.FTP(server, 'anonymous', 'm.stantoncook@gmail.com') + files = ftp.dir() + #http://www.pythonforbeginners.com/code-snippets-source-code/how-to-use-ftp-in-python + print files def core(args): @@ -110,6 +117,7 @@ def core(args): metadata = get_required_metadata(args.study_id) rename, urls = parse_meta_data(metadata) print_data_stats(urls) + download_files(urls) if __name__ == '__main__': From a00678cde1d7942b565522a067df33d2bf63e1d2 Mon Sep 17 00:00:00 2001 From: Mitchell Stanton-Cook Date: Mon, 5 May 2014 14:26:06 +1000 Subject: [PATCH 09/10] Working (?) FTP method --- data_fetcher/data_fetcher.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index fbcb3f0..c0c6f77 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -97,14 +97,20 @@ def print_data_stats(urls): def download_files(ftp_data): """ Use FTP to connect and download all files in given list + + :param ftp_data: a list of ftp server paths + :type ftp_data: list """ - tmp = ftp_data[0].split('/') - server = tmp[0] - location = '/'.join(tmp[1:]) - ftp = ftplib.FTP(server, 'anonymous', 'm.stantoncook@gmail.com') - files = ftp.dir() - #http://www.pythonforbeginners.com/code-snippets-source-code/how-to-use-ftp-in-python - print files + for url in ftp_data: + tmp = url.split('/') + server = tmp[0] + location = '/'.join(tmp[1:]) + fname = tmp[-1] + ftp = ftplib.FTP(server, 'anonymous', 'm.stantoncook@gmail.com') + print "Getting %s " % (fname) + + with open(fname, 'w') as out: + ftp.retrbinary('RETR ' + location, out.write) def core(args): From 7f5581551bad1f98c2273a1d10a2b976743fd712 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Wed, 7 May 2014 11:54:18 +1000 Subject: [PATCH 10/10] Pull down md5 and build files required for Banzai --- data_fetcher/data_fetcher.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/data_fetcher/data_fetcher.py b/data_fetcher/data_fetcher.py index c0c6f77..f8f9259 100644 --- a/data_fetcher/data_fetcher.py +++ b/data_fetcher/data_fetcher.py @@ -35,7 +35,7 @@ def get_required_metadata(study_id): "secondary_sample_accession,experiment_accession,run_accession" ",scientific_name,instrument_model,library_layout,fastq_ftp" ",fastq_galaxy,submitted_ftp,submitted_galaxy,col_tax_id," - "col_scientific_name,sra_ftp,sra_galaxy") + "col_scientific_name,sra_ftp,sra_galaxy,fastq_md5") BASE = BASE.replace('CURRENT', study_id) response = urllib2.urlopen(BASE) # Turn into a list @@ -50,11 +50,12 @@ def parse_meta_data(metadata_list): get_required_metadata(study_id): :type metadata_list: list - :returns: 2 lists. First is a Banzai renaming file, 2 is a list of FTP - files to download + :returns: 3 lists. First is a Banzai renaming file, 2nd is md5 hases and + 3rd is a list of FTP files to download """ ftp_urls = [] rename = [] + md5sum = [] # strip off header, skip blank last line metadata_list = metadata_list[1:-1] for ele in metadata_list: @@ -64,15 +65,18 @@ def parse_meta_data(metadata_list): if cur[8] == 'PAIRED': # Get the read ftp URLs r1, r2 = cur[9].split(';') + c1, c2 = cur[-1].strip().split(';') if r1.find("_1.fastq.gz") != -1: tmp = sid+"_1.fastq.gz,"+r1.split('/')[-1] rename.append(tmp) + md5sum.append(c1+"\t"+r1.split('/')[-1]) else: print "Read naming does not follow expected pattern" sys.exit(1) if r2.find("_2.fastq.gz") != -1: tmp = sid+"_2.fastq.gz,"+r2.split('/')[-1] rename.append(tmp) + md5sum.append(c2+"\t"+r2.split('/')[-1]) else: print "Read naming does not follow expected pattern" sys.exit(1) @@ -81,7 +85,7 @@ def parse_meta_data(metadata_list): else: print "Needs paired read files" sys.exit(1) - return rename, ftp_urls + return rename, md5sum, ftp_urls def print_data_stats(urls): @@ -93,6 +97,15 @@ def print_data_stats(urls): # Assume paired print "Will download paired reads for %i strains" % (len(urls)/2) +def build_files(rename, md5): + """ + TODO: Document + """ + with open("rename.dat", 'w') as f1, open ("checksums.md5", 'w') as f2: + for line in rename: + f1.write(line+"\n") + for line in md5: + f2.write(line+"\n") def download_files(ftp_data): """ @@ -121,8 +134,9 @@ def core(args): """ # Download the fastq metadata = get_required_metadata(args.study_id) - rename, urls = parse_meta_data(metadata) + rename, md5, urls = parse_meta_data(metadata) print_data_stats(urls) + build_files(rename, md5) download_files(urls)