Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates #56

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 57 additions & 10 deletions Scripts/Validation/Validate_Templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@

def get_id_set(tsv_path):
with open(tsv_path) as infile:
return set([line.split('\t')[0] for line in infile])
ids = set()
obs = {}
for line in DictReader(infile, dialect='excel-tab'):
if 'is_obsolete' in line and int(line['is_obsolete']):
obs[line['id']] = line.get('linked_reaction', "").split(';')[0]
continue
if 'status' in line and ("MI" in line['status']):
continue
ids.add(line['id'])
return ids, obs


def get_id_dict(tsv_path):
with open(tsv_path) as infile:
id_dict = {}
for line in DictReader(infile, dialect='excel-tab'):
id_dict[line['id']] = line
return id_dict


def validate_biomass_compounds(path, comp_set):
Expand All @@ -32,6 +49,24 @@ def validate_reaction_list(path, rxn_set, complex_set):
template_complexes.update(line['complexes'].strip().split("|"))
return template_rxns - rxn_set, template_complexes - complex_set


def remove_ids(path, ids):
ids = set(ids)
txt = open(path).readlines()
with open(path, 'w') as outfile:
outfile.writelines([x for x in txt if (x.split('\t')[0] not in ids)])

def update_obsolete(path, obs_rxns):
txt = open(path).readlines()
with open(path, 'w') as outfile:
for line in txt:
rid = line.split('\t')[0]
if rid in obs_rxns and obs_rxns[rid]:
outfile.write(line.replace(rid, obs_rxns[rid]))
else:
outfile.write(line)


if __name__ == '__main__':
script_dir = os.path.dirname(__file__)
parser = argparse.ArgumentParser(
Expand All @@ -48,10 +83,16 @@ def validate_reaction_list(path, rxn_set, complex_set):
parser.add_argument('-t', dest='template_dir',
default=script_dir+'/../../Templates',
help='Path to the templates directory')
parser.add_argument('-d', dest='delete',
default=False, action='store_true',
help='Delete reactions which are invalid')
parser.add_argument('-u', dest='update',
default=False, action='store_true',
help='Update reactions which are invalid')
args = parser.parse_args()
comp_ids = get_id_set(args.comp_tsv)
rxn_ids = get_id_set(args.rxn_tsv)
complex_ids = get_id_set(args.complex_tsv) | {'universal', 'null'}
comp_ids, obs_comps = get_id_set(args.comp_tsv)
rxn_ids, obs_rxns = get_id_set(args.rxn_tsv)
complex_ids = get_id_set(args.complex_tsv)[0] | {'universal', 'null'}
exit_code = 0
for template in os.listdir(args.template_dir):
if not os.path.isdir(os.path.join(args.template_dir, template)):
Expand All @@ -65,16 +106,22 @@ def validate_reaction_list(path, rxn_set, complex_set):
complex_ids)

if undef_comps:
print("ERROR-Undefined Compounds: " + ", ".join(undef_comps),
file=sys.stderr)
print("ERROR-%s Invalid Compounds: %s"
% (len(undef_comps), ", ".join(undef_comps)))
exit_code = 1
if undef_rxns:
print("ERROR-Undefined Reactions: " + ", ".join(undef_rxns),
file=sys.stderr)
print("ERROR-%s Invalid Reactions: %s"
% (len(undef_rxns), ", ".join(undef_rxns)))
exit_code = 1
if undef_complex:
print("ERROR-Undefined Complexes: " + ", ".join(undef_complex),
file=sys.stderr)
print("ERROR-%s Invalid Complexes: %s"
% (len(undef_complex), ", ".join(undef_complex)))
exit_code = 1
if args.delete and undef_comps:
remove_ids('%s/%s/BiomassCompounds.tsv' % (args.template_dir, template), undef_comps)
if args.delete and undef_rxns:
remove_ids('%s/%s/Reactions.tsv' % (args.template_dir, template), undef_rxns)
if args.update and undef_rxns:
update_obsolete('%s/%s/Reactions.tsv' % (args.template_dir, template), obs_rxns)

exit(exit_code)
Loading