@@ -31,6 +31,14 @@ def __init__(self, endpoint, auth_provider):
3131 self ._auth_provider = auth_provider
3232 self ._endpoint = endpoint
3333
34+ def __export_file (self , filename , output ):
35+ """Writes an API response to a file.
36+ """
37+ outfile = open (filename , "w" )
38+ outfile .write (output )
39+ outfile .close
40+ print ("\n Output written to file: " + filename )
41+
3442 def query (self , query_txt , variables = None , max_tries = 1 ):
3543 """Execute a GraphQL query against a data commons.
3644
@@ -69,49 +77,63 @@ def query(self, query_txt, variables=None, max_tries=1):
6977
7078 return data
7179
72- def export_node (self , program , project , uuid ):
80+ def export_record (self , program , project , uuid , fileformat , filename = None ):
7381 """Export a single record into json.
7482
7583 Args:
7684 program (str): The program the record is under.
7785 project (str): The project the record is under.
7886 uuid (str): The UUID of the record to export.
87+ fileformat (str): Export data as either 'json' or 'tsv'
88+ filename (str): Name of the file to export to; if no filename is provided, prints data to screen
7989
8090 Examples:
8191 This exports a single record from the sandbox commons.
8292
83- >>> Gen3Submission.export_node ("DCF", "CCLE", "d70b41b9-6f90-4714-8420-e043ab8b77b9")
93+ >>> Gen3Submission.export_record ("DCF", "CCLE", "d70b41b9-6f90-4714-8420-e043ab8b77b9", "json", filename="DCF-CCLE_one_record.json ")
8494
8595 """
86- api_url = "{}/api/v0/submission/{}/{}/export?ids={}&format=json" .format (
87- self ._endpoint , program , project , uuid
96+ assert fileformat in ["json" ,"tsv" ],"File format must be either 'json' or 'tsv'"
97+ api_url = "{}/api/v0/submission/{}/{}/export?ids={}&format={}" .format (
98+ self ._endpoint , program , project , uuid , fileformat
8899 )
89100 output = requests .get (api_url , auth = self ._auth_provider ).text
90- data = json .loads (output )
91- return data
101+ if filename is None :
102+ if fileformat == 'json' : output = json .loads (output )
103+ return output
104+ else :
105+ self .__export_file (filename , output )
106+ return output
92107
93- def export_node_all_type (self , program , project , node_type ):
94- """Export all records in a single node.
108+ def export_node (self , program , project , node_type , fileformat , filename = None ):
109+ """Export all records in a single node type of a project .
95110
96111 Args:
97112 program (str): The program to which records belong.
98113 project (str): The project to which records belong.
99114 node_type (str): The name of the node to export.
115+ fileformat (str): Export data as either 'json' or 'tsv'
116+ filename (str): Name of the file to export to; if no filename is provided, prints data to screen
100117
101118 Examples:
102119 This exports all records in the "sample" node from the CCLE project in the sandbox commons.
103120
104- >>> Gen3Submission.export_node_all_type ("DCF", "CCLE", "sample")
121+ >>> Gen3Submission.export_node ("DCF", "CCLE", "sample", "tsv", filename="DCF-CCLE_sample_node.tsv ")
105122
106123 """
107- api_url = "{}/api/v0/submission/{}/{}/export/?node_label={}&format=json" .format (
108- self ._endpoint , program , project , node_type
124+ assert fileformat in ["json" ,"tsv" ],"File format must be either 'json' or 'tsv'"
125+ api_url = "{}/api/v0/submission/{}/{}/export/?node_label={}&format={}" .format (
126+ self ._endpoint , program , project , node_type , fileformat
109127 )
110128 output = requests .get (api_url , auth = self ._auth_provider ).text
111- data = json .loads (output )
112- return data
129+ if filename is None :
130+ if fileformat == 'json' : output = json .loads (output )
131+ return output
132+ else :
133+ self .__export_file (filename , output )
134+ return output
113135
114- def submit_node (self , program , project , json ):
136+ def submit_record (self , program , project , json ):
115137 """Submit record(s) to a project as json.
116138
117139 Args:
@@ -122,14 +144,14 @@ def submit_node(self, program, project, json):
122144 Examples:
123145 This submits records to the CCLE project in the sandbox commons.
124146
125- >>> Gen3Submission.submit_node ("DCF", "CCLE", json)
147+ >>> Gen3Submission.submit_record ("DCF", "CCLE", json)
126148
127149 """
128150 api_url = "{}/api/v0/submission/{}/{}" .format (self ._endpoint , program , project )
129151 output = requests .put (api_url , auth = self ._auth_provider , json = json ).text
130152 return output
131153
132- def delete_node (self , program , project , uuid ):
154+ def delete_record (self , program , project , uuid ):
133155 """Delete a record from a project.
134156 Args:
135157 program (str): The program to delete from.
@@ -139,7 +161,7 @@ def delete_node(self, program, project, uuid):
139161 Examples:
140162 This deletes a record from the CCLE project in the sandbox commons.
141163
142- >>> Gen3Submission.delete_node ("DCF", "CCLE", uuid)
164+ >>> Gen3Submission.delete_record ("DCF", "CCLE", uuid)
143165 """
144166 api_url = "{}/api/v0/submission/{}/{}/entities/{}" .format (
145167 self ._endpoint , program , project , uuid
0 commit comments