PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pride-inspector/trunk/src/main/java/uk/ac/ebi/pride/gui/component/table/TableDataRetriever.java

http://pride-toolsuite.googlecode.com/
Java | 636 lines | 335 code | 96 blank | 205 comment | 108 complexity | d2f4a6f341956e1f8abbae26be86b60c MD5 | raw file
  1. package uk.ac.ebi.pride.gui.component.table;
  2. import uk.ac.ebi.pride.data.controller.DataAccessController;
  3. import uk.ac.ebi.pride.data.controller.DataAccessException;
  4. import uk.ac.ebi.pride.data.core.*;
  5. import uk.ac.ebi.pride.data.utils.QuantCvTermReference;
  6. import uk.ac.ebi.pride.gui.PrideInspectorCacheManager;
  7. import uk.ac.ebi.pride.gui.component.sequence.AnnotatedProtein;
  8. import uk.ac.ebi.pride.gui.component.sequence.PeptideFitState;
  9. import uk.ac.ebi.pride.gui.utils.Constants;
  10. import uk.ac.ebi.pride.mol.IsoelectricPointUtils;
  11. import uk.ac.ebi.pride.mol.MoleculeUtilities;
  12. import uk.ac.ebi.pride.tools.protein_details_fetcher.model.Protein;
  13. import uk.ac.ebi.pride.tools.utils.AccessionResolver;
  14. import uk.ac.ebi.pride.util.NumberUtilities;
  15. import java.util.*;
  16. /**
  17. * <code>TableDataRetriever </code> provides methods for retrieving row data for tables.
  18. * <p/>
  19. * User: rwang
  20. * Date: 13-Oct-2010
  21. * Time: 16:56:47
  22. */
  23. public class TableDataRetriever {
  24. /**
  25. * Get a row of data for peptide table.
  26. *
  27. * @param controller data access controller
  28. * @param identId identification id
  29. * @param peptideId peptide id
  30. * @return List<Object> row data
  31. * @throws uk.ac.ebi.pride.data.controller.DataAccessException
  32. * data access exception
  33. */
  34. public static List<Object> getPeptideTableRow(DataAccessController controller,
  35. Comparable identId,
  36. Comparable peptideId) throws DataAccessException {
  37. List<Object> content = new ArrayList<Object>();
  38. // peptide sequence with modifications
  39. List<Modification> mods = new ArrayList<Modification>(controller.getPTMs(identId, peptideId));
  40. String sequence = controller.getPeptideSequence(identId, peptideId);
  41. content.add(new Peptide(null, sequence, 0, 0, mods, null, null));
  42. // start and end position
  43. int start = controller.getPeptideSequenceStart(identId, peptideId);
  44. int end = controller.getPeptideSequenceEnd(identId, peptideId);
  45. // Original Protein Accession
  46. String protAcc = controller.getProteinAccession(identId);
  47. String protAccVersion = controller.getProteinAccessionVersion(identId);
  48. String database = controller.getSearchDatabase(identId);
  49. content.add(protAcc);
  50. // Mapped Protein Accession
  51. AccessionResolver resolver = new AccessionResolver(protAcc, protAccVersion, database, true);
  52. String mappedProtAcc = resolver.isValidAccession() ? resolver.getAccession() : null;
  53. content.add(mappedProtAcc);
  54. // get protein details
  55. Protein protein = PrideInspectorCacheManager.getInstance().getProteinDetails(mappedProtAcc);
  56. if (protein != null) {
  57. protein = new AnnotatedProtein(protein);
  58. }
  59. // Protein name
  60. content.add(protein == null ? null : protein.getName());
  61. // protein status
  62. content.add(protein == null ? null : protein.getStatus().name());
  63. // sequence coverage
  64. Double coverage = PrideInspectorCacheManager.getInstance().getSequenceCoverage(controller.getUid(), identId);
  65. content.add(coverage);
  66. // peptide present
  67. if (protein == null || protein.getSequenceString() == null || "".equals(protein.getSequenceString())) {
  68. content.add(PeptideFitState.UNKNOWN);
  69. } else {
  70. if (protein.hasSubSequenceString(sequence, start, end)) {
  71. content.add(PeptideFitState.STRICT_FIT);
  72. } else if (protein.hasSubSequenceString(sequence)) {
  73. content.add(PeptideFitState.FIT);
  74. } else {
  75. content.add(PeptideFitState.NOT_FIT);
  76. }
  77. }
  78. // precursor charge
  79. Comparable specId = controller.getPeptideSpectrumId(identId, peptideId);
  80. if (specId != null) {
  81. int charge = controller.getPrecursorCharge(specId);
  82. content.add(charge == 0 ? null : charge);
  83. // delta mass
  84. // theoretical mass
  85. double mz = controller.getPrecursorMz(specId);
  86. List<Double> ptmMasses = new ArrayList<Double>();
  87. for (Modification mod : mods) {
  88. List<Double> monoMasses = mod.getMonoMassDeltas();
  89. if (monoMasses != null && !monoMasses.isEmpty()) {
  90. ptmMasses.add(monoMasses.get(0));
  91. }
  92. }
  93. Double deltaMass = MoleculeUtilities.calculateDeltaMz(sequence, mz, charge, ptmMasses);
  94. content.add(deltaMass == null ? null : NumberUtilities.scaleDouble(deltaMass, 2));
  95. // precursor m/z
  96. content.add(mz == -1 ? null : NumberUtilities.scaleDouble(mz, 2));
  97. } else {
  98. content.add(null);
  99. content.add(null);
  100. content.add(null);
  101. }
  102. // peptide ptms
  103. content.add(mods.size());
  104. // Map for grouping PTMs based on location
  105. Map<Integer, List<Double>> locationMap = new LinkedHashMap<Integer, List<Double>>();
  106. // Map for count PTMs based on accession
  107. Map<String, Integer> accessionCntMap = new LinkedHashMap<String, Integer>();
  108. // Iterate over each modification
  109. for (Modification mod : mods) {
  110. // store the location
  111. int location = mod.getLocation();
  112. if (location == 0) {
  113. location = 1;
  114. } else if (location == (sequence.length() + 1)) {
  115. location--;
  116. }
  117. List<Double> massDiffs = locationMap.get(location);
  118. if (massDiffs == null) {
  119. massDiffs = new ArrayList<Double>();
  120. locationMap.put(location, massDiffs);
  121. List<Double> md = mod.getMonoMassDeltas();
  122. if (md != null && !md.isEmpty()) {
  123. massDiffs.add(md.get(0));
  124. }
  125. }
  126. // store the accession
  127. String accession = mod.getAccession();
  128. Integer cnt = accessionCntMap.get(accession);
  129. cnt = cnt == null ? 1 : cnt + 1;
  130. accessionCntMap.put(accession, cnt);
  131. }
  132. // Modified Peptide Sequence
  133. StringBuilder modPeptide = new StringBuilder();
  134. for (int i = 0; i < sequence.length(); i++) {
  135. // append the amino acid
  136. modPeptide.append(sequence.charAt(i));
  137. // append mass differences if there is any
  138. List<Double> massDiffs = locationMap.get(i + 1);
  139. if (massDiffs != null) {
  140. modPeptide.append("[");
  141. if (massDiffs.isEmpty()) {
  142. modPeptide.append("*");
  143. } else {
  144. for (int j = 0; j < massDiffs.size(); j++) {
  145. if (j != 0) {
  146. modPeptide.append(",");
  147. }
  148. modPeptide.append(NumberUtilities.scaleDouble(massDiffs.get(j), 1));
  149. }
  150. }
  151. modPeptide.append("]");
  152. }
  153. }
  154. content.add(modPeptide.toString());
  155. // PTM Accessions with count
  156. StringBuilder ptmCnt = new StringBuilder();
  157. for (Map.Entry<String, Integer> entry : accessionCntMap.entrySet()) {
  158. if (ptmCnt.length() != 0) {
  159. ptmCnt.append(",");
  160. }
  161. ptmCnt.append(entry.getKey());
  162. ptmCnt.append("[");
  163. ptmCnt.append(entry.getValue());
  164. ptmCnt.append("]");
  165. }
  166. content.add(ptmCnt.toString());
  167. // Number of fragment ions
  168. content.add(controller.getNumberOfFragmentIons(identId, peptideId));
  169. // peptide scores
  170. PeptideScore score = controller.getPeptideScore(identId, peptideId);
  171. if (score != null) {
  172. List<Number> nums = score.getAllPeptideScores();
  173. if (nums != null && !nums.isEmpty()) {
  174. for (Number num : nums) {
  175. content.add(num == null ? num : num.doubleValue());
  176. }
  177. }
  178. }
  179. // Sequence length
  180. content.add(sequence.length());
  181. // Start
  182. content.add(start == -1 ? null : start);
  183. // End
  184. content.add(end == -1 ? null : end);
  185. // Theoritical isoelectric point
  186. // content.add(IsoelectricPointUtils.calculate(sequence));
  187. content.add(null);
  188. // Spectrum reference
  189. content.add(specId);
  190. // identification id
  191. content.add(identId);
  192. // peptide index
  193. content.add(peptideId);
  194. // additional details
  195. content.add(identId + Constants.COMMA + peptideId);
  196. return content;
  197. }
  198. /**
  199. * Retrieve a row of data for identification table.
  200. *
  201. * @param controller data access controller
  202. * @param identId identification id
  203. * @return List<Object> row data
  204. * @throws DataAccessException data access exception
  205. */
  206. public static List<Object> getProteinTableRow(DataAccessController controller,
  207. Comparable identId) throws DataAccessException {
  208. List<Object> content = new ArrayList<Object>();
  209. // Original Protein Accession
  210. String protAcc = controller.getProteinAccession(identId);
  211. String protAccVersion = controller.getProteinAccessionVersion(identId);
  212. String database = controller.getSearchDatabase(identId);
  213. content.add(protAcc);
  214. // Mapped Protein Accession
  215. AccessionResolver resolver = new AccessionResolver(protAcc, protAccVersion, database, true);
  216. String mappedProtAcc = resolver.isValidAccession() ? resolver.getAccession() : null;
  217. content.add(mappedProtAcc);
  218. // get protein details
  219. Protein protein = PrideInspectorCacheManager.getInstance().getProteinDetails(mappedProtAcc);
  220. if (protein != null) {
  221. protein = new AnnotatedProtein(protein);
  222. }
  223. // Protein name
  224. content.add(protein == null ? null : protein.getName());
  225. // protein status
  226. content.add(protein == null ? null : protein.getStatus().name());
  227. // sequence coverage
  228. Double coverage = PrideInspectorCacheManager.getInstance().getSequenceCoverage(controller.getUid(), identId);
  229. content.add(coverage);
  230. // isoelectric points
  231. if (protein != null) {
  232. String sequence = protein.getSequenceString();
  233. if (sequence != null) {
  234. content.add(IsoelectricPointUtils.calculate(protein.getSequenceString()));
  235. } else {
  236. content.add(null);
  237. }
  238. } else {
  239. content.add(null);
  240. }
  241. // Score
  242. double score = controller.getIdentificationScore(identId);
  243. content.add(score == -1 ? null : score);
  244. // Threshold
  245. double threshold = controller.getIdentificationThreshold(identId);
  246. content.add(threshold == -1 ? null : threshold);
  247. // number of peptides
  248. content.add(controller.getNumberOfPeptides(identId));
  249. // unique peptides
  250. content.add(controller.getNumberOfUniquePeptides(identId));
  251. // number of PTMs
  252. content.add(controller.getNumberOfPTMs(identId));
  253. // unique id for identification
  254. content.add(identId);
  255. // additional details is always null
  256. content.add(identId);
  257. return content;
  258. }
  259. /**
  260. * Get the headers for the identification quantitative table
  261. *
  262. * @param controller data access controller
  263. * @param refSampleIndex given reference sub sample index
  264. * @return List<Object> a list of headers
  265. * @throws DataAccessException data access exception
  266. */
  267. public static List<Object> getProteinQuantTableHeaders(DataAccessController controller, int refSampleIndex) throws DataAccessException {
  268. return getQuantTableHeaders(controller, refSampleIndex, true);
  269. }
  270. /**
  271. * Get the headers for the peptide quantitative table
  272. *
  273. * @param controller data access controller
  274. * @param refSampleIndex reference sub sample index
  275. * @return List<Object> peptide quantitative table headers
  276. * @throws DataAccessException data access exception
  277. */
  278. public static List<Object> getPeptideQuantTableHeaders(DataAccessController controller, int refSampleIndex) throws DataAccessException {
  279. return getQuantTableHeaders(controller, refSampleIndex, false);
  280. }
  281. /**
  282. * Get table header for quantitative data
  283. *
  284. * @param controller data access controller
  285. * @param refSampleIndex reference sub sample index
  286. * @param isProteinIdent whether it is protein identification or peptide identification
  287. * @return List<Object> a list of quantitative table headers
  288. * @throws DataAccessException data access exception
  289. */
  290. private static List<Object> getQuantTableHeaders(DataAccessController controller, int refSampleIndex, boolean isProteinIdent) throws DataAccessException {
  291. List<Object> headers = new ArrayList<Object>();
  292. // label free methods
  293. if (controller.hasLabelFreeQuantMethods()) {
  294. Collection<QuantCvTermReference> methods = isProteinIdent ? controller.getProteinLabelFreeQuantMethods() : controller.getPeptideLabelFreeQuantMethods();
  295. headers.addAll(getLabelFreeMethodHeaders(methods));
  296. }
  297. // isotope labelling methods
  298. if (controller.hasIsotopeLabellingQuantMethods()) {
  299. Collection<QuantCvTermReference> methods = isProteinIdent ? controller.getProteinIsotopeLabellingQuantMethods() : controller.getPeptideIsotopeLabellingQuantMethods();
  300. headers.addAll(getIsotopeLabellingMethodHeaders(methods, controller, refSampleIndex, isProteinIdent));
  301. }
  302. return headers;
  303. }
  304. /**
  305. * Create isotope labelling method headers
  306. *
  307. * @param methods isotope labelling methods
  308. * @param controller data access controller
  309. * @param refSampleIndex reference sub sample index
  310. * @param isProteinIdent whether is protein identification or peptide identification
  311. * @return List<Object> a list of headers
  312. * @throws DataAccessException data access exception
  313. */
  314. private static List<Object> getIsotopeLabellingMethodHeaders(Collection<QuantCvTermReference> methods,
  315. DataAccessController controller,
  316. int refSampleIndex,
  317. boolean isProteinIdent) throws DataAccessException {
  318. List<Object> headers = new ArrayList<Object>();
  319. if (methods.size() > 0) {
  320. QuantitativeSample sample = controller.getQuantSample();
  321. // total intensities
  322. boolean hasTotalIntensities = isProteinIdent ? controller.hasProteinTotalIntensities() : controller.hasPeptideTotalIntensities();
  323. if (hasTotalIntensities) {
  324. headers.addAll(getTotalIntensityHeaders(sample));
  325. }
  326. int existingRefSampleIndex = controller.getReferenceSubSampleIndex();
  327. if (refSampleIndex < 1 || refSampleIndex == existingRefSampleIndex) {
  328. // show the original ratios
  329. if (existingRefSampleIndex >= 1) {
  330. // the original quant data has a reference sample already
  331. headers.addAll(getReagentRatioHeaders(sample, existingRefSampleIndex));
  332. }
  333. } else {
  334. // show the newly calculated ratios
  335. headers.addAll(getReagentRatioHeaders(sample, refSampleIndex));
  336. }
  337. }
  338. return headers;
  339. }
  340. /**
  341. * Create a list of label free method headers
  342. *
  343. * @param methods label free methods
  344. * @return List<Object> label free method headers
  345. */
  346. private static List<Object> getLabelFreeMethodHeaders(Collection<QuantCvTermReference> methods) {
  347. List<Object> headers = new ArrayList<Object>();
  348. for (QuantCvTermReference method : methods) {
  349. headers.add(method.getName());
  350. }
  351. return headers;
  352. }
  353. /**
  354. * Create a list of headers for intensities
  355. *
  356. * @param sample qauntitative sample
  357. * @return List<String> total intensity headers
  358. */
  359. private static List<Object> getTotalIntensityHeaders(QuantitativeSample sample) {
  360. List<Object> headers = new ArrayList<Object>();
  361. for (int i = 1; i <= QuantitativeSample.MAX_SUB_SAMPLE_SIZE; i++) {
  362. CvParam reagent = sample.getReagent(i);
  363. if (reagent != null) {
  364. headers.add(QuantCvTermReference.getReagentShortLabel(reagent));
  365. }
  366. }
  367. return headers;
  368. }
  369. /**
  370. * Create a list of headers for reagents accorrding to the given reference sample
  371. *
  372. * @param sample qauntitative sample
  373. * @param refSampleIndex reference sub sample index
  374. * @return List<Object> a list of headers
  375. */
  376. private static List<Object> getReagentRatioHeaders(QuantitativeSample sample, int refSampleIndex) {
  377. List<Object> headers = new ArrayList<Object>();
  378. // get reference reagent
  379. CvParam referenceReagent = sample.getReagent(refSampleIndex);
  380. // get short label for the reagent
  381. String shortenedReferenceReagent = QuantCvTermReference.getReagentShortLabel(referenceReagent);
  382. for (int i = 1; i < QuantitativeSample.MAX_SUB_SAMPLE_SIZE; i++) {
  383. if (refSampleIndex != i) {
  384. CvParam reagent = sample.getReagent(i);
  385. if (reagent != null) {
  386. headers.add(QuantCvTermReference.getReagentShortLabel(reagent) + "/" + shortenedReferenceReagent);
  387. }
  388. }
  389. }
  390. return headers;
  391. }
  392. /**
  393. * Retrieve a row for identification quantitative table
  394. *
  395. * @param controller data access controller
  396. * @param identId identification id
  397. * @param referenceSubSampleIndex reference sub sample index
  398. * @return List<Object> a list of results
  399. * @throws DataAccessException data access exception
  400. */
  401. public static List<Object> getProteinQuantTableRow(DataAccessController controller,
  402. Comparable identId,
  403. int referenceSubSampleIndex) throws DataAccessException {
  404. Quantitation quant = controller.getProteinQuantData(identId);
  405. return getQuantTableRow(controller, quant, referenceSubSampleIndex, true);
  406. }
  407. /**
  408. * Retrieve a row for peptide quantitative table
  409. *
  410. * @param controller data access controller
  411. * @param identId identification id
  412. * @param peptideId peptide id
  413. * @param referenceSubSampleIndex reference sub sample index
  414. * @return List<Object> a list of results
  415. * @throws DataAccessException data access exception
  416. */
  417. public static List<Object> getPeptideQuantTableRow(DataAccessController controller,
  418. Comparable identId,
  419. Comparable peptideId,
  420. int referenceSubSampleIndex) throws DataAccessException {
  421. Quantitation quant = controller.getPeptideQuantData(identId, peptideId);
  422. return getQuantTableRow(controller, quant, referenceSubSampleIndex, false);
  423. }
  424. /**
  425. * Get table header for quantitative data
  426. *
  427. * @param controller data access controller
  428. * @param quant quantitative data
  429. * @param refSampleIndex reference sub sample index
  430. * @param isProteinIdent whether it is protein identification or peptide identification
  431. * @return List<String> a list of quantitative table headers
  432. * @throws DataAccessException data access exception
  433. */
  434. private static List<Object> getQuantTableRow(DataAccessController controller, Quantitation quant, int refSampleIndex, boolean isProteinIdent) throws DataAccessException {
  435. List<Object> contents = new ArrayList<Object>();
  436. // label free methods
  437. if (controller.hasLabelFreeQuantMethods()) {
  438. Collection<QuantCvTermReference> methods = isProteinIdent ? controller.getProteinLabelFreeQuantMethods() : controller.getPeptideLabelFreeQuantMethods();
  439. contents.addAll(getLabelFreeQuantData(methods, quant));
  440. }
  441. // isotope labelling methods
  442. if (controller.hasIsotopeLabellingQuantMethods()) {
  443. Collection<QuantCvTermReference> methods = isProteinIdent ? controller.getProteinIsotopeLabellingQuantMethods() : controller.getPeptideIsotopeLabellingQuantMethods();
  444. contents.addAll(getIsotopeLabellingQuantData(methods, controller, quant, refSampleIndex, isProteinIdent));
  445. }
  446. return contents;
  447. }
  448. /**
  449. * Get label free quantitative data
  450. *
  451. * @param methods label free methods
  452. * @param quant quantitative object
  453. * @return List<Double> a list of label free results
  454. */
  455. private static List<Double> getLabelFreeQuantData(Collection<QuantCvTermReference> methods, Quantitation quant) {
  456. return quant.getLabelFreeResults(methods);
  457. }
  458. /**
  459. * Get isotope labeling quantitative data
  460. *
  461. * @param methods isotople labelling methods
  462. * @param controller data access controller
  463. * @param quant quantitative object
  464. * @param refSampleIndex reference sub sample index
  465. * @param isProteinIdent whether is protein identification or peptide identification
  466. * @return List<Object> a list of results
  467. * @throws DataAccessException data access exception
  468. */
  469. private static List<Object> getIsotopeLabellingQuantData(Collection<QuantCvTermReference> methods, DataAccessController controller,
  470. Quantitation quant, int refSampleIndex, boolean isProteinIdent) throws DataAccessException {
  471. List<Object> contents = new ArrayList<Object>();
  472. if (methods.size() > 0) {
  473. QuantitativeSample sample = controller.getQuantSample();
  474. // total intensities
  475. boolean hasTotalIntensities = isProteinIdent ? controller.hasProteinTotalIntensities() : controller.hasPeptideTotalIntensities();
  476. if (hasTotalIntensities) {
  477. contents.addAll(getTotalIntensityQuantData(sample, quant));
  478. }
  479. int existingRefSampleIndex = controller.getReferenceSubSampleIndex();
  480. if (refSampleIndex < 1 || refSampleIndex == existingRefSampleIndex) {
  481. // show the original ratios
  482. if (existingRefSampleIndex >= 1) {
  483. // the original quant data has a reference sample already
  484. contents.addAll(getReagentRatioQuantData(sample, quant, existingRefSampleIndex));
  485. }
  486. } else {
  487. // show the newly calculated ratios
  488. contents.addAll(getReagentRatioQuantData(sample, quant, refSampleIndex));
  489. }
  490. }
  491. return contents;
  492. }
  493. /**
  494. * Get total intensities
  495. *
  496. * @param sample quantitative sample
  497. * @param quant quantitative data
  498. * @return List<Object> a list of total intensities
  499. */
  500. private static List<Object> getTotalIntensityQuantData(QuantitativeSample sample, Quantitation quant) {
  501. List<Object> contents = new ArrayList<Object>();
  502. for (int i = 1; i <= QuantitativeSample.MAX_SUB_SAMPLE_SIZE; i++) {
  503. CvParam reagent = sample.getReagent(i);
  504. if (reagent != null) {
  505. contents.add(quant.getIsotopeLabellingResult(i));
  506. }
  507. }
  508. return contents;
  509. }
  510. /**
  511. * Get reagent quantitative data
  512. *
  513. * @param sample quantitative sample
  514. * @param quant quantitative data
  515. * @param refSampleIndex reference sub sample index
  516. * @return List<Object> a list of reagent ratio data
  517. */
  518. private static List<Object> getReagentRatioQuantData(QuantitativeSample sample,
  519. Quantitation quant,
  520. int refSampleIndex) {
  521. List<Object> contents = new ArrayList<Object>();
  522. // get reference reagent
  523. Double referenceReagentResult = quant.getIsotopeLabellingResult(refSampleIndex);
  524. // get short label for the reagent
  525. for (int i = 1; i < QuantitativeSample.MAX_SUB_SAMPLE_SIZE; i++) {
  526. if (refSampleIndex != i) {
  527. CvParam reagent = sample.getReagent(i);
  528. if (reagent != null) {
  529. Double reagentResult = quant.getIsotopeLabellingResult(i);
  530. if (referenceReagentResult != null && reagentResult != null) {
  531. contents.add(reagentResult / referenceReagentResult);
  532. } else {
  533. contents.add(null);
  534. }
  535. }
  536. }
  537. }
  538. return contents;
  539. }
  540. }