#!/usr/bin/env python
import json
import argparse
from chado import ChadoAuth, ChadoInstance, Analysis

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Create a new analysis')

    parser.add_argument("--name", required=True, help="Analysis name")
    parser.add_argument("--program", required=True, help="Program name")
    parser.add_argument("--program-version", required=True, help="Program version")
    parser.add_argument("--algorithm", help="Algorithm name")
    parser.add_argument("--source-name", required=True, help="Source name")
    parser.add_argument("--source-version", help="Source version")
    parser.add_argument("--source-uri", help="Source URI")

    ChadoAuth(parser)
    args = parser.parse_args()

    ci = ChadoInstance(args.dbhost, args.dbname, args.dbuser, args.dbpass, args.dbschema, args.debug)

    ci.connect()

    # check if the analysis exists
    res = ci.session.query(Analysis).filter_by(name = args.name)
    found_res = (res.count() > 0)

    if not found_res:
        newa = Analysis()
        newa.name = args.name
        newa.program = args.program
        newa.programversion = args.program_version
        newa.algorithm = args.algorithm
        newa.sourcename = args.source_name
        newa.sourceversion = args.source_version
        newa.sourceuri = args.source_uri
        newa.timeexecuted = args.timeexecuted # This should be a timestamp
        session.add(newa)
        session.commit()
    else:
        raise Exception("Found a preexisting analysis with the same attributes in the database %s" % (self._engine.url))

@@ -43,16 +43,46 @@
 rgparse%0A
+from datetime import datetime%0A
 from cha
@@ -720,16 +720,136 @@
 ce URI%22)
+%0A    parser.add_argument(%22--date-executed%22, help=%22Date of execution of the analysis (format=YYYY-MM-DD, default=today)%22)
 %0A%0A    Ch
@@ -1121,25 +1121,16 @@
 me)%0A
+%0A
     
-found_res = (
+if 
 res.
@@ -1144,37 +1144,253 @@
  %3E 0
-)%0A%0A    if not found_res:%0A    
+:%0A        raise Exception(%22Found a preexisting analysis with the same attributes in the database %25s%22 %25 (self._engine.url))%0A%0A    date = datetime.today()%0A    if args.args_executed:%0A        date = datetime.strptime(args.args_executed, '%25Y-%25m-%25d')%0A%0A
     
@@ -1407,28 +1407,24 @@
 lysis()%0A    
-  
-  
 newa.name = 
@@ -1429,28 +1429,24 @@
 = args.name%0A
-    
     newa.pro
@@ -1465,28 +1465,24 @@
 program%0A    
- 
-   
 newa.program
@@ -1504,36 +1504,32 @@
 program_version%0A
-    
     newa.algorit
@@ -1548,20 +1548,16 @@
 gorithm%0A
-    
     newa
@@ -1587,28 +1587,24 @@
 ce_name%0A    
-   
- 
 newa.sourcev
@@ -1632,20 +1632,16 @@
 version%0A
-    
     newa
@@ -1673,20 +1673,16 @@
 uri%0A    
-  
-  
 newa.tim
@@ -1697,59 +1697,13 @@
 d = 
-args.timeexecuted # This should be a timestamp%0A    
+date%0A
     
@@ -1724,20 +1724,16 @@
 wa)%0A    
- 
-   
 session.
@@ -1745,135 +1745,4 @@
 t()%0A
-    else:%0A        raise Exception(%22Found a preexisting analysis with the same attributes in the database %25s%22 %25 (self._engine.url))%0A

refactor + timeexecuted