Skip to content

Load a Protocol from Autoprotocol JSON #219

Description

@opensean

Hello,

Is there way to load a protocol from a json file?

For example, the protocol can be serialized to JSON using:

# serialize the protocol as Autoprotocol JSON
print(json.dumps(p.as_dict(), indent=2))

How do I go the other way? Ideally, something like:

with open("protocol.json", "r") as f:
    p = Protocol.from_dict(json.load(f))

Activity

  1. bmiles commented on Nov 1, 2019

    @bmiles
    Contributor

    Hey Sean, that's a cool idea, it isn't supported in Ap-py. I'd be interested to hear how that could be done?

  2. opensean commented on Nov 6, 2019

    @opensean
    Author

    I spent some time looking at the code and came across some differences between the spec and this package. For example, spec for a container_ref does not include the container type, which is required by autoprotocol-python to generate a Container.

      "refs": {
        String: {
          "id": Option<String>,
          "new": Option<String>,
          "store": Option<{
            "where": Option<Enum(
              "cold_80",
              "cold_20",
              "cold_4",
              "ambient",
              "warm_30",
              "warm_37"
            )>
          }>,
          "discard": Option<Boolean>,
          "cover": Option<String>
        }
      }
    }
    

    The missing container type means I can't do this

    def from_json(path):
        """
        Load an Autoprotocol formatted JSON.
    
        Args:
            path (str):  file path.
    
        Returns:
            [autoprotocol.container.Container], autoprotocol.protocol.Protocol
        """
        containers = []
        with open(path, "r") as f:
            p_dict = json.load(f)
            p = Protocol()
            for k,v in p_dict["refs"].items():
    
                ## get storage info
    
                storage = v.get("store", None)
                if storage:
                    storage = v["store"]["where"]
    
    
                ## cont_type is not available in spec unless it is a "new" 
                ## container e.g,
                ##
                ## "sample_plate_1": {
                ##   "new": "96-pcr",
                ##   "discard": true
                ## }
                cont_type = v.get("new", None)
    
                ## create container, this will break without cont_type
                c =  Container(v.get("id", None), cont_type, name = k,
                               storage = storage,
                               cover = v.get("cover", None))
    
                containers.append(c)
    
                ## create Ref and add to protocol
                r = Ref(k, v, c)
    
                p.refs[k] = r
    
    
            ## create instructions
            for i in p_dict["instructions"]:
    
                ## insert code here
    
                ## p._append_and_return(Instruction)
    
        return containers, p
    

    I haven't explored rebuilding the instructions yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions