Best Python code snippet using tappy_python
classic.py
Source:classic.py  
...238        for sample in sample_list:239            # grab reads from both sides of breakpoint240            read_batch, many = gather_all_reads(sample, chromA, posA, ciA, chromB, posB, ciB, z, max_reads)241            if many:242                var.genotype(sample.name).set_format('GT', './.')243                continue244            # initialize counts to zero245            ref_span, alt_span = 0, 0246            ref_seq, alt_seq = 0, 0247            alt_clip = 0248            # ref_ciA = ciA249            # ref_ciB = ciB250            ref_ciA = [0,0]251            ref_ciB = [0,0]252            for query_name in sorted(read_batch.keys()):253                fragment = read_batch[query_name]254                # boolean on whether to write the fragment255                write_fragment = False256                # -------------------------------------257                # Check for split-read evidence258                # -------------------------------------259                # get reference sequences260                for read in fragment.primary_reads:261                    is_ref_seq_A = fragment.is_ref_seq(read, var, chromA, posA, ciA, min_aligned)262                    is_ref_seq_B = fragment.is_ref_seq(read, var, chromB, posB, ciB, min_aligned)263                    if (is_ref_seq_A or is_ref_seq_B):264                        p_reference = prob_mapq(read)265                        ref_seq += p_reference266                        read.set_tag('XV', 'R')267                        write_fragment = True268                # get non-reference split-read support269                for split in fragment.split_reads:270                    split_lr = split.is_split_straddle(chromA, posA, ciA,271                                                       chromB, posB, ciB,272                                                       o1_is_reverse, o2_is_reverse,273                                                       svtype, split_slop)274                    # p_alt = prob_mapq(split.query_left) * prob_mapq(split.query_right)275                    p_alt = (prob_mapq(split.query_left) * split_lr[0] + prob_mapq(split.query_right) * split_lr[1]) / 2.0276                    if split.is_soft_clip:277                        alt_clip += p_alt278                    else:279                        alt_seq += p_alt280                    if p_alt > 0:281                        split.tag_split(p_alt)282                        write_fragment = True283                # -------------------------------------284                # Check for paired-end evidence285                # -------------------------------------286                # tally spanning alternate pairs287                if svtype == 'DEL' and posB - posA < 2 * fragment.lib.sd:288                    alt_straddle = False289                else:290                    alt_straddle = fragment.is_pair_straddle(chromA, posA, ciA,291                                                             chromB, posB, ciB,292                                                             o1_is_reverse, o2_is_reverse,293                                                             min_aligned,294                                                             fragment.lib)295                # check both sides if inversion (perhaps should do this for BND as well?)296                if svtype in ('INV'):297                    alt_straddle_reciprocal = fragment.is_pair_straddle(chromA, posA, ciA,298                                                                        chromB, posB, ciB,299                                                                        not o1_is_reverse,300                                                                        not o2_is_reverse,301                                                                        min_aligned,302                                                                        fragment.lib)303                else:304                    alt_straddle_reciprocal = False305                if alt_straddle or alt_straddle_reciprocal:306                    if svtype == 'DEL':307                        p_conc = fragment.p_concordant(var_length)308                        if p_conc is not None:309                            p_alt = (1 - p_conc) * prob_mapq(fragment.readA) * prob_mapq(fragment.readB)310                            alt_span += p_alt311                            # # since an alt straddler is by definition also a reference straddler,312                            # # we can bail out early here to save some time313                            # p_reference = p_conc * prob_mapq(fragment.readA) * prob_mapq(fragment.readB)314                            # ref_span += p_reference315                            # continue316                            fragment.tag_span(p_alt)317                            write_fragment = True318                    else:319                        p_alt = prob_mapq(fragment.readA) * prob_mapq(fragment.readB)320                        alt_span += p_alt321                        fragment.tag_span(p_alt)322                        write_fragment = True323                # # tally spanning reference pairs324                if svtype == 'DEL' and posB - posA < 2 * fragment.lib.sd:325                    ref_straddle_A = False326                    ref_straddle_B = False327                else:328                    ref_straddle_A = fragment.is_pair_straddle(chromA, posA, ref_ciA,329                                                               chromA, posA, ref_ciA,330                                                               False, True,331                                                               min_aligned,332                                                               fragment.lib)333                    ref_straddle_B = fragment.is_pair_straddle(chromB, posB, ref_ciB,334                                                               chromB, posB, ref_ciB,335                                                               False, True,336                                                               min_aligned,337                                                               fragment.lib)338                if ref_straddle_A or ref_straddle_B:339                    # don't allow the pair to jump the entire variant, except for340                    # length-changing SVs like deletions341                    if not (ref_straddle_A and ref_straddle_B) or svtype == 'DEL':342                        p_conc = fragment.p_concordant(var_length)343                        if p_conc is not None:344                            p_reference = p_conc * prob_mapq(fragment.readA) * prob_mapq(fragment.readB)345                            ref_span += (ref_straddle_A + ref_straddle_B) * p_reference / 2346                            fragment.tag_span(1 - p_conc)347                            write_fragment = True348                # write to BAM if requested349                if alignment_outpath is not None and  write_fragment:350                    for read in fragment.primary_reads + [split.read for split in fragment.split_reads]:351                        out_bam_written_reads = write_alignment(read, out_bam, out_bam_written_reads)352            if debug:353                print '--------------------------'354                print 'ref_span:', ref_span355                print 'alt_span:', alt_span356                print 'ref_seq:', ref_seq357                print 'alt_seq:', alt_seq358                print 'alt_clip:', alt_clip359            # in the absence of evidence for a particular type, ignore the reference360            # support for that type as well361            if (alt_seq + alt_clip) < 0.5 and alt_span >= 1:362                alt_seq = 0363                alt_clip = 0364                ref_seq = 0365            if alt_span < 0.5 and (alt_seq + alt_clip) >= 1:366                alt_span = 0367                ref_span = 0368            if alt_span + alt_seq == 0 and alt_clip > 0:369                # discount any SV that's only supported by clips.370                alt_clip = 0371            if ref_seq + alt_seq + ref_span + alt_span + alt_clip > 0:372                # get bayesian classifier373                if var.info['SVTYPE'] == "DUP": is_dup = True374                else: is_dup = False375                alt_splitters = alt_seq + alt_clip376                QR = int(split_weight * ref_seq) + int(disc_weight * ref_span)377                QA = int(split_weight * alt_splitters) + int(disc_weight * alt_span)378                gt_lplist = bayes_gt(QR, QA, is_dup)379                best, second_best = sorted([ (i, e) for i, e in enumerate(gt_lplist) ], key=lambda(x): x[1], reverse=True)[0:2]380                gt_idx = best[0]381                # print log probabilities of homref, het, homalt382                if debug:383                    print gt_lplist384                # set the overall variant QUAL score and sample specific fields385                var.genotype(sample.name).set_format('GL', ','.join(['%.0f' % x for x in gt_lplist]))386                var.genotype(sample.name).set_format('DP', int(ref_seq + alt_seq + alt_clip + ref_span + alt_span))387                var.genotype(sample.name).set_format('RO', int(ref_seq + ref_span))388                var.genotype(sample.name).set_format('AO', int(alt_seq + alt_clip + alt_span))389                var.genotype(sample.name).set_format('QR', QR)390                var.genotype(sample.name).set_format('QA', QA)391                # if detailed:392                var.genotype(sample.name).set_format('RS', int(ref_seq))393                var.genotype(sample.name).set_format('AS', int(alt_seq))394                var.genotype(sample.name).set_format('ASC', int(alt_clip))395                var.genotype(sample.name).set_format('RP', int(ref_span))396                var.genotype(sample.name).set_format('AP', int(alt_span))397                try:398                    var.genotype(sample.name).set_format('AB', '%.2g' % (QA / float(QR + QA)))399                except ZeroDivisionError:400                    var.genotype(sample.name).set_format('AB', '.')401                # assign genotypes402                gt_sum = 0403                for gt in gt_lplist:404                    try:405                        gt_sum += 10**gt406                    except OverflowError:407                        gt_sum += 0408                if gt_sum > 0:409                    gt_sum_log = math.log(gt_sum, 10)410                    sample_qual = abs(-10 * (gt_lplist[0] - gt_sum_log)) # phred-scaled probability site is non-reference in this sample411                    phred_gq = min(-10 * (second_best[1] - best[1]), 200)412                    var.genotype(sample.name).set_format('GQ', int(phred_gq))413                    var.genotype(sample.name).set_format('SQ', sample_qual)414                    var.qual += sample_qual415                    if gt_idx == 1:416                        var.genotype(sample.name).set_format('GT', '0/1')417                    elif gt_idx == 2:418                        var.genotype(sample.name).set_format('GT', '1/1')419                    elif gt_idx == 0:420                        var.genotype(sample.name).set_format('GT', '0/0')421                else:422                    var.genotype(sample.name).set_format('GQ', '.')423                    var.genotype(sample.name).set_format('SQ', '.')424                    var.genotype(sample.name).set_format('GT', './.')425            else:426                var.genotype(sample.name).set_format('GT', './.')427                var.qual = 0428                var.genotype(sample.name).set_format('GQ', '.')429                var.genotype(sample.name).set_format('SQ', '.')430                var.genotype(sample.name).set_format('GL', '.')431                var.genotype(sample.name).set_format('DP', 0)432                var.genotype(sample.name).set_format('AO', 0)433                var.genotype(sample.name).set_format('RO', 0)434                # if detailed:435                var.genotype(sample.name).set_format('AS', 0)436                var.genotype(sample.name).set_format('ASC', 0)437                var.genotype(sample.name).set_format('RS', 0)438                var.genotype(sample.name).set_format('AP', 0)439                var.genotype(sample.name).set_format('RP', 0)440                var.genotype(sample.name).set_format('QR', 0)441                var.genotype(sample.name).set_format('QA', 0)442                var.genotype(sample.name).set_format('AB', '.')443        # after all samples have been processed, write444        vcf_out.write(var.get_var_string() + '\n')445        if var.info['SVTYPE'] == 'BND':446            var2.qual = var.qual447            var2.active_formats = var.active_formats448            var2.genotype = var.genotype449            vcf_out.write(var2.get_var_string() + '\n')450    # throw warning if we've lost unpaired breakends451    if breakend_dict:452        logging.warning('Unpaired breakends found in file. These will not be present in output.')453    # close the files454    vcf_in.close()455    vcf_out.close()456    if alignment_outpath is not None:...CollegeData.py
Source:CollegeData.py  
...24    total_schools = json.loads(res.text)["results"]25    return [x["slug"] for x in total_schools]26def find_specific_function(title):27    if "Freshman" in title:28        return freshman_admission_reqs.set_format()29    elif "Applying" in title:30        return applying_for_admission()31    elif "Selection" in title:32        return selection_of_student33    else:34        return profile_of_fall_admission35class CollegeData:36    def __init__(self):37        self.build_id = get_build_id()38        school_nums = get_total_school_nums()39        self.slugs = get_total_school_slugs(school_nums)40    def get_total_school_data(self):41        school = dict()42        num = 143        start = time.time()44        with tqdm(total=len(self.slugs)) as bar:45            i = 046            while i < len(self.slugs):47                slug = self.slugs[i]48                try:49                    # print(slug)50                    overview, admission, campus, money, academic, student = dict(), dict(), dict(), dict(), dict(), dict()51                    num += 152                    self.__set_overview(slug, overview, admission)53                    self.__set_admission(slug, admission)54                    self.__set_money(slug, money)55                    self.__set_academic(slug, academic)56                    self.__set_campus(slug, campus)57                    self.__set_student(slug, student)58                    school.update({59                        slug: {60                            "overview": overview,61                            "admissions": admission,62                            "moneys": money,63                            "academics": academic,64                            "campus": campus,65                            "students": student66                        }67                    })68                    i += 169                    bar.update(1)70                except ValueError:71                    continue72        end = time.time()73        print("++++++++ College Data Total ++++++++")74        print(end - start)75        print("++++++++++++++++++++++++++++++++++++")76        return school77    def __set_overview(self, slug, overview, admission):78        # print(f"{slug} overview")79        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}.json"80        res = requests.get(link)81        if res.text and "profile" in json.loads(res.text)["pageProps"]:82            profile = json.loads(res.text)["pageProps"]["profile"]83            entrance_difficulty = profile["bodyContent"][0]["data"]["children"][0]["data"]["value"][0]84            overview.update({85                "name": profile["name"],86                "website": profile["website"],87                "overview": profile["description"]88            })89            admission.update({90                "entrance_difficulty": entrance_difficulty91            })92        else:93            console = Console()94            console.print(f"{slug} overview", style="bold red")95    def __set_admission(self, slug, admission):96        # print(f"{slug} admission")97        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}/admission.json"98        res = requests.get(link)99        if res.text and "profile" in json.loads(res.text)["pageProps"]:100            profile = json.loads(res.text)["pageProps"]["profile"]101            for el in profile["bodyContent"]:102                data = el["data"]103                title = data["title"]104                if "Freshman" in title:105                    freshman_admission_reqs.set_format(data["children"], admission)106                elif "Applying" in title:107                    applying_for_admission.set_format(data["children"], admission)108                elif "Selection" in title:109                    selection_of_student.set_format(data["children"], admission)110                else:111                    profile_of_fall_admission.set_format(data["children"], admission)112        else:113            console = Console()114            console.print(f"{slug} admission", style="bold red")115    def __set_money(self, slug, money):116        # print(f"{slug} money")117        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}/money-matters.json"118        res = requests.get(link)119        if res.text and "profile" in json.loads(res.text)["pageProps"]:120            profile = json.loads(res.text)["pageProps"]["profile"]121            tuition_and_expenses.set_format(profile, money)122            for content in profile["bodyContent"]:123                data = content["data"]124                title = data["title"]125                if "Applying" in title:126                    applying_for_financial_aid.set_format(data["children"], money)127                elif "Profile" in title:128                    profile_of_financial_aid.set_format(data["children"], money)129                elif "Financial" in title:130                    financial_aid_programs.set_format(data["children"], money)131        else:132            console = Console()133            console.print(f"{slug} money", style="bold red")134    def __set_academic(self, slug, academic):135        # print(f"{slug} academic")136        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}/academics.json"137        res = requests.get(link)138        if res.text and "profile" in json.loads(res.text)["pageProps"]:139            profile = json.loads(res.text)["pageProps"]["profile"]140            general_information.set_format(profile, academic)141            for content in profile["bodyContent"]:142                data = content["data"]143                title = data["title"]144                if "Undergraduate" in title:145                    undergraduate_education.set_format(data["children"], academic)146                elif "Curriculum" in title:147                    curriculum_and_graduation_requirements.set_format(data["children"], academic)148                elif "Faculty" in title:149                    faculty_and_instruction.set_format(data["children"], academic)150                elif "Advanced" in title:151                    advanced_placement.set_format(data["children"], academic)152                elif "Academic Resources" in title:153                    academic_resources.set_format(data["children"], academic)154                elif "Academic Support Services" in title:155                    academic_support_services.set_format(data["children"], academic)156                elif "Graduate/Professional" in title:157                    school_education.set_format(data["children"], academic)158        else:159            console = Console()160            console.print(f"{slug} academic", style="bold red")161    def __set_campus(self, slug, campus):162        # print(f"{slug} campus")163        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}/campus-life.json"164        res = requests.get(link)165        if res.text and "profile" in json.loads(res.text)["pageProps"]:166            profile = json.loads(res.text)["pageProps"]["profile"]167            for content in profile["bodyContent"]:168                data = content["data"]169                title = data["title"]170                if title == "Location and Setting":171                    location_and_setting.set_format(data["children"], campus)172                elif title == "Housing":173                    housing.set_format(data["children"], campus)174                elif title == "Security":175                    security.set_format(data["children"], campus)176                elif title == "Personal Support Services":177                    personal_support_services.set_format(data["children"], campus)178                elif title == "Sports & Recreation":179                    sports_and_recreation.set_format(data["children"], campus)180        else:181            console = Console()182            console.print(f"{slug} campus", style="bold red")183    def __set_student(self, slug, student):184        # print(f"{slug} student")185        link = f"https://www.collegedata.com/_next/data/{self.build_id}/college-search/{slug}/students.json"186        res = requests.get(link)187        if res.text and "profile" in json.loads(res.text)["pageProps"]:188            profile = json.loads(res.text)["pageProps"]["profile"]189            for content in profile["bodyContent"]:190                data = content["data"]191                title = data["title"]192                if title == "Student Body":193                    student_body.set_format(data["children"], student)194                elif "Undergraduate Retention" in title:195                    undergraduate_retention_and_graduation.set_format(data["children"], student)196                elif title == "After Graduation":197                    after_graduation.set_format(data["children"], student)198        else:199            console = Console()...dataset.py
Source:dataset.py  
...3import torch4from datasets import Dataset, load_from_disk5def get_amazon_polarity(path_to_dataset: str) -> Dict[str, Dataset]:6    train_dataset = load_from_disk(os.path.join(path_to_dataset, "train"))7    train_dataset.set_format(8        type="torch",9        columns=["input_ids", "token_type_ids", "attention_mask", "labels"],10    )11    val_dataset = load_from_disk(os.path.join(path_to_dataset, "val"))12    val_dataset.set_format(13        type="torch",14        columns=["input_ids", "token_type_ids", "attention_mask", "labels"],15    )16    return {"train": train_dataset, "val": val_dataset}17def get_acronym_identification(path_to_dataset: str) -> Dict[str, Dataset]:18    train_dataset = load_from_disk(os.path.join(path_to_dataset, "train"))19    train_dataset.set_format(20        type="torch",21        columns=["input_ids", "token_type_ids", "attention_mask", "labels"],22    )23    val_dataset = load_from_disk(os.path.join(path_to_dataset, "val"))24    val_dataset.set_format(25        type="torch",26        columns=["input_ids", "token_type_ids", "attention_mask", "labels"],27    )28    return {"train": train_dataset, "val": val_dataset}29def get_swag(path_to_dataset: str) -> Dict[str, Dataset]:30    def set_format(row):31        return {32            "input_ids": torch.tensor(row["input_ids"]),33            "token_type_ids": torch.tensor(row["token_type_ids"]),34            "attention_mask": torch.tensor(row["attention_mask"]),35            "labels": row["labels"],36        }37    train_dataset = load_from_disk(os.path.join(path_to_dataset, "train"))38    train_dataset.set_transform(set_format)39    val_dataset = load_from_disk(os.path.join(path_to_dataset, "val"))40    val_dataset.set_transform(set_format)41    return {"train": train_dataset, "val": val_dataset}42GET_DATASET = {43    "amazon_polarity": get_amazon_polarity,44    "acronym_identification": get_acronym_identification,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
