Process PathwayCommons paths from-to genes, return INDRA Statements.
509278
def biopax_process_pc_pathsfromto():
    """"""
    if request.method == 'OPTIONS':
        return {}
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    source = body.get('source')
    target = body.get('target')
    bp = biopax.process_pc_pathsfromto(source, target)
    return _stmts_from_proc(bp)
1210781
def to_path_globs(self, relpath, conjunction):
    """"""
    return PathGlobs(
      include=tuple(os.path.join(relpath, glob) for glob in self._file_globs),
      exclude=tuple(os.path.join(relpath, exclude) for exclude in self._excluded_file_globs),
      conjunction=conjunction)
1170094
def parse_network(network_fp):
    
    graph = nx.Graph()
    gene_set = set()

    with open(network_fp) as inFile:
        inFile.readline()  # Skip header.
        for line in inFile.readlines():
            gene1, gene2 = line.strip().split()

            graph.add_edge(gene1, gene2)
            gene_set.add(gene1)
            gene_set.add(gene2)

    return graph, gene_set
1150902
def main(args):
   """"""
   txome = Transcriptome()
   #read the reference gpd if one is gven
   if args.reference: 
      rinf = None
      if re.search('\.gz$',args.reference):
         rinf = gzip.open(args.reference)
      else:
         rinf = open(args.reference)
      sys.stderr.write("Reading in reference\n")
      z = 0
      # populate txome with reference transcripts for each chromosome
      for line in rinf:
         z += 1
         gpd = GPD(line)
         gpd.set_payload(z)
         if z%100 == 0:  sys.stderr.write(str(z)+"          \r")
         txome.add_transcript(gpd)
      rinf.close()
      sys.stderr.write(str(z)+"          \r")
      sys.stderr.write("\n")
   txome.sort_transcripts()
   sys.stderr.write("Buffering mappings\n")
   inf = sys.stdin
   if args.input != '-':
      if re.search('\.gz$',args.input):
         inf = gzip.open(args.input)
      else:
         inf = open(args.input)
   tof = gzip.open(args.tempdir+'/reads.gpd.gz','w')
   for line in inf: tof.write(line.rstrip()+"\n")
   tof.close()
  
   sys.stderr.write("1. Process by overlapping locus.\n")
   annotated_reads(txome,gzip.open(args.tempdir+'/reads.gpd.gz'),'initial',args)

   #of.close()

   """Now sequences we should be able to annotate as partial have
      consensus transcripts.  Lets put those to annotation scrutiny again.
   """
   sys.stderr.write("2. Reannotated partially annotated by overlapping locus.\n")
   annotated_reads(txome,gzip.open(args.tempdir+'/initial/partial_annotated_multiexon.sorted.gpd.gz'),'repartial',args)


   txs = {}
   for transcript in txome.transcripts:
      txs[transcript.name] = transcript
   detected = {}
   tinf = gzip.open(args.tempdir+'/initial/annotated.txt.gz')
   for line in tinf:
      f = line.rstrip().split("\t")
      detected[f[1]] = True
   tinf.close()
   tinf = gzip.open(args.tempdir+'/repartial/annotated.txt.gz')
   remove_partial = {}
   for line in tinf:
      f = line.rstrip().split("\t")
      detected[f[1]] = True
      remove_partial[f[1]] = True
   tinf.close()
   tof = gzip.open(args.tempdir+'/candidate.gpd.gz','w')
   for name in detected:
      tof.write(txs[name].get_gpd_line()+"\n")
   tinf = gzip.open(args.tempdir+'/initial/partial_annotated_multiexon.sorted.gpd.gz')
   for line in tinf:
      f = line.rstrip().split("\t")
      if f[1] not in remove_partial:
         tof.write(line)
   tinf = gzip.open(args.tempdir+'/initial/unannotated_singleexon.sorted.gpd.gz')
   for line in tinf:
      f = line.rstrip().split("\t")
      f[0] = str(uuid.uuid4())
      tof.write("\t".join(f)+"\n")
   tinf.close()
   tof.close()
   sort_gpd(args.tempdir+'/candidate.gpd.gz',args.tempdir+'/candidate.sorted.gpd.gz',args)
   """We can ignore the partial annotations that have been detected"""
   ntxome = Transcriptome()
   tinf = gzip.open(args.tempdir+'/candidate.sorted.gpd.gz')
   for line in tinf: ntxome.add_transcript(GPD(line))     
   annotated_reads(ntxome,gzip.open(args.tempdir+'/initial/unannotated_multiexon.sorted.gpd.gz'),'unannotated',args)
   """now we know which unannotated reads actually have annotations"""
   tinf.close()

   tof = gzip.open(args.tempdir+'/final.gpd.gz','w')
   tinf = gzip.open(args.tempdir+'/candidate.sorted.gpd.gz')   
   for line in tinf: tof.write(line)
   tinf.close()
   tinf = gzip.open(args.tempdir+'/unannotated/unannotated_multiexon.sorted.gpd.gz')   
   for line in tinf: tof.write(line)
   tinf.close()
   tinf = gzip.open(args.tempdir+'/unannotated/partial_annotated_multiexon.sorted.gpd.gz')   
   for line in tinf: tof.write(line)
   tinf.close()
   tof.close()
   sort_gpd(args.tempdir+'/final.gpd.gz',args.tempdir+'/final.sorted.gpd.gz',args)
   """Prepare outputs"""
   of = sys.stdout
   if args.output:
      if re.search('\.gz$',args.output):
         of = gzip.open(args.output,'w')
      else:
         of = open(args.output,'w')
   tinf = gzip.open(args.tempdir+'/final.sorted.gpd.gz')
   for line in tinf: of.write(line)
   tinf.close()
   of.close()
1118889
def parseNoise(rawArray): 
    '''
    
    '''
    nnGenes=[] 
    for i in range(0,(rawArray.shape[1])): #Checks all genes
        count0=np.asarray(np.where(rawArray[:,i]==0))
        count1=np.asarray(np.where(rawArray[:,i]==1))
        if ((count1.shape[1]+count0.shape[1])<rawArray.shape[0]):
            nnGenes=np.append(nnGenes,i)
    return nnGenes.astype(int)
924478
def _get_result_paths(self, data):
        

        result = {}

        # get the file extension of the reads file (sortmerna
        # internally outputs all results with this extension)
        fileExtension = splitext(self.Parameters['--reads'].Value)[1]

        # at this point the parameter --aligned should be set as
        # sortmerna will not run without it
        if self.Parameters['--aligned'].isOff():
            raise ValueError("Error: the --aligned parameter must be set.")

        # file base name for aligned reads
        output_base = self.Parameters['--aligned'].Value

        # Blast alignments
        result['BlastAlignments'] =\
            ResultPath(Path=output_base + '.blast',
                       IsWritten=self.Parameters['--blast'].isOn())

        # SAM alignments
        result['SAMAlignments'] =\
            ResultPath(Path=output_base + '.sam',
                       IsWritten=self.Parameters['--sam'].isOn())

        # OTU map (mandatory output)
        result['OtuMap'] =\
            ResultPath(Path=output_base + '_otus.txt',
                       IsWritten=self.Parameters['--otu_map'].isOn())

        # FASTA file of sequences in the OTU map (madatory output)
        result['FastaMatches'] =\
            ResultPath(Path=output_base + fileExtension,
                       IsWritten=self.Parameters['--fastx'].isOn())

        # FASTA file of sequences not in the OTU map (mandatory output)
        result['FastaForDenovo'] =\
            ResultPath(Path=output_base + '_denovo' +
                       fileExtension,
                       IsWritten=self.Parameters['--de_novo_otu'].isOn())
        # Log file
        result['LogFile'] =\
            ResultPath(Path=output_base + '.log',
                       IsWritten=self.Parameters['--log'].isOn())

        return result
218926
def get_gene_seqs(database_path, gene):
    
    gene_path = database_path + "/" + gene + ".fsa"
    gene_seq = ""
    # Open fasta file
    with open(gene_path) as gene_file:
        header = gene_file.readline()
        for line in gene_file:
            seq = line.strip()
            gene_seq += seq
    return gene_seq
249872
def from_gene_ids_and_names(cls, gene_names: Dict[str, str]):
        """"""
        genes = [ExpGene(id_, name=name) for id_, name in gene_names.items()]
        return cls.from_genes(genes)
704847
def get_adjacent_pathways(self, pathway):
        
        vertex_id = self.pathways[pathway]
        adjacent = self.vertices[vertex_id].get_adjacent_vertex_ids()
        adjacent_pathways = []
        for adjacent_id in adjacent:
            adjacent_pathways.append(self.get_pathway_from_vertex_id(
                adjacent_id))
        return adjacent_pathways
913918
def _process_pathway_ko(self, limit):
        
        LOG.info("Processing KEGG pathways to kegg ortholog classes")
        if self.test_mode:
            graph = self.testgraph
        else:
            graph = self.graph
        line_counter = 0

        raw = '/'.join((self.rawdir, self.files['pathway_ko']['file']))
        with open(raw, 'r', encoding="iso-8859-1") as csvfile:
            filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
            for row in filereader:
                line_counter += 1
                (ko_id, pathway_id) = row

                if self.test_mode and pathway_id not in self.test_ids['pathway']:
                    continue

                pathway_id = 'KEGG-' + pathway_id
                ko_id = 'KEGG-' + ko_id

                p = Pathway(graph)
                p.addGeneToPathway(ko_id, pathway_id)

                if not self.test_mode and limit is not None and line_counter > limit:
                    break

        return
85871
def _get_genesplicer(data):
    

    genesplicer_exec = os.path.realpath(config_utils.get_program("genesplicer", data["config"]))
    genesplicer_training = tz.get_in(("genome_resources", "variation", "genesplicer"), data)
    if genesplicer_exec and os.path.exists(genesplicer_exec) and genesplicer_training and os.path.exists(genesplicer_training) :
        return ["--plugin", "GeneSplicer,%s,%s" % (genesplicer_exec,genesplicer_training)]
    else:
        return []
140833
def _process_features(self, limit):
        

        if self.test_mode:
            graph = self.testgraph
        else:
            graph = self.graph
        model = Model(graph)
        raw = '/'.join((self.rawdir, 'feature'))
        LOG.info("building labels for features")

        line_counter = 0
        with open(raw, 'r') as f:
            filereader = csv.reader(f, delimiter='\t', quotechar='\"')
            f.readline()  # read the header row; skip
            for line in filereader:
                (feature_id, dbxref_id, organism_id, name, uniquename,
                 residues, seqlen, md5checksum, type_id, is_analysis,
                 timeaccessioned, timelastmodified) = line

                feature_key = feature_id
                if re.search(r'[\|\s\[\]\{\}\\<\>]', uniquename):
                    # some uniquenames have pipes or other nasty chars!
                    # for example: FB||||FBrf0133242|Hugh-u1
                    feature_id = self._makeInternalIdentifier('feature', feature_key)
                else:
                    feature_id = 'FlyBase:' + uniquename
                self.idhash['feature'][feature_key] = feature_id
                self.feature_types[feature_key] = type_id
                self.label_hash[feature_id] = name

                if feature_key not in self.feature_to_organism_hash:
                    self.feature_to_organism_hash[feature_key] = set()
                self.feature_to_organism_hash[feature_key].add(organism_id)

                # HACK - FBgn are genes, and therefore classes,
                # all else be individuals
                is_gene = False
                if re.search(r'(FBgn|FBog)', feature_id):
                    self.idhash['gene'][feature_key] = feature_id
                    is_gene = True
                elif re.search(r'FBa[lb]', feature_id):
                    self.idhash['allele'][feature_key] = feature_id
                elif re.search(r'FBt[ip]', feature_id):
                    self.idhash['feature'][feature_key] = feature_id

                if self.test_mode and \
                        feature_key not in self.test_keys['gene'] and \
                        feature_key not in self.test_keys['allele'] and \
                        feature_key not in self.test_keys['feature']:
                    continue

                # now do something with it!
                # switch on type_id
                if name.strip() == '':
                    name = uniquename

                type_key = type_id
                type_id = self.idhash['cvterm'][type_key]

                # skip some features by type
                types_to_skip = [
                    'SO:0000316',  # CDS
                    'SO:0000696',  # oligos
                    'SO:0000358',  # polypeptide
                    'SO:0000234',  # transcripts
                    ]

                type_keys_to_skip = [
                    596,    # pcr_product
                    57096,  # mature peptide
                    57097,  # signal_peptide
                    57270,  # repeat masker
                    58210,  # alignment
                    59643,  # cDNA_clone
                    60006,  # uncharacterized_change_in_nucleotide_sequence
                    61351,  # oligo
                    61467,  # polypeptide_domain
                    257,    # exon
                    286,    # intron
                ]

                organisms_to_skip = [
                    2  # computational result
                ]

                if type_id in types_to_skip \
                        or int(type_key) in type_keys_to_skip\
                        or int(organism_id) in organisms_to_skip:
                    continue

                line_counter += 1

                if int(type_key) == 604:  # RNAi_reagent
                    # TODO add other reagents?
                    self.idhash['reagent'][feature_key] = feature_id

                # deal with the taxonomy
                # only get taxa for features that are actually used in our set
                tax_internal_id = self._makeInternalIdentifier(
                    'organism', organism_id)
                if organism_id not in self.checked_organisms:
                    # will get the NCBITax if necessary
                    tax_id = self._get_organism_id(organism_id)
                    self.checked_organisms.add(organism_id)
                else:
                    tax_id = self.idhash['organism'][organism_id]

                tax_label = self.label_hash.get(tax_id)
                if not re.search(r'FBog', feature_id) and \
                        re.search(r'Drosophila', tax_label):
                    # make only fly things leaders
                    model.makeLeader(feature_id)

                if not self.test_mode and limit is not None and line_counter > limit:
                    pass
                else:
                    if is_gene:
                        model.addClassToGraph(feature_id, name, type_id)
                        graph.addTriple(feature_id, self.globaltt['in taxon'], tax_id)
                    else:
                        if re.search('FBa[lb]', feature_id):
                            type_id = self.globaltt['allele']
                        model.addIndividualToGraph(feature_id, name, type_id)

                    # stop adding what we do not appreciate
                    # if is_obsolete == 't':
                    #    if is_gene:
                    #        model.addDeprecatedClass(feature_id)
                    #    else:
                    #        model.addDeprecatedIndividual(feature_id)
                    #    self.deprecated_features.add(feature_key)

                    model.addClassToGraph(tax_id)
                    if tax_id != tax_internal_id:
                        model.addEquivalentClass(tax_id, tax_internal_id)

                    model.addComment(
                        feature_id,
                        self._makeInternalIdentifier('feature', feature_key))

            # TODO save checked_organisms fbid to ncbitax mapping to
            # a local file to speed up subsequent searches

        return
1203916
def _get_result_paths(self, data):
        
        result = {}

        # Always output:
        result['UnassembledReads1'] = ResultPath(Path=
                                                 self._unassembled_reads1_out_file_name(
                                                 ),
                                                 IsWritten=True)
        result['UnassembledReads2'] = ResultPath(Path=
                                                 self._unassembled_reads2_out_file_name(
                                                 ),
                                                 IsWritten=True)

        # optional output, so we check for each
        # check for assembled reads file
        if self.Parameters['-s'].isOn():
            result['Assembled'] = ResultPath(Path=
                                             self._assembled_out_file_name(),
                                             IsWritten=True)

        # check for discarded (unassembled) reads1 file
        if self.Parameters['-3'].isOn():
            result['Reads1Discarded'] = ResultPath(Path=
                                                   self._discarded_reads1_out_file_name(
                                                   ),
                                                   IsWritten=True)

        # check for discarded (unassembled) reads2 file
        if self.Parameters['-4'].isOn():
            result['Reads2Discarded'] = ResultPath(Path=
                                                   self._discarded_reads2_out_file_name(
                                                   ),
                                                   IsWritten=True)

        # check for pretty-alignment file
        if self.Parameters['-E'].isOn():
            result['PrettyAlignments'] = ResultPath(Path=
                                                    self._pretty_alignment_out_file_name(
                                                    ),
                                                    IsWritten=True)

        return result
842765
def _apply_correction_on_genes(genes,
                               pathway_column_names,
                               pathway_definitions):
    
    gene_row_names = index_element_map(genes)
    membership_matrix = initialize_membership_matrix(
        genes, pathway_definitions)
    crosstalk_corrected_index_map = maximum_impact_estimation(
        membership_matrix)

    updated_pathway_definitions = _update_pathway_definitions(
        crosstalk_corrected_index_map,
        gene_row_names, pathway_column_names)
    return updated_pathway_definitions
879993
def FromFile(cls, inpath):
        

        with open(inpath, "r") as infile:
            indata = infile.read()

        return cls.FromString(indata)
994719
def _construct_from_dataframe(self):
        
        direct_edges = {}
        feature_side_grouping = self.feature_pathway_df.groupby(
            ["feature", "side"])
        for (feature, _), group in feature_side_grouping:
            co_occurring_pathways = group["pathway"].tolist()
            pairings = list(itertools.combinations(co_occurring_pathways, 2))
            if not pairings:
                continue
            self.features.add(feature)
            for pathway0, pathway1 in pairings:
                vertex0_id = self.add_pathway(pathway0)
                vertex1_id = self.add_pathway(pathway1)
                new_edge = self.edge_tuple(vertex0_id, vertex1_id)
                if new_edge not in direct_edges:
                    direct_edges[new_edge] = []
                direct_edges[new_edge].append(feature)
        self._augment_network(direct_edges)
80381
def _edges_from_permutation(self, feature_pathway_dict):
        
        network_edges = {}
        for feature, pathway_list in feature_pathway_dict.items():
            for i in range(len(pathway_list)):
                for j in range(i + 1, len(pathway_list)):
                    vertex_i = pathway_list[i]
                    vertex_j = pathway_list[j]
                    new_edge = self.edge_tuple(vertex_i, vertex_j)
                    if new_edge not in network_edges:
                        network_edges[new_edge] = []
                    network_edges[new_edge].append(feature)
        self._augment_network(network_edges)