@@ -0,0 +1,1922 @@
+#!/usr/bin/python%0A# -*- coding: utf-8 -*-%0A%0Aimport os%0Aimport csv%0A%0Afrom django.core.management.base import BaseCommand%0A%0Afrom emgapimetadata import models as m_models%0A%0A%0Aclass Command(BaseCommand):%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument('importpath', type=str)%0A%0A    def handle(self, *args, **options):%0A        self.populate(options)%0A%0A    def populate(self, options):%0A        # check if path is valid%0A        _path = options.get('importpath', None)%0A        if os.path.exists(_path):%0A            if os.path.isdir(_path):%0A                for root, dirs, files in os.walk(_path, topdown=False):%0A                    for name in files:%0A                        accession = name.split(%22_%22)%5B0%5D%0A                        f = os.path.join(root, name)%0A                        if name.endswith(%22go%22):%0A                            self.import_go(f, accession)%0A            # TODO: is file get dir:%0A            elif os.path.isfile(_path):%0A                raise NotImplemented(%22Give path to directory.%22)%0A        else:%0A            raise NotImplemented(%22Path doesn't exist.%22)%0A%0A    def import_go(self, f, accession):%0A        with open(f, newline='') as fcsv:%0A            reader = csv.reader(fcsv)%0A            run = m_models.Run()%0A            run.accession = %22ERR700147%22%0A            run.pipeline_version = %221.0%22%0A            for row in reader:%0A                try:%0A                    ann = m_models.Annotation(%0A                        accession=row%5B0%5D,%0A                        description=row%5B1%5D,%0A                        lineage=row%5B2%5D,%0A                    ).save()%0A                except:%0A                    ann = m_models.Annotation.objects.get(accession=row%5B0%5D)%0A                rann = m_models.RunAnnotation()%0A                rann.count = row%5B3%5D%0A                rann.annotation = ann%0A                run.annotations.append(rann)%0A            # ranns = m_models.RunAnnotation.objects.insert(ranns)%0A            run.save()%0A

Add command line tool to import metadata