Example #1
0
    def factory(cls, args):
        """
        We get the args dict from argparse

        From this dict we need to pop the track names and create the
        trackContent objects. The rest we give to the object as the kwargs.

        Any arg starting with track is interpreted as a track.

        Track* is reserved for tracks only.

        :param args:
        :return:
        """

        # args is a namespace. Get it's dict
        args = vars(args)

        # Get the genome
        assert 'genome' in args.keys()
        genome = Genome.createFromJson(args['genome'])
        del args['genome']

        # Any key matching 'track*' is assumed to be a track.
        trackKeys = [key for key in args.keys()
                     if key.startswith('track')]
        assert len(trackKeys) > 0

        # TODO: Add support for different allowOverlap on each track.
        allowOverlap = False
        if 'allowOverlap' in args:
            allowOverlap = args['allowOverlap']
        else:
            # Assume False if not set.
            allowOverlap = False

        # Sort the keys
        # The tracks will be given to the operation in alphabetical order.
        trackKeys = sorted(trackKeys)

        tracks = [createTrackContentFromFile(genome, args[key], allowOverlap)
                  for key in trackKeys]

        # Delete the tracks from args
        for key in trackKeys:
            del args[key]

        return cls(*tracks, **args)
Example #2
0
    def runOperation(self):
        """
        Run the selected operation.
        :return: None
        """
        logging.debug(self._args)

        logging.debug("Loaded operations: {0}".format(
            self._importedOperations.keys()))

        operation = self._args.which

        if operation == 'list':
            self._listTracksInGTrackCore(self._args.genome)

        elif operation == 'import':
            genome = Genome.createFromJson(self._args.genome)
            importTrackIntoGTrack(self._args.name, genome, self._args.path)

        elif operation == 'export':

            #genome = Genome.createFromJson(self._args.genome)

            # fileFormatName = GTrack

            exportFile(outFileName=self._args.outName,
                       genome=self._args.genome,
                       trackName=self._args.name,
                       fileFormatName='GTrack',
                       allowOverlaps=self._args.allowOverlaps)

        else:
            assert operation in self._importedOperations.keys()

            logging.debug("Running operation: {0}".format(operation))

            oper = self._importedOperations[operation]
            # check args?
            a = oper.factory(self._args)
            res = a.calculate()

            if a.resultIsTrack:
                pass

            if res is not None and isinstance(res, TrackContents):
                # Save result if any
                # TODO add support for custom name..
                name = a.createTrackName()
                logging.debug("Importing track. Name: {0}".format(name))
                print(name)

                for k,v in res.trackViews.iteritems():
                    s = v.startsAsNumpyArray()

                    if s is not None and len(s) > 0:
                        print(s)
                        print(type(s))
                        print(v.endsAsNumpyArray())
                        print(type(v.endsAsNumpyArray()))
                        print(v.idsAsNumpyArray())
                        print(type(v.idsAsNumpyArray()))
                        print(v.edgesAsNumpyArray())
                        print(type(v.edgesAsNumpyArray()))
                        print(v.weightsAsNumpyArray())
                        print(type(v.weightsAsNumpyArray()))

                importTrackFromTrackContents(trackContents=res, trackName=name)