Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Does your version work? It looks like you only ever create one Peptide object. Also, what is the "if(row[5])" statement doing? In your example data there are always 5 elements. Also, mod_indeces is always supposed to be a list, correct? Because in your example output file mod_indeces isn't a list in the first peptide. Anyway, here is what I came up with in python:</p> <pre><code>import csv import json data = {} with open('proteins.csv','rb') as f: reader = csv.reader(f) for row in reader: name = row[0] sequence = row[1] mod_sequence = row[2] mod_indeces = map(int,row[3].split(', ')) spectral_count = int(row[4]) peptide = {'sequence':sequence,'mod_sequence':mod_sequence, 'mod_indeces':mod_indeces,'spectral_count':spectral_count} if name in data: data[name]['peptides'].append(peptide) else: data[name] = {'peptides':[peptide]} f.close() f = open('output.txt','wb') for protein in data: f.write(protein) f.write(',') f.write(json.dumps(data[protein])) f.write('\n') f.close() </code></pre> <p>If you are on windows and want to view the file as plain text, you may want to replace '\n' with '\r\n' or os.linesep. </p> <p>If you want to skip some rows (if there is a header or something), you can do something like this:</p> <pre><code>import csv import json data = {} rows_to_skip = 1 rows_read = 0 with open('proteins.csv','rb') as f: reader = csv.reader(f) for row in reader: if rows_read &gt;= rows_to_skip: name = row[0] sequence = row[1] mod_sequence = row[2] mod_indeces = map(int,row[3].split(', ')) spectral_count = int(row[4]) peptide = {'sequence':sequence,'mod_sequence':mod_sequence, 'mod_indeces':mod_indeces,'spectral_count':spectral_count} if name in data: data[name]['peptides'].append(peptide) else: data[name] = {'peptides':[peptide]} rows_read += 1 f.close() f = open('output.txt','wb') for protein in data: f.write(protein) f.write(',') f.write(json.dumps(data[protein])) f.write('\n') f.close() </code></pre> <p>If you want the keys of the dictionary to be in a particular order, you can use an orderedDict instead of the default dict. Just replace the peptide line with the following:</p> <pre><code>peptide = OrderedDict([('sequence',sequence), ('mod_sequence',mod_sequence), ('mod_indeces',mod_indeces), ('spectral_count',spectral_count)]) </code></pre> <p>Now the order is preserved. That is, <code>sequence</code> is followed by <code>mod_sequence</code> followed by <code>mod_indeces</code> followed by <code>spectral_count</code>. To change the order, just change the order of elements in the OrderedDict.</p> <p>Note that you will also have to add <code>from collections import OrderedDict</code> in order to be able to use OrderedDict.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload