001/* 002 * Copyright 2020 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V.. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.isas.mztab2.model; 017 018import java.util.List; 019import java.util.Optional; 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022import java.util.stream.Collectors; 023import static uk.ac.ebi.pride.jmztab2.model.MZTabConstants.REGEX_ABUNDANCE_ASSAY_COLUMN_NAME; 024 025/** 026 * Utility class for easier access to linked information within an mztab-M data 027 * object. 028 * 029 * @author nilshoffmann 030 */ 031public class MzTabAccess { 032 033 private final MzTab mzTab; 034 035 /** 036 * Create an MzTabAccess object for the provided mzTab object. 037 * 038 * @param mzTab 039 */ 040 public MzTabAccess(MzTab mzTab) { 041 this.mzTab = mzTab; 042 } 043 044 /** 045 * Retrieve all small molecule features in order of definition for the 046 * provided small molecule summary. 047 * 048 * @param sms the small molecule summary. 049 * @return the list of small molecule features. 050 */ 051 public List<SmallMoleculeFeature> getFeatures(SmallMoleculeSummary sms) { 052 List<Integer> smfIds = sms.getSmfIdRefs(); 053 return mzTab.getSmallMoleculeFeature().stream().filter((t) -> { 054 return smfIds.contains(t.getSmfId()); 055 }).collect(Collectors.toList()); 056 } 057 058 /** 059 * Retrieve all small molecule evidences in order of definition for the 060 * provided small molecule feature. 061 * 062 * @param smf the small molecule feature. 063 * @return the list of small molecule evidences. 064 */ 065 public List<SmallMoleculeEvidence> getEvidences(SmallMoleculeFeature smf) { 066 List<Integer> smeIds = smf.getSmeIdRefs(); 067 return mzTab.getSmallMoleculeEvidence().stream().filter((t) -> { 068 return smeIds.contains(t.getSmeId()); 069 }).collect(Collectors.toList()); 070 } 071 072 /** 073 * Retrieve all small molecule evidences for the provided evidence input id. 074 * 075 * @param evidenceInputId the evidence input it. 076 * @return the list of small molecule evidences. 077 */ 078 public List<SmallMoleculeEvidence> getEvidencesByEvidenceInputId(String evidenceInputId) { 079 return mzTab.getSmallMoleculeEvidence().stream().filter((t) -> { 080 return t.getEvidenceInputId().equals(evidenceInputId); 081 }).collect(Collectors.toList()); 082 } 083 084 /** 085 * Retrieves the abundance value for the provided assay and small molecule 086 * feature object. 087 * 088 * @param assay the assay. 089 * @param smf the small molecule feature object. 090 * @return the feature abundance. May be null. 091 */ 092 public Double getAbundanceFor(Assay assay, SmallMoleculeFeature smf) { 093 return smf.getAbundanceAssay().get(assay.getId() - 1); 094 } 095 096 /** 097 * Retrieves the abundance value for the provided study variable and small 098 * molecule summary object. 099 * 100 * @param studyVariable the study variable. 101 * @param sms the small molecule summary object. 102 * @return the study variable abundance. May be null. 103 */ 104 public Double getAbundanceFor(StudyVariable studyVariable, SmallMoleculeSummary sms) { 105 return sms.getAbundanceStudyVariable().get(studyVariable.getId() - 1); 106 } 107 108 /** 109 * Retrieves the abundance variation value for the provided study variable 110 * and small molecule summary object. 111 * 112 * @param studyVariable the study variable. 113 * @param sms the small molecule summary object. 114 * @return the study variable abundance variation. May be null. 115 */ 116 public Double getAbundanceVariationFor(StudyVariable studyVariable, SmallMoleculeSummary sms) { 117 return sms.getAbundanceVariationStudyVariable().get(studyVariable.getId() - 1); 118 } 119 120 /** 121 * Retrieves the abundance value for the provided assay and small molecule 122 * summary object. 123 * 124 * @param assay the assay. 125 * @param sms the small molecule summary object. 126 * @return the assay abundance. May be null. 127 */ 128 public Double getAbundanceFor(Assay assay, SmallMoleculeSummary sms) { 129 return sms.getAbundanceAssay().get(assay.getId() - 1); 130 } 131 132 /** 133 * Tries to locate the assay referenced by id from the metadata section. 134 * 135 * @param id the assay id. 136 * @param metadata the metadata used to locate the assay. 137 * @return an optional with the assay object, or an empty optiona 138 */ 139 public Optional<Assay> getAssayFor(Integer id, Metadata metadata) { 140 return metadata.getAssay().stream().filter((t) -> { 141 return t.getId().equals(id); 142 }).findFirst(); 143 } 144 145 /** 146 * Tries to locate the study variable referenced by id from the metadata 147 * section. 148 * 149 * @param id the study variable id. 150 * @param metadata the metadata used to locate the study variable. 151 * @return an optional with the study variable object, or an empty optiona 152 */ 153 public Optional<StudyVariable> getStudyVariableFor(Integer id, Metadata metadata) { 154 return metadata.getStudyVariable().stream().filter((t) -> { 155 return t.getId().equals(id); 156 }).findFirst(); 157 } 158 159 /** 160 * Tries to locate the ms run referenced by id from the metadata section. 161 * 162 * @param id the ms run id. 163 * @param metadata the metadata used to locate the ms run. 164 * @return an optional with the ms run object, or an empty optional. 165 */ 166 public Optional<MsRun> getMsRunFor(Integer id, Metadata metadata) { 167 return metadata.getMsRun().stream().filter((t) -> { 168 return t.getId().equals(id); 169 }).findFirst(); 170 } 171 172 /** 173 * Tries to locate the database referenced by id from the metadata section. 174 * 175 * @param id the database id. 176 * @param metadata the metadata used to locate the database. 177 * @return an optional with the database object, or an empty optional. 178 */ 179 public Optional<Database> getDatabaseFor(Integer id, Metadata metadata) { 180 return metadata.getDatabase().stream().filter((t) -> { 181 return t.getId().equals(id); 182 }).findFirst(); 183 } 184 185 /** 186 * Tries to locate the assay referenced by id from the columnMapping 187 * identifier. Returns an empty optional if either the identifier was null 188 * or not an assay, or no matching assay was found. 189 * 190 * @param columnMapping the column mapping. 191 * @param metadata the metadata used to locate the assay. 192 * @return an optional with the assay object, or an empty optional. 193 */ 194 public Optional<Assay> getAssayFor(OptColumnMapping columnMapping, Metadata metadata) { 195 String identifier = columnMapping.getIdentifier(); 196 if (identifier != null) { 197 Pattern p = Pattern.compile(REGEX_ABUNDANCE_ASSAY_COLUMN_NAME); 198 Matcher m = p.matcher(identifier); 199 if (m.find()) { 200 Integer assayId = Integer.parseInt(m.group(1)); 201 return getAssayFor(assayId, metadata); 202 } 203 } 204 return Optional.empty(); 205 } 206}