<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import os
from os import listdir
from os.path import isfile, join
import networkx as nx
import networkxgmml
import re
import regexes


def TargetInteractionFinder(
        source_xgmml, node_ids=None, node_id_list_column_index=0,
        output_dir='./', debug=False):
    def print_debug(message):
        if debug:
            print message

    def get_file_paths(filelike_object, file_extension):
        if hasattr(filelike_object, 'name'):
            return filelike_object.name
        else:
            source_xgmml_file_paths = []
            if os.path.isdir(filelike_object):
                source_xgmml_file_paths += [filelike_object + f for f in
                                            listdir(filelike_object)
                                            if isfile(join(filelike_object, f))
                                            and has_file_extension(
                                                f, file_extension)]
            elif os.path.isfile(filelike_object):
                source_xgmml_file_paths += [filelike_object]
            else:
                print filelike_object
                raise ValueError('File or directory input error.')

            return source_xgmml_file_paths

    def has_file_extension(filename, expected_file_extension):
        expected_index = len(filename) - len(expected_file_extension)
        actual_index = filename.find(
            expected_file_extension, expected_index, len(filename))
        return actual_index == expected_index

    def has_matching_node(current_mapping_graph, node_id):
        verified_node_id = None
        if current_mapping_graph.has_node(node_id):
            verified_node_id = node_id
        else:
            for onenode in current_mapping_graph.nodes():
                current_node = current_mapping_graph.node[onenode]
                if ((('identifiers' in current_node)
                    and (node_id in current_node['identifiers']))
                    or (('@label' in current_node)
                        and (node_id == current_node['@label']))
                        or (('label' in current_node)
                            and (node_id == current_node['label']))
                        or (('mimat id' in current_node)
                            and (node_id == current_node['mimat id']))
                        or (('name' in current_node)
                            and (node_id == current_node['name']))):
                    if (('mimat id' in current_node)
                            and (node_id == current_node['mimat id'])):
                        verified_node_id = onenode
                        break
                    elif (('identifiers' in current_node)
                            and (node_id in current_node['identifiers'])):
                        verified_node_id = onenode
                    elif not verified_node_id:
                        verified_node_id = onenode
        return verified_node_id

    identifiers_org_warning_triggered = False

    results_file_path = output_dir + 'interactions.csv'
    results_file = open(results_file_path, 'w')
    results_file.write('queryid,targetid,score,pvalue,pmid,datasource\n')
    results_file.close()

    logfile_path = output_dir + 'summary.log'
    open(logfile_path, 'w').close()

    log_result = dict()
    log_result['total_result_count'] = 0
    log_result['skipped_count'] = 0
    log_result['results_by_source'] = {}

    file_extension = '.xgmml'
    source_xgmml_file_paths = []
    if hasattr(source_xgmml, '__iter__'):
        for source_xgmml_filelike_object in source_xgmml:
            source_xgmml_file_paths += get_file_paths(
                source_xgmml_filelike_object, file_extension)
    else:
        source_xgmml_file_paths += get_file_paths(source_xgmml, file_extension)

    print_debug('source_xgmml_file_paths')
    print_debug(source_xgmml_file_paths)

    versions = []

    node_id_list = []
    if os.path.isfile(node_ids):
        with open(node_ids, 'rb') as csvfile:
            node_id_list_reader = csv.reader(
                csvfile, delimiter='\t', quotechar='|')
            for row in node_id_list_reader:
                node_id_list.append(row[node_id_list_column_index])
    else:
        if hasattr(node_ids, '__iter__'):
            node_id_list += node_ids
        else:
            node_id_list.append(node_ids)

    log_result['query_count'] = len(node_id_list)

    for source_xgmml_file_path in source_xgmml_file_paths:
        source_xgmml_file_path_components = source_xgmml_file_path.split('/')
        version = source_xgmml_file_path_components[len(
            source_xgmml_file_path_components) - 1].replace('.xgmml', '')
        versions.append(version)

        current_xgmml_file = open(source_xgmml_file_path)
        current_mapping_graph = networkxgmml.XGMMLReader(current_xgmml_file)

        log_result[version + '_result_count'] = 0

        datasource_subgraph_node_ids = []

        id_mappings = {}

        for node_id in node_id_list:
            verified_node_id = None
            name_or_identifier = None
            if not node_id:
                print '''Warning: No node_id in line. Please provide a
                    name (e.g., hsa-miR-542-3p),
                    identifier (MIMAT0003389) or
                    IRI (http://identifiers.org/mirbase.mature/MIMAT0003389)'''
                log_result['skipped_count'] += 1
            else:
                if regexes.identifiers_org.match(node_id):
                    name_or_identifier = re.sub(
                        regexes.identifiers_org, '', node_id)
                elif not identifiers_org_warning_triggered:
                    identifiers_org_warning_triggered = True
                    print '''Warning: To avoid errors, please consider using
                        identifiers.org IDs (from the Miriam team)
                        in your node ID list,
                        e.g., convert "MIMAT0003389" or "hsa-miR-542-3p" to
                        "http://identifiers.org/mirbase.mature/MIMAT0003389"'''

                verified_node_id = has_matching_node(
                    current_mapping_graph, node_id)
                # If provided node_id is an identifiers.org IRI,
                # but it doesn't match any nodes, we can
                # try to find nodes by matching the identifier
                # alone (stripping off the namespace).
                if name_or_identifier and (not verified_node_id):
                    verified_node_id = has_matching_node(
                        current_mapping_graph, name_or_identifier)
<commit_msg>Close #6.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import os
from os import listdir
from os.path import isfile, join
import networkx as nx
import networkxgmml
import re
import regexes


def TargetInteractionFinder(
        source_xgmml, node_ids=None, node_id_list_column_index=0,
        output_dir='./', debug=False):
    def print_debug(message):
        if debug:
            print message

    def get_file_paths(filelike_object, file_extension):
        if hasattr(filelike_object, 'name'):
            return filelike_object.name
        else:
            source_xgmml_file_paths = []
            if os.path.isdir(filelike_object):
                source_xgmml_file_paths += [filelike_object + f for f in
                                            listdir(filelike_object)
                                            if isfile(join(filelike_object, f))
                                            and has_file_extension(
                                                f, file_extension)]
            elif os.path.isfile(filelike_object):
                source_xgmml_file_paths += [filelike_object]
            else:
                print filelike_object
                raise ValueError('File or directory input error.')

            return source_xgmml_file_paths

    def has_file_extension(filename, expected_file_extension):
        expected_index = len(filename) - len(expected_file_extension)
        actual_index = filename.find(
            expected_file_extension, expected_index, len(filename))
        return actual_index == expected_index

    def has_matching_node(current_mapping_graph, node_id):
        verified_node_id = None
        if current_mapping_graph.has_node(node_id):
            verified_node_id = node_id
        else:
            for onenode in current_mapping_graph.nodes():
                current_node = current_mapping_graph.node[onenode]
                if ((('identifiers' in current_node)
                    and (node_id in current_node['identifiers']))
                    or (('@label' in current_node)
                        and (node_id == current_node['@label']))
                        or (('label' in current_node)
                            and (node_id == current_node['label']))
                        or (('mimat id' in current_node)
                            and (node_id == current_node['mimat id']))
                        or (('name' in current_node)
                            and (node_id == current_node['name']))):
                    if (('mimat id' in current_node)
                            and (node_id == current_node['mimat id'])):
                        verified_node_id = onenode
                        break
                    elif (('identifiers' in current_node)
                            and (node_id in current_node['identifiers'])):
                        verified_node_id = onenode
                    elif not verified_node_id:
                        verified_node_id = onenode
        return verified_node_id

    results_file_path = output_dir + 'interactions.csv'
    results_file = open(results_file_path, 'w')
    results_file.write('queryid,targetid,score,pvalue,pmid,datasource\n')
    results_file.close()

    logfile_path = output_dir + 'summary.log'
    open(logfile_path, 'w').close()

    log_result = dict()
    log_result['total_result_count'] = 0
    log_result['skipped_count'] = 0
    log_result['results_by_source'] = {}

    file_extension = '.xgmml'
    source_xgmml_file_paths = []
    if hasattr(source_xgmml, '__iter__'):
        for source_xgmml_filelike_object in source_xgmml:
            source_xgmml_file_paths += get_file_paths(
                source_xgmml_filelike_object, file_extension)
    else:
        source_xgmml_file_paths += get_file_paths(source_xgmml, file_extension)

    print_debug('source_xgmml_file_paths')
    print_debug(source_xgmml_file_paths)

    versions = []

    node_id_list = []
    if os.path.isfile(node_ids):
        with open(node_ids, 'rb') as csvfile:
            node_id_list_reader = csv.reader(
                csvfile, delimiter='\t', quotechar='|')
            for row in node_id_list_reader:
                node_id_list.append(row[node_id_list_column_index])
    else:
        if hasattr(node_ids, '__iter__'):
            node_id_list += node_ids
        else:
            node_id_list.append(node_ids)

    log_result['query_count'] = len(node_id_list)

    for source_xgmml_file_path in source_xgmml_file_paths:
        source_xgmml_file_path_components = source_xgmml_file_path.split('/')
        version = source_xgmml_file_path_components[len(
            source_xgmml_file_path_components) - 1].replace('.xgmml', '')
        versions.append(version)

        current_xgmml_file = open(source_xgmml_file_path)
        current_mapping_graph = networkxgmml.XGMMLReader(current_xgmml_file)

        log_result[version + '_result_count'] = 0

        datasource_subgraph_node_ids = []

        id_mappings = {}

        for node_id in node_id_list:
            verified_node_id = None
            name_or_identifier = None
            if not node_id:
                print '''Warning: No node_id in line. Please provide a
                    name (e.g., hsa-miR-542-3p),
                    identifier (MIMAT0003389) or
                    IRI (http://identifiers.org/mirbase.mature/MIMAT0003389)'''
                log_result['skipped_count'] += 1
            else:
                if regexes.identifiers_org.match(node_id):
                    name_or_identifier = re.sub(
                        regexes.identifiers_org, '', node_id)

                verified_node_id = has_matching_node(
                    current_mapping_graph, node_id)
                # If provided node_id is an identifiers.org IRI,
                # but it doesn't match any nodes, we can
                # try to find nodes by matching the identifier
                # alone (stripping off the namespace).
                if name_or_identifier and (not verified_node_id):
                    verified_node_id = has_matching_node(
                        current_mapping_graph, name_or_identifier)

{'fraction_non_alphanumeric': 0.08014911463187326, 'fraction_numerical': 0.005358807082945014, 'mean_word_length': 1.9021861618210503, '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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4664949', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3592, 'n_words': 724}