1
$\begingroup$

I am writing a benchling uploader script in python to upload data from a csv file in a JSON formatted schema to the benchling API platform. My function to create the JSON schema in the script looks like the following:

def create_antismash_schema(dataframe):
    """Create a json object for antismash output"""
    return {
        "assayResults": [
            {
                "fields": {
                    "entity": {"value": j['SBI ID']},
                    "sequence_id": {"value": j['sequence_id']},
                    "category": {"value": j['Category']},
                    "product": {"value": j['Product']},
                    "start": {"value": j['start']},
                    "end": {"value": j['end']},
                    "nucleotide_length": {"value": j['nucl_length']}
                },
                "schemaId": "assaysch_Z6B7TlWG",
                "projectId": 'src_tIUAcxDH'
            } for i, j in dataframe.iterrows()
        ]
    }    

Now the data I have in benchling for the fields matches the keys in the schema and the values match the headers in the csv file I'm pulling the data from.

benchling results schema looks like:

enter image description here

csv file:

enter image description here

The error I am getting when I try to upload is the following:

    
Uploading Antismash data to Benchling...
  0%|                                                                                                                                                                               | 0/159 [00:00<?, ?it/s]{
    "assayResults": null,
    "errors": [
        {
            "fields": {
                "nucleotide_length": "Value must be an integer"
            },
            "global": []
        },
        {
            "fields": {
                "nucleotide_length": "Value must be an integer"
            },
            "global": []
        },
        {
            "fields": {
                "nucleotide_length": "Value must be an integer"
            },
            "global": []
        }
    ]
}
  0%|                                                                                                                                                                               | 0/159 [00:00<?, ?it/s]
Failed to upload summary.
HTTP status code: 400


The field is set to be an integer in benchling and all the values I'm pulling from in the csv file for the nucl_length column are integer values so not sure why this error is beig thrown. Any guidance here appreciated, thanks.

$\endgroup$

1 Answer 1

1
$\begingroup$

There is a major issue about the json format required, however just converting to json is easy because iterrows has to be pandas and pandas has its own output json format ... For example,

dataframe.to_json('benchling.json'', orient='records', lines=True)

If that fails then,

jsonOut = dataframe.to_json(orient='records')[1:-1].replace('},{', '} {')
with open('benchling.json', 'w') as fout:
    fout.write(jsonOut)

However, I don't think it would work and at a minimum I think the headers of the csv will need to change.


The error

"nucleotide_length": "Value must be an integer"

Looking at your code ....

"nucleotide_length": {"value": j['nucl_length']}

Sure this is right?

Why not try ...

"nucleotide_length": int(j['nucl_length'])

Thats what the error seems to be saying.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.