SciCodePile/SciCode-Domain-Code
DATA1: Domain-Specific Code Dataset Dataset Overview DATA1 is a large-scale domain-specific code dataset focusing on code samples from interdisciplinary fields such as biology, chemistry, materials science, and related areas. The dataset is collected and organized from GitHub repositories, covering 178 different domain topics with over 1.1 billion lines of code. Dataset Statistics Total Datasets: 178 CSV files Total Data Size: ~115 GB Total Lines… See the full description on the dataset page: https://huggingface.co/datasets/SciCodePile/SciCode-Domain-Code.
42.4k
1"keyword","repo_name","file_path","file_extension","file_size","line_count","content","language"
2"Epigenetics","Weikai-47/Pepper_T2T","2. Validation/plot_coverage.depth.py",".py","1742","47","import pandas as pd3import matplotlib.pyplot as plt4plt.rc('font',family='Times New Roman')5import numpy as np6 7data = pd.read_excel('./副本Andean_coverage6.xlsx')8data['ONT_position'] = 09data.columns = ['HiFi_chr','HiFi_start','HiFi_end','HiFi_value','HiFi_position',10 'NGS_chr','NGS_start','NGS_end','NGS_value','NGS_position',11 'ONT_chr','ONT_start','ONT_end','ONT_value','ONT_position']12list_data = list(data.groupby('NGS_chr'))13 14chr_dict = {}15for i in list_data:16 chr_dict[i[0]] = i[1]17 18labels = [0]19count = 020for i in range(1,14):21 for j in ['HiFi_start','NGS_start','ONT_start','HiFi_end','NGS_end','ONT_end']:22 chr_dict['chr{}'.format(i)][j] += count23 count = chr_dict['chr{}'.format(i)].iloc[-1,:]['NGS_end']24 labels.append(count/1000000)25 26data1 = chr_dict['chr1']27for i in range(2,14):28 data1 = pd.concat([data1,chr_dict['chr{}'.format(i)]])29 30for i in ['NGS','ONT','HiFi']:31 data1[i+'_position'] = (data1[i+'_start'].astype('float') + data1[i+'_end'].astype('float'))/2.032 33 34name = ['HiFi','NGS','ONT']35color = ['pink','aqua','lavender']36for i in range(0,3):37 plt.subplot(3, 1, i+1)38 ax = plt.gca() # gca:get current axis得到当前轴39 # 设置图片的右边框和上边框为不显示40 ax.spines['right'].set_color('none')41 ax.spines['top'].set_color('none')42 print(data1[name[i]+'_value'])43 plt.bar(data1[name[i]+'_position'] / 1000000, data1[name[i]+'_value'], width=1.0, color=color[i])44 plt.scatter(data1[name[i]+'_position'] / 1000000, data1[name[i]+'_value'], color='black', s=0.8, alpha=0.6, marker='s')45 plt.ylabel(name[i]+' Sequence Depth', fontsize=14)46 plt.xticks(labels)47plt.show()48","Python"
49"Epigenetics","Weikai-47/Pepper_T2T","SVM-based classifier/2.FeatureImportances.py",".py","1518","39","import numpy as np50from sklearn.svm import SVC51from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier52 53 54def stat_feature_importance(X, Y, model, name_list):55 X = np.array(X)56 Y = np.array(Y)57 58 # 训练模型59 model.fit(X, Y)60 61 # 根据模型类型获取特征重要性62 if hasattr(model, 'coef_'):63 # 对于线性模型64 feature_importance = np.abs(model.coef_)65 elif hasattr(model, 'feature_importances_'):66 # 对于支持feature_importances_属性的模型,如RF和Gradient Boosting67 feature_importance = model.feature_importances_68 else:69 raise ValueError(""Model does not have feature importance attribute"")70 71 # 对于线性模型,feature_importance是一个二维数组,我们需要将其转化为一维72 if len(feature_importance.shape) > 1:73 feature_importance = np.mean(feature_importance, axis=0)74 75 # 创建一个字典来存储特征名称和它们的重要性76 feature_importance_dict = dict(zip(name_list, feature_importance))77 # 按重要性降序排序字典78 sorted_feature_importance = dict(sorted(feature_importance_dict.items(), key=lambda item: item[1], reverse=True))79 80 with open(""../features_importance.csv"", ""w"") as f:81 # 输出排序后的特征重要性82 for feature, importance in sorted_feature_importance.items():83 print(f""{feature}: {importance}"")84 f.write(feature + "","" + str(importance) + '\n')85 86 87","Python"
88"Epigenetics","Weikai-47/Pepper_T2T","SVM-based classifier/1.GridSearch.py",".py","5067","150","import numpy as np89import pandas as pd90from sklearn.model_selection import cross_val_score, GridSearchCV91from sklearn.svm import SVC92from sklearn.ensemble import RandomForestClassifier93from sklearn.neighbors import KNeighborsClassifier94from sklearn.ensemble import GradientBoostingClassifier95import Visualization as vis96from sklearn.model_selection import StratifiedKFold97from sklearn.feature_selection import RFECV98from Feature_Importance import stat_feature_importance99 100# (1) load and transform the data101data = pd.read_excel(""../CBGs_cnv.xlsx"")102data1 = pd.read_excel(""../STab.356_reseq.xlsx"")[[""Sample name"",""Capsaicinoids content (mg/kg DW)""]]103 104index = data.columns105data = pd.DataFrame(np.array(data).T)106data.index = index107data = data.reset_index()108data.columns = data.iloc[1,:]109data = data.iloc[2:,:]110data = data.merge(data1,left_on=""Gene"",right_on=""Sample name"")111 112name_list = []113for i in data.columns:114 if i != ""Sample name"":115 name_list.append(i)116 117data = data[name_list]118list1 = [i for i in data.columns]119list1[0] = 'SampleID'120data.columns = list1121data['Capsaicinoids content (mg/kg DW)'] = data['Capsaicinoids content (mg/kg DW)'].astype('float')122data = data[data[""Capsaicinoids content (mg/kg DW)""] >= 0]123 124# split the labels according to the median, so we can get the balanced dataset125C_level = np.array(data['Capsaicinoids content (mg/kg DW)'].astype(float)).tolist()126C_level.sort(reverse=True)127 128def func2(x):129 if x <= C_level[int(len(C_level)/2)]:130 return 0.0131 else:132 return 1.0133 134data['Capsaicinoids content (mg/kg DW)'] = data['Capsaicinoids content (mg/kg DW)'].apply(func2).astype(int)135 136# (2) Features selection137X = data.drop([""SampleID"", ""Capsaicinoids content (mg/kg DW)""], axis=1)138y = data[""Capsaicinoids content (mg/kg DW)""]139 140# 初始化一个SVM分类器141RF = RandomForestClassifier()142# 使用RFECV进行特征选择,以找到最佳特征数量143# StratifiedKFold用于确保每个类的样本比例保持一致144# step表示每次迭代要移除的特征数145# cv代表交叉验证的策略,这里使用5折交叉验证146rfecv = RFECV(estimator=RF, step=1, cv=StratifiedKFold(5), scoring='accuracy')147rfecv.fit(X, y)148 149# 打印出最佳特征数量150print(""Optimal number of features : %d"" % rfecv.n_features_)151 152# 选择特征153X_new = rfecv.transform(X)154 155# 获取被选中的特征的布尔掩码156selected_features_mask = rfecv.support_157 158# 使用布尔掩码来获取被选中的特征名称159selected_features_names = X.columns[selected_features_mask]160 161print(""Selected features names:"")162print(selected_features_names)163 164# (3) Set the parameters for models165models = [166 (""Support Vector Machine (Kernal: linear)"", SVC(), {""C"": [0.0001, 0.001, 0.01, 0.1, 1, 10], ""kernel"": [ ""linear"" ]}),167 (""Random Forest"", RandomForestClassifier(), {""n_estimators"": [10, 50, 100, 150], ""max_depth"": [1, 10, 20, 30], 'max_features':[None, 'sqrt', 'log2']}),168 (""K Nearest Neighbors"", KNeighborsClassifier(), {""n_neighbors"": [3, 5, 7, 10], ""weights"": [""uniform"", ""distance""]}),169 (""Gradient Boosting"", GradientBoostingClassifier(),170 {171 'n_estimators': [10, 50, 100, 150],172 'learning_rate': [0.01, 0.1, 0.2],173 'max_depth': [1, 10, 20, 30],174 'max_features': [None, 'sqrt', 'log2'],175 'subsample': [0.8, 0.9, 1.0]176 }177 )178]179 180acc_scores = {}181precision_scores = {}182recall_scores = {}183 184model_list = []185best_acc = 0186best_model = None187 188# (4) train and evaluate the models189for name, model, params in models:190 # Grid Search the hyperparameters191 grid_search = GridSearchCV(model, params, cv=10, scoring=""accuracy"")192 grid_search.fit(X_new, y)193 194 # print the best hyperparameters195 print(f""{name}:"")196 print(""Best Parameters:"", grid_search.best_params_)197 print(""Best Score:"", grid_search.best_score_)198 199 # use the best hyperparameters to test200 model = grid_search.best_estimator_201 model_list.append(model)202 scores1 = cross_val_score(model, X_new, y, cv=10, scoring=""accuracy"")203 acc_scores[name] = scores1204 205 # output the outcome206 print(""Cross-Validation Scores:"")207 print(""Accuracy:"", scores1.mean())208 209 if scores1.mean() > best_acc:210 best_acc = scores1.mean()211 best_model = model212 213 # Calculate precision and recall214 scores2 = cross_val_score(model, X_new, y, cv=10, scoring=""precision"")215 precision_scores[name] = scores2216 217 # output the outcome218 print(""Cross-Validation Scores:"")219 print(""Precision:"", scores2.mean())220 221 scores3 = cross_val_score(model, X_new, y, cv=10, scoring=""recall"")222 recall_scores[name] = scores3223 224 # output the outcome225 print(""Cross-Validation Scores:"")226 print(""Recall:"", scores3.mean())227 228 print(""-------------------------------"")229 230# (5) Viualization231vis.Visualize_Performance(acc_scores)232 233vis.Draw_ROC_curve(X_new,y,model_list,89)234 235# (6) Statistic Feature importance236stat_feature_importance(X_new,y,best_model,selected_features_names)237","Python"
238"Epigenetics","Weikai-47/Pepper_T2T","SVM-based classifier/0.Count_CNVs.sh",".sh","976","49","fr = open(""/data/pepper/CaT2T.CBGs.bed"",'r')239 240REGIONS = []241 242for line in fr:243 chr = line.strip().split()[0]244 start = line.strip().split()[1]245 end = line.strip().split()[2]246 247 region = chr + "":"" + start + ""-"" + end248 249 REGIONS.append(region)250 251print(REGIONS)252 253 254import glob255 256SAMPLES = [i.split(""/"")[-2] for i in glob.glob(""/data/pepper/*/CRR*_CaT2T.bam"")]257 258print(SAMPLES)259print(""Total sample size: "",len(SAMPLES))260 261 262rule all:263 input:264 expand(""{sample}/{sample}_{region}.CN"",sample=SAMPLES,region=REGIONS)265 266 267rule amycne:268 input:269 gc = ""/data/pepper/gc_content.tab"",270 cov = ""{sample}/{sample}.regions.bed""271 output:272 ""{sample}/{sample}_{region}.CN""273 threads:274 4275 resources:276 mem_mb = 8000277 params:278 ""{region}""279 shell:280 """"""281 python /home/biotools/AMYCNE-master/AMYCNE.py \282 --genotype --gc {input.gc} \283 --coverage {input.cov} \284 --R {params} > {output}285 """"""286","Shell"
287"Epigenetics","Weikai-47/Pepper_T2T","SVM-based classifier/3.Visualization.py",".py","3172","85","import numpy as np288import pandas as pd289from sklearn.model_selection import cross_val_score, GridSearchCV, cross_val_predict290from sklearn.svm import SVC291from sklearn.ensemble import RandomForestClassifier292from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score293from sklearn.neighbors import KNeighborsClassifier294from sklearn.ensemble import GradientBoostingClassifier295import matplotlib.pyplot as plt296import seaborn as sns297from sklearn.metrics import precision_score, recall_score298from sklearn.metrics import roc_curve, auc299from sklearn.model_selection import train_test_split300 301def Visualize_Performance(acc_scores):302 # Generate some random data to represent in the boxplot303 np.random.seed(10)304 data = pd.DataFrame(acc_scores)305 306 # Set the style of the seaborn library307 sns.set_style(""whitegrid"")308 309 # Create a figure and a set of subplots310 plt.figure(figsize=(10, 6))311 312 # Create the boxplot with additional parameters for better aesthetics313 sns.boxplot(data=data,314 showmeans=True,315 meanprops={""marker"": ""o"",316 ""markerfacecolor"": ""white"",317 ""markeredgecolor"": ""black"",318 ""markersize"": ""10""})319 320 # Set the labels and title321 plt.xlabel('Prediction Algorithms', fontsize=14)322 plt.ylabel('Accuracy', fontsize=14)323 324 # Improve the aesthetics of the plot axes325 plt.tick_params(axis='both', which='major', labelsize=12)326 327 # Show the plot328 plt.show()329 330def Draw_ROC_curve(X,Y,model_list,random_state):331 # (1) split the train dataset and test dataset332 X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=random_state)333 334 # (2) 建立model,绘制ROC曲线335 SVM_model = model_list[0]336 RF_model = model_list[1]337 KNN_model = model_list[2]338 GBoost_model = model_list[3]339 340 # 训练模型341 SVM_model.fit(X_train, y_train)342 RF_model.fit(X_train, y_train)343 KNN_model.fit(X_train, y_train)344 GBoost_model.fit(X_train, y_train)345 346 # 计算各个模型的概率预测值并绘制ROC曲线347 models = [SVM_model, RF_model, KNN_model, GBoost_model]348 model_names = ['SVM', 'Random Forest', 'K-Nearest Neighbors', 'Gradient Boosting']349 350 plt.figure()351 for model, model_name in zip(models, model_names):352 if isinstance(model, RandomForestClassifier) or isinstance(model, KNeighborsClassifier):353 y_pred_proba = model.predict_proba(X_test)[:, 1] # 使用predict_proba获取概率值354 else:355 y_pred_proba = model.decision_function(X_test)356 357 fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)358 roc_auc = auc(fpr, tpr)359 360 plt.plot(fpr, tpr, lw=2, label=f'{model_name} (AUC = {roc_auc:.2f})')361 362 plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')363 plt.xlim([0.0, 1.0])364 plt.ylim([0.0, 1.05])365 plt.xlabel('False Positive Rate')366 plt.ylabel('True Positive Rate')367 plt.title('Receiver Operating Characteristic (ROC) Curves')368 plt.legend(loc='lower right')369 plt.show()370 371","Python"
372"Epigenetics","epiviz/epiviz","closure-library/closure/bin/scopify.py",".py","6785","222","#!/usr/bin/python373#374# Copyright 2010 The Closure Library Authors. All Rights Reserved.375#376# Licensed under the Apache License, Version 2.0 (the ""License"");377# you may not use this file except in compliance with the License.378# You may obtain a copy of the License at379#380# http://www.apache.org/licenses/LICENSE-2.0381#382# Unless required by applicable law or agreed to in writing, software383# distributed under the License is distributed on an ""AS-IS"" BASIS,384# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.385# See the License for the specific language governing permissions and386# limitations under the License.387 388 389""""""Automatically converts codebases over to goog.scope.390 391Usage:392cd path/to/my/dir;393../../../../javascript/closure/bin/scopify.py394 395Scans every file in this directory, recursively. Looks for existing396goog.scope calls, and goog.require'd symbols. If it makes sense to397generate a goog.scope call for the file, then we will do so, and398try to auto-generate some aliases based on the goog.require'd symbols.399 400Known Issues:401 402 When a file is goog.scope'd, the file contents will be indented +2.403 This may put some lines over 80 chars. These will need to be fixed manually.404 405 We will only try to create aliases for capitalized names. We do not check406 to see if those names will conflict with any existing locals.407 408 This creates merge conflicts for every line of every outstanding change.409 If you intend to run this on your codebase, make sure your team members410 know. Better yet, send them this script so that they can scopify their411 outstanding changes and ""accept theirs"".412 413 When an alias is ""captured"", it can no longer be stubbed out for testing.414 Run your tests.415 416""""""417 418__author__ = 'nicksantos@google.com (Nick Santos)'419 420import os.path421import re422import sys423 424REQUIRES_RE = re.compile(r""goog.require\('([^']*)'\)"")425 426# Edit this manually if you want something to ""always"" be aliased.427# TODO(nicksantos): Add a flag for this.428DEFAULT_ALIASES = {}429 430def Transform(lines):431 """"""Converts the contents of a file into javascript that uses goog.scope.432 433 Arguments:434 lines: A list of strings, corresponding to each line of the file.435 Returns:436 A new list of strings, or None if the file was not modified.437 """"""438 requires = []439 440 # Do an initial scan to be sure that this file can be processed.441 for line in lines:442 # Skip this file if it has already been scopified.443 if line.find('goog.scope') != -1:444 return None445 446 # If there are any global vars or functions, then we also have447 # to skip the whole file. We might be able to deal with this448 # more elegantly.449 if line.find('var ') == 0 or line.find('function ') == 0:450 return None451 452 for match in REQUIRES_RE.finditer(line):453 requires.append(match.group(1))454 455 if len(requires) == 0:456 return None457 458 # Backwards-sort the requires, so that when one is a substring of another,459 # we match the longer one first.460 for val in DEFAULT_ALIASES.values():461 if requires.count(val) == 0:462 requires.append(val)463 464 requires.sort()465 requires.reverse()466 467 # Generate a map of requires to their aliases468 aliases_to_globals = DEFAULT_ALIASES.copy()469 for req in requires:470 index = req.rfind('.')471 if index == -1:472 alias = req473 else:474 alias = req[(index + 1):]475 476 # Don't scopify lowercase namespaces, because they may conflict with477 # local variables.478 if alias[0].isupper():479 aliases_to_globals[alias] = req480 481 aliases_to_matchers = {}482 globals_to_aliases = {}483 for alias, symbol in aliases_to_globals.items():484 globals_to_aliases[symbol] = alias485 aliases_to_matchers[alias] = re.compile('\\b%s\\b' % symbol)486 487 # Insert a goog.scope that aliases all required symbols.488 result = []489 490 START = 0491 SEEN_REQUIRES = 1492 IN_SCOPE = 2493 494 mode = START495 aliases_used = set()496 insertion_index = None497 num_blank_lines = 0498 for line in lines:499 if mode == START:500 result.append(line)501 502 if re.search(REQUIRES_RE, line):503 mode = SEEN_REQUIRES504 505 elif mode == SEEN_REQUIRES:506 if (line and507 not re.search(REQUIRES_RE, line) and508 not line.isspace()):509 # There should be two blank lines before goog.scope510 result += ['\n'] * 2511 result.append('goog.scope(function() {\n')512 insertion_index = len(result)513 result += ['\n'] * num_blank_lines514 mode = IN_SCOPE515 elif line.isspace():516 # Keep track of the number of blank lines before each block of code so517 # that we can move them after the goog.scope line if necessary.518 num_blank_lines += 1519 else:520 # Print the blank lines we saw before this code block521 result += ['\n'] * num_blank_lines522 num_blank_lines = 0523 result.append(line)524 525 if mode == IN_SCOPE:526 for symbol in requires:527 if not symbol in globals_to_aliases:528 continue529 530 alias = globals_to_aliases[symbol]531 matcher = aliases_to_matchers[alias]532 for match in matcher.finditer(line):533 # Check to make sure we're not in a string.534 # We do this by being as conservative as possible:535 # if there are any quote or double quote characters536 # before the symbol on this line, then bail out.537 before_symbol = line[:match.start(0)]538 if before_symbol.count('""') > 0 or before_symbol.count(""'"") > 0:539 continue540 541 line = line.replace(match.group(0), alias)542 aliases_used.add(alias)543 544 if line.isspace():545 # Truncate all-whitespace lines546 result.append('\n')547 else:548 result.append(line)549 550 if len(aliases_used):551 aliases_used = [alias for alias in aliases_used]552 aliases_used.sort()553 aliases_used.reverse()554 for alias in aliases_used:555 symbol = aliases_to_globals[alias]556 result.insert(insertion_index,557 'var %s = %s;\n' % (alias, symbol))558 result.append('}); // goog.scope\n')559 return result560 else:561 return None562 563def TransformFileAt(path):564 """"""Converts a file into javascript that uses goog.scope.565 566 Arguments:567 path: A path to a file.568 """"""569 f = open(path)570 lines = Transform(f.readlines())571 if lines:572 f = open(path, 'w')573 for l in lines:574 f.write(l)575 f.close()576 577if __name__ == '__main__':578 args = sys.argv[1:]579 if not len(args):580 args = '.'581 582 for file_name in args:583 if os.path.isdir(file_name):584 for root, dirs, files in os.walk(file_name):585 for name in files:586 if name.endswith('.js') and \587 not os.path.islink(os.path.join(root, name)):588 TransformFileAt(os.path.join(root, name))589 else:590 if file_name.endswith('.js') and \591 not os.path.islink(file_name):592 TransformFileAt(file_name)593","Python"
594"Epigenetics","epiviz/epiviz","closure-library/closure/bin/calcdeps.py",".py","18576","591","#!/usr/bin/env python595#596# Copyright 2006 The Closure Library Authors. All Rights Reserved.597#598# Licensed under the Apache License, Version 2.0 (the ""License"");599# you may not use this file except in compliance with the License.600# You may obtain a copy of the License at601#602# http://www.apache.org/licenses/LICENSE-2.0603#604# Unless required by applicable law or agreed to in writing, software605# distributed under the License is distributed on an ""AS-IS"" BASIS,606# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.607# See the License for the specific language governing permissions and608# limitations under the License.609 610 611""""""Calculates JavaScript dependencies without requiring Google's build system.612 613This tool is deprecated and is provided for legacy users.614See build/closurebuilder.py and build/depswriter.py for the current tools.615 616It iterates over a number of search paths and builds a dependency tree. With617the inputs provided, it walks the dependency tree and outputs all the files618required for compilation.619""""""620 621 622 623 624 625try:626 import distutils.version627except ImportError:628 # distutils is not available in all environments629 distutils = None630 631import logging632import optparse633import os634import re635import subprocess636import sys637 638 639_BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'""](.+)[\'""]\s*\)'640req_regex = re.compile(_BASE_REGEX_STRING % 'require')641prov_regex = re.compile(_BASE_REGEX_STRING % 'provide')642ns_regex = re.compile('^ns:((\w+\.)*(\w+))$')643version_regex = re.compile('[\.0-9]+')644 645 646def IsValidFile(ref):647 """"""Returns true if the provided reference is a file and exists.""""""648 return os.path.isfile(ref)649 650 651def IsJsFile(ref):652 """"""Returns true if the provided reference is a Javascript file.""""""653 return ref.endswith('.js')654 655 656def IsNamespace(ref):657 """"""Returns true if the provided reference is a namespace.""""""658 return re.match(ns_regex, ref) is not None659 660 661def IsDirectory(ref):662 """"""Returns true if the provided reference is a directory.""""""663 return os.path.isdir(ref)664 665 666def ExpandDirectories(refs):667 """"""Expands any directory references into inputs.668 669 Description:670 Looks for any directories in the provided references. Found directories671 are recursively searched for .js files, which are then added to the result672 list.673 674 Args:675 refs: a list of references such as files, directories, and namespaces676 677 Returns:678 A list of references with directories removed and replaced by any679 .js files that are found in them. Also, the paths will be normalized.680 """"""681 result = []682 for ref in refs:683 if IsDirectory(ref):684 # Disable 'Unused variable' for subdirs685 # pylint: disable=unused-variable686 for (directory, subdirs, filenames) in os.walk(ref):687 for filename in filenames:688 if IsJsFile(filename):689 result.append(os.path.join(directory, filename))690 else:691 result.append(ref)692 return map(os.path.normpath, result)693 694 695class DependencyInfo(object):696 """"""Represents a dependency that is used to build and walk a tree.""""""697 698 def __init__(self, filename):699 self.filename = filename700 self.provides = []701 self.requires = []702 703 def __str__(self):704 return '%s Provides: %s Requires: %s' % (self.filename,705 repr(self.provides),706 repr(self.requires))707 708 709def BuildDependenciesFromFiles(files):710 """"""Build a list of dependencies from a list of files.711 712 Description:713 Takes a list of files, extracts their provides and requires, and builds714 out a list of dependency objects.715 716 Args:717 files: a list of files to be parsed for goog.provides and goog.requires.718 719 Returns:720 A list of dependency objects, one for each file in the files argument.721 """"""722 result = []723 filenames = set()724 for filename in files:725 if filename in filenames:726 continue727 728 # Python 3 requires the file encoding to be specified729 if (sys.version_info[0] < 3):730 file_handle = open(filename, 'r')731 else:732 file_handle = open(filename, 'r', encoding='utf8')733 734 try:735 dep = CreateDependencyInfo(filename, file_handle)736 result.append(dep)737 finally:738 file_handle.close()739 740 filenames.add(filename)741 742 return result743 744 745def CreateDependencyInfo(filename, source):746 """"""Create dependency info.747 748 Args:749 filename: Filename for source.750 source: File-like object containing source.751 752 Returns:753 A DependencyInfo object with provides and requires filled.754 """"""755 dep = DependencyInfo(filename)756 for line in source:757 if re.match(req_regex, line):758 dep.requires.append(re.search(req_regex, line).group(1))759 if re.match(prov_regex, line):760 dep.provides.append(re.search(prov_regex, line).group(1))761 return dep762 763 764def BuildDependencyHashFromDependencies(deps):765 """"""Builds a hash for searching dependencies by the namespaces they provide.766 767 Description:768 Dependency objects can provide multiple namespaces. This method enumerates769 the provides of each dependency and adds them to a hash that can be used770 to easily resolve a given dependency by a namespace it provides.771 772 Args:773 deps: a list of dependency objects used to build the hash.774 775 Raises:776 Exception: If a multiple files try to provide the same namepace.777 778 Returns:779 A hash table { namespace: dependency } that can be used to resolve a780 dependency by a namespace it provides.781 """"""782 dep_hash = {}783 for dep in deps:784 for provide in dep.provides:785 if provide in dep_hash:786 raise Exception('Duplicate provide (%s) in (%s, %s)' % (787 provide,788 dep_hash[provide].filename,789 dep.filename))790 dep_hash[provide] = dep791 return dep_hash792 793 794def CalculateDependencies(paths, inputs):795 """"""Calculates the dependencies for given inputs.796 797 Description:798 This method takes a list of paths (files, directories) and builds a799 searchable data structure based on the namespaces that each .js file800 provides. It then parses through each input, resolving dependencies801 against this data structure. The final output is a list of files,802 including the inputs, that represent all of the code that is needed to803 compile the given inputs.804 805 Args:806 paths: the references (files, directories) that are used to build the807 dependency hash.808 inputs: the inputs (files, directories, namespaces) that have dependencies809 that need to be calculated.810 811 Raises:812 Exception: if a provided input is invalid.813 814 Returns:815 A list of all files, including inputs, that are needed to compile the given816 inputs.817 """"""818 deps = BuildDependenciesFromFiles(paths + inputs)819 search_hash = BuildDependencyHashFromDependencies(deps)820 result_list = []821 seen_list = []822 for input_file in inputs:823 if IsNamespace(input_file):824 namespace = re.search(ns_regex, input_file).group(1)825 if namespace not in search_hash:826 raise Exception('Invalid namespace (%s)' % namespace)827 input_file = search_hash[namespace].filename828 if not IsValidFile(input_file) or not IsJsFile(input_file):829 raise Exception('Invalid file (%s)' % input_file)830 seen_list.append(input_file)831 file_handle = open(input_file, 'r')832 try:833 for line in file_handle:834 if re.match(req_regex, line):835 require = re.search(req_regex, line).group(1)836 ResolveDependencies(require, search_hash, result_list, seen_list)837 finally:838 file_handle.close()839 result_list.append(input_file)840 841 # All files depend on base.js, so put it first.842 base_js_path = FindClosureBasePath(paths)843 if base_js_path:844 result_list.insert(0, base_js_path)845 else:846 logging.warning('Closure Library base.js not found.')847 848 return result_list849 850 851def FindClosureBasePath(paths):852 """"""Given a list of file paths, return Closure base.js path, if any.853 854 Args:855 paths: A list of paths.856 857 Returns:858 The path to Closure's base.js file including filename, if found.859 """"""860 861 for path in paths:862 pathname, filename = os.path.split(path)863 864 if filename == 'base.js':865 f = open(path)866 867 is_base = False868 869 # Sanity check that this is the Closure base file. Check that this870 # is where goog is defined. This is determined by the @provideGoog871 # flag.872 for line in f:873 if '@provideGoog' in line:874 is_base = True875 break876 877 f.close()878 879 if is_base:880 return path881 882def ResolveDependencies(require, search_hash, result_list, seen_list):883 """"""Takes a given requirement and resolves all of the dependencies for it.884 885 Description:886 A given requirement may require other dependencies. This method887 recursively resolves all dependencies for the given requirement.888 889 Raises:890 Exception: when require does not exist in the search_hash.891 892 Args:893 require: the namespace to resolve dependencies for.894 search_hash: the data structure used for resolving dependencies.895 result_list: a list of filenames that have been calculated as dependencies.896 This variable is the output for this function.897 seen_list: a list of filenames that have been 'seen'. This is required898 for the dependency->dependent ordering.899 """"""900 if require not in search_hash:901 raise Exception('Missing provider for (%s)' % require)902 903 dep = search_hash[require]904 if not dep.filename in seen_list:905 seen_list.append(dep.filename)906 for sub_require in dep.requires:907 ResolveDependencies(sub_require, search_hash, result_list, seen_list)908 result_list.append(dep.filename)909 910 911def GetDepsLine(dep, base_path):912 """"""Returns a JS string for a dependency statement in the deps.js file.913 914 Args:915 dep: The dependency that we're printing.916 base_path: The path to Closure's base.js including filename.917 """"""918 return 'goog.addDependency(""%s"", %s, %s);' % (919 GetRelpath(dep.filename, base_path), dep.provides, dep.requires)920 921 922def GetRelpath(path, start):923 """"""Return a relative path to |path| from |start|.""""""924 # NOTE: Python 2.6 provides os.path.relpath, which has almost the same925 # functionality as this function. Since we want to support 2.4, we have926 # to implement it manually. :(927 path_list = os.path.abspath(os.path.normpath(path)).split(os.sep)928 start_list = os.path.abspath(929 os.path.normpath(os.path.dirname(start))).split(os.sep)930 931 common_prefix_count = 0932 for i in range(0, min(len(path_list), len(start_list))):933 if path_list[i] != start_list[i]:934 break935 common_prefix_count += 1936 937 # Always use forward slashes, because this will get expanded to a url,938 # not a file path.939 return '/'.join(['..'] * (len(start_list) - common_prefix_count) +940 path_list[common_prefix_count:])941 942 943def PrintLine(msg, out):944 out.write(msg)945 out.write('\n')946 947 948def PrintDeps(source_paths, deps, out):949 """"""Print out a deps.js file from a list of source paths.950 951 Args:952 source_paths: Paths that we should generate dependency info for.953 deps: Paths that provide dependency info. Their dependency info should954 not appear in the deps file.955 out: The output file.956 957 Returns:958 True on success, false if it was unable to find the base path959 to generate deps relative to.960 """"""961 base_path = FindClosureBasePath(source_paths + deps)962 if not base_path:963 return False964 965 PrintLine('// This file was autogenerated by calcdeps.py', out)966 excludesSet = set(deps)967 968 for dep in BuildDependenciesFromFiles(source_paths + deps):969 if not dep.filename in excludesSet:970 PrintLine(GetDepsLine(dep, base_path), out)971 972 return True973 974 975def PrintScript(source_paths, out):976 for index, dep in enumerate(source_paths):977 PrintLine('// Input %d' % index, out)978 f = open(dep, 'r')979 PrintLine(f.read(), out)980 f.close()981 982 983def GetJavaVersion():984 """"""Returns the string for the current version of Java installed.""""""985 proc = subprocess.Popen(['java', '-version'], stderr=subprocess.PIPE)986 proc.wait()987 version_line = proc.stderr.read().splitlines()[0]988 return version_regex.search(version_line.decode('utf-8')).group()989 990 991def FilterByExcludes(options, files):992 """"""Filters the given files by the exlusions specified at the command line.993 994 Args:995 options: The flags to calcdeps.996 files: The files to filter.997 Returns:998 A list of files.999 """"""1000 excludes = []1001 if options.excludes:1002 excludes = ExpandDirectories(options.excludes)1003 1004 excludesSet = set(excludes)1005 return [i for i in files if not i in excludesSet]1006 1007 1008def GetPathsFromOptions(options):1009 """"""Generates the path files from flag options.1010 1011 Args:1012 options: The flags to calcdeps.1013 Returns:1014 A list of files in the specified paths. (strings).1015 """"""1016 1017 search_paths = options.paths1018 if not search_paths:1019 search_paths = ['.'] # Add default folder if no path is specified.1020 1021 search_paths = ExpandDirectories(search_paths)1022 return FilterByExcludes(options, search_paths)1023 1024 1025def GetInputsFromOptions(options):1026 """"""Generates the inputs from flag options.1027 1028 Args:1029 options: The flags to calcdeps.1030 Returns:1031 A list of inputs (strings).1032 """"""1033 inputs = options.inputs1034 if not inputs: # Parse stdin1035 logging.info('No inputs specified. Reading from stdin...')1036 inputs = filter(None, [line.strip('\n') for line in sys.stdin.readlines()])1037 1038 logging.info('Scanning files...')1039 inputs = ExpandDirectories(inputs)1040 1041 return FilterByExcludes(options, inputs)1042 1043 1044def Compile(compiler_jar_path, source_paths, out, flags=None):1045 """"""Prepares command-line call to Closure compiler.1046 1047 Args:1048 compiler_jar_path: Path to the Closure compiler .jar file.1049 source_paths: Source paths to build, in order.1050 flags: A list of additional flags to pass on to Closure compiler.1051 """"""1052 args = ['java', '-jar', compiler_jar_path]1053 for path in source_paths:1054 args += ['--js', path]1055 1056 if flags:1057 args += flags1058 1059 logging.info('Compiling with the following command: %s', ' '.join(args))1060 proc = subprocess.Popen(args, stdout=subprocess.PIPE)1061 (stdoutdata, stderrdata) = proc.communicate()1062 if proc.returncode != 0:1063 logging.error('JavaScript compilation failed.')1064 sys.exit(1)1065 else:1066 out.write(stdoutdata.decode('utf-8'))1067 1068 1069def main():1070 """"""The entrypoint for this script.""""""1071 1072 logging.basicConfig(format='calcdeps.py: %(message)s', level=logging.INFO)1073 1074 usage = 'usage: %prog [options] arg'1075 parser = optparse.OptionParser(usage)1076 parser.add_option('-i',1077 '--input',1078 dest='inputs',1079 action='append',1080 help='The inputs to calculate dependencies for. Valid '1081 'values can be files, directories, or namespaces '1082 '(ns:goog.net.XhrIo). Only relevant to ""list"" and '1083 '""script"" output.')1084 parser.add_option('-p',1085 '--path',1086 dest='paths',1087 action='append',1088 help='The paths that should be traversed to build the '1089 'dependencies.')1090 parser.add_option('-d',1091 '--dep',1092 dest='deps',1093 action='append',1094 help='Directories or files that should be traversed to '1095 'find required dependencies for the deps file. '1096 'Does not generate dependency information for names '1097 'provided by these files. Only useful in ""deps"" mode.')1098 parser.add_option('-e',1099 '--exclude',1100 dest='excludes',1101 action='append',1102 help='Files or directories to exclude from the --path '1103 'and --input flags')1104 parser.add_option('-o',1105 '--output_mode',1106 dest='output_mode',1107 action='store',1108 default='list',1109 help='The type of output to generate from this script. '1110 'Options are ""list"" for a list of filenames, ""script"" '1111 'for a single script containing the contents of all the '1112 'file, ""deps"" to generate a deps.js file for all '1113 'paths, or ""compiled"" to produce compiled output with '1114 'the Closure compiler.')1115 parser.add_option('-c',1116 '--compiler_jar',1117 dest='compiler_jar',1118 action='store',1119 help='The location of the Closure compiler .jar file.')1120 parser.add_option('-f',1121 '--compiler_flag',1122 '--compiler_flags', # for backwards compatibility1123 dest='compiler_flags',1124 action='append',1125 help='Additional flag to pass to the Closure compiler. '1126 'May be specified multiple times to pass multiple flags.')1127 parser.add_option('--output_file',1128 dest='output_file',1129 action='store',1130 help=('If specified, write output to this path instead of '1131 'writing to standard output.'))1132 1133 (options, args) = parser.parse_args()1134 1135 search_paths = GetPathsFromOptions(options)1136 1137 if options.output_file:1138 out = open(options.output_file, 'w')1139 else:1140 out = sys.stdout1141 1142 if options.output_mode == 'deps':1143 result = PrintDeps(search_paths, ExpandDirectories(options.deps or []), out)1144 if not result:1145 logging.error('Could not find Closure Library in the specified paths')1146 sys.exit(1)1147 1148 return1149 1150 inputs = GetInputsFromOptions(options)1151 1152 logging.info('Finding Closure dependencies...')1153 deps = CalculateDependencies(search_paths, inputs)1154 output_mode = options.output_mode1155 1156 if output_mode == 'script':1157 PrintScript(deps, out)1158 elif output_mode == 'list':1159 # Just print out a dep per line1160 for dep in deps:1161 PrintLine(dep, out)1162 elif output_mode == 'compiled':1163 # Make sure a .jar is specified.1164 if not options.compiler_jar:1165 logging.error('--compiler_jar flag must be specified if --output is '1166 '""compiled""')1167 sys.exit(1)1168 1169 # User friendly version check.1170 if distutils and not (distutils.version.LooseVersion(GetJavaVersion()) >1171 distutils.version.LooseVersion('1.6')):1172 logging.error('Closure Compiler requires Java 1.6 or higher.')1173 logging.error('Please visit http://www.java.com/getjava')1174 sys.exit(1)1175 1176 Compile(options.compiler_jar, deps, out, options.compiler_flags)1177 1178 else:1179 logging.error('Invalid value for --output flag.')1180 sys.exit(1)1181 1182if __name__ == '__main__':1183 main()1184","Python"
1185"Epigenetics","epiviz/epiviz","closure-library/closure/bin/labs/code/generate_jsdoc_test.py",".py","3494","168","#!/usr/bin/env python1186#1187# Copyright 2013 The Closure Library Authors. All Rights Reserved.1188#1189# Licensed under the Apache License, Version 2.0 (the ""License"");1190# you may not use this file except in compliance with the License.1191# You may obtain a copy of the License at1192#1193# http://www.apache.org/licenses/LICENSE-2.01194#1195# Unless required `by applicable law or agreed to in writing, software1196# distributed under the License is distributed on an ""AS-IS"" BASIS,1197# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.1198# See the License for the specific language governing permissions and1199# limitations under the License.1200 