/* Copyright @ 2003, The Institute for Genomic Research (TIGR). All rights reserved. */ /***************************************************************************** * Description: A class that reads a set of ImaGene (txt) files and write to * MEV files. * Company: TIGR * @author Jianwei (Jerry) Li * @version 1.0 * @Date: Created: 04/04/2003 and modified: 06/29/2004 *****************************************************************************/ package org.tigr.microarray.converter; import java.util.*; import java.awt.*; import java.io.*; import javax.swing.*; import org.tigr.microarray.MevAttributes; import org.tigr.util.FileReading; import org.tigr.util.StringSplitter; import org.tigr.util.io.MyIni; import org.tigr.util.io.IOUtil; import org.tigr.util.MyTimer; public class ImaGeneMev extends MevConverter implements ImaGeneAttributes, MevAttributes{ private JFrame parent; private String ch1FileName, ch2FileName; private String title; // the massege showed on the bars of all windows. private Vector ig2FileNames; private int fPixAddr, gAnnType; private final String TAB = "\t"; private final String PAN = "#"; /**************************************************************************** *Constructor: *

Parameters: *
gfName --- the name of GenePix file to be converted. *
mfName --- the name of .mev file that is an output. *****************************************************************************/ public ImaGeneMev(String gfName, String mfName) { inFileNames = new Vector(1); outFileNames = new Vector(1); inFileNames.add(gfName); outFileNames.add(mfName); title = new String("Genepix vs Mev"); msg = new String(""); ready = false; wantSort = true; fetalError = false; minorError = false; } /**************************************************************************** *Constructor: *

Parameters: *
frame -- the parent that uses this class. *
ig1Names --- the names of ImaGene files for channel 1. *
ig2Names --- the names of ImaGene files for channel 2. *
mfNames --- the names of .mev files that are output. *
barTitle --- the string showed on the bar. *
annNum --- the ann file type, one for one / many for one. *****************************************************************************/ public ImaGeneMev(JFrame frame, Vector ig1Names, Vector ig2Names, Vector mfNames, String barTitle, int annNum) { parent = frame; inFileNames = ig1Names; ig2FileNames = ig2Names; outFileNames = mfNames; msg = new String(""); title = barTitle; gAnnType = annNum; ready = false; wantSort = true; fetalError = false; minorError = false; } public int getTaskEnd() { return taskEnd; } public int getCurrent() { return counter; } public String getErrorMsg() { return msg; } public String getFileName() { return ch1FileName; } /*************************************************************************** * Description: * overrides an abstract method to implement the main function of class. **************************************************************************/ public void run () { boolean going, noError; String aLine = new String(""); String densA = new String(""); String densB = new String(""); String flagA = new String(""); String flagB = new String(""); String ident = new String(""); String spAreaA = new String(""); String spAreaB = new String(""); String bgA = new String(""); // background info for channel A String bgB = new String(""); // background info for channel B String medA = new String(""); String medB = new String(""); String mevFileName; //String annoFileNamePath; String inputFileName; //String extraInfo = new String(""); String mevFileHeader[]; //String annoFileHeader[]; //String annoFile[] = null; String f1ColNames[], f2ColNames[]; String f1ColValues[], f2ColValues[]; StringBuffer mevLine = new StringBuffer(200); int ch1HeaderLines, ch2HeaderLines; // the number of header lines for the data input file. int mRowIndex, mColIndex; // meta row and column indices; int f1SignalArea, f2SignalArea; int f1SignalMedian, f2SignalMedian; int f1BgMedian, f2BgMedian; int f1SignalTotal, f2SignalTotal; int f1BgTotal, f2BgTotal, f1BgArea, f2BgArea; int[] ch1NumSpotsAndMaxMetaCol, ch2NumSpotsAndMaxMetaCol; int maxRow = 0; int maxCol = 0; int maxMetaCol, row, col, metaRow, metaCol, slideRow, slideCol; int i, j, k, f1FlagIndex, f2FlagIndex, flagInt, colIndex, rowIndex, geneIndex; int f532m; int numOfFiles, numSpots; BufferedWriter mevWriter = null; // BufferedWriter annoWriter = null; FileOutputStream mevFileOut = null; //FileOutputStream annoFileOut = null; String[] ch1File, ch2File; taskEnd = determineTaskEnd(); if(taskEnd == 1){ return; } taskEnd <<= 1; counter = 0; numOfFiles = inFileNames.size(); for(i=0; i 1 && f2SignalMedian > 1){ // converting based on signal median for(j = ch1HeaderLines+2, k=0; j < numSpots; j++, k++){ f1ColValues = separateLine(ch1File[j]); f2ColValues = separateLine(ch2File[j]); metaRow = Integer.parseInt(f1ColValues[mRowIndex]); metaCol = Integer.parseInt(f1ColValues[mColIndex]); col = Integer.parseInt(f1ColValues[colIndex]); row = Integer.parseInt(f1ColValues[rowIndex]); ident = f1ColValues[geneIndex]; spAreaA = f1ColValues[f1SignalArea]; spAreaB = f2ColValues[f2SignalArea]; bgA = calculateBackground(f1ColValues, f1BgMedian, f1SignalArea); bgB = calculateBackground(f2ColValues, f2BgMedian, f2SignalArea); flagA = setFlag(f1ColValues, f1FlagIndex); flagB = setFlag(f2ColValues, f2FlagIndex); maxRow = ch1NumSpotsAndMaxMetaCol[2]; maxCol = ch1NumSpotsAndMaxMetaCol[3]; slideRow = (metaRow - 1) * maxRow + row; slideCol = (metaCol -1 ) * maxCol + col; mevLine.append(ident).append(TAB); if(gBkgCorrect){ densA = calculateIntegrateIntensity(f1ColValues, f1SignalMedian, f1BgMedian, f1SignalArea, true); densB = calculateIntegrateIntensity(f2ColValues, f2SignalMedian, f2BgMedian, f2SignalArea, true); medA = calculateMedianIntensity(f1ColValues, f1SignalMedian, f1BgMedian, true); medB = calculateMedianIntensity(f2ColValues, f2SignalMedian, f2BgMedian, true); } else { densA = calculateIntegrateIntensity(f1ColValues, f1SignalMedian, f1BgMedian, f1SignalArea, false); densB = calculateIntegrateIntensity(f2ColValues, f2SignalMedian, f2BgMedian, f2SignalArea, false); medA = calculateMedianIntensity(f1ColValues, f1SignalMedian, f1BgMedian, false); medB = calculateMedianIntensity(f2ColValues, f2SignalMedian, f2BgMedian, false); } if(gUseMedian){ mevLine.append(medA).append(TAB).append(medB); aLine = densA + TAB + densB; } else { mevLine.append(densA).append(TAB).append(densB); aLine = medA + TAB + medB; } mevLine.append(TAB).append(slideRow).append(TAB).append(slideCol); mevLine.append(TAB).append(metaRow).append(TAB).append(metaCol); mevLine.append(TAB).append(row).append(TAB).append(col).append(TAB); mevLine.append(bgA).append(TAB).append(bgB).append(TAB).append(flagA); mevLine.append(TAB).append(flagB).append(TAB).append(spAreaA).append(TAB); mevLine.append(spAreaB).append(TAB).append(aLine); outFile[k] = new String(mevLine.toString()); mevLine.delete(0, mevLine.length()); // because the Gene ID is used as UID, it is unneccessary to generate // ann file /*extraInfo = ident + TAB + slideRow + TAB + slideCol; extraInfo += TAB + f1ColValues[geneIndex]; annoFile[k] = new String(extraInfo);*/ sortKeys[0][k] = k; sortKeys[1][k] = slideRow; sortKeys[2][k] = slideCol; counter++; } // end of the for loop for a single file. } else { // converting based on signal total for(j = ch1HeaderLines+2, k=0; jDescription: * calculate background for a channel with bg = median * area *

Parameters: *
tuple -- all information in one row *
backg -- address for background medien column *
area -- address of Spot Area column. *

Return: the background information ***************************************************************************/ private String calculateBackground(String[] tuple, int backg, int area){ float fPix, bMedian; int temp; String bg = new String(""); try{ fPix = Float.parseFloat(tuple[area]); bMedian = Float.parseFloat(tuple[backg]); temp = (int)(bMedian * fPix); bg += temp; } catch (NumberFormatException nfex) { minorError = true; bg = "null"; msg += "**********\n"; msg += "There is an unacceptable value for either Signal Area or Backgroud Median; and the background was set to null.\n"; } return bg; } /**************************************************************************** * Description: * calculate integrate intensity for a channel with signal median method *

Parameters: *
tuple -- all information in one row *
med -- address of Siganl Median for either of files *
backg -- address of Background Medien column. *
sArea -- address of Signal Area column. *
bgSub -- indicate if an intensity should subtract backgroud. *

Return: the intensity ***************************************************************************/ private String calculateIntegrateIntensity(String[] tuple, int med, int backg, int sArea, boolean bgSub){ float signal, bg, area; int temp; String den = new String(""); try{ signal = Float.parseFloat(tuple[med]); bg = Float.parseFloat(tuple[backg]); area = Float.parseFloat(tuple[sArea]); if(bgSub){ temp = (int)((signal - bg) * area); } else { temp = (int)(signal * area); } den += temp; } catch (NumberFormatException nfex) { minorError = true; den = "null"; msg += "**********\n"; msg += "There is a null value for either Signal Median or Background Median "; msg += "and the intensity was set to null.\n"; } return den; } /**************************************************************************** * Description: * calculate integrate intensity for a channel with signal total method *

Parameters: *
tuple -- all information in one row *
sigTot -- address of Siganl Total for either of files *
bgTot -- address of Background Total column. *
bgArea -- address of Background Area column. *
sArea -- address of Signal Area column. *
bgSub -- indicate if an intensity should subtract backgroud. *

Return: the density ***************************************************************************/ private String calculateIntegrateIntensity(String[] tuple, int sigTot, int bgTot, int bgArea, int sArea, boolean bgSub){ float st, bt, ba, area; String den = new String(""); int temp; st = Float.parseFloat(tuple[sigTot]); bt = Float.parseFloat(tuple[bgTot]); ba = Float.parseFloat(tuple[bgArea]); area = Float.parseFloat(tuple[sArea]); if(bgSub){ temp = (int)(st - (bt/ba) * area); } else { temp = (int)(st); } den += temp; return den; } /**************************************************************************** * Description: * calculate median intensity for a channel *

Parameters: *
tuple -- all information in one row *
med -- address for either channel A or B median column *
backg -- address for background medien column *
bgSub -- indicate if an intensity should subtract backgroud. *

Return: the density ***************************************************************************/ private String calculateMedianIntensity(String[] tuple, int med, int backg, boolean bgSub){ float fMedian, bMedian; float temp; String den = new String(""); try{ fMedian = Float.parseFloat(tuple[med]); bMedian = Float.parseFloat(tuple[backg]); if(bgSub){ temp = fMedian - bMedian; } else { temp = fMedian; } den += (int)temp; } catch (NumberFormatException nfex) { minorError = true; den = "null"; msg += "**********\n"; msg += "There is an unacceptable value to calculate intensity; and the intensity was set to null.\n"; } return den; } /**************************************************************************** * Description: * calculate median density for a channel with signal total method *

Parameters: *
tuple -- all information in one row *
sigTot -- address of Siganl Total for either of files *
bgTot -- address of Background Total column. *
bgArea -- address of Background Area column. *
sArea -- address of Signal Area column. *
bgSub -- indicate if an intensity should subtract backgroud. *

Return: the density ***************************************************************************/ private String calculateMedianIntensity(String[] tuple, int sigTot, int bgTot, int bgArea, int sArea, boolean bgSub){ float st, bt, ba, area; String den = new String(""); int temp; st = Float.parseFloat(tuple[sigTot]); bt = Float.parseFloat(tuple[bgTot]); ba = Float.parseFloat(tuple[bgArea]); area = Float.parseFloat(tuple[sArea]); if(bgSub){ temp = (int)(st/area - bt/ba); } else { temp = (int)(st/area); } den += temp; return den; } /**************************************************************************** * Description: * creates a header for both mev and dat(annotation) file. *

Parameter: *
fName -- the mev file name that should be written to the file. *
numRow -- the number of spots (genes/row). *

Return: the header as an array. **************************************************************************/ private String[] generateHeader(String fName, int numRow){ String[] tempHeader = new String[8]; String bkgVal; if(gBkgCorrect){ bkgVal = ON; } else { bkgVal = OFF; } tempHeader[0] = new String(PAN + SPACE + VERSION + COLON + " V1.0"); tempHeader[1] = new String(PAN + SPACE + FORMAT_VERSION + COLON + ExpressConverter.MEV_VER); tempHeader[2] = new String(PAN + SPACE + DATE + COLON + SPACE + MyTimer.getCurrentDateAndTime('.')); tempHeader[3] = new String(PAN + SPACE + CREATED_BY + COLON + SPACE + title); tempHeader[4] = new String(PAN + SPACE + ROW_COUNT + COLON + numRow); tempHeader[5] = new String(PAN + " Converted from ImaGene files: " + fName); tempHeader[6] = new String(PAN + SPACE + BKG_CORRECT + COLON + bkgVal); if(gUseMedian){ tempHeader[7] = UID + TAB + MEDA + TAB + MEDB + TAB + R + TAB + C + TAB + MR + TAB + MC + TAB + SR + TAB + SC + TAB + BKGA + TAB + BKGB + TAB + FLAGA + TAB + FLAGB + TAB + SAA + TAB + SAB + TAB + IA + TAB + IB; } else { tempHeader[7] = UID + TAB + IA + TAB + IB + TAB + R + TAB + C + TAB + MR + TAB + MC + TAB + SR + TAB + SC + TAB + BKGA + TAB + BKGB + TAB + FLAGA + TAB + FLAGB + TAB + SAA + TAB + SAB + TAB + MEDA + TAB + MEDB; } return tempHeader; } /**************************************************************************** * Description: * determine the end line of row data *

Parameters: *
dataFile -- the data file as vector *

Return: the index of the end. ****************************************************************************/ private int getDataEndLine(String[] dataFile){ int end, i, j; end = 0; String line, temp, elements[]; for(i=dataFile.length-1; i>0; i--){ line = (String)dataFile[i]; elements = separateLine(line); end = i; for(j=0; jDescription: * determine the number of header lines *

Parameters: *
dataFile -- the data file as a String array *

Return: the number of header lines. ****************************************************************************/ private int getHeaderLines(String[] dataFile){ int numLine, i, j; String line, temp, elements[]; numLine = 0; for(i=0; iDescription: * instantiates a writer object for ann file writing. *

Parameters: *
fName --- ann file name to be written. *
inName --- the input file name. *
numSpots --- the number of rows in the input file. *

Return: the writer ****************************************************************************/ private BufferedWriter createAnnoWriter(String fName, String inName, int numSpots){ BufferedWriter out; FileOutputStream fOut; String annoFileHeader[]; try { fOut = new FileOutputStream(fName); out = new BufferedWriter(new OutputStreamWriter(fOut)); annoFileHeader = generateHeader(inName, numSpots); annoFileHeader[5] = UID + TAB + R + TAB + C; for(int i=0; iDescription: * find the address of the column that is interested. *

Parameters: *
str -- a line of the file *
key -- column name to be searched *

Return: the index of the column ****************************************************************************/ private int findRightCol(String str[], String key){ int addr = 0; int size = str.length; int i; String temp = new String(""); for(i=0; iDescription: * reads an input file and return the contents as a vector. ***************************************************************************/ private String[] readInput(String inName){ String[] tempVec = null; FileReading fileReader; try { fileReader = new FileReading(inName, FileReading.OUT_AS_ARRAY); if(fileReader.done){ tempVec = fileReader.getFileContentAsArray(); } else { setErrorMessage(fileReader.getProcessMessage()); taskEnd = 1; counter = 1; } } catch (IOException e){ setErrorMessage(e.getMessage()); taskEnd = 1; counter = 1; } return tempVec; } /**************************************************************************** * Description: * checks if the input file is original without any modification. *

Parameter: *
dataFile -- the input file. *
dataStart -- the starting line of data *

Return: the number of spots (rows) * ************************************************************************/ private int[] noSpotDropped(String fName, String[] dataFile, int dataStart, int mRow, int mCol, int row, int col){ int num[] = new int[5]; int dataEnd, heading; int maxMetaRow, maxMetaCol, maxCol, maxRow; String tempLine, vecVal[]; heading = dataStart; dataEnd = getDataEndLine(dataFile); tempLine = (String)dataFile[dataEnd]; vecVal = separateLine(tempLine); maxMetaRow = Integer.parseInt(vecVal[mRow]); maxMetaCol = Integer.parseInt(vecVal[mCol]); maxRow = Integer.parseInt(vecVal[row]); maxCol = Integer.parseInt(vecVal[col]); num[0] = maxMetaRow * maxMetaCol * maxRow * maxCol; num[1] = maxMetaCol; num[2] = maxRow; num[3] = maxCol; num[4] = dataEnd; if(num[0] != (dataEnd - heading - 1)){ JOptionPane.showMessageDialog(null, "You might have dropped some of your spots in the file \n(" + fName + ".\nThe converter requires an original ImaGene output file." + "\nConverting will continue for the remaining files", parent.getTitle(), JOptionPane.ERROR_MESSAGE); num[0] = -1; } return num; } /**************************************************************************** *Description: * check if a parameter is a character. *

Parameter: *
x --- the input to be checked. *

Return: true if the input is a character; otherwise, false. ****************************************************************************/ private boolean isChar(char x){ boolean b = true; switch(x) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': b = false; break; default: b = true; } return b; } /***************************************************************************** *DESCRIPTION: * set flag based on the flag values in the original file *

Parameters: *
tuple --- the information of one spot *
indx --- the address of orginal flag *

Return: the flag. ***************************************************************************/ private String setFlag(String[] tuple, int indx){ String f = new String(""); int flagInt; flagInt = Integer.parseInt(tuple[indx]); if(flagInt != 0){ f = "X"; } else { f = "B"; } return f; } }