io.jl 5.15 KB
Newer Older
St. Elmo's avatar
St. Elmo committed
1
"""
St. Elmo's avatar
St. Elmo committed
2
    read_model(file_location::String))
St. Elmo's avatar
St. Elmo committed
3

4
Reads a model at `file_location` and returns a constraint based `model::CobraModel`.
St. Elmo's avatar
St. Elmo committed
5
Currently supported formats include SBML (.xml), Matlab (.mat) and JSON (.json) models.
St. Elmo's avatar
St. Elmo committed
6
The model format is inferred from the `file_location` extension.
St. Elmo's avatar
St. Elmo committed
7
8
9

Note, some meta-information may be lost when importing a model. Importantly, only information regarding the
reactions, metabolites and genes are imported. Currently reading JSON models captures the most meta-information
10
regarding reactions, metabolites and genes (e.g. the notes and annotation fields).
St. Elmo's avatar
St. Elmo committed
11

St. Elmo's avatar
St. Elmo committed
12
When importing Matlab models some annotation and notes may not be imported because of non-standard field names used by some models.
13
Gene reaction rules are successfully imported only if they adhere to this format: `"(YIL010W and YLR043C) or (YIL010W and YGR209C)"`,
St. Elmo's avatar
St. Elmo committed
14
where `or` can be interchanged with `OR, |, ||` and `and` can be interchanged with `AND, &, &&`.
15
Other gene reaction rules formats are not supported yet, but file an issue if your format is standard and needs to be included.
St. Elmo's avatar
St. Elmo committed
16

17
However, in all cases the basic information needed to perform constraint based analysis should be imported successfully,
St. Elmo's avatar
St. Elmo committed
18
e.g. stoichiometrix matrix, constraints etc..
St. Elmo's avatar
St. Elmo committed
19
Advanced tools that require, e.g. metabolite formulas, gene reaction rules, and KEGG or BIGG IDs, will not function if these are improperly imported.
20
Always inspect the imported model before running analysis (garbage in -> garbage out).
St. Elmo's avatar
St. Elmo committed
21
"""
St. Elmo's avatar
St. Elmo committed
22
function read_model(file_location::String)
St. Elmo's avatar
St. Elmo committed
23
    if endswith(file_location, ".json")
St. Elmo's avatar
St. Elmo committed
24
        try
25
            model = read_json_model(file_location)
St. Elmo's avatar
St. Elmo committed
26
        catch err
St. Elmo's avatar
St. Elmo committed
27
            @error "JSON model reading error.\n$err"
28
            model = CobraModel()
St. Elmo's avatar
St. Elmo committed
29
        end
St. Elmo's avatar
St. Elmo committed
30
    elseif endswith(file_location, ".xml")
St. Elmo's avatar
St. Elmo committed
31
        try
32
            model = reconstruct_model_sbml(file_location)
St. Elmo's avatar
St. Elmo committed
33
        catch err
St. Elmo's avatar
St. Elmo committed
34
            @error "SBML model reading error.\n$err"
35
            model = CobraModel()
St. Elmo's avatar
St. Elmo committed
36
        end
St. Elmo's avatar
St. Elmo committed
37
    elseif endswith(file_location, ".mat")
St. Elmo's avatar
St. Elmo committed
38
        try
39
            model = read_matlab_model(file_location)
St. Elmo's avatar
St. Elmo committed
40
        catch err
St. Elmo's avatar
St. Elmo committed
41
            @error "Matlab model reading error.\n$err"
42
            model = CobraModel()
St. Elmo's avatar
St. Elmo committed
43
        end
St. Elmo's avatar
St. Elmo committed
44
45
    else
        @error "Model format not supported. The format is inferred from the file extension. Supported formats: *.mat, *.xml, *.json."
46
        model = CobraModel()
St. Elmo's avatar
update    
St. Elmo committed
47
48
49
50
    end
    return model
end

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
    save_model(model::CobraModel, file_location::String)

Save model at `file_location`. Infers format from `file_location` extension.
Supported formats include SBML (.xml), Matlab COBRA models (.mat) and JSON COBRA models (.json).

Note, only the fields contained in model are saved. Make sure that information isn't
lost between reading a model and writing a model (e.g. check gene reaction rules, notes and annotations).
"""
function save_model(model::CobraModel, file_location::String)
    if endswith(file_location, ".json")
        write_json_model(model, file_location)
    elseif endswith(file_location, ".xml")
        @warn "Not implemented!"
    elseif endswith(file_location, ".mat")
        write_matlab_model(model, file_location)
    else
        @error "Model format not supported. The format is inferred from the file extension. Supported formats: *.mat, *.xml, *.json."
    end
end


73
74
75
"""
    parsegrr(string_rule, genes::Array{Gene, 1})

76
Parse a gene reaction rule string `string_rule` into a nested `gene` array `Array{Array{Gene, 1}, 1}`.
St. Elmo's avatar
St. Elmo committed
77
78

Format: (YIL010W and YLR043C) or (YIL010W and YGR209C) where `or` can also be `OR, |, ||` and where `and` can also be `AND, &, &&`.
79
"""
St. Elmo's avatar
St. Elmo committed
80
function parse_grr(s::String, genes::Array{Gene,1})
81
    if s == "" || isnothing(s)
St. Elmo's avatar
St. Elmo committed
82
        return Array{Array{Gene,1},1}()
83
84
    end
    # first get the gene id list in string format
St. Elmo's avatar
St. Elmo committed
85
    gene_string_rules = Array{Array{String,1},1}()
86
87
88
89
90
91
    or_genes = split(s, r"\s?(or|OR|(\|\|)|\|)\s?") # separate or terms
    for or_gene in or_genes
        and_genes = split(replace(or_gene, r"\(|\)" => ""), r"\s?(and|AND|(\&\&)|\&)\s?")
        push!(gene_string_rules, and_genes)
    end
    # now map these gene string ids to genes
St. Elmo's avatar
St. Elmo committed
92
    grr = Array{Array{Gene,1},1}()
93
    for gsr in gene_string_rules
St. Elmo's avatar
St. Elmo committed
94
        gene_list = Array{Gene,1}()
95
96
97
98
99
100
101
102
103
104
105
106
107
        for g in gsr
            gene = findfirst(genes, g)
            isnothing(gene) && (@warn "Gene not found..."; continue)
            push!(gene_list, gene)
        end
        push!(grr, gene_list)
    end
    return grr
end

"""
    unparse_grr(grr::Array{Array{Gene, 1}, 1}

St. Elmo's avatar
St. Elmo committed
108
Converts a nested `gene` array, `grr`, back into a grr string.
109
"""
St. Elmo's avatar
St. Elmo committed
110
function unparse_grr(grr::Array{Array{Gene,1},1})
111
112
    grr_strings = String[]
    for gr in grr
St. Elmo's avatar
St. Elmo committed
113
        push!(grr_strings, "(" * join([g.id for g in gr], " and ") * ")")
114
115
116
117
118
    end
    grr_string = join(grr_strings, " or ")
    return grr_string
end

St. Elmo's avatar
St. Elmo committed
119
120
121
122
"""
    reconstruct_model_sbml(file_location::String)
"""
function reconstruct_model_sbml(file_location::String)
123
    # m = read_sbml(file_location)
124
125
126
127
    # m is now a Model structure with:
    # m.reactions
    # m.species
    # m.compartments
St. Elmo's avatar
St. Elmo committed
128
    # return Model()
129
    return CobraModel()
St. Elmo's avatar
St. Elmo committed
130
end
St. Elmo's avatar
St. Elmo committed
131

St. Elmo's avatar
St. Elmo committed
132

St. Elmo's avatar
St. Elmo committed
133
"""
134
    save_sbml_model(model::CobraModel, file_location::String)
St. Elmo's avatar
St. Elmo committed
135
"""
136
function save_sbml_model(model::CobraModel, file_location::String)
St. Elmo's avatar
St. Elmo committed
137
    # To do...
St. Elmo's avatar
St. Elmo committed
138
end