rendered paste body/* * TCellReportory.java * * Created on November 11, 2007, 11:22 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package aisim;import org.apache.xerces.impl.dv.util.Base64;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import java.util.TreeMap;import java.util.logging.Level;import java.util.logging.Logger;import java.util.ArrayList;import java.util.Hashtable;import uk.ac.shef.wit.simmetrics.similaritymetrics.Levenshtein;/** * * @author saraiva */public class TCellReportory { //Versão Singleton private static TCellReportory _instance; private TreeMap<String, TCell> tCellReportory; private TCellReportory(){ tCellReportory = new TreeMap<String, TCell>(); } protected static void createNewInstance() { _instance = new TCellReportory(); } protected static TCellReportory getInstance() { if (_instance == null) { _instance = new TCellReportory(); } return _instance; } protected TreeMap<String, TCell> getReportory() { return _instance.tCellReportory; } protected void tCellReportoryUpdateMatchTraining(int iterationAPC) { ArrayList<TCell> tCellAuxReportory = null; int auxMatch = 0; if (Configuration.isReportMode()) { saveStringInReport("APC: " + iterationAPC + "\n\n"); } //Iterar as tcells Hashtable<String, Peptide> aux = (Hashtable<String, Peptide>) APCSet.getInstance().getLastAPCInAPCSet().getPeptides().clone(); for (Iterator<TCell> it1 = tCellReportory.values().iterator(); it1.hasNext();) { TCell tCell = it1.next(); float auxS = 0; String tCellReceptor = tCell.getTCellReceptor(); if (aux.containsKey(tCellReceptor)) { auxS = APCSet.getInstance().countPeptidesInAPCSet(tCellReceptor); aux.remove(tCellReceptor); auxMatch++; } tCell.updateTCellValues(auxS, iterationAPC); //Matar as tcells (marca para ser eliminada no fim da iteracao) com valores abaixo dos valores basais ou escrever para ficheiro// if (Configuration.isKill() && tCell.checkTerminationValues()) {// if (tCellAuxReportory == null) {// tCellAuxReportory = new ArrayList<TCell>();// }// tCellAuxReportory.add(tCell);// } if (Configuration.isKill() && tCell.isToKill(iterationAPC)) { if (tCellAuxReportory == null) { tCellAuxReportory = new ArrayList<TCell>(); } tCellAuxReportory.add(tCell); } }//Fim Iterar tcells reportorio if (Configuration.isKill() && tCellAuxReportory != null) { for (int i = 0; i < tCellAuxReportory.size(); i++) { tCellReportory.remove(tCellAuxReportory.get(i).getTCellReceptor()); } tCellAuxReportory.clear();// Ou igual a null? } for (Iterator<Peptide> it = aux.values().iterator(); it.hasNext();) { Peptide peptide = it.next(); tCellReportory.put(peptide.getLigand(), new TCell(peptide, APCSet.getInstance().countPeptidesInAPCSet(peptide.getLigand()), iterationAPC)); } if (Configuration.isReportMode()) { saveStringInReport("\tMatchTCells=" + auxMatch + "\tNewTCells=" + aux.size() + "\n\n\tTotalTCells=" + TCellReportory.getInstance().getReportory().size() + "\tAPCSetSize=" + APCSet.getInstance().getNumAPCS() + "\tDifPeptides=" + APCSet.getInstance().getLastAPCInAPCSet().getPeptides().size() + "\tTotalPeptides=" + APCSet.getInstance().getLastAPCInAPCSet().getTotalPeptides() + "\n\n"); } } protected void tCellReportoryUpdateMatchTesting(int iterationAPC) { try { ArrayList<Comity> arrayPeptideComitys = new ArrayList<Comity>(); //ArrayList<Peptide> arrayRarePeptides = null; int rarePeptideComity = 0; ArrayList<TCell> tCellAuxReportory = null; String strAux = null; if (Configuration.isReportMode()) { saveStringInReport("APC: " + iterationAPC + "\n\n"); } //Preparar os comites for (Iterator<Peptide> it1 = APCSet.getInstance().getLastAPCInAPCSet().getPeptides().values().iterator(); it1.hasNext();) { Peptide peptide = (Peptide) it1.next(); Comity auxComity = new Comity(peptide); arrayPeptideComitys.add(auxComity); } //Iterar as tcells for (Iterator<TCell> it2 = tCellReportory.values().iterator(); it2.hasNext();) { TCell tCell = it2.next(); float auxS = 0; int iterationComity = 0; //Iterar os peptideos for (Iterator<Peptide> it3 = APCSet.getInstance().getLastAPCInAPCSet().getPeptides().values().iterator(); it3.hasNext();) { Peptide peptide = (Peptide) it3.next(); String auxLigand = peptide.getLigand(); float affinity = getAffinity(auxLigand, tCell.getTCellReceptor()); //Tem determinada Afinidade logo vao para vectors de calculo e sao estimuladas if (affinity >= Configuration.getAffinityThreshold()) { auxS += APCSet.getInstance().countPeptidesInAPCSet(auxLigand) * affinity; arrayPeptideComitys.get(iterationComity).getComity().add(tCell); } iterationComity++; }//Fim de iterar os peptideos tCell.updateTCellValues(auxS, iterationAPC); //Matar as tcells (marca para ser eliminada no fim da iteracao) com valores abaixo dos valores basais ou escrever para ficheiro// if (Configuration.isKill() && tCell.checkTerminationValues()) {// if (tCellAuxReportory == null) {// tCellAuxReportory = new ArrayList<TCell>();// }// tCellAuxReportory.add(tCell);// } if (Configuration.isKill() && tCell.isToKill(iterationAPC)) { if (tCellAuxReportory == null) { tCellAuxReportory = new ArrayList<TCell>(); } tCellAuxReportory.add(tCell); } }//Fim Iterar tcells reportorio /* * TCell Death: chamar checkTerminationValues e remover entradas no tCellReportory. */ if (Configuration.isKill() && tCellAuxReportory != null) { for (int i = 0; i < tCellAuxReportory.size(); i++) { tCellReportory.remove(tCellAuxReportory.get(i).getTCellReceptor()); } tCellAuxReportory.clear();// Ou igual a null? } for (Iterator<Comity> it = arrayPeptideComitys.iterator(); it.hasNext();) { Comity comity = it.next(); Peptide peptide = comity.getPeptide(); ArrayList<TCell> matchingTCellComity = comity.getComity(); String ligand = peptide.getLigand(), auxLigand = null; int comityKP = 0; if (Configuration.isBase64() && Configuration.isReportMode()) { byte[] pepByte = ligand.getBytes("ISO-8859-1"); auxLigand = Base64.encode(pepByte); } if (matchingTCellComity.isEmpty()) { tCellReportory.put(ligand, new TCell(peptide, APCSet.getInstance().countPeptidesInAPCSet(ligand), iterationAPC)); if (Configuration.isReportMode()) { if (Configuration.isBase64()) { ligand = auxLigand; } saveStringInReport("\tPeptideLigand: " + ligand + "\tOcurrences=" + peptide.getCountAPC() + "\tMatchTCells=0\tK>P=0\tRarePeptide=N\n"); } } else { if (Configuration.isReportMode()) { if (Configuration.isBase64()) { ligand = auxLigand; } saveStringInReport("\tPeptideLigand: " + ligand + "\tOcurrences=" + peptide.getCountAPC() + "\tMatchTCells=" + matchingTCellComity.size() + "\t"); } for (Iterator<TCell> it1 = matchingTCellComity.iterator(); it1.hasNext();) { TCell tCell = it1.next(); //Criar comite células K>P if (tCell.getK() > tCell.getP()) { comityKP++; //Escrever nivel 1 //saveAlertLogFile("APC " + iterationAPC + " - AlertLevel 1"); } } if (Configuration.isReportMode()) { saveStringInReport("K>P=" + comityKP + "\t"); } if (comityKP != 0 && (comityKP >= matchingTCellComity.size() * Configuration.getComiteKPThreshold())) { // Em vez de getCountAPC deveria/poderia ser apenas incrementar de 1 o rarePeptideComity. Ver tb alerta 3. Confirmar com o JC. rarePeptideComity += peptide.getCountAPC(); //rarePeptideComity ++; //Escrever nivel 2 //saveAlertLogFile("APC " + iterationAPC + " - AlertLevel 2"); if (Configuration.isReportMode()) { saveStringInReport("RarePeptide=Y\n"); } } else { if (Configuration.isReportMode()) { saveStringInReport("RarePeptide=N\n"); } } } } strAux = "N"; // O getTotalPeptides tem TODAS as ocorrências. Deve usar-se o peptides.size(). Confirmar com o JC. //if (rarePeptideComity != 0 && (rarePeptideComity >= APCSet.getInstance().getLastAPCInAPCSet().size() * Configuration.getAPCThreshold())) { if (rarePeptideComity != 0 && (rarePeptideComity >= APCSet.getInstance().getLastAPCInAPCSet().getTotalPeptides() * Configuration.getAPCThreshold())) { //Escrever no ficheiro nivel 3 usar iteração para dizer qual a linha TCellReportory.getInstance().saveAlertLogFile("APC " + iterationAPC + " - AlertLevel 3 (" + APCSet.getInstance().getLastAPCInAPCSet().getMETA_INFO() + ")"); strAux = "Y"; } if (Configuration.isReportMode()) { saveStringInReport("\n\tTotalTCells=" + TCellReportory.getInstance().getReportory().size() + "\tAPCSetSize=" + APCSet.getInstance().getNumAPCS() + "\tDifPeptides=" + APCSet.getInstance().getLastAPCInAPCSet().getPeptides().size() + "\tTotalPeptides=" + APCSet.getInstance().getLastAPCInAPCSet().getTotalPeptides() + "\tRareAPC=" + strAux + "\n\n"); } } catch (Exception e) { System.out.println("Erro a codificar Base64"); } } /*public void showTCellReportory() { int i = 0; for (Iterator<TCell> tCellIterator = this.getReportory().values().iterator(); tCellIterator.hasNext();) { TCell tCellAux = tCellIterator.next(); System.out.println("TCell[" + i++ + "]: " + tCellAux.getTCellReceptor()); } System.out.println("Total TCells:" + tCellReportory.size()); }*/ protected void saveTCellsFile(String tCellsFile) { BufferedWriter outputStream; try { outputStream = new BufferedWriter(new FileWriter(tCellsFile)); for (Iterator<TCell> it = this.getReportory().values().iterator(); it.hasNext();) { TCell auxTCell = it.next(); outputStream.write(auxTCell.toString()); outputStream.newLine(); } outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } protected void readTCellsFile(String tCellsFile) { BufferedReader inputStream = null; try { inputStream = new BufferedReader(new FileReader(tCellsFile)); String readedData; while ((readedData = inputStream.readLine()) != null) { TCell newTCell = new TCell(readedData); tCellReportory.put(newTCell.getTCellReceptor(), newTCell); } inputStream.close(); } catch (FileNotFoundException ex) { Logger.getLogger(TCellReportory.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TCellReportory.class.getName()).log(Level.SEVERE, null, ex); } } protected void saveAlertLogFile(String info) { BufferedWriter outputStream; try { outputStream = new BufferedWriter(new FileWriter(Configuration.getAlertLogFile(), true)); outputStream.write(info); outputStream.newLine(); outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } /*public void saveReportWithCurrentSizes(int it, int totalPeptides, int sizeAPC, int totalAPCInSet) { BufferedWriter outputStream; try { outputStream = new BufferedWriter(new FileWriter(Configuration.getReportoryGraphicFile(), true)); outputStream.write(it + "\t" + tCellReportory.size() + "\t" + totalPeptides + "\t" + sizeAPC + "\t" + totalAPCInSet); outputStream.newLine(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } }*/ protected void saveStringInReport(String info) { BufferedWriter outputStream; try { outputStream = new BufferedWriter(new FileWriter(Configuration.getReportPath(), true)); outputStream.write(info); //outputStream.newLine(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } } protected static float getAffinity(String s, String t) { Levenshtein metric = new Levenshtein(); float affinity = metric.getSimilarity(s, t); //System.out.println("AFINIDADE (" + s + ")(" + t + ")(" + affinity + ")"); return affinity; } protected static void prepareReportoryToTest() { for (Iterator it = _instance.tCellReportory.values().iterator(); it.hasNext();) { ((TCell) it.next()).prepareTCelltoTest(); } } }