Tripal Plant PopGen Submit
zenodo.inc File Reference

Go to the source code of this file.

Functions

 tpps_doi_create ()
 
 tpps_doi_format_name ($name)
 
 tpps_doi_load ($id)
 
 tpps_doi_metadata ($id, $metadata)
 
 tpps_doi_publish ($id)
 
 tpps_doi_upload_files ($id, array &$state)
 
 tpps_generate_doi ($accession)
 

Detailed Description

Defines functions to create and populate Zenodo DOIs.

Definition in file zenodo.inc.

Function Documentation

◆ tpps_doi_create()

tpps_doi_create ( )

Creates a blank Zenodo DOI Deposition. Returns the Zenodo id number.

Returns
int The Zenodo Deposition id number.

Definition at line 81 of file zenodo.inc.

81  {
82  $token = variable_get('tpps_zenodo_api_key', NULL);
83  $prefix = variable_get('tpps_zenodo_prefix', '');
84  $ch = curl_init("https://{$prefix}zenodo.org/api/deposit/depositions?" . http_build_query(array('access_token' => $token)));
85  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
86  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
87  curl_setopt($ch, CURLOPT_POST, 1);
88  curl_setopt($ch, CURLOPT_POSTFIELDS, '{}');
89 
90  $out = json_decode(curl_exec($ch));
91  curl_close($ch);
92  return $out->id;
93 }

◆ tpps_doi_format_name()

tpps_doi_format_name (   $name)

Formats an author name for Zenodo metadata fields.

Parameters
string$nameThe name to format.
Returns
string The formatted name.

Definition at line 267 of file zenodo.inc.

267  {
268  $parts = explode(' ', $name);
269  $first = $parts[0];
270  $last = implode(' ', array_slice($parts, 1));
271  return "$last, $first";
272 }

◆ tpps_doi_load()

tpps_doi_load (   $id)

Loads and returns the Deposition object for an existing Zenodo Deposition.

Parameters
int$idThe identifier for the Deposition.
Returns
object The loaded Deposition object.

Definition at line 247 of file zenodo.inc.

247  {
248  $token = variable_get('tpps_zenodo_api_key', NULL);
249  $prefix = variable_get('tpps_zenodo_prefix', '');
250  $ch = curl_init("https://{$prefix}zenodo.org/api/deposit/depositions/$id?" . http_build_query(array('access_token' => $token)));
251  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
252 
253  $out = json_decode(curl_exec($ch));
254  curl_close($ch);
255  return $out;
256 }

◆ tpps_doi_metadata()

tpps_doi_metadata (   $id,
  $metadata 
)

Adds metadata to a Zenodo DOI Deposition. Returns the Deposition object.

Parameters
int$idThe Deposition identifier.
object$metadataThe metadata object describing the deposition.
Returns
object The updated Deposition object.

Definition at line 106 of file zenodo.inc.

106  {
107 
108  $data = new stdClass();
109  $data->metadata = $metadata;
110  $data_json = json_encode($data);
111 
112  $token = variable_get('tpps_zenodo_api_key', NULL);
113  $prefix = variable_get('tpps_zenodo_prefix', '');
114  $ch = curl_init("https://{$prefix}zenodo.org/api/deposit/depositions/$id?" . http_build_query(array('access_token' => $token)));
115  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
116  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
117  'Content-Type: application/json',
118  'Content-Length: ' . strlen($data_json),
119  ));
120  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
121  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
122 
123  $out = json_decode(curl_exec($ch));
124  curl_close($ch);
125  return $out;
126 }

◆ tpps_doi_publish()

tpps_doi_publish (   $id)

Publishes a completed Zenodo Deposition. Returns the Deposition object.

Parameters
int$idThe identifier for the Deposition.
Returns
object The updated Deposition object.

Definition at line 225 of file zenodo.inc.

225  {
226  $token = variable_get('tpps_zenodo_api_key', NULL);
227  $prefix = variable_get('tpps_zenodo_prefix', '');
228  $ch = curl_init("https://{$prefix}zenodo.org/api/deposit/depositions/$id/actions/publish?" . http_build_query(array('access_token' => $token)));
229  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
230  curl_setopt($ch, CURLOPT_POST, 1);
231  curl_setopt($ch, CURLOPT_POSTFIELDS, '');
232 
233  $out = json_decode(curl_exec($ch));
234  curl_close($ch);
235  return $out;
236 }

◆ tpps_doi_upload_files()

tpps_doi_upload_files (   $id,
array &  $state 
)

Adds files to a Zenodo Deposition from the state of a TPPS Submission form.

Parameters
int$idThe identifier for the Deposition.
array$stateThe form_state of the TPPS Submission form.

Definition at line 136 of file zenodo.inc.

136  {
137  $token = variable_get('tpps_zenodo_api_key', NULL);
138  $prefix = variable_get('tpps_zenodo_prefix', '');
139  $files = array();
140 
141  for ($i = 1; $i <= $state['stats']['species_count']; $i++) {
142  $files[] = $state['saved_values'][TPPS_PAGE_3]['tree-accession']["species-$i"]['file'];
143  if (empty($state['saved_values'][TPPS_PAGE_3]['tree-accession']['check'])) {
144  break;
145  }
146  }
147 
148  for ($i = 1; $i <= $state['saved_values'][TPPS_PAGE_1]['organism']['number']; $i++) {
149  if (isset($state['saved_values'][TPPS_PAGE_4]["organism-$i"]['phenotype'])) {
150  $phenotype = &$state['saved_values'][TPPS_PAGE_4]["organism-$i"]['phenotype'];
151  if (!empty($phenotype['normal-check'])) {
152  $files[] = $phenotype['file'];
153  if ($phenotype['check']) {
154  $files[] = $phenotype['metadata'];
155  }
156  }
157 
158  if (!empty($phenotype['iso-check'])) {
159  $files[] = $phenotype['iso'];
160  }
161  }
162 
163  if (isset($state['saved_values'][TPPS_PAGE_4]["organism-$i"]['genotype'])) {
164  $genotype = &$state['saved_values'][TPPS_PAGE_4]["organism-$i"]['genotype'];
165  if ($genotype['ref-genome'] == 'manual' or $genotype['ref-genome'] == 'manual2' or $genotype['ref-genome'] == 'url') {
166  if ($genotype['tripal_fasta']['file_upload']) {
167  $files[] = $genotype['tripal_fasta']['file_upload'];
168  }
169 
170  if ($genotype['tripal_fasta']['file_upload_existing']) {
171  $files[] = $genotype['tripal_fasta']['file_upload_existing'];
172  }
173  }
174 
175  if (!empty($genotype['files']['file-type']['SNPs Genotype Assay'])) {
176  $files[] = $genotype['files']['snps-assay'];
177  }
178 
179  if (!empty($genotype['files']['file-type']['Assay Design']) and $genotype['marker-type']['SNPs']) {
180  $files[] = $genotype['files']['assay-design'];
181  }
182 
183  if (!empty($genotype['files']['file-type']['SSRs/cpSSRs Genotype Spreadsheet'])) {
184  $files[] = $genotype['files']['ssrs'];
185  }
186 
187  if (!empty($genotype['files']['file-type']['Other Marker Genotype Spreadsheet'])) {
188  $files[] = $genotype['files']['other'];
189  }
190 
191  if (!empty($genotype['files']['file-type']['VCF'])) {
192  $files[] = $genotype['files']['vcf'];
193  }
194  }
195  }
196 
197  foreach ($files as $file) {
198  $file = file_load($file);
199  if ($file->filesize < 20000000000) {
200  $path = drupal_realpath($file->uri);
201  $data = array(
202  'file' => curl_file_create($path, $file->filemime, $file->filename),
203  );
204 
205  $ch = curl_init("https://{$prefix}zenodo.org/api/deposit/depositions/$id/files?" . http_build_query(array('access_token' => $token)));
206  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
207  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
208  curl_setopt($ch, CURLOPT_POST, 1);
209  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
210  curl_exec($ch);
211  curl_close($ch);
212  }
213  }
214 }
const TPPS_PAGE_1
Definition: tpps.module:12
const TPPS_PAGE_4
Definition: tpps.module:15
const TPPS_PAGE_3
Definition: tpps.module:14

◆ tpps_generate_doi()

tpps_generate_doi (   $accession)

Creates a Zenodo DOI and loads metadata from a TPPS Submission accession.

Parameters
string$accessionThe accession number of the TPPS Submission.

string $base_url The base URL of the site.

Returns
object The Zenodo DOI Deposition object.

Definition at line 20 of file zenodo.inc.

20  {
21  global $base_url;
22  $state = tpps_load_submission($accession);
23 
24  $id = tpps_doi_create();
25  $creators = array(
26  (object) array(
27  'name' => tpps_doi_format_name($state['saved_values'][TPPS_PAGE_1]['primaryAuthor']),
28  'affiliation' => $state['saved_values'][TPPS_PAGE_1]['organization'],
29  ),
30  );
31  for ($i = 1; $i <= $state['saved_values'][TPPS_PAGE_1]['publication']['secondaryAuthors']['number']; $i++) {
32  $creators[] = (object) array(
33  'name' => tpps_doi_format_name($state['saved_values'][TPPS_PAGE_1]['publication']['secondaryAuthors'][$i]),
34  );
35  }
36 
37  $keywords = array();
38  for ($i = 1; $i <= $state['saved_values'][TPPS_PAGE_1]['organism']['number']; $i++) {
39  $keywords[] = $state['saved_values'][TPPS_PAGE_1]['organism'][$i]['name'];
40  }
41 
42  $study_type_options = array(
43  1 => 'Natural Population (Landscape)',
44  2 => 'Growth Chamber',
45  3 => 'Greenhouse',
46  4 => 'Experimental/Common Garden',
47  5 => 'Plantation',
48  );
49 
50  $keywords[] = $study_type_options[$state['saved_values'][TPPS_PAGE_2]['study_type']];
51 
52  $metadata = (object) array(
53  'title' => $state['saved_values'][TPPS_PAGE_1]['publication']['title'],
54  'upload_type' => 'dataset',
55  'description' => $state['saved_values'][TPPS_PAGE_1]['publication']['abstract'],
56  'creators' => $creators,
57  // 'access_right' => 'embargoed', //TODO: collect embargo date from user.
58  'keywords' => $keywords,
59  'notes' => $state['saved_values']['summarypage']['comments'],
60  'related_identifiers' => array(
61  (object) array(
62  'relation' => 'isAlternateIdentifier',
63  'identifier' => "$base_url/tpps/details/$accession",
64  ),
65  ),
66  );
67 
68  tpps_doi_upload_files($id, $state);
69 
70  $upload = tpps_doi_metadata($id, $metadata);
71 
72  return $upload;
73 }
tpps_doi_format_name($name)
Definition: zenodo.inc:267
const TPPS_PAGE_1
Definition: tpps.module:12
tpps_doi_upload_files($id, array &$state)
Definition: zenodo.inc:136
tpps_load_submission($accession, $state=TRUE)
Definition: submissions.inc:27
tpps_doi_create()
Definition: zenodo.inc:81
tpps_doi_metadata($id, $metadata)
Definition: zenodo.inc:106
const TPPS_PAGE_2
Definition: tpps.module:13