44import java .io .IOException ;
55import java .io .OutputStreamWriter ;
66import java .io .UnsupportedEncodingException ;
7+ import java .nio .file .Files ;
8+ import java .nio .file .Paths ;
79import java .text .DecimalFormat ;
8- import java .util .ArrayList ;
9- import java .util .Arrays ;
10- import java .util .Collections ;
11- import java .util .HashMap ;
12- import java .util .HashSet ;
13- import java .util .List ;
14- import java .util .Map ;
15- import java .util .Set ;
10+ import java .time .Duration ;
11+ import java .time .Instant ;
12+ import java .util .*;
1613import java .util .concurrent .atomic .AtomicInteger ;
14+ import java .util .stream .Collectors ;
1715import java .util .stream .IntStream ;
16+ import java .util .stream .Stream ;
1817
1918import org .apache .commons .cli .CommandLine ;
2019import org .apache .commons .cli .CommandLineParser ;
3534import org .openscience .cdk .io .SDFWriter ;
3635import org .openscience .cdk .tools .manipulator .AtomContainerManipulator ;
3736
37+ import static java .util .stream .Collectors .toList ;
38+
3839public class MORGEN {
3940 public int size =0 ;
4041 public int total =0 ;
@@ -51,6 +52,7 @@ public class MORGEN {
5152 public boolean learningFromConnectivity =false ;
5253 public SDFWriter outFile ;
5354 public String formula ;
55+ public String formulaList ;
5456 public boolean outputOption ;
5557 public boolean flag =true ;
5658 public boolean learningFromCanonicalTest =false ;
@@ -1420,14 +1422,14 @@ public int nextCount(int i, int size, Integer[] degrees, ArrayList<Integer> part
14201422 * @throws CloneNotSupportedException
14211423 */
14221424
1423- public List <int [][]> run () throws IOException , CDKException , CloneNotSupportedException {
1424- if (canBuildGraph (formula )) {
1425+ public List <int [][]> run (String localFormula ) throws IOException , CDKException , CloneNotSupportedException {
1426+ if (canBuildGraph (localFormula )) {
14251427 clearGlobals ();
14261428 long startTime = System .nanoTime ();
1427- if (verbose ) System .err .println ("MORGEN is generating isomers of " +formula +"..." );
1428- getSymbolsOccurrences (formula );
1429+ if (verbose ) System .err .println ("MORGEN is generating isomers of " +localFormula +"..." );
1430+ getSymbolsOccurrences (localFormula );
14291431 initialDegrees ();
1430- build (formula );
1432+ build (localFormula );
14311433 outFile = new SDFWriter (new BufferedWriter (new OutputStreamWriter (System .out )));
14321434 structureGenerator ();
14331435 if (verbose ) System .err .println ("The number of structures is: " +count );
@@ -1437,7 +1439,7 @@ public List<int[][]> run() throws IOException, CDKException, CloneNotSupportedEx
14371439 DecimalFormat d = new DecimalFormat (".###" );
14381440 if (verbose ) System .out .println ("Time: " +d .format (seconds )+" seconds" );
14391441 }else {
1440- if (verbose ) System .out .println ("The input formula, " +formula +", does not represent any molecule." );
1442+ if (verbose ) System .out .println ("The input formula, " +localFormula +", does not represent any molecule." );
14411443 }
14421444 return output ;
14431445 }
@@ -2316,31 +2318,46 @@ private void parseArgs(String[] args) throws ParseException, IOException, org.ap
23162318 try {
23172319 CommandLine cmd = parser .parse (options , args );
23182320 this .formula = cmd .getOptionValue ("formula" );
2321+ this .formulaList = cmd .getOptionValue ("formula-list" );
23192322 this .outputOption = cmd .hasOption ("output" );
23202323 if (cmd .hasOption ("verbose" )) this .verbose = true ;
2324+ if (Objects .isNull (formula ) && Objects .isNull (formulaList )) {
2325+ displayHelp (options );
2326+ }
23212327 } catch (ParseException e ) {
2322- HelpFormatter formatter = new HelpFormatter ();
2323- formatter .setOptionComparator (null );
2324- String header = "\n Generates molecular structures for a given molecular formula."
2325- + " The input is a molecular formula string."
2326- + "For example 'C2OH4'."
2327- + "Besides this formula, the directory is needed to be specified for the output"
2328- + "file. \n \n " ;
2329- String footer = "\n Please report issues at https://github.com/MehmetAzizYirik/AlgorithmicGroupTheory" ;
2330- formatter .printHelp ( "java -jar MORGEN.jar" , header , options , footer , true );
2328+ displayHelp (options );
23312329 throw new ParseException ("Problem parsing command line" );
23322330 }
23332331 }
2334-
2335- private Options setupOptions (String [] args ){
2332+
2333+ private void displayHelp (Options options ) {
2334+ HelpFormatter formatter = new HelpFormatter ();
2335+ formatter .setOptionComparator (null );
2336+ String header = "\n Generates molecular structures for a given molecular formula."
2337+ + " The input is a molecular formula string."
2338+ + "For example 'C2OH4'."
2339+ + "Besides this formula, the directory is needed to be specified for the output"
2340+ + "file. \n \n " ;
2341+ String footer = "\n Please report issues at https://github.com/MehmetAzizYirik/AlgorithmicGroupTheory" ;
2342+ formatter .printHelp ( "java -jar MORGEN.jar" , header , options , footer , true );
2343+ }
2344+
2345+ private Options setupOptions (String [] args ){
23362346 Options options = new Options ();
23372347 Option formula = Option .builder ("f" )
2338- .required (true )
2348+ .required (false )
23392349 .hasArg ()
23402350 .longOpt ("formula" )
23412351 .desc ("formula (required)" )
23422352 .build ();
23432353 options .addOption (formula );
2354+ Option formulaList = Option .builder ("fl" )
2355+ .required (false )
2356+ .hasArg ()
2357+ .longOpt ("formula-list" )
2358+ .desc ("file name with formulas" )
2359+ .build ();
2360+ options .addOption (formulaList );
23442361 Option verbose = Option .builder ("v" )
23452362 .required (false )
23462363 .longOpt ("verbose" )
@@ -2361,9 +2378,49 @@ public static void main(String[] args) throws IOException, CDKException, CloneNo
23612378 //String[] arg= {"-f", "C6H6", "-v", "-d", "C:\\Users\\mehme\\Desktop\\"};
23622379 try {
23632380 gen .parseArgs (args );
2364- gen .run ();
2381+ if (Objects .nonNull (gen .formula )) {
2382+ gen .run (gen .formula );
2383+ } else if (Objects .nonNull (gen .formulaList )) {
2384+ class Line {
2385+ String formula ;
2386+ double averageTime ;
2387+ long numberOfStructure ;
2388+ public Line (String formula , double averageTime , long numberOfStructure ) {
2389+ this .formula = formula ;
2390+ this .averageTime = averageTime ;
2391+ this .numberOfStructure = numberOfStructure ;
2392+ }
2393+
2394+ @ Override
2395+ public String toString () {
2396+ return "Line{" +
2397+ "formula='" + formula + '\'' +
2398+ ", averageTime=" + averageTime +
2399+ ", numberOfStructure=" + numberOfStructure +
2400+ '}' ;
2401+ }
2402+ }
2403+ List <Line > lines = Files .lines (Paths .get (gen .formulaList )).map (formula -> {
2404+ double average = IntStream .rangeClosed (1 , 5 ).asLongStream ().map (
2405+ i -> execMorgen (gen , formula )).average ().getAsDouble ();
2406+ return new Line (formula , average , MORGEN .count .get ());
2407+ }).collect (toList ());
2408+ System .err .println (lines );
2409+ }
23652410 } catch (Exception e ) {
23662411 if (gen .verbose ) e .getCause ();
23672412 }
23682413 }
2414+
2415+ private static long execMorgen (MORGEN gen , String formula ) {
2416+ Instant start = Instant .now ();
2417+ MORGEN .count .set (0 );
2418+ try {
2419+ gen .run (formula );
2420+ } catch (Exception e ) {
2421+ e .printStackTrace ();
2422+ }
2423+ Instant finish = Instant .now ();
2424+ return Duration .between (start , finish ).toMillis ();
2425+ }
23692426}
0 commit comments