Skip to content
Snippets Groups Projects
Commit 9ac9ba7f authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

fix pep8 and python2.7 syntax

parent 2319b154
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ Created on 27 dec. 2011 ...@@ -5,7 +5,7 @@ Created on 27 dec. 2011
@author: Bertrand Néron @author: Bertrand Néron
""" """
from __future__ import print_function
from collections import namedtuple from collections import namedtuple
from couchdbkit.client import Server from couchdbkit.client import Server
from couchdbkit.exceptions import ResourceNotFound from couchdbkit.exceptions import ResourceNotFound
...@@ -23,7 +23,7 @@ def replicon_parser( replicon_data ): ...@@ -23,7 +23,7 @@ def replicon_parser( replicon_data ):
@rtype: dict @rtype: dict
""" """
replicon_db = {} replicon_db = {}
Replicon_info = namedtuple('Replicon_info', 'name, taxid, strain, taxonomy, type') Replicon_info = namedtuple('Replicon_info', ('name', 'taxid', 'strain', 'taxonomy', 'type'))
with open(replicon_data, 'r') as replicon_file: with open(replicon_data, 'r') as replicon_file:
for line in replicon_file: for line in replicon_file:
if line[0] != '#': if line[0] != '#':
...@@ -33,16 +33,20 @@ def replicon_parser( replicon_data ): ...@@ -33,16 +33,20 @@ def replicon_parser( replicon_data ):
raise KeyError("duplicate replicon:" + fields[0]) raise KeyError("duplicate replicon:" + fields[0])
else: else:
try: try:
replicon_db[ fields[0] ] = Replicon_info( fields[0] , int(fields[1]) , fields[2] , fields[3].split('; ') , fields[4]) replicon_db[fields[0]] = Replicon_info(fields[0],
except Exception, err: int(fields[1]),
raise Exception( "Error during parsing line :"+line ) fields[2],
#remove ending dot or semi-colon from the last term of taxonnomy fields[3].split('; '),
if( replicon_db[ fields[0] ].taxonomy[-1].endswith('.') or replicon_db[ fields[0] ].taxonomy[-1].endswith(';')): fields[4])
except Exception as err:
raise Exception("Error during parsing line: {0}\n{1}".format(line, err))
# remove ending dot or semi-colon from the last term of taxonomy
tax_last_char = replicon_db[fields[0]].taxonomy[-1]
if tax_last_char.endswith('.') or tax_last_char.endswith(';'):
replicon_db[fields[0]].taxonomy[-1] = replicon_db[fields[0]].taxonomy[-1][:-1] replicon_db[fields[0]].taxonomy[-1] = replicon_db[fields[0]].taxonomy[-1][:-1]
return replicon_db return replicon_db
def system_parser(system_data): def system_parser(system_data):
""" """
@param system_data: the path of secretion system information file @param system_data: the path of secretion system information file
...@@ -52,7 +56,9 @@ def system_parser( system_data ): ...@@ -52,7 +56,9 @@ def system_parser( system_data ):
""" """
system_db = {} system_db = {}
System_info = namedtuple('System_info', 'code, T3SS_family, replicon, genes') System_info = namedtuple('System_info', 'code, T3SS_family, replicon, genes')
Gene = namedtuple( 'Gene', 'code, id, protein_length, strand, begin, end, match, full_score, e_value, best_domain_score, best_domain_evalue, c_value, coverage_profile, match_begin, match_end, name, description') Gene = namedtuple('Gene', ('code', 'id', 'protein_length', 'strand', 'begin', 'end', 'match', 'full_score',
'e_value', 'best_domain_score', 'best_domain_evalue', 'c_value', 'coverage_profile',
'match_begin', 'match_end', 'name', 'description'))
with open(system_data, 'r') as system_file: with open(system_data, 'r') as system_file:
for line in system_file: for line in system_file:
...@@ -87,7 +93,7 @@ def system_parser( system_data ): ...@@ -87,7 +93,7 @@ def system_parser( system_data ):
# append this gene to System_info genes # append this gene to System_info genes
system_db[fields[16]].genes[gene.code] = gene system_db[fields[16]].genes[gene.code] = gene
else: else:
#create a new Sysem_info entry # create a new System_info entry
system_db[fields[16]] = System_info(fields[16], fields[17], fields[15], genes={gene.code: gene}) system_db[fields[16]] = System_info(fields[16], fields[17], fields[15], genes={gene.code: gene})
return system_db return system_db
...@@ -153,7 +159,7 @@ def fill_db( server_uri, db_name, user, passwd, replicon_db , system_db , force_ ...@@ -153,7 +159,7 @@ def fill_db( server_uri, db_name, user, passwd, replicon_db , system_db , force_
secreton_db.save_doc(secretion_system, force_update=force_update) secreton_db.save_doc(secretion_system, force_update=force_update)
if __name__ == '__main__': if __name__ == '__main__':
from optparse import OptionParser , OptionGroup import argparse
import sys import sys
import getpass import getpass
...@@ -167,61 +173,56 @@ if __name__ == '__main__': ...@@ -167,61 +173,56 @@ if __name__ == '__main__':
parse a file containing replicon informations and a file containing system informations parse a file containing replicon informations and a file containing system informations
and fill a couchDB data base with these informations and fill a couchDB data base with these informations
""" """
parser = OptionParser( usage= usage ) parser = argparse.ArgumentParser(usage=usage)
server_opt = OptionGroup(parser, "Server Options") server_opt = parser.add_argument_group(title="Server Options")
server_opt.add_option( "-S" , "--server" , server_opt.add_argument("-S", "--server",
action="store", action="store",
type="string", type="string",
dest="server_url", dest="server_url",
help="the url of the couchDB server (with the port)") help="the url of the couchDB server (with the port)")
server_opt.add_option( "-d" , "--database" , server_opt.add_argument("-d", "--database",
action="store", action="store",
type="string", type="string",
dest="db_name", dest="db_name",
help="the name of the data base") help="the name of the data base")
parser.add_option_group( server_opt ) parsing_opt = parser.add_argument_group(title="Parsing Options")
parsing_opt.add_argument("-r", "--replicon",
parsing_opt = OptionGroup(parser, "Parsing Options")
parsing_opt.add_option( "-r" , "--replicon" ,
action="store", action="store",
type="string", type="string",
dest="replicon_path", dest="replicon_path",
help="the path to the replicon file to parse") help="the path to the replicon file to parse")
parsing_opt.add_option( "-s" , "--system" , parsing_opt.add_argument("-s", "--system",
action="store", action="store",
type="string", type="string",
dest="system_path", dest="system_path",
help="the path to the system secretion file to parse") help="the path to the system secretion file to parse")
parsing_opt.add_option( "-f" , "--force_update" , parsing_opt.add_argument("-f", "--force_update",
action="store_true", action="store_true",
dest="force_update", dest="force_update",
default=False, default=False,
help="") help="")
parser.add_option_group( parsing_opt )
options, args = parser.parse_args() options, args = parser.parse_args()
if not options.server_url: if not options.server_url:
print >> sys.stderr , "You must specify a server url" print("You must specify a server url", file=sys.stderr)
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
sys.exit(1) sys.exit(1)
if not options.db_name: if not options.db_name:
print >> sys.stderr , "You must specify a data base name" print("You must specify a data base name", file=sys.stderr)
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
sys.exit(1) sys.exit(1)
if not options.replicon_path: if not options.replicon_path:
print >> sys.stderr , "You must specify the path to the replicon information file" print("You must specify the path to the replicon information file", file=sys.stderr)
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
sys.exit(1) sys.exit(1)
if not options.system_path: if not options.system_path:
print >> sys.stderr , "You must specify the path to the secretion system information file" print("You must specify the path to the secretion system information file", file=sys.stderr)
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
sys.exit(1) sys.exit(1)
replicon_db = replicon_parser(options.replicon_path) replicon_db = replicon_parser(options.replicon_path)
system_db = system_parser(options.system_path) system_db = system_parser(options.system_path)
...@@ -229,14 +230,12 @@ if __name__ == '__main__': ...@@ -229,14 +230,12 @@ if __name__ == '__main__':
while True: while True:
user, password = get_credentials() user, password = get_credentials()
try: try:
fill_db(options.server_url, options.db_name, user, password, replicon_db, system_db, force_update = options.force_update) fill_db(options.server_url, options.db_name, user, password, replicon_db, system_db,
force_update=options.force_update)
break break
except restkit.errors.Unauthorized, err: except restkit.errors.Unauthorized as err:
print >> sys.stderr, "Bad authentication, try again" print("Bad authentication, try again", file=sys.stderr)
try_again += 1 try_again += 1
if try_again > 2: if try_again > 2:
sys.exit("Authentication failure") sys.exit("Authentication failure")
except Exception, err:
print >> sys.stderr, err
sys.exit(2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment