def test_class_sequence_name(self): cases_seq = Sequence("cases") invoices_seq = Sequence("invoices") self.assertEqual(cases_seq.get_last_value(), None) self.assertEqual(cases_seq.get_next_value(), 1) self.assertEqual(cases_seq.get_last_value(), 1) self.assertEqual(cases_seq.get_next_value(), 2) self.assertEqual(invoices_seq.get_last_value(), None) self.assertEqual(invoices_seq.get_next_value(), 1) self.assertEqual(invoices_seq.get_last_value(), 1) self.assertEqual(invoices_seq.get_next_value(), 2)
def load_from_alignment_file(self, filename): ''' Loads the sequences from the given sequence file (does not need to be an aligned file) ''' self.seq_file = filename self.sequences = Sequence.readSequences(filename)
def test_class_defaults(self): seq = Sequence() self.assertEqual(seq.get_last_value(), None) self.assertEqual(seq.get_next_value(), 1) self.assertEqual(seq.get_last_value(), 1) self.assertEqual(seq.get_next_value(), 2) self.assertEqual(seq.get_last_value(), 2) self.assertEqual(seq.get_next_value(), 3)
def new_sequence(json): try: new_sequence = Sequence(json['name'], json['directory'], json['camera'], json.get('filterWheel')) controller.sequences.append(new_sequence) return new_sequence.to_map() except KeyError: raise BadRequestError('Invalid json')
def load_scores(self, gene_family): scores = [[0 for col in range(len(gene_family.sequences))] for row in range(len(gene_family.sequences))] #cluster won't work if nbseqs <= 2. In this case, we'll just throw out some scores if len(gene_family.sequences) == 1: scores[0][0] = 1 gene_family.scores = scores return elif len(gene_family.sequences) == 2: scores[0][0] = 1 scores[0][1] = 1 scores[1][0] = 1 scores[1][1] = 1 gene_family.scores = scores return cldealignedfile = gene_family.seq_file + ".dealigned.fa" Sequence.outputSequencesToFasta(gene_family.sequences, cldealignedfile) clfile = gene_family.seq_file + ".clustal.fa" pfile = gene_family.seq_file + ".clustal.pctid" cmd = "clustalo -i " + cldealignedfile + " -o " + clfile + " --distmat-out=" + pfile + " --percent-id --full" print("EXEC " + cmd) if not os.path.isfile(pfile) or not "use_cache" in self.other_args: os.system(cmd) else: print("Actually, file exists and we'll use it.") d = Distances.readMatrixFile(pfile, "count", " ") genes = d["labels"] matrix = d["matrix"] genes_pos = {} for i in range(len(genes)): genes_pos[genes[i]] = i for i in range(len(gene_family.sequences)): for j in range(len(gene_family.sequences)): scores[i][j] = matrix[genes_pos[gene_family.sequences[ i].name]][genes_pos[gene_family.sequences[j].name]] gene_family.scores = scores
def length(self, text): """T.length(text) -> int Return the printable length of string ``text``, which may contain terminal sequences. Strings containing sequences such as 'clear', which repositions the cursor, does not give accurate results, and their printable length is evaluated *0*.. """ return Sequence(text, self).length()
def strip(self, text): """T.strip(text) -> unicode Return string ``text`` stripped of its whitespace *and* sequences. Text containing backspace or term.left will "overstrike", so that the string ``u"_\\b"`` or ``u"__\\b\\b="`` becomes ``u"x"``, not ``u"="`` (as would actually be printed on a terminal). """ return Sequence(text, self).strip()
def center(self, text, width=None, fillchar=u' '): """T.center(text, [width], [fillchar]) -> unicode Return string ``text``, centered by printable length ``width``. Padding is done using the specified fill character (default is a space). Default ``width`` is the attached terminal's width. ``text`` may contain terminal sequences.""" if width is None: width = self.width return Sequence(text, self).center(width, fillchar)
def load_scores(self, gene_family): dealignedfile = gene_family.seq_file + ".dealigned.fa" Sequence.outputSequencesToFasta(gene_family.sequences, dealignedfile) fsafile = gene_family.seq_file + ".fsa.fa" cmd = "fsa " + dealignedfile + " > " + fsafile print("EXEC " + cmd) if not os.path.isfile( fsafile ) or not "use_cache" in self.other_args or os.path.getsize( fsafile) <= 10: os.system(cmd) else: print("Actually, file exists and we'll use it.") seqs = Sequence.readSequences(fsafile) gene_family.scores = Distances.getPairwisePctID(sequences=seqs, verbose=False, run_nw_algorithm=False)
def new_sequence(json): new_sequence = Sequence(json['name'], json['directory'], json['camera'], json.get('filterWheel')) controller.sequences.append(new_sequence) return new_sequence.to_map()
def import_sequence(json): sequence = Sequence.import_from_data(json) controller.sequences.append(sequence) return sequence.to_map()
class YouthProfile(AuditLogModel, UUIDModel, SerializableMixin): user = models.OneToOneField( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE ) # Post-save signal generates the membership number membership_number = models.CharField( max_length=16, blank=True, help_text=_("Youth's membership number") ) birth_date = models.DateField() school_name = models.CharField(max_length=128, blank=True) school_class = models.CharField(max_length=10, blank=True) expiration = models.DateField(default=calculate_expiration) language_at_home = EnumField( LanguageAtHome, max_length=32, default=LanguageAtHome.FINNISH ) # Permissions approver_first_name = models.CharField(max_length=255, blank=True) approver_last_name = models.CharField(max_length=255, blank=True) approver_phone = models.CharField(max_length=50, blank=True) approver_email = models.EmailField(max_length=254, blank=True) approval_token = models.CharField(max_length=36, blank=True, editable=False) approval_notification_timestamp = models.DateTimeField( null=True, blank=True, editable=False ) approved_time = models.DateTimeField(null=True, blank=True, editable=False) photo_usage_approved = models.NullBooleanField() profile_access_token = models.CharField( max_length=36, blank=True, help_text=_( "Temporary read access token for the profile linked to this youth profile." ), ) profile_access_token_expiration = models.DateTimeField(null=True, blank=True) # Source sequence of integer values for a membership number. membership_number_sequence = Sequence("membership_number") def make_approvable(self, youth_name: str): self.approval_token = uuid.uuid4() send_notification( email=self.approver_email, notification_type=NotificationType.YOUTH_PROFILE_CONFIRMATION_NEEDED.value, context={ "youth_profile": self, "youth_name": youth_name, "youth_membership_ui_base_url": settings.EMAIL_TEMPLATE_YOUTH_MEMBERSHIP_UI_BASE_URL, }, language=self.language_at_home.value, ) self.approval_notification_timestamp = timezone.now() def set_approved(self, save=False): """Set profile as approved and remove access tokens.""" self.approved_time = timezone.now() self.approval_token = "" self.profile_access_token = "" self.profile_access_token_expiration = None if save: self.save( update_fields=( "approved_time", "approval_token", "profile_access_token", "profile_access_token_expiration", ) ) @property def membership_status(self): """Current membership status of the youth profile. - ACTIVE = membership is active - PENDING = membership is waiting for approval and is either inactive or expired - RENEWING = membership is active, but renewal is waiting for approval - EXPIRED = membership has expired """ if self.expiration < date.today(): # Membership is considered valid on the expiration date return MembershipStatus.EXPIRED elif not self.approved_time: return MembershipStatus.PENDING approved_period_expiration = calculate_expiration(self.approved_time.date()) if self.expiration > approved_period_expiration: # If current expiration is greater than the approved expiration, the status is # considered to be RENEWING, since it requires additional approval from a guardian. # If approved expiration date is in the past, the status is PENDING. if approved_period_expiration < date.today(): return MembershipStatus.PENDING return MembershipStatus.RENEWING return MembershipStatus.ACTIVE @property def renewable(self): return self.membership_status == MembershipStatus.EXPIRED or ( bool(self.approved_time) and self.expiration != calculate_expiration(date.today()) ) def __str__(self): if self.user: return "{} {} ({})".format( self.user.first_name, self.user.last_name, self.pk ) else: return str(self.pk) def save(self, *args, **kwargs): self.full_clean() return super().save(*args, **kwargs) serialize_fields = ( {"name": "birth_date", "accessor": lambda x: x.strftime("%Y-%m-%d")}, {"name": "school_name"}, {"name": "school_class"}, {"name": "language_at_home", "accessor": lambda x: x.value}, {"name": "approver_first_name"}, {"name": "approver_last_name"}, {"name": "approver_phone"}, {"name": "approver_email"}, {"name": "expiration", "accessor": lambda x: x.strftime("%Y-%m-%d %H:%M")}, {"name": "photo_usage_approved"}, {"name": "additional_contact_persons"}, )
from typing import Optional from fractions import Fraction from django.db import models from django.utils.translation import gettext_lazy as _ from django.db.models import F from django.db.transaction import atomic from ool import VersionField, VersionedMixin from sequences import Sequence positions_seq = Sequence('bookshelf_items_order') class Bookshelf(VersionedMixin, models.Model): """Represents Bookshelf """ version = VersionField() title = models.CharField(max_length=256, verbose_name=_("Bookshelf name")) def __str__(self): return self.title class BookshelfItemManager(models.Manager): def get_queryset(self): """Apply fractional order, In production use db specific speedups such as functional indices """ return (super().get_queryset().annotate( _position=F('pos_numerator') / F('pos_denominator'),
def test_class_reset_value_smaller_than_initial_value(self): with self.assertRaises(AssertionError): Sequence("error", initial_value=1, reset_value=1)
def test_class_reset_value(self): reference_seq = Sequence("reference", reset_value=3) self.assertEqual(reference_seq.get_next_value(), 1) self.assertEqual(reference_seq.get_next_value(), 2) self.assertEqual(reference_seq.get_next_value(), 1) self.assertEqual(reference_seq.get_next_value(), 2)
def test_class_initial_value(self): customers_seq = Sequence("customers", initial_value=1000) self.assertEqual(customers_seq.get_next_value(), 1000) self.assertEqual(customers_seq.get_next_value(), 1001)
def strip_seqs(self, text): """T.strip_seqs(text) -> unicode Return string ``text`` stripped only of its sequences. """ return Sequence(text, self).strip_seqs()
def test_class_next(self): seq = Sequence(initial_value=0) for value in range(5): self.assertEqual(next(seq), value)
def test_class_iter(self): seq = Sequence(initial_value=0) self.assertEqual(list(itertools.islice(seq, 5)), list(range(5)))
from django.shortcuts import render, redirect from .models import Product, OrderHead, UserCart, OrderDetail, Counter, Category from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import HttpResponseRedirect from django.db.models import Sum, F, FloatField from django.db import transaction from sequences import Sequence from django.contrib.auth import settings order_num = Sequence("order_num") # Create your views here. """ def index(request): products_list = Product.objects.all().order_by('id') item_name = request.GET.get('item_name') if item_name!='' and item_name is not None: products_list = products_list.filter(name__icontains=item_name) paginator = Paginator(products_list,8) page = request.GET.get('page') products_list = paginator.get_page(page) context = { 'product_list':products_list, } if request.method == "POST": if request.user.is_authenticated: print('authenticated') item_id = Product.objects.get(id=request.POST["item_id"]) user = request.user add_to_cart(item_id,user) return redirect('shop:index') else :
from sequences import Sequence from sequences.examples import multiplicationReduction_GenerationFunction, lowestAvailable_GenerationFunction import numpy if __name__ == "__main__": S = Sequence(lowestAvailable_GenerationFunction, numpy.array([[0]])) S.generateTerms(1e4) print(S.values) S.showSequence()
#here's an annoying problem: when having multiple input files, some (distinct) genes will have the same name #(this happens with simphy) #so here we copy the alginment files, but rename the genes with its file index newinfiles = "" files_list = infiles.split(",") newfile_to_old_file = {} if rename_genes: print("Copying alignment files, ensuring gene name uniqueness...") for i in range(len(files_list)): f = files_list[i] filename, ext = os.path.splitext(f) newfile = join(workdir, os.path.basename(f).replace(ext, "_" + str(i) + ".fa")) sequences = Sequence.readSequences(f) Sequence.outputSequencesToFasta(sequences, newfile, str(i), True) if newinfiles != "": newinfiles += "," newinfiles += newfile newfile_to_old_file[newfile] = f infiles = newinfiles else: for i in range(len(files_list)): f = files_list[i] newfile_to_old_file[f] = f #############################################################################################
def predict_orthologs(self, files, workdir, speciestree_file): runOMA = True if os.path.isfile(join(workdir, "Output/OrthologousGroups.txt") ) and "use_cache" in self.other_args: print("Will use cached output files from oma") runOMA = False #quite a few preprocessing steps are needed for OMA. #first, we create directories and files required. dbdir = join(workdir, "DB") if not os.path.exists(dbdir): os.mkdir(dbdir) #OMA requires one file per species. We split our files accordingly here. seqs_by_species = Sequence.combineSequenceFilesBySpecies( files, self.other_args["species_separator"], int(self.other_args["species_index"])) if speciestree_file == "": print("OMA is unpredicatable without a species tree.") spprefix = "" if "species_prefix" in self.other_args: spprefix = self.other_args["species_prefix"] doAA = False if "convertToAA" in self.other_args: doAA = True species_list = "" genes_list = [] for key in seqs_by_species: outfile = join(dbdir, spprefix + key + ".fa") Sequence.outputSequencesToFasta(sequences=seqs_by_species[key], filename=outfile, name_suffix="", aligned=False, convertToAA=doAA, name_prefix=spprefix) if species_list != "": species_list += "," species_list += key for seq in seqs_by_species[key]: genes_list.append(seq.name) #if we use a species tree, OMA requires removing species not appearing in the files #sgutils can restrict the species tree to a subset of species. if speciestree_file != "": oma_sptree_file = join(workdir, "oma_species_tree.nw") with open(speciestree_file, 'r') as myfile: speciestree_newick = myfile.read().replace('\n', '') cmd = "OCR -m restrict_species_tree -l \"" + species_list + "\" -s \"" + speciestree_newick + "\" -o \"" + oma_sptree_file + "\"" print("EXEC " + cmd) os.system(cmd) f = open(oma_sptree_file) speciestree_newick_restricted = f.readline().replace("\n", "") f.close() ################################################################ #special case here: if only one species present, oma makes an error. #In this case, we make 1 gene = 1 cluster if "," not in speciestree_newick_restricted: print("Only one species found. Will make all genes paralogs.") self.clusters_filenames = [join(workdir, "oma.clusters")] self.relations_filenames = [join(workdir, "oma.relations")] clusters = [] for g in genes_list: clusters.append([g]) write_clusters(self.clusters_filenames[0], clusters) f = open(self.relations_filenames[0], 'w') tmp = "" for a in range(len(genes_list)): for b in range(a + 1, len(genes_list)): tmp += genes_list[a] + "\t" + genes_list[ b] + "\t" + "Paralogs;;" tmp = tmp[0:-2] f.write(tmp) f.close() return ################################################################ input_type = "DNA" if "seqtype" in self.other_args and self.other_args["seqtype"] == "AA": print("Input type set to AA") input_type = self.other_args["seqtype"] #now, get into oma dir and execute it cwd = os.getcwd() os.chdir(workdir) os.system("rm parameters.drw") os.system("OMA -p") #this creates a default config file. #we must edit the config to put "DNA", and give the species tree restrcted to the genes at hand. outcfg = "" f = open("parameters.drw") for line in f: line = line.replace("\n", "") if line.startswith("InputDataType"): line = "InputDataType := '" + input_type + "';" elif line.startswith("SpeciesTree") and speciestree_file != "": line = "SpeciesTree := '" + speciestree_newick_restricted + "';" outcfg += line + "\n" f.close() f = open("parameters.drw", 'w') f.write(outcfg) f.close() print("Config edited") cmd = "OMA -n 7" print("EXEC " + cmd) if runOMA: os.system(cmd) else: print("Not really - using cache instead.") os.chdir(cwd) #now that the inference is done, we translate the oma output into our format. #the result is a oma.clusters file and a oma.relations file. self.parse_groups(genes_list, workdir) self.parse_relations(genes_list, workdir) self.clusters_filenames = [join(workdir, "oma.clusters")] self.relations_filenames = [join(workdir, "oma.relations")]
def predict_orthologs(self, files, workdir, speciestree_file): workdir_seqs = join(workdir, "in") workdir_out = join(workdir, "out") allseqs = [] for f in files: seqs = Sequence.readSequences(f) for s in seqs: allseqs.append(s) #one file per species, see OMA comments above if len(self.cached_clusters) == 0: seqs_by_species = Sequence.combineSequenceFilesBySpecies( files, self.other_args["species_separator"], int(self.other_args["species_index"])) isdna = True if "seqtype" in self.other_args and self.other_args[ "seqtype"] == "AA": print("Input type set to AA") isdna = False if not os.path.exists(workdir_seqs): os.mkdir(workdir_seqs) for key in seqs_by_species: outfile = join(workdir_seqs, key + ".fasta") Sequence.outputSequencesToFasta(sequences=seqs_by_species[key], filename=outfile, name_suffix="", aligned=False, convertToAA=isdna, name_prefix="") cmd = "/u/lafonman/src/orthomcl-pipeline/bin/orthomcl-pipeline -i " + workdir_seqs + " -o " + workdir_out + " -m /u/lafonman/src/orthomcl-pipeline/orthomcl.conf --nocompliant --yes" print("EXEC " + cmd) os.system(cmd) seen_genes = set() clusters = [] clfile = join(workdir_out, "groups/groups.txt") f = open(clfile, 'r') for line in f: line = line.replace("\n", "") if line != "": gz = line.split(":")[1].split() cluster = set() for g in gz: gname = g.split("|")[1] cluster.add(gname) seen_genes.add(gname) clusters.append(cluster) f.close() for s in allseqs: name = s.name if not name in seen_genes: cl = set() cl.add(name) clusters.append(cl) self.cached_clusters = clusters else: print("USING CACHED CLUSTERS") clusters = self.cached_clusters #restrict clusters to current family #f_set = set() #for s in gene_family.sequences: # f_set.add(s.name) #f_clusters = [] #for cl in clusters: # inter = f_set.intersection(cl) # if len(inter) > 0: # f_clusters.append(inter) self.clusters_filenames = [join(workdir, "orthomcl.clusters")] self.relations_filenames = [join(workdir, "orthomcl.relations")] write_clusters(self.clusters_filenames[0], clusters) #output relations relstr = "" seen_keys = {} for c in clusters: for c1 in c: for c2 in c: if c1 != c2: key1 = c1 + ";;" + c2 key2 = c2 + ";;" + c1 if key1 not in seen_keys and key2 not in seen_keys: seen_keys[key1] = 1 seen_keys[key2] = 1 relstr += c1 + "\t" + c2 + "\t" sp1 = c1.split( self.other_args["species_separator"])[int( self.other_args["species_index"])] sp2 = c2.split( self.other_args["species_separator"])[int( self.other_args["species_index"])] if sp1 != sp2: relstr += "Orthologs" else: relstr += "Paralogs" relstr += ";;" relstr = relstr[0:-2] #extra ;; at end for i in range(len(allseqs)): for j in range(i + 1, len(allseqs)): n1 = allseqs[i].name n2 = allseqs[j].name key = n1 + ";;" + n2 if not key in seen_keys: relstr += n1 + "\t" + n2 + "\t" + "Paralogs" + ";;" f = open(self.relations_filenames[0], 'w') f.write(relstr) f.close()