Tripal Plant PopGen Submit
zenodo.inc
Go to the documentation of this file.
1 <?php
2 
20 function tpps_generate_doi($accession) {
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 }
74 
81 function tpps_doi_create() {
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 }
94 
106 function tpps_doi_metadata($id, $metadata) {
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 }
127 
136 function tpps_doi_upload_files($id, array &$state) {
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 }
215 
225 function tpps_doi_publish($id) {
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 }
237 
247 function tpps_doi_load($id) {
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 }
257 
267 function tpps_doi_format_name($name) {
268  $parts = explode(' ', $name);
269  $first = $parts[0];
270  $last = implode(' ', array_slice($parts, 1));
271  return "$last, $first";
272 }
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_publish($id)
Definition: zenodo.inc:225
const TPPS_PAGE_4
Definition: tpps.module:15
tpps_doi_load($id)
Definition: zenodo.inc:247
tpps_doi_create()
Definition: zenodo.inc:81
tpps_generate_doi($accession)
Definition: zenodo.inc:20
tpps_doi_metadata($id, $metadata)
Definition: zenodo.inc:106
const TPPS_PAGE_3
Definition: tpps.module:14
const TPPS_PAGE_2
Definition: tpps.module:13