lib/ms/ident/pepxml.rb in ms-ident-0.0.3 vs lib/ms/ident/pepxml.rb in ms-ident-0.0.17

- old
+ new

@@ -41,22 +41,49 @@ xml_stylesheet = Nokogiri::XML::ProcessingInstruction.new(doc, "xml-stylesheet", %Q{type="text/xsl" href="#{location}"}) doc.root.add_previous_sibling xml_stylesheet doc end - # writes xml file named msms_pipeline_analysis.summary_xml into the msms_run_summary.base_name directory - def to_xml_file - to_xml(File.dirname(msms_pipeline_analysis.msms_run_summary.base_name) + '/' + msms_pipeline_analysis.summary_xml) - end + # if no options are given, an xml string is returned. If either :outdir or + # :outfile is given, the xml is written to file and the output filename is returned. + # A single string argument will be interpreted as :outfile if it ends in + # '.xml' and the :outdir otherwise. In this case, update_summary_xml is still true + # + # options: + # + # arg default + # :outdir => nil write to disk using this outdir with summary_xml basename + # :outfile => nil write to this filename (overrides outdir) + # :update_summary_xml => true update summary_xml attribute to point to the output file true/false + # + # set outdir to + # File.dirname(pepxml_obj.msms_pipeline_analysis.msms_run_summary.base_name) + # to write to the same directory as the input search file. + def to_xml(opts={}) + opts ||= {} + if opts.is_a?(String) + opts = ( opts.match(/\.xml$/) ? {:outfile => opts} : {:outdir => opts } ) + end + opt = {:update_summary_xml => true, :outdir => nil, :outfile => nil}.merge(opts) - # if no outfile is given, an xml string is returned. summary_xml should - # have already been set and is not influenced by the outfile given here. - def to_xml(outfile=nil) + if opt[:outfile] + outfile = opt[:outfile] + elsif opt[:outdir] + outfile = File.join(opt[:outdir], msms_pipeline_analysis.summary_xml.split(/[\/\\]/).last) + end + self.msms_pipeline_analysis.summary_xml = File.expand_path(outfile) if (opt[:update_summary_xml] && outfile) + builder = Nokogiri::XML::Builder.new(:encoding => XML_ENCODING) msms_pipeline_analysis.to_xml(builder) add_stylesheet(builder.doc, Ms::Ident::Pepxml::XML_STYLESHEET_LOCATION) string = builder.doc.to_xml - outfile ? File.open(outfile,'w') {|out| out.print(string) } : string + + if outfile + File.open(outfile,'w') {|out| out.print(string) } + outfile + else + string + end end end