From: <mg...@us...> - 2006-09-14 15:33:55
|
Revision: 127 http://svn.sourceforge.net/obo/?rev=127&view=rev Author: mgibson Date: 2006-09-14 08:33:40 -0700 (Thu, 14 Sep 2006) Log Message: ----------- can now read files from command line with phenote -f psfile.psx will figure data adapter from suffix .psx & .syn -> pheno syntax .xml -> pheno xml .pxml? working on writeback from cmd line Modified Paths: -------------- phenote/trunk/build.xml phenote/trunk/doc/todo phenote/trunk/src/java/phenote/dataadapter/DataAdapterI.java phenote/trunk/src/java/phenote/dataadapter/phenosyntax/PhenoSyntaxFileAdapter.java phenote/trunk/src/java/phenote/dataadapter/phenoxml/PhenoXmlAdapter.java phenote/trunk/src/java/phenote/main/Phenote.java Added Paths: ----------- phenote/trunk/jars/te-common.jar phenote/trunk/src/java/phenote/main/CommandLine.java Modified: phenote/trunk/build.xml =================================================================== --- phenote/trunk/build.xml 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/build.xml 2006-09-14 15:33:40 UTC (rev 127) @@ -144,6 +144,7 @@ <java classname="phenote.main.Phenote" fork="yes" jvmargs="-Xmx120M"> <!-- arg value="-c configfilehereventually"/ --> <arg value="-c"/> + <!-- from command line with -DCONF-FILE=initial-zfin.cfg --> <arg value="${CONF_FILE}"/> <classpath> <pathelement path="${classfiles}"/> Modified: phenote/trunk/doc/todo =================================================================== --- phenote/trunk/doc/todo 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/doc/todo 2006-09-14 15:33:40 UTC (rev 127) @@ -1,3 +1,11 @@ +on load keeps blank character (from before load) around in table - shouldnt + +auto load from sourcforge or bioportal + +post compose gui +post compose syntax +post compose pheno xml + column sorting - do shift for descending, also if new added sort it in, and perhaps indicate sorted column in header with < & >? Added: phenote/trunk/jars/te-common.jar =================================================================== (Binary files differ) Property changes on: phenote/trunk/jars/te-common.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: phenote/trunk/src/java/phenote/dataadapter/DataAdapterI.java =================================================================== --- phenote/trunk/src/java/phenote/dataadapter/DataAdapterI.java 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/src/java/phenote/dataadapter/DataAdapterI.java 2006-09-14 15:33:40 UTC (rev 127) @@ -10,4 +10,11 @@ public void commit(CharacterListI charList); + /** Set value to use for loading or writeback, for a file adapter this would be + the file name - is there a better name for this method? + For now just doing String - which may be sufficient - may need an + AdapterValue/DataInput object if this gets more involved - but that may + not be necasary - certianly string ok for now */ + public void setAdapterValue(String adapterValue); + } Modified: phenote/trunk/src/java/phenote/dataadapter/phenosyntax/PhenoSyntaxFileAdapter.java =================================================================== --- phenote/trunk/src/java/phenote/dataadapter/phenosyntax/PhenoSyntaxFileAdapter.java 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/src/java/phenote/dataadapter/phenosyntax/PhenoSyntaxFileAdapter.java 2006-09-14 15:33:40 UTC (rev 127) @@ -23,12 +23,19 @@ public class PhenoSyntaxFileAdapter implements DataAdapterI { private File previousFile; + private File file; + /** command line setting of file */ + public void setAdapterValue(String filename) { + file = new File(filename); + } + /** this should return CharacterList and caller should load CharListMan or CLM makes the call itself? */ public void load() { - File file = getFileFromUser(previousFile); + if (file == null) + file = getFileFromUser(previousFile); if (file == null) return; previousFile = file; @@ -51,6 +58,7 @@ catch (IOException e) { System.out.println("PhenoSyntax read failure "+e); } + file = null; // null it for next load/commit } /** returns null if user fails to pick a file */ @@ -60,7 +68,8 @@ public void commit(CharacterListI charList) { - File file = getFileFromUser(previousFile); + if (file == null) + file = getFileFromUser(previousFile); if (file == null) return; previousFile = file; @@ -85,6 +94,7 @@ } } pw.close(); + file = null; } } Modified: phenote/trunk/src/java/phenote/dataadapter/phenoxml/PhenoXmlAdapter.java =================================================================== --- phenote/trunk/src/java/phenote/dataadapter/phenoxml/PhenoXmlAdapter.java 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/src/java/phenote/dataadapter/phenoxml/PhenoXmlAdapter.java 2006-09-14 15:33:40 UTC (rev 127) @@ -29,13 +29,20 @@ private Set genotypesAlreadyAdded = new HashSet<String>(); private File previousFile; + private File file; + /** command line setting of file */ + public void setAdapterValue(String filename) { + file = new File(filename); + } + public void load() {} public void commit(CharacterListI charList) { - File file = getFileFromUser(previousFile); if (file == null) + file = getFileFromUser(previousFile); + if (file == null) return; previousFile = file; Added: phenote/trunk/src/java/phenote/main/CommandLine.java =================================================================== --- phenote/trunk/src/java/phenote/main/CommandLine.java (rev 0) +++ phenote/trunk/src/java/phenote/main/CommandLine.java 2006-09-14 15:33:40 UTC (rev 127) @@ -0,0 +1,310 @@ +package phenote.main; + +import com.townleyenterprises.command.CommandOption; +import com.townleyenterprises.command.CommandParser; +import com.townleyenterprises.command.DefaultCommandListener; + +import phenote.dataadapter.DataAdapterI; +import phenote.dataadapter.phenosyntax.PhenoSyntaxFileAdapter; +import phenote.dataadapter.phenoxml.PhenoXmlAdapter; + +/** a lot of this is copied from apollo.main.CommandLine - theres potential for + some generic super class but where would it go? in a jar file with one class? + org.bdgp? */ +public class CommandLine { + + /** the class that parses all the options */ + private CommandParser parser; + /** stores adapter class, filenames, etc. for reading data */ + private IOOptions readOptions; + /** stores adapter class, filenames, etc. for writing data */ + private IOOptions writeOptions; + private CommandOption inputFile = new InputFileCommandOption(); + //private CommandOption inputFmtOption = new InputFormatCommandOption(); + private CommandOption writeFile = new WriteFileCommandOption(); + + CommandOption[] options = new CommandOption[] { inputFile, writeFile }; + + // this guarantees that we get the right classes (compile time check) + private final static String PHENOXML = PhenoXmlAdapter.class.getName(); + private final static String PHENOSYNTAX = PhenoSyntaxFileAdapter.class.getName(); + + // there can be only one instance of this class (per JVM) + private static CommandLine commandLineSingleton; + + public static CommandLine inst() { + if (commandLineSingleton == null) + commandLineSingleton = new CommandLine(); + return commandLineSingleton; + } + + /** + * Set command-line arguments passed along from main(). + * + * @param args argStrings from command line + */ + public void setArgs(String[] args) throws Exception { + if (args.length == 0) + return; + if (parser != null) { + System.out.println("CommandLine: WARNING multiple calls to setArgs(), ignoring"); + return; // ? + } + parser = new CommandParser("Phenote"); // help text? + parser.addCommandListener(new DefaultCommandListener("Options",options)); + parser.parse(args); + //addConstraints(parser); + // does execute() on CommandOptions, throws generic Exception + //try { // should i wrap the exception like apollo? + parser.executeCommands(); + //} catch (Exception e) { throw new ApolloAdapterException(e); } + } + + /** + * If input & output (or batch) is fully specified, then we are in command line + * mode; no need for GUI. + */ + public static boolean isInCommandLineMode() { + return inst().writeIsSpecified(); //|| inst().isBatchMode(); + } + + /** if read has been (correctly) specified, read adapter is non null */ + boolean readIsSpecified() { + return getReadOptions().getSpecifiedState(); + } + + boolean writeIsSpecified() { + return getWriteOptions().getSpecifiedState(); + } + + private IOOptions getReadOptions() { + if (readOptions == null) + readOptions = new IOOptions(true); + return readOptions; + } + + private IOOptions getWriteOptions() { + if (writeOptions == null) + writeOptions = new IOOptions(false); + return writeOptions; + } + + /** + * If input/read was specified on the command line this returns the + * correctly-initialized read data adapter for it. Note that + * adapter returned may be the same object as that returned by + * <code>getWriteAdapter()</code> + * @return null if no read adapter specified on command line. + */ + DataAdapterI getReadAdapter() throws Exception { + IOOptions ioo = getReadOptions(); + DataAdapterI adapter = ioo.getAdapter(); + adapter.setAdapterValue(ioo.getAdapterValue()); + return adapter; + } + + + /** INPUT FILE COMMAND OPTION */ + private class InputFileCommandOption extends CommandOption { + private final static String help = + "Specify filename to read in (phenoxml.xml,phenosyntax.syn)"; + private InputFileCommandOption() { + // true -> has argument + super("inputFile",'f',true,"filename",help); + } + public void execute() throws Exception { + //System.out.println("executing input file command option "+getArg()); + getReadOptions().setFilename(getArg()); + setAdapterForFile(getReadOptions(), false); + } + } + + /** + * OUTPUT FILE COMMAND OPTION Specifies name of target/output file. + */ + private class WriteFileCommandOption extends CommandOption { + private WriteFileCommandOption() { + super("writeFile",'w',true,"filename","Filename to write to"); + } + public void execute() throws Exception { + getWriteOptions().setFilename(getArg()); + setAdapterForFile(getWriteOptions(), false); // sets data adapter from file suffix + } + } + + + /** + * Read & write file method. Retrieves data adapter either from format option or + * suffix and sets its DataInput with filename + */ + private void setAdapterForFile(IOOptions options, boolean setAdapterInput) + throws Exception + { + DataAdapterI adapter = getAdapterForFile(options); + if (!options.hasAdapter()) + options.setAdapter(adapter); + // is it scandalous to use DataInput for output?? should be called DataInfo? + // of DataSpecifitation? + String inputFile = options.getFilename(); + //DataInputType inputType = DataInputType.FILE; + //if (inputFile.startsWith("http:") || inputFile.startsWith("file:")) { + //inputType = DataInputType.URL; } + //DataInput di = new DataInput(inputType, inputFile); + // gff input may have seq file. + //if (haveSequenceFilename()) di.setSequenceFilename(getSequenceFilename()); + //options.setDataInput(di); + adapter.setAdapterValue(inputFile); + options.setSpecifiedState(true); + //if (setAdapterInput) adapter.setDataInput(di); + } + + private DataAdapterI getAdapterForFile(IOOptions options) throws Exception { + //DataAdapterI adapter; + // first see if input format explicitly specified + if (options.hasAdapter()) + return options.getAdapter(); + + // if nothing specified try to get format from file suffix + try { return getDataAdapterFromSuffix(options.getFilename()); } + catch (AdapterEx e) { + throw new AdapterEx("No input format specified and "+e.getMessage()); + } + + //return adapter; + } + + private DataAdapterI getDataAdapterFromSuffix(String filename) throws AdapterEx { + String suffix = getFileSuffix(filename); + // anything with game in it?? + if (suffix.matches(".*syn.*|psx")) // ??? + return getDataAdapter(PHENOSYNTAX); + if (suffix.matches(".*xml.*")) + return getDataAdapter(PHENOXML); + // configuration can specify what to do with .xml suffix (game or chado) + //else if (suffix.equals("xml") && xmlSuffixIsConfigged()) + // return getConfiggedXmlDataAdapter(); + throw new AdapterEx("Suffix "+suffix+" doesnt map to known format"); + } + + private class AdapterEx extends Exception { + private AdapterEx(String m) { super(m); } + private AdapterEx(Exception e) { super(e.getMessage()); } // save e? + } + + /** should this include the '.'? probably not. changing it to not include . */ + private String getFileSuffix(String filename) { + int index = filename.lastIndexOf('.'); + return filename.substring(index+1); // 1 past . + } + + private DataAdapterI getDataAdapter(String classString) throws AdapterEx { + try { + // introspection? or switch on string? + Class c = Class.forName(classString); + Object o = c.newInstance(); + if (!(o instanceof DataAdapterI)) // shouldnt happen + System.out.println("Class string is not data adapter "+o); + return (DataAdapterI)o; + } catch (Exception e) { throw new AdapterEx(e); } + } + + // eventually - multiple xmls? +// private boolean xmlSuffixIsConfigged() { +// return Config.commandLineXmlFileFormatIsConfigged(); +// } + +// private ApolloDataAdapterI getConfiggedXmlDataAdapter() { +// String config = Config.getCommandLineXmlFileFormat(); +// if (config.equalsIgnoreCase("game")) +// return getDataAdapter(GAME); +// if (config.equalsIgnoreCase("chado")) +// return getDataAdapter(CHADOXML); +// return null; +// } + + void printHelp() { + parser.help(); + } + + private void error(String m) throws Exception { + System.out.println(m); + printHelp(); + throw new Exception(m); + } + + + // ----------------------------------------------------------------------- + // IOOptions inner class + // ----------------------------------------------------------------------- + + /** + * IOOptions holds state for either read or write + */ + private class IOOptions { + private DataAdapterI adapter; + private String filename; + private boolean specified = false; + private boolean isRead; + private String adapterValue; + + + // ---------------------------------------------- + // Constructor + // ---------------------------------------------- + + private IOOptions(boolean isRead) { + this.isRead = isRead; + } + + // ---------------------------------------------- + // CommandLine - simple getters/setters + // ---------------------------------------------- + + private String getAdapterValue() { return adapterValue; } + + private void setFilename(String filename) { + this.filename = filename; + adapterValue = filename; + } + private String getFilename() { return filename; } + + private void setSpecifiedState(boolean specified) { this.specified = specified; } + private boolean getSpecifiedState() { return specified; } + + // ---------------------------------------------- + // CommandLine + // ---------------------------------------------- + + private void setAdapter(DataAdapterI adapter) { + this.adapter = adapter; + } + private DataAdapterI getAdapter() throws Exception { + if (adapter == null) { + //System.out.println("dont have adapter"); + // if not set from file extension get from format option +// if (isRead) { +// adapter = inputFmtOption.getAdapter(); +// } else { +// adapter = outputFmtOption.getAdapter(); +// } + } + return adapter; + } + private boolean hasAdapter() throws Exception { return getAdapter() != null; } + + /** type of input - type only needed for input */ + //private void setInputType(String inputTypeString) { + // actually i think this is adapter dependent - for game want to do + // stringToType - for chado its a so type + //try {inputType = DataInputType.stringToType(inputTypeString); } + //catch (UnknownTypeException e) { + // System.out.println(e.getMessage()+" Can not set input type"); } + //} + //private DataInputType getInputType() { return inputType; } + //private boolean haveInputType() { return inputType != null; } + + //private void setDataInput(DataInput dataInput) { this.dataInput = dataInput; } + //private DataInput getDataInput() { return this.dataInput; } + + } +} Modified: phenote/trunk/src/java/phenote/main/Phenote.java =================================================================== --- phenote/trunk/src/java/phenote/main/Phenote.java 2006-09-13 14:31:06 UTC (rev 126) +++ phenote/trunk/src/java/phenote/main/Phenote.java 2006-09-14 15:33:40 UTC (rev 127) @@ -35,6 +35,7 @@ private TermPanel termPanel; private static Phenote phenote; private TermInfo termInfo; + private CommandLine commandLine = CommandLine.inst(); public static void main(String[] args) { @@ -46,35 +47,50 @@ catch (UnsupportedLookAndFeelException e) { System.out.println("Failed to set to Java/Metal look & feel"); } - //System.out.println("sys CONFIG prop "+System.getProperty("CONFIG")); phenote = getPhenote(); - phenote.initConfig(args); + //phenote.initConfig(args); + phenote.doCommandLine(args); // does config // put this is in a phenote.util.Log class? - get file from config - default? try { DOMConfigurator.configure(Config.inst().getLogConfigUrl()); } catch (FileNotFoundException e) { LOG.error(e.getMessage()); } phenote.initOntologies(); + phenote.loadFromCommandLine(); // cant load data til ontologies loaded i think + phenote.initGui(); } /** private constructor -> singleton */ private Phenote() {} - /** args is most likely null if not called from command line */ - public void initConfig(String[] args) { - // gets config file from command line & loads - if no config file - // loads default. should actually put that logic here. - doCommandLine(args); // load config file - } +// /** args is most likely null if not called from command line */ +// public void initConfig(String[] args) { +// // gets config file from command line & loads - if no config file +// // loads default. should actually put that logic here. +// doCommandLine(args); // load config file +// } public void initOntologies() { OntologyDataAdapter oda = new OntologyDataAdapter(); // singleton? oda.loadOntologies(); // loads up OntologyManager } + + private void loadFromCommandLine() { + //LOG.debug("read spec "+commandLine.readIsSpecified()); + if (!commandLine.readIsSpecified()) return; + try { commandLine.getReadAdapter().load(); } + catch (Exception e) { LOG.error("Failed to do load via command line "+e); } + } public void initGui() { makeWindow(); } + private void doCommandLine(String[] args) { + doCommandLineOld(args); // -c -i --> move to CommandLine! + try { commandLine.setArgs(args); } // no log yet - sys.out + catch (Exception e) { System.out.println("Command line read failed"+e); } + } + /** for now just looking for '-c configFile.cfg', use command line package if we need to get more sophisticated so if user has personal config file should override this - however maybe @@ -85,7 +101,7 @@ and -c file.cfg will load/overwrite that cfg into .phenote/my-phenote.cfg (if it exists) - we can always add --init later if we need it -c overwrites, -i doesnt -i is for initial startup of phenote */ - private void doCommandLine(String[] args) { + private void doCommandLineOld(String[] args) { String configFile = getConfigFileFromCommandLine(args); // if no config file specified then set default initial config file. this will be // overridden by a personal config file if it exists This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |