annotate process_input.py @ 3:7d751b5943b0 draft default tip

Uploaded
author ieguinoa
date Tue, 22 Feb 2022 11:03:34 +0000
parents e25357392813
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
1 import gzip
3
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
2 import json
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
3 import os
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
4 import sys
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
5 import shutil
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
6 import yaml
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
7
3
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
8 def get_section_string(f, start_line, end_line, return_string=False):
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
9 # consume starting lines
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
10 start_string = iter(f.readline, start_line)
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
11 start_string = ''.join(line for line in start_string)
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
12 # read YAML lines
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
13 yaml_string = iter(f.readline, end_line)
3
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
14 if return_string:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
15 return ''.join(x for x in yaml_string)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
16 else:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
17 return [x for x in yaml_string]
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
18
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
19 def fill_from_yaml_data(yaml_only_dict, studies_samples_dict):
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
20 # fill experiment information (platform) ****
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
21 for index,exp in yaml_only_dict['ENA_experiment'].items():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
22 study_alias = exp['study_alias']
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
23 sample_alias = exp['sample_alias']
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
24 if study_alias in studies_samples_dict.keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
25 if sample_alias in studies_samples_dict[study_alias].keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
26 studies_samples_dict[study_alias][sample_alias]['experiments'].append({'platform': exp['platform']})
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
27 else:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
28 studies_samples_dict[study_alias][sample_alias] = {'experiments': [{'platform': exp['platform']}]}
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
29 else:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
30 studies_samples_dict[study_alias] = {sample_alias: {'experiments':[{'platform': exp['platform']}]}}
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
31
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
32
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
33 def load_receipt_data(input_file_path):
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
34 # should do some health check of the input file?
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
35 # load yaml section
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
36 loaded_data = {}
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
37 yaml_delimiter = 'YAML -------------\n'
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
38 with open(input_file_path) as input_file:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
39 yaml_only_section = yaml.safe_load(get_section_string(input_file, start_line=yaml_delimiter, end_line=yaml_delimiter, return_string=True))
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
40 fill_from_yaml_data(yaml_only_section, loaded_data)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
41 # read study accessions
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
42 study_delimiter = 'Study accession details:\n'
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
43 end_line = '\n'
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
44 with open(input_file_path) as input_file:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
45 studies_accession_lines = get_section_string(input_file, start_line=study_delimiter, end_line=end_line)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
46 # loaded_data['studies'] = {}
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
47 for study_line in studies_accession_lines:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
48 if study_line != '\n':
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
49 alias, accession, *_ = study_line.split('\t')
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
50 try:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
51 loaded_data[alias]['accession'] = accession
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
52 except KeyError:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
53 print(f"Experiment {exp} has unknown study or sample")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
54 # loaded_data['studies'][alias]['accession'] = accession
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
55 samples_delimiter = 'Sample accession details:\n'
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
56 with open(input_file_path) as input_file:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
57 samples_accession_lines = get_section_string(input_file, start_line=samples_delimiter, end_line=end_line)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
58 ## need to iterate over all studies, because here I don't know which study is the sample from.
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
59 # loaded_data['samples'] = {}
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
60 for sample_line in samples_accession_lines:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
61 if sample_line != '\n':
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
62 alias, accession, *_ = sample_line.split('\t')
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
63 for study in loaded_data.keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
64 if alias in loaded_data[study].keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
65 loaded_data[study][alias]['accession'] = accession
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
66 break
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
67 return loaded_data
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
68
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
69
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
70 """
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
71 Takes as input:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
72 1. A receipt obtained from ENA submission tool:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
73 a txt file that contains sections describing submission details.
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
74 2. A json file with the list of fasta that the user loaded
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
75 3. Path to write generated manifests
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
76 4. Manifest template path: the manifest with the global values set
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
77 (e.g COVERAGE, MINGAPLENGHT..)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
78 """
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
79
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
80
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
81
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
82 def main():
3
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
83 input_file_path = sys.argv[1]
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
84 fasta_names_list_path = sys.argv[2]
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
85 out_manifest_base = sys.argv[3]
3
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
86 manifest_template = sys.argv[4]
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
87 # load submitted data from receipt file
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
88 data_dict = load_receipt_data(input_file_path)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
89 # iterate over the list of fasta files
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
90 with open(fasta_names_list_path, 'r') as fasta_files_json_file:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
91 fasta_files_list = json.load(fasta_files_json_file)
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
92 with open('submit_list.tab', 'w') as written_manifests_out:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
93 for fasta_file in fasta_files_list:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
94 if fasta_file.endswith('.fasta.gz'):
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
95 sample_alias = fasta_file[:-9]
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
96 else:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
97 sample_alias = fasta_file[:-6]
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
98 print(f'Processing {sample_alias}')
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
99 found_metadata = False
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
100 for study_alias in data_dict.keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
101 if sample_alias in data_dict[study_alias].keys():
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
102 sample_accession = data_dict[study_alias][sample_alias]['accession']
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
103 study_accession = data_dict[study_alias]['accession']
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
104 ### TODO get a string that concatenates plaform information from multiple exp
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
105 platform = data_dict[study_alias][sample_alias]['experiments'][0]['platform']
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
106 manifest_path = os.path.join(out_manifest_base, sample_alias + '.manifest.txt')
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
107 with open(manifest_path, "w") as output_handle:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
108 # first dump the contents of manifest template
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
109 # containing the global vars
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
110 with open(manifest_template) as m_template:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
111 output_handle.write(m_template.read())
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
112 output_handle.write("ASSEMBLYNAME\tconsensus_" + sample_alias + "\n")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
113 output_handle.write("PLATFORM\t" + platform + "\n")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
114 output_handle.write("STUDY\t" + study_accession + "\n")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
115 output_handle.write("SAMPLE\t" + sample_accession + "\n")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
116 # files should be available in the corresponding dir and named:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
117 # sample_alias.fasta.gz
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
118 output_handle.write("FASTA\t" + sample_alias + '.fasta.gz' + "\n")
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
119 found_metadata = True
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
120 written_manifests_out.write(manifest_path + '\n')
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
121 break
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
122 if not found_metadata:
7d751b5943b0 Uploaded
ieguinoa
parents: 0
diff changeset
123 print(f'No metadata found for sample {sample_alias}')
0
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
124
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
125
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
126 if __name__ == '__main__':
e25357392813 Uploaded
ieguinoa
parents:
diff changeset
127 main()