How is it possible to define own _id in couchdb-python (0.9), because when I tried '_id': i[5] I got the following error message?
$ python test3.py
828288
Traceback (most recent call last):
File "test3.py", line 42, in <module>
db.save(doc)
File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 415, in save
func = _doc_resource(self.resource, doc['_id']).put_json
File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 954, in _doc_resource
if doc_id[:1] == '_':
TypeError: 'int' object has no attribute '__getitem__'
Below is the script which is causing the above error:
from couchdb.mapping import Document, TextField, IntegerField, Mapping
from couchdb.mapping import DictField, ViewField, BooleanField, ListField
from couchdb import Server
# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/
server = Server()
db = server.create("test")
r = [["Test", "A", "B01", 828288, 1, 7, 'C', 5],
["Test", "A", "B01", 828288, 1, 7, 'T', 6],
["Test", "A", "B01", 171878, 3, 8, 'C', 5],
["Test", "A", "B01", 171878, 3, 8, 'T', 6],
["Test", "A", "B01", 871963, 3, 9, 'A', 5],
["Test", "A", "B01", 871963, 3, 9, 'G', 6],
["Test", "A", "B01", 1932523, 1, 10, 'T', 4],
["Test", "A", "B01", 1932523, 1, 10, 'A', 5],
["Test", "A", "B01", 1932523, 1, 10, 'X', 6],
["Test", "A", "B01", 667214, 1, 14, 'T', 4],
["Test", "A", "B01", 667214, 1, 14, 'G', 5],
["Test", "A", "B01", 667214, 1, 14, 'G', 6]]
for i in r:
print i[3]
doc = {
'type': i[0],
'name': i[1],
'sub_name': i[2],
'pos': i[3],
's_type': i[4],
'_id': i[5],
'chr':[]
}
doc['chr'].append({
"letter":i[6],
"no":i[7]
})
db.save(doc)
It expects _id to be a string and you are passing a type of int. The error is caused by this line:
if doc_id[:1] == '_':
Because script is trying to slice an int object.
So change it to string type:
...
...
'_id': str(i[5]),
...
Related
I am trying to save the result of this code as a CSV file:
import pandas as pd
df = pd.DataFrame({'ID': ['a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'a01', 'b02', 'b02','b02', 'b02', 'b02', 'b02', 'b02'],
'Row': [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 3, 3, 3],
'Col': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3, 1, 3, 1, 2, 3],
'Result': ['p', 'f', 'p', 'p', 'p', 'f', 'p', 'p', 'p', 'p', 'p', 'p', 'f', 'p', 'p', 'p']})
dfs = {}
for n, g in df.groupby('ID'):
dfs[n] = g.pivot('Row', 'Col', 'Result').fillna('')
print(f'ID: {n}')
print(dfs[n])
print('\n')
print(dfs[n].stack().value_counts().to_dict())
print('\n')
I found several methods and tried to save the output (dictionary form) into a CSV file, but without success. Any thoughts?
P.S. This is one of the methods I found, but I didn't know how to name the column based on my output?
with open("Output.csv", "w", newline="") as csv_file:
cols = ["???????????"]
writer = csv.DictWriter(csv_file, fieldnames=cols)
writer.writeheader()
writer.writerows(data)
df.to_csv('Output.csv', index = False)
For more details goto:
https://datatofish.com/export-dataframe-to-csv/
https://www.geeksforgeeks.org/saving-a-pandas-dataframe-as-a-csv/
Use the method provided by pandas data frame abject
df.to_csv()
You can use df.to_csv() to convert your data to csv.
I want to combine 2 dictionarys. Search a bit but couldn't find it yet. Because I don't really know how it's called.
But this are the two dictonary's I got:
Number 1:
Counter({'gag': 19, 'ccc': 15, 'cct': 15, 'ctg': 15, 'tcc': 13, 'aag': 13, 'atg': 12, 'cag': 12, 'gcc': 12, 'gaa': 11, 'gat': 11, 'aac': 10, 'gtg': 10, 'cca': 9, 'gac': 9, 'acc': 9, 'ggg': 9, 'agc': 8, 'cgc': 8, 'tct': 7, 'aaa': 7, 'ttc': 7, 'atc': 7, 'cac': 7, 'aca': 7, 'gct': 6, 'ccg': 6, 'ttg': 6, 'ggc': 6, 'tgc': 6, 'tca': 6, 'tac': 6, 'gca': 5, 'ctc': 5, 'ggt': 5, 'cat': 5, 'gtt': 5, 'cgt': 5, 'ttt': 4, 'tgt': 4, 'act': 4, 'aat': 4, 'tgg': 4, 'cga': 4, 'agt': 4, 'tat': 3, 'gga': 3, 'cgg': 3, 'cta': 3, 'agg': 3, 'caa': 3, 'ctt': 3, 'gtc': 3, 'aga': 3, 'acg': 2, 'gcg': 1, 'att': 1, 'tga': 1})
And number 2:
{'Phe': ['ttt', 'ttc'], 'Stop': ['tag', 'tga', 'taa'], 'Pro': ['cct', 'ccc', 'cca', 'ccg'], 'Trp': ['tgg'], 'Met': ['atg'], 'Lys': ['aaa', 'aag'], 'His': ['cat', 'cac'], 'Asp': ['gat', 'gac'], 'Start': ['atg', 'ctg', 'ttg', 'gtg', 'att'], 'Thr': ['act', 'acc', 'aca', 'acg'], 'Tyr': ['tat', 'tac'], 'Glu': ['gaa', 'cag'], 'Asn': ['aat', 'aac'], 'Val': ['gtt', 'gtc', 'gta', 'gtg'], 'Ser': ['tct', 'tcc', 'tca', 'tcg', 'agt', 'agc'], 'Cys': ['tgt', 'tgc'], 'Ile': ['att', 'atc', 'ata'], 'Leu': ['tta', 'ttg', 'ctt', 'ctc', 'cta', 'ctg'], 'Gly': ['ggt', 'ggc', 'gga', 'ggg'], 'Gln': ['caa', 'cag'], 'Arg': ['cgt', 'cgc', 'cga', 'cgg', 'aga', 'agg'], 'Ala': ['gct', 'gcc', 'gca', 'gcg']}
Now I want to count how much from every amino acid (dic 2 with phe, pro, trp, etc.) are made. So for example:
Phe has 'ttt' and 'ttc' I want to count how many phe's are in the 1st dic. But I don't know how, can anyone help?
You can calculate the sums using a default value of 0 for those not found using this line:
sums = {k: sum(counter.get(tv, 0) for tv in v) for k, v in dictionary.items()}
Assuming counter is the first mapping and dictionary is the second mapping.
You can create a new dictionary for the sums:
total = {k: sum(number1.get(subk, 0) for subk in v) for k, v in number2.items()}
If I understand correctly you are looking for something like this.
def get_count(name):
count = 0
for item in number2[name]:
count += number1.get(item) or 0
return count
get_count('Phe')
I have a list r which contains 12 lists as elements. Each list contains some elements (i[0] to i[5]) which are the same compare to other lists.
I tried only to insert unique documents so as result I should get 5 documents in the DB. If the document already exist then append i[6] and i[7] to chr.
import couchdb
# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/
server = couchdb.Server()
try:
db = server.create("test")
except couchdb.http.ResourceConflict:
db = server["test"]
r = [["Test", "A", "B01", 828288, 1, 7, 'C', 5],
["Test", "A", "B01", 828288, 1, 7, 'T', 6],
["Test", "A", "B01", 171878, 3, 8, 'C', 5],
["Test", "A", "B01", 171878, 3, 8, 'T', 6],
["Test", "A", "B01", 871963, 3, 9, 'A', 5],
["Test", "A", "B01", 871963, 3, 9, 'G', 6],
["Test", "A", "B01", 1932523, 1, 10, 'T', 4],
["Test", "A", "B01", 1932523, 1, 10, 'A', 5],
["Test", "A", "B01", 1932523, 1, 10, 'X', 6],
["Test", "A", "B01", 667214, 1, 14, 'T', 4],
["Test", "A", "B01", 667214, 1, 14, 'G', 5],
["Test", "A", "B01", 667214, 1, 14, 'G', 6]]
_id = None
for i in r:
_id = str(i[5])
doc = {
'type': i[0],
'name': i[1],
'sub_name': i[2],
'pos': i[3],
's_type': i[4],
'_id': _id,
'chr':[]
}
doc['chr'].append({
"letter":i[6],
"no":i[7]
})
a = db.save(doc)
UPDATE
The first two lists in r contains the same fields (i[0] to i[5]). The final document for the two lists should like this:
{
"_id": "7",
"_rev": "1-bc0b4e6f3aa855a486225f4a0dcd76c8",
"sub_name": "B01",
"name": "A",
"pos": 828288,
"s_type": 1,
"chr": [
{
"letter": "C",
"no": 5
},
{
"letter": "T",
"no": 6
}
],
"type": "Test"
}
How is possible to update a document and append to a list a dict?
Is this the most efficient solution?
import couchdb
# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/
server = couchdb.Server()
db = server.create("test")
# except couchdb.http.ResourceConflict:
#db = server["test"]
r = [["Test", "A", "B01", 828288, 1, 7, 'C', 5],
["Test", "A", "B01", 828288, 1, 7, 'T', 6],
["Test", "A", "B01", 171878, 3, 8, 'C', 5],
["Test", "A", "B01", 171878, 3, 8, 'T', 6],
["Test", "A", "B01", 871963, 3, 9, 'A', 5],
["Test", "A", "B01", 871963, 3, 9, 'G', 6],
["Test", "A", "B01", 1932523, 1, 10, 'T', 4],
["Test", "A", "B01", 1932523, 1, 10, 'A', 5],
["Test", "A", "B01", 1932523, 1, 10, 'X', 6],
["Test", "A", "B01", 667214, 1, 14, 'T', 4],
["Test", "A", "B01", 667214, 1, 14, 'G', 5],
["Test", "A", "B01", 667214, 1, 14, 'G', 6]]
# _id = None
for i in r:
_id = str(i[5])
doc = db.get(_id)
if doc is None:
doc = {
'type': i[0],
'name': i[1],
'sub_name': i[2],
'pos': i[3],
's_type': i[4],
'_id': _id,
'chr':[]
}
doc['chr'].append({
"letter":i[6],
"no":i[7]
})
else:
doc['chr'].append({
"letter":i[6],
"no":i[7]
})
db.save(doc)
I want to replace the value of first key of list1 with the elements of list2. But the output with following code is:
output
list[21, 21, 22, 22]
[{'a': array([[22, 22, 3]]), 'i': 'stop', 'c': array([7, 8, 9]), 'b': array([4, 5, 6])}, {'a': array([[1, 2, 3]]), 'i': 'stop', 'c': array([7, 8, 9]), 'b': array([4, 5, 6])}]
my code
list1 = [{'a': array([(1,2,3)]), 'b': array([4,5,6]), 'i': 'stop', 'c': array([7,8,9])}, {'a': array([(1,2,3)]), 'b': array([4,5,6]), 'i': 'stop', 'c': array([7,8,9])}]
list2 = [ array([21, 22, 23]), array([25, 26, 27])]
list3=[]
for item in liste:
for key,value in item.iteritems():
if key == 'a':
list3.append(value)
list4=[]
for i in range(len(g_a)):
for j in range(len(list3)):
list3[0][0][j] = g_a[0][i]
list4.append(list3[0][0][j])
print list4
print list
I want to get this:
liste = [{'a': array([(21,22,23)]), 'b': array([4,5,6]), 'i': 'stop', 'c': array([7,8,9])}, {'a': array([(25,26,27)]), 'b': array([4,5,6]), 'i': 'stop', 'c': array([7,8,9])}]
Where is the error? Thank you very much in advance!
Here is a dict comprehension in python 2.7 with a one-liner:
[{k: d[k] if k != 'a' else list2[i] for k in d} for i, d in enumerate(list1)]
It gives:
[{'a': [21, 22, 23], 'i': 'stop', 'c': [7, 8, 9], 'b': [4, 5, 6]}, {'a': [25, 26, 27], 'i': 'stop', 'c': [7, 8, 9], 'b': [4, 5, 6]}]
It seems they are behaving exactly the same way.
>>> data
[('a', 'b'), {'a': 1, 'b': 2}, ['a', 'b'], 'a', 'b']
>>> json.dumps(data)
'[["a", "b"], {"a": 1, "b": 2}, ["a", "b"], "a", "b"]'
>>> tornado.escape.json_encode(data)
'[["a", "b"], {"a": 1, "b": 2}, ["a", "b"], "a", "b"]'
>>> json.loads(json.dumps(data))
[[u'a', u'b'], {u'a': 1, u'b': 2}, [u'a', u'b'], u'a', u'b']
>>> tornado.escape.json_decode(json.dumps(data))
[[u'a', u'b'], {u'a': 1, u'b': 2}, [u'a', u'b'], u'a', u'b']
Sometimes it's useful to read the source code:
def json_encode(value):
return json.dumps(value).replace("</", "<\\/")
def json_decode(value):
return json.loads(to_basestring(value))
def to_basestring(value):
if isinstance(value, _BASESTRING_TYPES):
return value
assert isinstance(value, bytes_type)
return value.decode("utf-8")
to_basestring is mostly needed for python 3.x to ensure the value has type str, not bytes, because json.loads can't deal with the latter.