PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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