Source code for MSQuant: PILinOut.cs, MSQlib1/src/utility/PILinOut.cs.

Table of contents page.

Home page for MSQuant.

/****************************************************************************
 * Copyright (C) 2008 Peter Mortensen and Matthias Mann                     *
 * This file is part of MSQuant.                                            *
 *                                                                          *
 * MSQuant is distributed under the terms of                                *
 * the GNU General Public License. See src/COPYING.TXT or                   *
 * <http://www.gnu.org/licenses/gpl.txt> for details.                       *
 *                                                                          *
 * MSQuant is free software; you can redistribute it                        *
 * and/or modify it under the terms of the GNU                              *
 * General Public License as published by the Free                          *
 * Software Foundation; either version 2 of the                             *
 * License, or (at your option) any later version.                          *
 *                                                                          *
 * MSQuant is distributed in the hope that it will be                       *
 * useful, but WITHOUT ANY WARRANTY; without even the                       *
 * implied warranty of MERCHANTABILITY or FITNESS FOR                       *
 * A PARTICULAR PURPOSE.  See the GNU General Public                        *
 * License for more details.                                                *
 *                                                                          *
 * You should have received a copy of the GNU General                       *
 * Public License along with MSQuant; if not, write to                      *
 * the Free Software Foundation, Inc., 59 Temple                            *
 * Place, Suite 330, Boston, MA  02111-1307  USA                            *
 *                                                                          *
 * Purpose: Holds XYZ, see below for documentation.                         *
 *                                                                          *
 ****************************************************************************/

/****************************************************************************
 *                               CEBI                                       *
 *                    Software Development Group                            *
 *                         Peter Mortensen                                  *
 *                E-mail: NUKESPAMMERSdrmortensen@get2netZZZZZZ.dk          *
 *                 WWW: http://www.cebi.sdu.dk/                             *
 *                                                                          *
 *  Program for post-processing of result from search in mass               *
 *    spectrometric data.                                                   *
 *                                                                          *
 *    FILENAME:   PILinOut.cs                                               *
 *    TYPE:       CSHARP                                                    *
 *                                                                          *
 * CREATED: PM 2008-03-14   Vrs 1.0. Translated from PILinOut.vb            *
 * UPDATED: PM 2008-xx-xx                                                   *
 *                                                                          *
 *                                                                          *
 ****************************************************************************/

//Future:
// 1.


using System; //For Exception and others.

using System.Diagnostics; //For Trace.

using System.Collections; //For ArrayList
using System.Collections.Generic; //For List

using System.IO; //For pushToFile
using System.Text; //For StringBuilder

////Note: this will only work if a reference is added manually
//// in Visual Studio: menu Project/Add Reference/
//// System.Runtime.Serialization.Formatters.soap/Select/OK.
//Imports System.Runtime.Serialization.Formatters.soap
//Imports System.Runtime.Serialization 'For StreamingContext

using massSpectrometryBase; //For signalStructure

//Note: "Excel.Application" and "Excel.Worksheet" requires adding
//      reference to project:
//
// menu Project/Add Reference/tab COM/Select,
// select "Microsoft Excel 9.0 Object Library"
//
// or:
// menu Project/Add Reference/tab Browse/,
// D:\Program Files\Microsoft Office\Office\EXCEL8.OLB

namespace MSQlib1xyz.src.utility
{
    class PILinOut
    {
    }
}


//****************************************************************************
//d$ <summary>
//d$ Purpose: Namespace for application independent and domain independent
//d$ utility classes (that could be reused in any application,
//d$ not just mass spectrometric applications.)
//d$ <see cref="T:VBXMLDoc.CVBXMLDoc" />.
//d$ <isUnitTest></isUnitTest>
//d$ <applicationname>XYZ</applicationname>
//d$ <author>Peter Mortensen</author>
//d$ <seealso>http://www.cebi.sdu.dk/</seealso>
//d$ <codetype>PLATFORM independent</codetype>
//d$ </summary>
namespace SDUPutility
{

    //Move this class to an appropriate place, but still in Namespace SDUPutility.
    public class PILInputOutput
    {

        //Moved to MSQuantCommon2.vb
        //private struct datapointSeparationInfoStructure
        //{
        //    public double datapointSeparation;
        //    public double startX;
        //    public double endX;
        //}

        //Changed PM_CSHARP 2008-03-13
        //public static string LINEEND = Constants.vbCr + Constants.vbLf;
        public static string LINEEND = "\r\n";

        //Changed PM_REFACTOR 2003-10-27. Does not exactly belong here, but at
        //least it is no longer in the GUI part. Should be moved to a different
        //place, but still in massSpectrometryBase or some lower level utility layer.
        //****************************************************************************
        //* <placeholder for header> *
        //****************************************************************************
        public static void pushToFile(string aFileName, string aStr)
        {

            FileStream saveFile = File.OpenWrite(aFileName);

            //Changed PM_NO_APPENDTOFILE 2003-11-04
            saveFile.SetLength(0);
            //Delete existing content...

            saveFile.Seek(0, SeekOrigin.End);
            StreamWriter theStreamWriter = new StreamWriter(saveFile);
            // theStreamWriter.Write(txtProteinHits.Text)
            theStreamWriter.Write(aStr);
            theStreamWriter.Flush();
            //this is necessary
            theStreamWriter.Close();

            //Changed PM_CLOSE_FILE_BUG 2008-04-24. File could not be renamed
            //or deleted until the program was terminated!!!
            saveFile.Close();
        } //pushToFile


        //Changed PM_REFACTOR 2003-03-21
        //To be moved to an application object.
        //****************************************************************************
        //* <placeholder for header> *
        //****************************************************************************
        public static void pushToExcel(string aStr)
        {
            //Moved from QuantWindow.vb.

            Trace.Assert((aStr != null),
                "PIL ASSERT. Could not export to excel. String passed pushToExcel() is undefined!. Contact a developer near you.");

            int len = aStr.Length;
            if (len > 0)
            {
                //Ignore empty strings...
                try
                {
                    //Changed PM_SAVEMEMORY_CLIPBOARD 2006-07-27. Disabled
                    //to avoid possible out-of-memory exception during this
                    //function.
                    // Clipboard.SetDataObject(aStr) 'Possible work-around for people
                    // 'where export to Excel does not work: at least put it into
                    // 'the clipboard. However this seemed not to work.

                    Excel.Application EXL = new Excel.Application();

                    if ((EXL != null))
                    {
                        //Dim WSheet As New Excel.Worksheet
                        //WSheet = _
                        //  CType(EXL.Workbooks.Add.Worksheets.Add, Excel.Worksheet)

                        //Need to raise an exception here.
                        Excel.Worksheet WSheet = new Excel.Worksheet();

                        //Changed PM_CSHARP 2008-03-13
                        //(Excel.Worksheet)
                        //WSheet = EXL.Workbooks.Add.Worksheets.Add();
                        //To use the paste function it has to go through the clipboard.
                        //EXL.Workbooks.Add(null).Worksheets.Add();
                        object sheet = 
                            EXL.Workbooks.Add(null).Worksheets.Add(null, null, null, null);
                        WSheet = (Excel.Worksheet) sheet;



                        //Changed PM_REFACTOR 2007-11-30
                        //Why do we need to have the whole "path" after we moved
                        //this file to MSQlib1??? We do have a reference
                        //to System.Windows.Forms in the project.
                        System.Windows.Forms.Clipboard.SetDataObject(aStr);

                        WSheet.Paste(null,null);
                        
                        EXL.Visible = true;
                    }
                }
                catch (Exception exceptionObject)
                {
                    string msgStr = 
                        "An error happend while trying to export to Excel. " + 
                        "On some systems it does not work (we have not yet found the reason). " + 
                        "The work-around is to use the corresponding 'Save ...' menu " + 
                        "command instead of the 'Export' menu command.";
                    System.Windows.Forms.MessageBox.Show(msgStr);
                } //Exception handler.
            } //Non-empty string
        } //pushToExcel()


        //****************************************************************************
        //* <placeholder for header>                                                 *
        //****************************************************************************
        public static string emptyForNothing(ref string anInStr)
        {
            string toReturn = anInStr;
            if (anInStr == null)
            {
                toReturn = "";
            }
            return toReturn;
        } //emptyForNothing


        //Moved to MSQuantCommon2.vb
        // //Move somewhere else
        // //****************************************************************************
        // //* <placeholder for header>                                                 *
        // //****************************************************************************
        // //* Real type for anInsignalToUse is signalStructure. *
        // public static string dumpXpointsInOneLine(ref ArrayList anInsignalToUse)
        // {
        //     StringBuilder strSB = new StringBuilder(1024);
        //     int lastIndex = anInsignalToUse.Count - 1;
        //     int index = 0;
        //     foreach (signalStructure someSignal in anInsignalToUse)
        //     {
        //         double curX = someSignal.Xsig;
        // 
        //         strSB.Append(someSignal.Xsig);
        // 
        //         if (index != lastIndex)
        //         {
        //             strSB.Append("\t");
        //         }
        //         index += 1;
        //     }
        //     return strSB.ToString();
        // } //dumpXpointsInOneLine


        //Moved to MSQuantCommon2.vb
        ////Move somewhere else
        // //****************************************************************************
        // //* <placeholder for header>                                                 *
        // //****************************************************************************
        // //* Real type for anInsignalToUse is signalStructure. *
        // public static string dumpXYSpectrumPoints(
        //     ref List<massSpectrometryBase.signalStructure> anInsignalToUse,
        //     string aDataTitle,
        //     ref string anInAAseq,
        //     ref string anInAccNum,
        //     ref string anInDatatypeStr,
        //     bool anInInsertZeros)
        // {
        //
        //     //Old:
        //     // ByRef anInsignalToUse As ArrayList, _
        //
        //     string toReturn = "";
        //
        //     StringBuilder strSB = new StringBuilder(30000);
        //     strSB.Append(anInDatatypeStr);
        //     strSB.Append(" for peptide ");
        //     strSB.Append(anInAAseq);
        //     strSB.Append(" in ");
        //     strSB.Append(anInAccNum);
        //     strSB.Append("\r\n");
        //
        //     string header = aDataTitle;
        //     strSB.Append("Spectrum header: ");
        //     strSB.Append(header);
        //     strSB.Append("\r\n");
        //     strSB.Append("\r\n");
        //
        //     strSB.Append("X");
        //     strSB.Append("\t");
        //     strSB.Append("Y");
        //     strSB.Append("\r\n");
        //
        //     //In order to get an appropriate display with tools that draw
        //     //lines between the datapoints we will insert some datapoints
        //     //with an intensity of 0.
        //
        //     //First iteration: find lowest datapoint separation. This will
        //     //be used as a default if no other value can found (e.g. only
        //     //one datapoint in a mass range.
        //     double prevX = -100;
        //     double globalSmallestXdistance = 10;
        //     foreach (signalStructure someSignal in anInsignalToUse)
        //     {
        //         double curX = someSignal.Xsig;
        //         double distance = curX - prevX;
        //         if (distance < globalSmallestXdistance)
        //         {
        //             globalSmallestXdistance = distance;
        //         }
        //         prevX = curX;
        //     }
        //
        //     //Second iteration: find typical data point separation.
        //     //This may depend on the mass.
        //
        //     //Changed PM_TYPESAFE 2008-03-13
        //     //ArrayList dataDistance = new ArrayList();
        //     int cap = 10; //Can we find .
        //     List<datapointSeparationInfoStructure> dataDistance2 =
        //       new List<datapointSeparationInfoStructure>(cap);
        //
        //     double rangeSize = 100.0;
        //     //100 Da
        //     double halfRangeSize = rangeSize / 2;
        //     double curStartX = -100.0;
        //     double curEndX = -100.0;
        //
        //     double currentSmallestXdistance = 1000.0;
        //
        //     prevX = -100;
        //     int signals = anInsignalToUse.Count;
        //     int count = 0;
        //     foreach (signalStructure someSignal in anInsignalToUse)
        //     {
        //         count += 1;
        //         double curX = someSignal.Xsig;
        //         double distance = curX - prevX;
        //
        //         if (curStartX < 0)
        //         {
        //             curStartX = curX;
        //             curEndX = curStartX + rangeSize;
        //         }
        //
        //         //This means we may use a distance to a datapoint into
        //         //the next mass segment.
        //         if (distance < currentSmallestXdistance)
        //         {
        //             currentSmallestXdistance = distance;
        //         }
        //
        //         bool lastSignal = count == signals;
        //         if (lastSignal)
        //         {
        //             int peter7 = 7;
        //         }
        //
        //         if (curX > curEndX | lastSignal)
        //         {
        //             //Last part is
        //             //to get the very last segment.
        //             if (lastSignal)
        //             {
        //                 currentSmallestXdistance = -10.0;
        //                 curEndX = curX + 5.0;
        //             }
        //
        //             if (currentSmallestXdistance > halfRangeSize)
        //             {
        //                 //No datapoints or only one or a few in range. Continue/extend range.
        //                 curEndX = curEndX + rangeSize;
        //             }
        //             else
        //             {
        //                 datapointSeparationInfoStructure datapointSeparationInfo;
        //                 datapointSeparationInfo.datapointSeparation = currentSmallestXdistance;
        //                 datapointSeparationInfo.startX = curStartX;
        //                 datapointSeparationInfo.endX = curEndX;
        //                 dataDistance2.Add(datapointSeparationInfo);
        //
        //                 currentSmallestXdistance = 1000.0;
        //                 //Reset for next mass range.
        //                 curStartX = curEndX;
        //                 curEndX += rangeSize;
        //             }
        //         }
        //         prevX = curX;
        //     }
        //
        //     int curSegmentIndex = 0;
        //     datapointSeparationInfoStructure curSegment;
        //     curSegment.endX = -10;
        //     curSegment.datapointSeparation = -44444.444;
        //
        //
        //     //So we will get started correct.
        //     prevX = -10.0;
        //     foreach (signalStructure someSignal in anInsignalToUse)
        //     {
        //         double curX = someSignal.Xsig;
        //         if (prevX < 0)
        //         {
        //             prevX = curX;
        //             //So we don't anything inserted to the
        //             // left of the first point...
        //         }
        //         double distance = curX - prevX;
        //
        //         if (curX >= curSegment.endX)
        //         {
        //             //Update to new segment
        //             while (
        //                 curX >=
        //                 dataDistance2[curSegmentIndex].endX)
        //             {
        //                 curSegmentIndex += 1;
        //             }
        //
        //             curSegment = dataDistance2[curSegmentIndex];
        //         }
        //
        //         //If the distance to the previous datapoint is too big then
        //         //we insert 2 datapoints with an intensity of zero, one a little
        //         //bit higher close to the previous data point and one
        //         //a little bit lower than the current datapoint.
        //
        //         double sep = curSegment.datapointSeparation;
        //
        //         Trace.Assert( sep >= -40000.0,
        //           "PIL ASSERT. curSegment.datapointSeparation is undefined: " +
        //           sep.ToString());
        //
        //         if (anInInsertZeros && distance > sep * 1.5)
        //         {
        //             double closeToPointDistance = 0.25 * curSegment.datapointSeparation;
        //
        //             strSB.Append(prevX + closeToPointDistance);
        //             strSB.Append("\t");
        //             strSB.Append(0);
        //             strSB.Append("\r\n");
        //
        //             strSB.Append(curX - closeToPointDistance);
        //             strSB.Append("\t");
        //             strSB.Append(0);
        //             strSB.Append("\r\n");
        //         }
        //
        //         strSB.Append(someSignal.Xsig.ToString("0.00000"));
        //         strSB.Append("\t");
        //         strSB.Append(someSignal.Ysig.ToString("0.00000"));
        //
        //         strSB.Append("\r\n");
        //
        //         prevX = curX;
        //     }
        //     toReturn = strSB.ToString();
        //
        //     return toReturn;
        // } //dumpXYSpectrumPoints


        //****************************************************************************
        //* <placeholder for header>                                                 *
        //****************************************************************************
        public static bool pushToUserSelectedFile2(
            string aStr,
            ref System.Windows.Forms.SaveFileDialog anInSaveFileDialog,
            //Note: System.Windows.Forms must be added as a reference to the project.

            ref string anOutInfoLabelTextRef,
            string aOKStatusStr,
            string aStartFileName)
        {
            bool wasSaved = false;

            int len = aStr.Length;
            if (len > 0)
            {
                //Ignore empty strings...
                try
                {
                    anInSaveFileDialog.AddExtension = true;
                    anInSaveFileDialog.Filter = "Plain Text (*.txt)|*.txt";
                    if (aStartFileName != "")
                    {
                        anInSaveFileDialog.FileName = aStartFileName;
                    }
                    if (anInSaveFileDialog.ShowDialog() == 
                        System.Windows.Forms.DialogResult.OK)
                    {
                        //Why isn't the file extension added??????????

                        PILInputOutput.pushToFile(anInSaveFileDialog.FileName, aStr);

                        wasSaved = true;

                        //lblProtInfo.Text = aOKStatusStr
                        anOutInfoLabelTextRef = aOKStatusStr;
                    }
                }
                catch (Exception exceptionObject)
                {
                    string errStr =
                        "Could not save to file " +
                        anInSaveFileDialog.FileName +
                        ". (" +
                        exceptionObject.ToString() +
                        ").";
                    //Interaction.MsgBox(errStr);
                    System.Windows.Forms.MessageBox.Show(errStr);

                    //lblProtInfo.Text = errStr
                    anOutInfoLabelTextRef = errStr;
                }
            } //Non-empty string

            return wasSaved;
        } //pushToUserSelectedFile2()


        //Changed PM_MB3_RELOCATION 2008-05-23
        //****************************************************************************
        //* <placeholder for header>                                                 *
        //****************************************************************************
        public static string newFolderBase(
            ref string anOldLocFile, ref string aNewFolderBase)
        {
            string fileName = Path.GetFileName(anOldLocFile);

            //Replace with new folder base.
            //string sep = ;
            string toReturn =
                aNewFolderBase + Path.DirectorySeparatorChar.ToString() + fileName;
            
            return toReturn;
        } //newFolderBase


    } //class PILInputOutput




} //SDUPutility

    

    

Generated by script codePublish.pl at 2008-09-23T11:59:18.