?x property:Located_in ?lo.
        filter regex(?label, "%s", "i").
        }
    """ % (neuronName))
#    print queryStr
    
    sparql.setQuery(queryStr)
    results = sparql.query().convert()
    queryResults = []
    for result in results["results"]["bindings"]:
        outStr = unicode(result['lo']["value"])
        loc =  re.sub(r'http://neurolex.org/wiki/Special:URIResolver/Category-3A', '', outStr)
        print neuronName + ' : ' + loc        
        neuronRegion1.append(loc)
    if len(result) is 0:
        neuronRegion1.append('')

neuronNameList = load_neuron_names()
queryTerm = 'Id'
usingTermMethod = 'Label'
for neuronName in neuronNameList:
    ## get all nlex id of neuron name
    nlex =  sparql_get(queryTerm, neuronName, usingTermMethod)
    if nlex:
        print neuronName, nlex[0]
        n = Neuron.objects.get_or_create(nlexID = nlex[0], name = neuronName)[0]
        n.save()
    else:
        print neuronName + " not found in nlex"
        n = Neuron.objects.get_or_create(nlexID = 0, name = neuronName)[0]
        n.save()
"""

from sparql_methods import sparql_get
import neuroelectro.models as m
neurons = m.Neuron.objects.all()
queryTerm = 'Label'
usingTermMethod = 'Id'

neurons_with_new_names = []
neurons_with_no_nlex = []
neurons_no_names = []
for n in neurons:
    if n.nlex_id:
        if n.nlex_id != u'0':
            #print n.nlex_id
            newName = sparql_get(queryTerm, n.nlex_id, usingTermMethod)
            if len(newName) > 1:
                matching = [s for s in newName if "cell" in s]
                if len(matching) == 0:
                    matching = [s for s in newName if "neuron" in s]
                newName = matching
                if len(newName) > 1:
                    print (n.nlex_id), newName
            if len(newName) == 0:
                neurons_no_names.append((n.nlex_id, n.name))
                continue
            #print n.name, newName[0]
            if n.name != newName[0]:
                neurons_with_new_names.append((n.nlex_id, newName[0], n.name))
        else:
            neurons_with_no_nlex.append(n.name)