public void ExecuteArbitraryPythonDirect()
 {
     PythonProcessManager.Verbose = true;
     PythonProcessManager proc = new PythonProcessManager("python", " -c print('Hello')");
     Console.WriteLine("PYTHONPATH = {0}", proc.PythonPath);
     proc.Start();
     Console.WriteLine("Running");
     proc.WaitForExit();
 }
        public String RunReclassifyWithModelTask(String targetFilePath, String modelFilePath, String method)
        {
            String argumentStringTemplate = " reclassify-with-model --target-file {0} --model-file {1} --method {2}";
            String arguments = String.Format(argumentStringTemplate, targetFilePath.QuoteWrap(), modelFilePath.QuoteWrap(), method.QuoteWrap());
            PythonProcessManager proc = new PythonProcessManager(PythonEntryPoint, arguments);

            proc.Start();
            proc.WaitForExit();
            if (!proc.CheckExitSuccessfully())
            {
                throw new PythonScriptErrorException(
                    proc.GenerateDumpMessage()
                    );
            }

            //Sniff the StdOut stream of the pipeline ProcessManager to pull out
            //the output file path. It should be the last line emitted.
            String outputFile = null;
            try
            {
                outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
            }
            catch (InvalidOperationException)
            {
                try
                {
                    Thread.Sleep(100);
                    outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
                }
                catch (InvalidOperationException ex)
                {
                    throw new PythonScriptErrorException("Could not find output file in " + proc.Out);
                }
            }

            //Store this ProcessManager instance for inspection later
            this.LastCall = proc;
            return outputFile;
        }
        public String RunModelBuildingPythonPipeline(String ms1MatchFilePath,
            String glycosylationSitesFilePath, String ms2DeconvolutionFilePath,
            double ms1MatchingTolerance, double ms2MatchingTolerance,
            String proteinProspectorXMLFilePath, String method, int numProcesses=2)
        {
            String argumentStringTemplate = " --n {7} build-model --ms1-results-file {0} --glycosylation-sites-file {1} --deconvoluted-spectra-file {2} --ms1-match-tolerance {3} --ms2-match-tolerance {4}" +
                " --protein_prospector_xml {5} --method {6}";

            String arguments = String.Format(argumentStringTemplate, new object[]{
               ms1MatchFilePath.QuoteWrap(),
               glycosylationSitesFilePath.QuoteWrap(),
               ms2DeconvolutionFilePath.QuoteWrap(), ms1MatchingTolerance,
               ms2MatchingTolerance, proteinProspectorXMLFilePath.QuoteWrap(),
               method.QuoteWrap(), numProcesses
            });

            Console.WriteLine(arguments);

            PythonProcessManager proc = new PythonProcessManager(PythonEntryPoint, arguments);

            proc.Start();
            proc.WaitForExit();
            if (!proc.CheckExitSuccessfully())
            {
                throw new PythonScriptErrorException(
                    proc.GenerateDumpMessage()
                    );
            }

            //Sniff the StdOut stream of the pipeline ProcessManager to pull out
            //the output file path. It should be the last line emitted.
            String outputFile = null;
            try
            {
                outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
            }
            catch (InvalidOperationException)
            {
                try
                {
                    Thread.Sleep(100);
                    outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
                }
                catch (InvalidOperationException ex)
                {
                    throw new PythonScriptErrorException("Could not find output file in " + proc.Out, new PythonScriptErrorException(
                    proc.GenerateDumpMessage()));
                }
            }

            //Store this ProcessManager instance for inspection later
            this.LastCall = proc;
            return outputFile;
        }
        //public void CheckPythonDependencies()
        //{
        //}
        /// <summary>
        /// Executes the Python script pipeline stored at @ScriptRoot, with the 
        /// entry_point.py entry-point. The pipeline emits intermediary file names,
        /// and the last line printed on a successful run is the final output 
        /// file name.
        /// 
        /// Will check the exit code of the pipeline, and throw a PythonScriptErrorException
        /// if it is not 0.
        /// </summary>
        /// <returns>String path to the output file generated by the pipeline</returns>
        public String RunClassificationPythonPipeline(String ms1MatchFilePath, 
            String glycosylationSitesFilePath, String ms2DeconvolutionFilePath,
            String modelDataFilePath, 
            double ms1MatchingTolerance, double ms2MatchingTolerance,
            String proteinProspectorXMLFilePath,
            String method, String outputFilePath = null,
            int numProcesses = 2,
            int decoyToRealRatio = 20,
            bool randomOnly = false
            )
        {
            String argumentStringTemplate = " --n {9}  classify-with-model --ms1-results-file {0} --glycosylation-sites-file {1} --deconvoluted-spectra-file {2} --model-file {3} --ms1-match-tolerance {4} --ms2-match-tolerance {5} --method {6} --protein_prospector_xml {7} {8} --decoy-to-real-ratio {10} {11}";

            String arguments = String.Format(argumentStringTemplate, new object[]{
               ms1MatchFilePath.QuoteWrap(),
               glycosylationSitesFilePath.QuoteWrap(),
               ms2DeconvolutionFilePath.QuoteWrap(),
               modelDataFilePath.QuoteWrap(),
               ms1MatchingTolerance,
               ms2MatchingTolerance,
               method.QuoteWrap(),
               proteinProspectorXMLFilePath.QuoteWrap(),
               outputFilePath == null ? "" : "--out " + outputFilePath.QuoteWrap(),
               numProcesses,
               decoyToRealRatio,
               randomOnly ? " --random-only " : ""
            });

            PythonProcessManager proc = new PythonProcessManager(PythonEntryPoint, arguments);

            proc.Start();
            proc.WaitForExit();
            if (!proc.CheckExitSuccessfully())
            {
                throw new PythonScriptErrorException(
                    proc.GenerateDumpMessage()
                    );
            }

            //Sniff the StdOut stream of the pipeline ProcessManager to pull out
            //the output file path. It should be the last line emitted.
            String outputFile = null;
            try
            {
                outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
            }
            catch (InvalidOperationException)
            {
                try
                {
                    Thread.Sleep(100);
                    outputFile = proc.Out.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
                }
                catch (InvalidOperationException ex)
                {
                    throw new PythonScriptErrorException("Could not find output file in " + proc.Out);
                }
            }

            this.PythonPipelineOutFile = outputFile;
            //Store this ProcessManager instance for inspection later
            this.LastCall = proc;

            return outputFile;
        }
 public void InstallPythonDependencies()
 {
     PythonProcessManager proc = new PythonProcessManager(this.PythonExecutablePath, "-m pip -h");
     proc.Start();
     proc.WaitForExit();
     if (!proc.CheckExitSuccessfully())
     {
         throw new PythonDependencyInstallerException("The Python Package Manager pip wasn't found. Is it installed? If not, go to https://pip.readthedocs.org/en/latest/installing.html for more information on how to get it. " + proc.GenerateDumpMessage());
     }
     proc = new PythonProcessManager(PythonEntryPoint, " -h");
     proc.Start();
     proc.WaitForExit();
     Console.WriteLine(proc.ExitCode);
     if (!proc.CheckExitSuccessfully())
     {
         throw new PythonScriptErrorException(proc.GenerateDumpMessage());
     }
 }
        public String ConvertToCSV(String targetFilePath, String resultFile=null)
        {
            resultFile = resultFile == null ?
                Path.Combine(Path.GetDirectoryName(targetFilePath), Path.GetFileNameWithoutExtension(targetFilePath) + ".csv") : resultFile;

            String arguments = String.Format(
            " -c \"from glycresoft_ms2_classification import prediction_tools;data=prediction_tools.prepare_model_file(r'{0}');data.to_csv(r'{1}')\"",
            targetFilePath, resultFile);
            Console.WriteLine(arguments);
            PythonProcessManager proc = new PythonProcessManager(PythonExecutablePath, arguments);

            proc.Start();
            proc.WaitForExit();
            if (!proc.CheckExitSuccessfully())
            {
                throw new PythonScriptErrorException(
                    proc.GenerateDumpMessage()
                    );
            }

            return resultFile;
        }