forked from pipebio/api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_02_download_example.py
85 lines (59 loc) · 2.7 KB
/
example_02_download_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
from library.models.export_format import ExportFormat
from library.pipebio_client import PipebioClient
from library.util import Util
def get_sequence_id():
sequence_document_id = os.environ['TARGET_DOCUMENT_ID']
if sequence_document_id is None:
raise Exception("Error! Set sequence_document_id to continue.")
else:
return int(sequence_document_id)
def example_02a_download_result_as_tsv(document_id: int):
"""
Download the raw file as a TSV.
"""
client = PipebioClient()
# Display api key user details.
user = client.user
print('\nUsing api key for {}. \n'.format(user['firstName'], user['lastName']))
# Set the download name and folder.
destination_filename = "download.tsv"
destination_location = Util.get_executed_file_location()
absolute_location = os.path.join(destination_location, '..', f'Downloads/{destination_filename}')
client.sequences.download(document_id, destination=absolute_location)
return absolute_location
def example_02b_download_result_to_memory_to_do_more_work(document_id: int):
"""
Download the file into memory to do more work
"""
client = PipebioClient()
# Display api key user details.
user = client.user
print('\nUsing api key for {}. \n'.format(user['firstName'], user['lastName']))
return client.sequences.download_to_memory([document_id])
def example_02c_download_result_to_biological_format(document_id):
"""
Download the format in Genbank, Fasta, Fastq, Ab1 etc.
"""
client = PipebioClient()
# Display api key user details.
user = client.user
print('\nUsing api key for {}. \n'.format(user['firstName'], user['lastName']))
# Specify a target folder on this computer to download the file to.
destination_folder = os.path.join(Util.get_executed_file_location(), '..', f'Downloads')
return client.export(document_id, ExportFormat.GENBANK.value, destination_folder)
def example_02d_download_original_file(document_id: int, destination_filename: str = None) -> str:
"""
Download the original, un-parsed file.
"""
client = PipebioClient()
# Display api key user details.
user = client.user
print('\nUsing api key for {}. \n'.format(user['firstName'], user['lastName']))
# Set the download name and folder.
destination_filename = "download.tsv" if destination_filename is None else destination_filename
destination_location = Util.get_executed_file_location()
absolute_location = os.path.join(destination_location, f'../Downloads/{destination_filename}')
return client.entities.download_original_file(document_id, absolute_location)
if __name__ == "__main__":
example_02a_download_result_as_tsv(298154)