<commit_before>import os
import settings
from django.core.management.base import BaseCommand
from xbrowse_server.base.models import Project, Individual
from django.core.exceptions import ObjectDoesNotExist

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('args', nargs='*')

    def handle(self, *args, **options):
        """Command line args:
            arg0 = project id
            arg1 = file path - where file is a tsv table with 2 columns:
                    column0: the individual ID
                    column1: bam file path relative to the base settings.READ_VIZ_BAM_PATH url or directory - so that
                        the absolute bam path can be derived by doing os.path.join(settings.READ_VIZ_BAM_PATH, column1)
        """
        project_id = args[0]
        project = Project.objects.get(project_id=project_id)

        for line in open(args[1]).readlines():
            try:
                indiv_id, bam_path = line.strip('\n').split('\t')
            except Exception as e:
                raise ValueError("Couldn't parse line: %s" % line, e) 
            
            try:
                indiv = Individual.objects.get(project=project, indiv_id=indiv_id)
            except ObjectDoesNotExist as e: 
                print("ERROR: Individual not found in xBrowse: '%s'. Skipping.." % indiv_id)
                continue
                
            absolute_path = os.path.join(settings.READ_VIZ_BAM_PATH, bam_path)
            if absolute_path.startswith('http'):
                response = requests.request("GET", absolute_path, auth=(settings.READ_VIZ_USERNAME, settings.READ_VIZ_PASSWD), verify=False)
                if response.status_code != 200:
                    print("ERROR: " + absolute_path + " reponse code == " + str(response.status_code) + ". Skipping..")
                    continue
            elif not os.path.isfile(absolute_path):
                print("ERROR: " + absolute_path + " not found. Skipping..")
                continue
            indiv.bam_file_path = bam_path
            indiv.save()
<commit_msg>Check that readviz bams exist before adding them to seqr<commit_after>import os
import settings
import requests
from django.core.management.base import BaseCommand
from xbrowse_server.base.models import Project, Individual
from django.core.exceptions import ObjectDoesNotExist

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('args', nargs='*')

    def handle(self, *args, **options):
        """Command line args:
            arg0 = project id
            arg1 = file path - where file is a tsv table with 2 columns:
                    column0: the individual ID
                    column1: bam file path relative to the base settings.READ_VIZ_BAM_PATH url or directory - so that
                        the absolute bam path can be derived by doing os.path.join(settings.READ_VIZ_BAM_PATH, column1)
        """
        project_id = args[0]
        project = Project.objects.get(project_id=project_id)

        for line in open(args[1]).readlines():
            try:
                indiv_id, bam_path = line.strip('\n').split('\t')
            except Exception as e:
                raise ValueError("Couldn't parse line: %s" % line, e) 
            
            try:
                indiv = Individual.objects.get(project=project, indiv_id=indiv_id)
            except ObjectDoesNotExist as e: 
                print("ERROR: Individual not found in xBrowse: '%s'. Skipping.." % indiv_id)
                continue
                
            absolute_path = os.path.join(settings.READ_VIZ_BAM_PATH, bam_path)
            if absolute_path.startswith('http'):
                if absolute_path.endswith(".bam"):
                    url_to_check = absolute_path.replace(".bam", ".bai")
                    print("Checking " + url_to_check)
                    response = requests.request("HEAD", url_to_check, auth=(settings.READ_VIZ_USERNAME, settings.READ_VIZ_PASSWD), verify=False)
                    if response.status_code != 200:
                        print("ERROR: " + url_to_check + " reponse code == " + str(response.status_code) + ". Skipping..")
                        continue
            elif not os.path.isfile(absolute_path):
                print("ERROR: " + absolute_path + " not found. Skipping..")
                continue
            indiv.bam_file_path = bam_path
            indiv.save()

{'fraction_non_alphanumeric': 0.09369409660107335, 'fraction_numerical': 0.004919499105545617, 'mean_word_length': 2.0080699394754538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2400473', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1209, 'n_words': 316}