Ejemplo n.º 1
0
def do_work(progress, in_queue, accepted_by_id, to_check_by_id, static_args,
            variants_by_id_out):
    path, seq_region, sources, all_accepted_by_id, all_to_check_by_id, only_poly_a = static_args
    # now I have a guarantee that a single ID will come only in this process (!)

    for lines in multiprocess.parser(progress, in_queue):

        variant_by_id_buffer = {}

        for line in lines:

            if line is None:
                in_queue.task_done()
                return

            data = line.split('\t')
            variant_id = int(data[0])

            in_accepted = variant_id in accepted_by_id
            in_to_check = variant_id in to_check_by_id

            if not (in_accepted or in_to_check):
                continue

            variant = Variant(chr_name=seq_region[int(data[1])],
                              chr_start=int(data[2]),
                              chr_end=int(data[3]),
                              chr_strand=data[4],
                              snp_id=data[7],
                              source=sources[int(data[10])])

            if in_accepted:
                variant.affected_transcripts = [
                    transcript_and_allele[0]
                    for transcript_and_allele in accepted_by_id[variant_id]
                ]

            if in_to_check:
                newly_accepted_transcripts = []
                for transcript, alleles in to_check_by_id[variant_id]:
                    accepted = test_poly_a_and_amend_transcript_if_present(
                        transcript, variant, alleles, only_poly_a)
                    if accepted:
                        newly_accepted_transcripts.append(transcript)

                variant.affected_transcripts.extend(newly_accepted_transcripts)

            if variant.affected_transcripts:
                ref = variant.affected_transcripts[0].sequence[OFFSET:-OFFSET]
                if any(ref != t.sequence[OFFSET:-OFFSET]
                       for t in variant.affected_transcripts):
                    print('Mismatch between transcripts reference allele!')
                    print(variant)
                    continue
                variant.ref = ref
                variant_by_id_buffer[variant_id] = variant

        variants_by_id_out.update(variant_by_id_buffer)
Ejemplo n.º 2
0
    def add_variant(self, item, cost_price, selling_price, quantity, **kwargs):
        variant = Variant(item=item, user=self.user)
        self.session.add(variant)
        spec_names = {
            'cost_price': cost_price,
            'selling_price': selling_price,
            'quantity': quantity
        }
        spec_names.update(kwargs)
        specs = Spec.ensure(self.session, self.user, spec_names.keys())

        for (name, value) in spec_names.items():
            spec = select(specs, 'name', name)
            assert (spec is not None)
            variant_spec = VariantSpec(variant=variant,
                                       spec=spec,
                                       value=value,
                                       user=self.user)
            self.session.add(variant_spec)

        self.session.commit()
        return variant
Ejemplo n.º 3
0
def clinvar_variants(args):

    # assert args.clinvar

    types = {
        'chrom': str,
        'pos': np.int32,
        'ref': str,
        'alt': str,
        'symbol': str,
        'hgvs_c': str,
        'hgvs_p': str,
        'molecular_consequence': str,
        'all_traits': str
    }

    df = read_csv(args.path, sep='\t', usecols=types.keys(), dtype=types)

    if args.trait:
        df = df[df.all_traits.str.contains(args.trait)]

    variants_by_gene = defaultdict(list)

    for row in df.iterrows():
        row = row[1]

        gene = row.symbol

        v = Variant(chr_name=row.chrom,
                    chr_start=row.pos,
                    chr_end=row.pos + len(row.alt) - len(row.ref),
                    ref=row.pos,
                    snp_id=row.hgvs_p,
                    alts=(row.alt, ),
                    gene=gene)
        variants_by_gene[gene].append(v)

    return variants_by_gene
Ejemplo n.º 4
0
def zcrb1(_):
    """
    Data based on:
    http://grch37-cancer.sanger.ac.uk/cosmic/mutation/overview?id=109189

    In spidex ref and alt is always from forward strand: (!)
        ref_allele: The reference allele at the variant position (forward-strand)
        mut_allele: The mutant allele at the variant position (forward-strand)
    """
    ref = 'G'
    alt = 'A'
    chrom = '12'
    strand = '-'
    pos = 42707711

    tb = tabix.open(SPIDEX_LOCATION)

    variant = Variant(chr_name=chrom,
                      chr_start=pos,
                      chr_end=pos,
                      chr_strand=strand,
                      snp_id='ZCRB1:c.411G>A',
                      ref=ref)

    records = spidex_get_variant(tb, variant)
    record = choose_record(records,
                           variant,
                           alt,
                           location='exonic',
                           strict=True)

    assert record

    z_score = float(record.dpsi_zscore)

    print(z_score, record)
Ejemplo n.º 5
0
def create_variant(name, description, user_id):
    name = name.title()
    variant = Variant(
        name=name,
        description=description,
        created_by=user_id
    )
    response = variant_pb2.Variant(result="failed")
    try:
        with session_scope() as session:
            session.add(variant)
            response = variant_pb2.Variant(result="success", status=status.STATUS_201_CREATED, message="Variant created successfully!")
            session.commit()
    except IntegrityError as e:
        with session_scope() as session:
            try:
                variant = session.query(Variant).filter(Variant.is_deleted==True, Variant.name.ilike(name)).first()
            except DataError:
                variant = None
            if variant:
                variant.name = name
                variant.description = description
                variant.created_at = datetime.now()
                variant.created_by = user_id
                variant.updated_at = None
                variant.updated_by = None
                variant.is_deleted = False
                variant.deleted_at = None
                variant.deleted_by = None
                session.commit()
                response = variant_pb2.Variant(result="success", status=status.STATUS_201_CREATED, message="Variant created successfully!")
                return response
            else:
                session.commit()
        response.message = "Variant already exists with same name!"
        response.status = status.STATUS_403_FORBIDDEN
        return response
    except Exception as e:
        print(e)
        response.message = "Unexpected error occurred!"
        response.status = status.STATUS_500_INTERNAL_SERVER_ERROR
        pass
    return response
Ejemplo n.º 6
0
            if len(a) > 1:
                type = "INDEL"

        if "snps" not in statistics[TOTAL_SNPS]:
            statistics[TOTAL_SNPS]["snps"] = 0
        if "indels" not in statistics[TOTAL_INDELS]:
            statistics[TOTAL_INDELS]["indels"] = 0

        if type == "SNP":
            statistics[TOTAL_SNPS]["snps"] += 1
        elif type == "INDEL":
            statistics[TOTAL_INDELS]["indels"] += 1

        variant = Variant(ID="#".join([chrom, pos, ref, alt]),
                          chrom=chrom,
                          pos=pos,
                          ref=ref,
                          alt=alt,
                          type=type)
        variant_info = VariantInfo(ID=chrom + "#" + pos + "#" +
                                   str(items_loaded),
                                   qual=qual,
                                   filter=filter,
                                   info=json.dumps(info_dict),
                                   format=":".join(format))

        # Nodes
        csv_files[Variant].writerow(variant.get_all())
        csv_files[VariantInfo].writerow(variant_info.get_all())

        # Edges
        csv_files[HasVariant].writerow([chrom, variant.ID])
Ejemplo n.º 7
0
def polycystic_kidney_disease_variants(args, exonic_only=True):
    """Fetch variants associated with PKD1 from PKDB."""
    variants_by_genes = defaultdict(list)

    table = get_raw_table()
    rows = iter(table)

    headers = next(rows)

    headers = [
        header.lower().replace(' ',
                               '_').replace('#',
                                            'count').replace('%', 'percent')
        for header in headers
    ]

    VariantRecord = recordclass('VariantRecord', headers)

    for row in rows:
        record = VariantRecord(*row)

        if exonic_only:
            if not record.region.startswith('EX'):
                continue

        # currently hardcoded
        hgvs_code = 'PDK1:c.' + record.cdnachange

        try:
            gene_name, pos, ref, alt = decode_hgvs_code(hgvs_code)
        except ValueError as orginal_e:
            # handle manually mutation code which do not comply with HGVS

            # range specified with '-' instead of underscore:
            if '-' in hgvs_code:
                print('Assuming - means range in %s.' % hgvs_code)
                hgvs_code = hgvs_code.replace('-', '_')

                try:
                    gene_name, pos, ref, alt = decode_hgvs_code(hgvs_code)
                except ValueError as e:
                    print(e.message)
                    continue
            else:
                print(orginal_e.message)
                continue

        gene = GENES[gene_name]

        variant = Variant(
            None,
            ref=ref,
            alts=[alt],
            chr_name=gene.chrom,
            chr_start=gene.cds_start + pos,
            ensembl_transcript_stable_id=gene.ensembl_transcript_stable_id,
            snp_id=hgvs_code)
        # TODO: tried to use
        # http://myvariant.info/ to map variants, get variants data.
        # as_hgvs won work until ref is given.
        # back in the same point
        print(variant.as_hgvs())

        variants_by_genes[gene_name].append(variant)

    return variants_by_genes
Ejemplo n.º 8
0
def create_variant_entry(request):
    if request.method == 'POST':
        received_json_data = json.loads(request.body)
        item_product_code = received_json_data.get('item_product_code', '')
        variant_name = received_json_data.get('variant_name', '')
        selling_price = received_json_data.get('selling_price', '')
        cost_price = received_json_data.get('cost_price', '')
        quantity = received_json_data.get('quantity', '')
        properties = received_json_data.get('properties', '')
        variant_code = received_json_data.get('variant_code', '')
        user_id = received_json_data.get('user_id', 'unknown')

        print(properties)

        #Check if property is a dict
        try:
            for k, v in properties.iteritems():
                print(k)
                print(v)
        except AttributeError as e:
            return JsonResponse(
                {
                    "success": "false",
                    "error": "Properties should be a dictionary"
                },
                status=500)

        #check if item exists:
        query = session.query(Item).filter(
            Item.product_code == item_product_code)
        _row = query.first()
        if _row is None:
            return JsonResponse(
                {
                    "success": "false",
                    "error": "Item entry for the variant not found"
                },
                status=500)

        #store in db
        try:
            row = Variant(item_product_code=item_product_code,
                          variant_name=variant_name,
                          selling_price=selling_price,
                          cost_price=cost_price,
                          quantity=quantity,
                          variant_code=variant_code,
                          properties=properties)
            session.add(row)
            session.commit()
            create_new_log(user_id, "created variant", "variant", "all",
                           str(variant_code))
        except SQLAlchemyError as e:
            session.rollback()
            return JsonResponse({
                "success": "false",
                "error": e.message
            },
                                status=500)
        except IntegrityError as e:
            session.rollback()
            return JsonResponse({
                "success": "false",
                "error": e.message
            },
                                status=500)

        return JsonResponse({"Success": "true"}, status=200)
    else:
        return JsonResponse({
            "success": "false",
            "error": "Method is POST"
        },
                            status=500)