sequence
stringlengths 546
16.2k
| code
stringlengths 108
19.3k
|
---|---|
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:merge; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:features; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ignore_strand; 8, False; 9, block; 9, 10; 9, 12; 9, 13; 9, 20; 9, 30; 9, 31; 9, 74; 9, 75; 9, 85; 9, 103; 9, 109; 9, 110; 9, 118; 9, 126; 9, 127; 9, 230; 9, 231; 9, 245; 9, 282; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:features; 16, call; 16, 17; 16, 18; 17, identifier:list; 18, argument_list; 18, 19; 19, identifier:features; 20, if_statement; 20, 21; 20, 27; 21, comparison_operator:==; 21, 22; 21, 26; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:features; 26, integer:0; 27, block; 27, 28; 28, raise_statement; 28, 29; 29, identifier:StopIteration; 30, comment; 31, if_statement; 31, 32; 31, 33; 31, 38; 32, identifier:ignore_strand; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:strand; 37, string:'.'; 38, else_clause; 38, 39; 39, block; 39, 40; 39, 50; 39, 68; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:strands; 43, list_comprehension; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:i; 46, identifier:strand; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:i; 49, identifier:features; 50, if_statement; 50, 51; 50, 60; 51, comparison_operator:>; 51, 52; 51, 59; 52, call; 52, 53; 52, 54; 53, identifier:len; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 57; 56, identifier:set; 57, argument_list; 57, 58; 58, identifier:strands; 59, integer:1; 60, block; 60, 61; 61, raise_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:ValueError; 64, argument_list; 64, 65; 65, concatenated_string; 65, 66; 65, 67; 66, string:'Specify ignore_strand=True to force merging '; 67, string:'of multiple strands'; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:strand; 71, subscript; 71, 72; 71, 73; 72, identifier:strands; 73, integer:0; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:chroms; 78, list_comprehension; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:i; 81, identifier:chrom; 82, for_in_clause; 82, 83; 82, 84; 83, identifier:i; 84, identifier:features; 85, if_statement; 85, 86; 85, 95; 86, comparison_operator:>; 86, 87; 86, 94; 87, call; 87, 88; 87, 89; 88, identifier:len; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:set; 92, argument_list; 92, 93; 93, identifier:chroms; 94, integer:1; 95, block; 95, 96; 96, raise_statement; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:NotImplementedError; 99, argument_list; 99, 100; 100, concatenated_string; 100, 101; 100, 102; 101, string:'Merging multiple chromosomes not '; 102, string:'implemented'; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:chrom; 106, subscript; 106, 107; 106, 108; 107, identifier:chroms; 108, integer:0; 109, comment; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:current_merged_start; 113, attribute; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:features; 116, integer:0; 117, identifier:start; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:current_merged_stop; 121, attribute; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:features; 124, integer:0; 125, identifier:stop; 126, comment; 127, for_statement; 127, 128; 127, 129; 127, 134; 127, 135; 128, identifier:feature; 129, subscript; 129, 130; 129, 131; 130, identifier:features; 131, slice; 131, 132; 131, 133; 132, integer:1; 133, colon; 134, comment; 135, block; 135, 136; 136, if_statement; 136, 137; 136, 144; 136, 145; 136, 146; 136, 166; 137, comparison_operator:<=; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:feature; 140, identifier:start; 141, binary_operator:+; 141, 142; 141, 143; 142, identifier:current_merged_stop; 143, integer:1; 144, comment; 145, comment; 146, block; 146, 147; 147, if_statement; 147, 148; 147, 153; 147, 154; 147, 161; 148, comparison_operator:>=; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:feature; 151, identifier:stop; 152, identifier:current_merged_stop; 153, comment; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:current_merged_stop; 158, attribute; 158, 159; 158, 160; 159, identifier:feature; 160, identifier:stop; 161, else_clause; 161, 162; 161, 163; 161, 164; 162, comment; 163, comment; 164, block; 164, 165; 165, continue_statement; 166, else_clause; 166, 167; 166, 168; 166, 169; 167, comment; 168, comment; 169, block; 169, 170; 169, 207; 169, 216; 169, 217; 169, 218; 169, 224; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:merged_feature; 173, call; 173, 174; 173, 175; 174, identifier:dict; 175, argument_list; 175, 176; 175, 181; 175, 184; 175, 189; 175, 192; 175, 195; 175, 198; 175, 201; 175, 204; 176, keyword_argument; 176, 177; 176, 178; 177, identifier:seqid; 178, attribute; 178, 179; 178, 180; 179, identifier:feature; 180, identifier:chrom; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:source; 183, string:'.'; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:featuretype; 186, attribute; 186, 187; 186, 188; 187, identifier:feature; 188, identifier:featuretype; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:start; 191, identifier:current_merged_start; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:end; 194, identifier:current_merged_stop; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:score; 197, string:'.'; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:strand; 200, identifier:strand; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:frame; 203, string:'.'; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:attributes; 206, string:''; 207, expression_statement; 207, 208; 208, yield; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:self; 212, identifier:_feature_returner; 213, argument_list; 213, 214; 214, dictionary_splat; 214, 215; 215, identifier:merged_feature; 216, comment; 217, comment; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:current_merged_start; 221, attribute; 221, 222; 221, 223; 222, identifier:feature; 223, identifier:start; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:current_merged_stop; 227, attribute; 227, 228; 227, 229; 228, identifier:feature; 229, identifier:stop; 230, comment; 231, if_statement; 231, 232; 231, 238; 232, comparison_operator:==; 232, 233; 232, 237; 233, call; 233, 234; 233, 235; 234, identifier:len; 235, argument_list; 235, 236; 236, identifier:features; 237, integer:1; 238, block; 238, 239; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:feature; 242, subscript; 242, 243; 242, 244; 243, identifier:features; 244, integer:0; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:merged_feature; 248, call; 248, 249; 248, 250; 249, identifier:dict; 250, argument_list; 250, 251; 250, 256; 250, 259; 250, 264; 250, 267; 250, 270; 250, 273; 250, 276; 250, 279; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:seqid; 253, attribute; 253, 254; 253, 255; 254, identifier:feature; 255, identifier:chrom; 256, keyword_argument; 256, 257; 256, 258; 257, identifier:source; 258, string:'.'; 259, keyword_argument; 259, 260; 259, 261; 260, identifier:featuretype; 261, attribute; 261, 262; 261, 263; 262, identifier:feature; 263, identifier:featuretype; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:start; 266, identifier:current_merged_start; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:end; 269, identifier:current_merged_stop; 270, keyword_argument; 270, 271; 270, 272; 271, identifier:score; 272, string:'.'; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:strand; 275, identifier:strand; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:frame; 278, string:'.'; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:attributes; 281, string:''; 282, expression_statement; 282, 283; 283, yield; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:self; 287, identifier:_feature_returner; 288, argument_list; 288, 289; 289, dictionary_splat; 289, 290; 290, identifier:merged_feature | def merge(self, features, ignore_strand=False):
"""
Merge overlapping features together.
Parameters
----------
features : iterator of Feature instances
ignore_strand : bool
If True, features on multiple strands will be merged, and the final
strand will be set to '.'. Otherwise, ValueError will be raised if
trying to merge features on differnt strands.
Returns
-------
A generator object that yields :class:`Feature` objects representing
the newly merged features.
"""
# Consume iterator up front...
features = list(features)
if len(features) == 0:
raise StopIteration
# Either set all strands to '+' or check for strand-consistency.
if ignore_strand:
strand = '.'
else:
strands = [i.strand for i in features]
if len(set(strands)) > 1:
raise ValueError('Specify ignore_strand=True to force merging '
'of multiple strands')
strand = strands[0]
# Sanity check to make sure all features are from the same chromosome.
chroms = [i.chrom for i in features]
if len(set(chroms)) > 1:
raise NotImplementedError('Merging multiple chromosomes not '
'implemented')
chrom = chroms[0]
# To start, we create a merged feature of just the first feature.
current_merged_start = features[0].start
current_merged_stop = features[0].stop
# We don't need to check the first one, so start at feature #2.
for feature in features[1:]:
# Does this feature start within the currently merged feature?...
if feature.start <= current_merged_stop + 1:
# ...It starts within, so leave current_merged_start where it
# is. Does it extend any farther?
if feature.stop >= current_merged_stop:
# Extends further, so set a new stop position
current_merged_stop = feature.stop
else:
# If feature.stop < current_merged_stop, it's completely
# within the previous feature. Nothing more to do.
continue
else:
# The start position is outside the merged feature, so we're
# done with the current merged feature. Prepare for output...
merged_feature = dict(
seqid=feature.chrom,
source='.',
featuretype=feature.featuretype,
start=current_merged_start,
end=current_merged_stop,
score='.',
strand=strand,
frame='.',
attributes='')
yield self._feature_returner(**merged_feature)
# and we start a new one, initializing with this feature's
# start and stop.
current_merged_start = feature.start
current_merged_stop = feature.stop
# need to yield the last one.
if len(features) == 1:
feature = features[0]
merged_feature = dict(
seqid=feature.chrom,
source='.',
featuretype=feature.featuretype,
start=current_merged_start,
end=current_merged_stop,
score='.',
strand=strand,
frame='.',
attributes='')
yield self._feature_returner(**merged_feature) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:bed12; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 10; 3, 14; 3, 17; 3, 20; 4, identifier:self; 5, identifier:feature; 6, default_parameter; 6, 7; 6, 8; 7, identifier:block_featuretype; 8, list:['exon']; 8, 9; 9, string:'exon'; 10, default_parameter; 10, 11; 10, 12; 11, identifier:thick_featuretype; 12, list:['CDS']; 12, 13; 13, string:'CDS'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:thin_featuretype; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:name_field; 19, string:'ID'; 20, default_parameter; 20, 21; 20, 22; 21, identifier:color; 22, None; 23, block; 23, 24; 23, 26; 23, 38; 23, 56; 23, 69; 23, 75; 23, 83; 23, 92; 23, 110; 23, 128; 23, 137; 23, 151; 23, 152; 23, 153; 23, 159; 23, 167; 23, 173; 23, 179; 23, 185; 23, 202; 23, 208; 23, 214; 23, 223; 23, 229; 23, 233; 23, 240; 23, 251; 23, 265; 23, 328; 23, 391; 23, 405; 23, 414; 23, 448; 24, expression_statement; 24, 25; 25, comment; 26, if_statement; 26, 27; 26, 30; 27, boolean_operator:and; 27, 28; 27, 29; 28, identifier:thick_featuretype; 29, identifier:thin_featuretype; 30, block; 30, 31; 31, raise_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:ValueError; 34, argument_list; 34, 35; 35, concatenated_string; 35, 36; 35, 37; 36, string:"Can only specify one of `thick_featuertype` or "; 37, string:"`thin_featuretype`"; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:exons; 41, call; 41, 42; 41, 43; 42, identifier:list; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:children; 48, argument_list; 48, 49; 48, 50; 48, 53; 49, identifier:feature; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:featuretype; 52, identifier:block_featuretype; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:order_by; 55, string:'start'; 56, if_statement; 56, 57; 56, 63; 57, comparison_operator:==; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:exons; 62, integer:0; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:exons; 67, list:[feature]; 67, 68; 68, identifier:feature; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:feature; 72, subscript; 72, 73; 72, 74; 73, identifier:self; 74, identifier:feature; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:first; 78, attribute; 78, 79; 78, 82; 79, subscript; 79, 80; 79, 81; 80, identifier:exons; 81, integer:0; 82, identifier:start; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:last; 86, attribute; 86, 87; 86, 91; 87, subscript; 87, 88; 87, 89; 88, identifier:exons; 89, unary_operator:-; 89, 90; 90, integer:1; 91, identifier:stop; 92, if_statement; 92, 93; 92, 98; 93, comparison_operator:!=; 93, 94; 93, 95; 94, identifier:first; 95, attribute; 95, 96; 95, 97; 96, identifier:feature; 97, identifier:start; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:ValueError; 102, argument_list; 102, 103; 103, binary_operator:%; 103, 104; 103, 105; 104, string:"Start of first exon (%s) does not match start of feature (%s)"; 105, tuple; 105, 106; 105, 107; 106, identifier:first; 107, attribute; 107, 108; 107, 109; 108, identifier:feature; 109, identifier:start; 110, if_statement; 110, 111; 110, 116; 111, comparison_operator:!=; 111, 112; 111, 113; 112, identifier:last; 113, attribute; 113, 114; 113, 115; 114, identifier:feature; 115, identifier:stop; 116, block; 116, 117; 117, raise_statement; 117, 118; 118, call; 118, 119; 118, 120; 119, identifier:ValueError; 120, argument_list; 120, 121; 121, binary_operator:%; 121, 122; 121, 123; 122, string:"End of last exon (%s) does not match end of feature (%s)"; 123, tuple; 123, 124; 123, 125; 124, identifier:last; 125, attribute; 125, 126; 125, 127; 126, identifier:feature; 127, identifier:stop; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:color; 131, None; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:color; 136, string:'0,0,0'; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:color; 140, call; 140, 141; 140, 150; 141, attribute; 141, 142; 141, 149; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:color; 145, identifier:replace; 146, argument_list; 146, 147; 146, 148; 147, string:' '; 148, string:''; 149, identifier:strip; 150, argument_list; 151, comment; 152, comment; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:chrom; 156, attribute; 156, 157; 156, 158; 157, identifier:feature; 158, identifier:chrom; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:chromStart; 162, binary_operator:-; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:feature; 165, identifier:start; 166, integer:1; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:chromEnd; 170, attribute; 170, 171; 170, 172; 171, identifier:feature; 172, identifier:stop; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:orig; 176, attribute; 176, 177; 176, 178; 177, identifier:constants; 178, identifier:always_return_list; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:constants; 183, identifier:always_return_list; 184, True; 185, try_statement; 185, 186; 185, 195; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:name; 190, subscript; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:feature; 193, identifier:name_field; 194, integer:0; 195, except_clause; 195, 196; 195, 197; 196, identifier:KeyError; 197, block; 197, 198; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:name; 201, string:"."; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:constants; 206, identifier:always_return_list; 207, identifier:orig; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:score; 211, attribute; 211, 212; 211, 213; 212, identifier:feature; 213, identifier:score; 214, if_statement; 214, 215; 214, 218; 215, comparison_operator:==; 215, 216; 215, 217; 216, identifier:score; 217, string:'.'; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:score; 222, string:'0'; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:strand; 226, attribute; 226, 227; 226, 228; 227, identifier:feature; 228, identifier:strand; 229, expression_statement; 229, 230; 230, assignment; 230, 231; 230, 232; 231, identifier:itemRgb; 232, identifier:color; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:blockCount; 236, call; 236, 237; 236, 238; 237, identifier:len; 238, argument_list; 238, 239; 239, identifier:exons; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:blockSizes; 243, list_comprehension; 243, 244; 243, 248; 244, call; 244, 245; 244, 246; 245, identifier:len; 246, argument_list; 246, 247; 247, identifier:i; 248, for_in_clause; 248, 249; 248, 250; 249, identifier:i; 250, identifier:exons; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:blockStarts; 254, list_comprehension; 254, 255; 254, 262; 255, binary_operator:-; 255, 256; 255, 261; 256, binary_operator:-; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:i; 259, identifier:start; 260, integer:1; 261, identifier:chromStart; 262, for_in_clause; 262, 263; 262, 264; 263, identifier:i; 264, identifier:exons; 265, if_statement; 265, 266; 265, 267; 266, identifier:thick_featuretype; 267, block; 267, 268; 267, 286; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:thick; 271, call; 271, 272; 271, 273; 272, identifier:list; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:self; 277, identifier:children; 278, argument_list; 278, 279; 278, 280; 278, 283; 279, identifier:feature; 280, keyword_argument; 280, 281; 280, 282; 281, identifier:featuretype; 282, identifier:thick_featuretype; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:order_by; 285, string:'start'; 286, if_statement; 286, 287; 286, 293; 286, 306; 287, comparison_operator:==; 287, 288; 287, 292; 288, call; 288, 289; 288, 290; 289, identifier:len; 290, argument_list; 290, 291; 291, identifier:thick; 292, integer:0; 293, block; 293, 294; 293, 300; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:thickStart; 297, attribute; 297, 298; 297, 299; 298, identifier:feature; 299, identifier:start; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 303; 302, identifier:thickEnd; 303, attribute; 303, 304; 303, 305; 304, identifier:feature; 305, identifier:stop; 306, else_clause; 306, 307; 307, block; 307, 308; 307, 318; 307, 319; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:thickStart; 311, binary_operator:-; 311, 312; 311, 317; 312, attribute; 312, 313; 312, 316; 313, subscript; 313, 314; 313, 315; 314, identifier:thick; 315, integer:0; 316, identifier:start; 317, integer:1; 318, comment; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:thickEnd; 322, attribute; 322, 323; 322, 327; 323, subscript; 323, 324; 323, 325; 324, identifier:thick; 325, unary_operator:-; 325, 326; 326, integer:1; 327, identifier:stop; 328, if_statement; 328, 329; 328, 330; 329, identifier:thin_featuretype; 330, block; 330, 331; 330, 349; 331, expression_statement; 331, 332; 332, assignment; 332, 333; 332, 334; 333, identifier:thin; 334, call; 334, 335; 334, 336; 335, identifier:list; 336, argument_list; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:self; 340, identifier:children; 341, argument_list; 341, 342; 341, 343; 341, 346; 342, identifier:feature; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:featuretype; 345, identifier:thin_featuretype; 346, keyword_argument; 346, 347; 346, 348; 347, identifier:order_by; 348, string:'start'; 349, if_statement; 349, 350; 349, 356; 349, 369; 350, comparison_operator:==; 350, 351; 350, 355; 351, call; 351, 352; 351, 353; 352, identifier:len; 353, argument_list; 353, 354; 354, identifier:thin; 355, integer:0; 356, block; 356, 357; 356, 363; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:thickStart; 360, attribute; 360, 361; 360, 362; 361, identifier:feature; 362, identifier:start; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 366; 365, identifier:thickEnd; 366, attribute; 366, 367; 366, 368; 367, identifier:feature; 368, identifier:stop; 369, else_clause; 369, 370; 370, block; 370, 371; 370, 379; 370, 390; 371, expression_statement; 371, 372; 372, assignment; 372, 373; 372, 374; 373, identifier:thickStart; 374, attribute; 374, 375; 374, 378; 375, subscript; 375, 376; 375, 377; 376, identifier:thin; 377, integer:0; 378, identifier:stop; 379, expression_statement; 379, 380; 380, assignment; 380, 381; 380, 382; 381, identifier:thickEnd; 382, binary_operator:-; 382, 383; 382, 389; 383, attribute; 383, 384; 383, 388; 384, subscript; 384, 385; 384, 386; 385, identifier:thin; 386, unary_operator:-; 386, 387; 387, integer:1; 388, identifier:start; 389, integer:1; 390, comment; 391, expression_statement; 391, 392; 392, assignment; 392, 393; 392, 394; 393, identifier:tst; 394, binary_operator:+; 394, 395; 394, 401; 395, binary_operator:+; 395, 396; 395, 397; 396, identifier:chromStart; 397, subscript; 397, 398; 397, 399; 398, identifier:blockStarts; 399, unary_operator:-; 399, 400; 400, integer:1; 401, subscript; 401, 402; 401, 403; 402, identifier:blockSizes; 403, unary_operator:-; 403, 404; 404, integer:1; 405, assert_statement; 405, 406; 405, 409; 406, comparison_operator:==; 406, 407; 406, 408; 407, identifier:tst; 408, identifier:chromEnd; 409, binary_operator:%; 409, 410; 409, 411; 410, string:"tst=%s; chromEnd=%s"; 411, tuple; 411, 412; 411, 413; 412, identifier:tst; 413, identifier:chromEnd; 414, expression_statement; 414, 415; 415, assignment; 415, 416; 415, 417; 416, identifier:fields; 417, list:[
chrom,
chromStart,
chromEnd,
name,
score,
strand,
thickStart,
thickEnd,
itemRgb,
blockCount,
','.join(map(str, blockSizes)),
','.join(map(str, blockStarts))]; 417, 418; 417, 419; 417, 420; 417, 421; 417, 422; 417, 423; 417, 424; 417, 425; 417, 426; 417, 427; 417, 428; 417, 438; 418, identifier:chrom; 419, identifier:chromStart; 420, identifier:chromEnd; 421, identifier:name; 422, identifier:score; 423, identifier:strand; 424, identifier:thickStart; 425, identifier:thickEnd; 426, identifier:itemRgb; 427, identifier:blockCount; 428, call; 428, 429; 428, 432; 429, attribute; 429, 430; 429, 431; 430, string:','; 431, identifier:join; 432, argument_list; 432, 433; 433, call; 433, 434; 433, 435; 434, identifier:map; 435, argument_list; 435, 436; 435, 437; 436, identifier:str; 437, identifier:blockSizes; 438, call; 438, 439; 438, 442; 439, attribute; 439, 440; 439, 441; 440, string:','; 441, identifier:join; 442, argument_list; 442, 443; 443, call; 443, 444; 443, 445; 444, identifier:map; 445, argument_list; 445, 446; 445, 447; 446, identifier:str; 447, identifier:blockStarts; 448, return_statement; 448, 449; 449, call; 449, 450; 449, 453; 450, attribute; 450, 451; 450, 452; 451, string:'\t'; 452, identifier:join; 453, argument_list; 453, 454; 454, call; 454, 455; 454, 456; 455, identifier:map; 456, argument_list; 456, 457; 456, 458; 457, identifier:str; 458, identifier:fields | def bed12(self, feature, block_featuretype=['exon'],
thick_featuretype=['CDS'], thin_featuretype=None,
name_field='ID', color=None):
"""
Converts `feature` into a BED12 format.
GFF and GTF files do not necessarily define genes consistently, so this
method provides flexiblity in specifying what to call a "transcript".
Parameters
----------
feature : str or Feature instance
In most cases, this feature should be a transcript rather than
a gene.
block_featuretype : str or list
Which featuretype to use as the exons. These are represented as
blocks in the BED12 format. Typically 'exon'.
Use the `thick_featuretype` and `thin_featuretype` arguments to
control the display of CDS as thicker blocks and UTRs as thinner
blocks.
Note that the features for `thick` or `thin` are *not*
automatically included in the blocks; if you do want them included,
then those featuretypes should be added to this `block_features`
list.
If no child features of type `block_featuretype` are found, then
the full `feature` is returned in BED12 format as if it had
a single exon.
thick_featuretype : str or list
Child featuretype(s) to use in order to determine the boundaries of
the "thick" blocks. In BED12 format, these represent coding
sequences; typically this would be set to "CDS". This argument is
mutually exclusive with `thin_featuretype`.
Specifically, the BED12 thickStart will be the start coord of the
first `thick` item and the thickEnd will be the stop coord of the
last `thick` item.
thin_featuretype : str or list
Child featuretype(s) to use in order to determine the boundaries of
the "thin" blocks. In BED12 format, these represent untranslated
regions. Typically "utr" or ['three_prime_UTR', 'five_prime_UTR'].
Mutually exclusive with `thick_featuretype`.
Specifically, the BED12 thickStart field will be the stop coord of
the first `thin` item and the thickEnd field will be the start
coord of the last `thin` item.
name_field : str
Which attribute of `feature` to use as the feature's name. If this
field is not present, a "." placeholder will be used instead.
color : None or str
If None, then use black (0,0,0) as the RGB color; otherwise this
should be a comma-separated string of R,G,B values each of which
are integers in the range 0-255.
"""
if thick_featuretype and thin_featuretype:
raise ValueError("Can only specify one of `thick_featuertype` or "
"`thin_featuretype`")
exons = list(self.children(feature, featuretype=block_featuretype,
order_by='start'))
if len(exons) == 0:
exons = [feature]
feature = self[feature]
first = exons[0].start
last = exons[-1].stop
if first != feature.start:
raise ValueError(
"Start of first exon (%s) does not match start of feature (%s)"
% (first, feature.start))
if last != feature.stop:
raise ValueError(
"End of last exon (%s) does not match end of feature (%s)"
% (last, feature.stop))
if color is None:
color = '0,0,0'
color = color.replace(' ', '').strip()
# Use field names as defined at
# http://genome.ucsc.edu/FAQ/FAQformat.html#format1
chrom = feature.chrom
chromStart = feature.start - 1
chromEnd = feature.stop
orig = constants.always_return_list
constants.always_return_list = True
try:
name = feature[name_field][0]
except KeyError:
name = "."
constants.always_return_list = orig
score = feature.score
if score == '.':
score = '0'
strand = feature.strand
itemRgb = color
blockCount = len(exons)
blockSizes = [len(i) for i in exons]
blockStarts = [i.start - 1 - chromStart for i in exons]
if thick_featuretype:
thick = list(self.children(feature, featuretype=thick_featuretype,
order_by='start'))
if len(thick) == 0:
thickStart = feature.start
thickEnd = feature.stop
else:
thickStart = thick[0].start - 1 # BED 0-based coords
thickEnd = thick[-1].stop
if thin_featuretype:
thin = list(self.children(feature, featuretype=thin_featuretype,
order_by='start'))
if len(thin) == 0:
thickStart = feature.start
thickEnd = feature.stop
else:
thickStart = thin[0].stop
thickEnd = thin[-1].start - 1 # BED 0-based coords
tst = chromStart + blockStarts[-1] + blockSizes[-1]
assert tst == chromEnd, "tst=%s; chromEnd=%s" % (tst, chromEnd)
fields = [
chrom,
chromStart,
chromEnd,
name,
score,
strand,
thickStart,
thickEnd,
itemRgb,
blockCount,
','.join(map(str, blockSizes)),
','.join(map(str, blockStarts))]
return '\t'.join(map(str, fields)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:inspect; 3, parameters; 3, 4; 3, 5; 3, 12; 3, 15; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:look_for; 7, list:['featuretype', 'chrom', 'attribute_keys',
'feature_count']; 7, 8; 7, 9; 7, 10; 7, 11; 8, string:'featuretype'; 9, string:'chrom'; 10, string:'attribute_keys'; 11, string:'feature_count'; 12, default_parameter; 12, 13; 12, 14; 13, identifier:limit; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:verbose; 17, True; 18, block; 18, 19; 18, 21; 18, 25; 18, 29; 18, 55; 18, 61; 18, 70; 18, 74; 18, 148; 18, 152; 18, 171; 18, 177; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:results; 24, dictionary; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:obj_attrs; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:i; 31, identifier:look_for; 32, block; 32, 33; 32, 47; 33, if_statement; 33, 34; 33, 39; 34, comparison_operator:not; 34, 35; 34, 36; 35, identifier:i; 36, list:['attribute_keys', 'feature_count']; 36, 37; 36, 38; 37, string:'attribute_keys'; 38, string:'feature_count'; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:obj_attrs; 44, identifier:append; 45, argument_list; 45, 46; 46, identifier:i; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:results; 51, identifier:i; 52, call; 52, 53; 52, 54; 53, identifier:Counter; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:attr_keys; 58, comparison_operator:in; 58, 59; 58, 60; 59, string:'attribute_keys'; 60, identifier:look_for; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:d; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:iterators; 67, identifier:DataIterator; 68, argument_list; 68, 69; 69, identifier:data; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:feature_count; 73, integer:0; 74, for_statement; 74, 75; 74, 76; 74, 77; 75, identifier:f; 76, identifier:d; 77, block; 77, 78; 77, 100; 77, 118; 77, 136; 77, 140; 78, if_statement; 78, 79; 78, 80; 79, identifier:verbose; 80, block; 80, 81; 80, 92; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:sys; 86, identifier:stderr; 87, identifier:write; 88, argument_list; 88, 89; 89, binary_operator:%; 89, 90; 89, 91; 90, string:'\r%s features inspected'; 91, identifier:feature_count; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:sys; 97, identifier:stderr; 98, identifier:flush; 99, argument_list; 100, for_statement; 100, 101; 100, 102; 100, 103; 101, identifier:obj_attr; 102, identifier:obj_attrs; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:results; 109, identifier:obj_attr; 110, identifier:update; 111, argument_list; 111, 112; 112, list:[getattr(f, obj_attr)]; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:getattr; 115, argument_list; 115, 116; 115, 117; 116, identifier:f; 117, identifier:obj_attr; 118, if_statement; 118, 119; 118, 120; 119, identifier:attr_keys; 120, block; 120, 121; 121, expression_statement; 121, 122; 122, call; 122, 123; 122, 128; 123, attribute; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:results; 126, string:'attribute_keys'; 127, identifier:update; 128, argument_list; 128, 129; 129, call; 129, 130; 129, 135; 130, attribute; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:f; 133, identifier:attributes; 134, identifier:keys; 135, argument_list; 136, expression_statement; 136, 137; 137, augmented_assignment:+=; 137, 138; 137, 139; 138, identifier:feature_count; 139, integer:1; 140, if_statement; 140, 141; 140, 146; 141, boolean_operator:and; 141, 142; 141, 143; 142, identifier:limit; 143, comparison_operator:==; 143, 144; 143, 145; 144, identifier:feature_count; 145, identifier:limit; 146, block; 146, 147; 147, break_statement; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:new_results; 151, dictionary; 152, for_statement; 152, 153; 152, 156; 152, 161; 153, pattern_list; 153, 154; 153, 155; 154, identifier:k; 155, identifier:v; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:results; 159, identifier:items; 160, argument_list; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:new_results; 166, identifier:k; 167, call; 167, 168; 167, 169; 168, identifier:dict; 169, argument_list; 169, 170; 170, identifier:v; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:new_results; 175, string:'feature_count'; 176, identifier:feature_count; 177, return_statement; 177, 178; 178, identifier:new_results | def inspect(data, look_for=['featuretype', 'chrom', 'attribute_keys',
'feature_count'], limit=None, verbose=True):
"""
Inspect a GFF or GTF data source.
This function is useful for figuring out the different featuretypes found
in a file (for potential removal before creating a FeatureDB).
Returns a dictionary with a key for each item in `look_for` and
a corresponding value that is a dictionary of how many of each unique item
were found.
There will always be a `feature_count` key, indicating how many features
were looked at (if `limit` is provided, then `feature_count` will be the
same as `limit`).
For example, if `look_for` is ['chrom', 'featuretype'], then the result
will be a dictionary like::
{
'chrom': {
'chr1': 500,
'chr2': 435,
'chr3': 200,
...
...
}.
'featuretype': {
'gene': 150,
'exon': 324,
...
},
'feature_count': 5000
}
Parameters
----------
data : str, FeatureDB instance, or iterator of Features
If `data` is a string, assume it's a GFF or GTF filename. If it's
a FeatureDB instance, then its `all_features()` method will be
automatically called. Otherwise, assume it's an iterable of Feature
objects.
look_for : list
List of things to keep track of. Options are:
- any attribute of a Feature object, such as chrom, source, start,
stop, strand.
- "attribute_keys", which will look at all the individual
attribute keys of each feature
limit : int
Number of features to look at. Default is no limit.
verbose : bool
Report how many features have been processed.
Returns
-------
dict
"""
results = {}
obj_attrs = []
for i in look_for:
if i not in ['attribute_keys', 'feature_count']:
obj_attrs.append(i)
results[i] = Counter()
attr_keys = 'attribute_keys' in look_for
d = iterators.DataIterator(data)
feature_count = 0
for f in d:
if verbose:
sys.stderr.write('\r%s features inspected' % feature_count)
sys.stderr.flush()
for obj_attr in obj_attrs:
results[obj_attr].update([getattr(f, obj_attr)])
if attr_keys:
results['attribute_keys'].update(f.attributes.keys())
feature_count += 1
if limit and feature_count == limit:
break
new_results = {}
for k, v in results.items():
new_results[k] = dict(v)
new_results['feature_count'] = feature_count
return new_results |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:feature_from_line; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:line; 5, default_parameter; 5, 6; 5, 7; 6, identifier:dialect; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:strict; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:keep_order; 13, False; 14, block; 14, 15; 14, 17; 14, 127; 14, 142; 14, 156; 14, 172; 14, 178; 14, 188; 14, 194; 14, 203; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 20; 17, 111; 18, not_operator; 18, 19; 19, identifier:strict; 20, block; 20, 21; 20, 30; 20, 34; 20, 61; 20, 69; 20, 75; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:lines; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:line; 27, identifier:splitlines; 28, argument_list; 28, 29; 29, False; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:_lines; 33, list:[]; 34, for_statement; 34, 35; 34, 36; 34, 37; 35, identifier:i; 36, identifier:lines; 37, block; 37, 38; 37, 46; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:i; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:i; 44, identifier:strip; 45, argument_list; 46, if_statement; 46, 47; 46, 53; 47, comparison_operator:>; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, identifier:i; 52, integer:0; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:_lines; 58, identifier:append; 59, argument_list; 59, 60; 60, identifier:i; 61, assert_statement; 61, 62; 61, 68; 62, comparison_operator:==; 62, 63; 62, 67; 63, call; 63, 64; 63, 65; 64, identifier:len; 65, argument_list; 65, 66; 66, identifier:_lines; 67, integer:1; 68, identifier:_lines; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:line; 72, subscript; 72, 73; 72, 74; 73, identifier:_lines; 74, integer:0; 75, if_statement; 75, 76; 75, 79; 75, 94; 76, comparison_operator:in; 76, 77; 76, 78; 77, string:'\t'; 78, identifier:line; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:fields; 83, call; 83, 84; 83, 92; 84, attribute; 84, 85; 84, 91; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:line; 88, identifier:rstrip; 89, argument_list; 89, 90; 90, string:'\n\r'; 91, identifier:split; 92, argument_list; 92, 93; 93, string:'\t'; 94, else_clause; 94, 95; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:fields; 99, call; 99, 100; 99, 108; 100, attribute; 100, 101; 100, 107; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:line; 104, identifier:rstrip; 105, argument_list; 105, 106; 106, string:'\n\r'; 107, identifier:split; 108, argument_list; 108, 109; 108, 110; 109, None; 110, integer:8; 111, else_clause; 111, 112; 112, block; 112, 113; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:fields; 116, call; 116, 117; 116, 125; 117, attribute; 117, 118; 117, 124; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:line; 121, identifier:rstrip; 122, argument_list; 122, 123; 123, string:'\n\r'; 124, identifier:split; 125, argument_list; 125, 126; 126, string:'\t'; 127, try_statement; 127, 128; 127, 135; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:attr_string; 132, subscript; 132, 133; 132, 134; 133, identifier:fields; 134, integer:8; 135, except_clause; 135, 136; 135, 137; 136, identifier:IndexError; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:attr_string; 141, string:""; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 147; 144, pattern_list; 144, 145; 144, 146; 145, identifier:attrs; 146, identifier:_dialect; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:parser; 150, identifier:_split_keyvals; 151, argument_list; 151, 152; 151, 153; 152, identifier:attr_string; 153, keyword_argument; 153, 154; 153, 155; 154, identifier:dialect; 155, identifier:dialect; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:d; 159, call; 159, 160; 159, 161; 160, identifier:dict; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 164; 163, identifier:list; 164, argument_list; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:zip; 167, argument_list; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:constants; 170, identifier:_gffkeys; 171, identifier:fields; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 177; 174, subscript; 174, 175; 174, 176; 175, identifier:d; 176, string:'attributes'; 177, identifier:attrs; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:d; 182, string:'extra'; 183, subscript; 183, 184; 183, 185; 184, identifier:fields; 185, slice; 185, 186; 185, 187; 186, integer:9; 187, colon; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:d; 192, string:'keep_order'; 193, identifier:keep_order; 194, if_statement; 194, 195; 194, 198; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:dialect; 197, None; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:dialect; 202, identifier:_dialect; 203, return_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:Feature; 206, argument_list; 206, 207; 206, 210; 207, keyword_argument; 207, 208; 207, 209; 208, identifier:dialect; 209, identifier:dialect; 210, dictionary_splat; 210, 211; 211, identifier:d | def feature_from_line(line, dialect=None, strict=True, keep_order=False):
"""
Given a line from a GFF file, return a Feature object
Parameters
----------
line : string
strict : bool
If True (default), assume `line` is a single, tab-delimited string that
has at least 9 fields.
If False, then the input can have a more flexible format, useful for
creating single ad hoc features or for writing tests. In this case,
`line` can be a multi-line string (as long as it has a single non-empty
line), and, as long as there are only 9 fields (standard GFF/GTF), then
it's OK to use spaces instead of tabs to separate fields in `line`.
But if >9 fields are to be used, then tabs must be used.
keep_order, dialect
Passed directly to :class:`Feature`; see docstring for that class for
description
Returns
-------
A new :class:`Feature` object.
"""
if not strict:
lines = line.splitlines(False)
_lines = []
for i in lines:
i = i.strip()
if len(i) > 0:
_lines.append(i)
assert len(_lines) == 1, _lines
line = _lines[0]
if '\t' in line:
fields = line.rstrip('\n\r').split('\t')
else:
fields = line.rstrip('\n\r').split(None, 8)
else:
fields = line.rstrip('\n\r').split('\t')
try:
attr_string = fields[8]
except IndexError:
attr_string = ""
attrs, _dialect = parser._split_keyvals(attr_string, dialect=dialect)
d = dict(list(zip(constants._gffkeys, fields)))
d['attributes'] = attrs
d['extra'] = fields[9:]
d['keep_order'] = keep_order
if dialect is None:
dialect = _dialect
return Feature(dialect=dialect, **d) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_reconstruct; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:keyvals; 5, identifier:dialect; 6, default_parameter; 6, 7; 6, 8; 7, identifier:keep_order; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort_attribute_values; 11, False; 12, block; 12, 13; 12, 15; 12, 23; 12, 29; 12, 33; 12, 34; 12, 95; 12, 96; 12, 161; 12, 186; 12, 198; 12, 293; 12, 294; 12, 305; 12, 306; 12, 315; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 18; 16, not_operator; 16, 17; 17, identifier:dialect; 18, block; 18, 19; 19, raise_statement; 19, 20; 20, call; 20, 21; 20, 22; 21, identifier:AttributeStringError; 22, argument_list; 23, if_statement; 23, 24; 23, 26; 24, not_operator; 24, 25; 25, identifier:keyvals; 26, block; 26, 27; 27, return_statement; 27, 28; 28, string:""; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:parts; 32, list:[]; 33, comment; 34, if_statement; 34, 35; 34, 44; 34, 49; 35, boolean_operator:or; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:constants; 38, identifier:ignore_url_escape_characters; 39, comparison_operator:!=; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:dialect; 42, string:'fmt'; 43, string:'gff3'; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:attributes; 48, identifier:keyvals; 49, else_clause; 49, 50; 50, block; 50, 51; 50, 55; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:attributes; 54, dictionary; 55, for_statement; 55, 56; 55, 59; 55, 64; 56, pattern_list; 56, 57; 56, 58; 57, identifier:k; 58, identifier:v; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:keyvals; 62, identifier:items; 63, argument_list; 64, block; 64, 65; 64, 71; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:attributes; 69, identifier:k; 70, list:[]; 71, for_statement; 71, 72; 71, 73; 71, 74; 72, identifier:i; 73, identifier:v; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:attributes; 80, identifier:k; 81, identifier:append; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, string:''; 86, identifier:join; 87, argument_list; 87, 88; 88, list_comprehension; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:quoter; 91, identifier:j; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:j; 94, identifier:i; 95, comment; 96, if_statement; 96, 97; 96, 100; 96, 148; 97, subscript; 97, 98; 97, 99; 98, identifier:dialect; 99, string:'repeated keys'; 100, block; 100, 101; 100, 105; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:items; 104, list:[]; 105, for_statement; 105, 106; 105, 109; 105, 114; 106, pattern_list; 106, 107; 106, 108; 107, identifier:key; 108, identifier:val; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:attributes; 112, identifier:items; 113, argument_list; 114, block; 114, 115; 115, if_statement; 115, 116; 115, 122; 115, 137; 116, comparison_operator:>; 116, 117; 116, 121; 117, call; 117, 118; 117, 119; 118, identifier:len; 119, argument_list; 119, 120; 120, identifier:val; 121, integer:1; 122, block; 122, 123; 123, for_statement; 123, 124; 123, 125; 123, 126; 124, identifier:v; 125, identifier:val; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:items; 131, identifier:append; 132, argument_list; 132, 133; 133, tuple; 133, 134; 133, 135; 134, identifier:key; 135, list:[v]; 135, 136; 136, identifier:v; 137, else_clause; 137, 138; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:items; 143, identifier:append; 144, argument_list; 144, 145; 145, tuple; 145, 146; 145, 147; 146, identifier:key; 147, identifier:val; 148, else_clause; 148, 149; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:items; 153, call; 153, 154; 153, 155; 154, identifier:list; 155, argument_list; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:attributes; 159, identifier:items; 160, argument_list; 161, function_definition; 161, 162; 161, 163; 161, 165; 161, 166; 161, 167; 162, function_name:sort_key; 163, parameters; 163, 164; 164, identifier:x; 165, comment; 166, comment; 167, block; 167, 168; 168, try_statement; 168, 169; 168, 181; 169, block; 169, 170; 170, return_statement; 170, 171; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:dialect; 175, string:'order'; 176, identifier:index; 177, argument_list; 177, 178; 178, subscript; 178, 179; 178, 180; 179, identifier:x; 180, integer:0; 181, except_clause; 181, 182; 181, 183; 182, identifier:ValueError; 183, block; 183, 184; 184, return_statement; 184, 185; 185, float:1e6; 186, if_statement; 186, 187; 186, 188; 187, identifier:keep_order; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:items; 193, identifier:sort; 194, argument_list; 194, 195; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:key; 197, identifier:sort_key; 198, for_statement; 198, 199; 198, 202; 198, 203; 198, 204; 199, pattern_list; 199, 200; 199, 201; 200, identifier:key; 201, identifier:val; 202, identifier:items; 203, comment; 204, block; 204, 205; 204, 286; 205, if_statement; 205, 206; 205, 207; 205, 258; 206, identifier:val; 207, block; 207, 208; 207, 218; 207, 229; 208, if_statement; 208, 209; 208, 210; 209, identifier:sort_attribute_values; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:val; 214, call; 214, 215; 214, 216; 215, identifier:sorted; 216, argument_list; 216, 217; 217, identifier:val; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:val_str; 221, call; 221, 222; 221, 227; 222, attribute; 222, 223; 222, 226; 223, subscript; 223, 224; 223, 225; 224, identifier:dialect; 225, string:'multival separator'; 226, identifier:join; 227, argument_list; 227, 228; 228, identifier:val; 229, if_statement; 229, 230; 229, 231; 229, 232; 230, identifier:val_str; 231, comment; 232, block; 232, 233; 232, 244; 232, 245; 233, if_statement; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:dialect; 236, string:'quoted GFF2 values'; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:val_str; 241, binary_operator:%; 241, 242; 241, 243; 242, string:'"%s"'; 243, identifier:val_str; 244, comment; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:part; 248, call; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, subscript; 250, 251; 250, 252; 251, identifier:dialect; 252, string:'keyval separator'; 253, identifier:join; 254, argument_list; 254, 255; 255, list:[key, val_str]; 255, 256; 255, 257; 256, identifier:key; 257, identifier:val_str; 258, else_clause; 258, 259; 259, block; 259, 260; 260, if_statement; 260, 261; 260, 266; 260, 280; 261, comparison_operator:==; 261, 262; 261, 265; 262, subscript; 262, 263; 262, 264; 263, identifier:dialect; 264, string:'fmt'; 265, string:'gtf'; 266, block; 266, 267; 267, expression_statement; 267, 268; 268, assignment; 268, 269; 268, 270; 269, identifier:part; 270, call; 270, 271; 270, 276; 271, attribute; 271, 272; 271, 275; 272, subscript; 272, 273; 272, 274; 273, identifier:dialect; 274, string:'keyval separator'; 275, identifier:join; 276, argument_list; 276, 277; 277, list:[key, '""']; 277, 278; 277, 279; 278, identifier:key; 279, string:'""'; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, assignment; 283, 284; 283, 285; 284, identifier:part; 285, identifier:key; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:parts; 290, identifier:append; 291, argument_list; 291, 292; 292, identifier:part; 293, comment; 294, expression_statement; 294, 295; 295, assignment; 295, 296; 295, 297; 296, identifier:parts_str; 297, call; 297, 298; 297, 303; 298, attribute; 298, 299; 298, 302; 299, subscript; 299, 300; 299, 301; 300, identifier:dialect; 301, string:'field separator'; 302, identifier:join; 303, argument_list; 303, 304; 304, identifier:parts; 305, comment; 306, if_statement; 306, 307; 306, 310; 307, subscript; 307, 308; 307, 309; 308, identifier:dialect; 309, string:'trailing semicolon'; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, augmented_assignment:+=; 312, 313; 312, 314; 313, identifier:parts_str; 314, string:';'; 315, return_statement; 315, 316; 316, identifier:parts_str | def _reconstruct(keyvals, dialect, keep_order=False,
sort_attribute_values=False):
"""
Reconstructs the original attributes string according to the dialect.
Parameters
==========
keyvals : dict
Attributes from a GFF/GTF feature
dialect : dict
Dialect containing info on how to reconstruct a string version of the
attributes
keep_order : bool
If True, then perform sorting of attribute keys to ensure they are in
the same order as those provided in the original file. Default is
False, which saves time especially on large data sets.
sort_attribute_values : bool
If True, then sort values to ensure they will always be in the same
order. Mostly only useful for testing; default is False.
"""
if not dialect:
raise AttributeStringError()
if not keyvals:
return ""
parts = []
# Re-encode when reconstructing attributes
if constants.ignore_url_escape_characters or dialect['fmt'] != 'gff3':
attributes = keyvals
else:
attributes = {}
for k, v in keyvals.items():
attributes[k] = []
for i in v:
attributes[k].append(''.join([quoter[j] for j in i]))
# May need to split multiple values into multiple key/val pairs
if dialect['repeated keys']:
items = []
for key, val in attributes.items():
if len(val) > 1:
for v in val:
items.append((key, [v]))
else:
items.append((key, val))
else:
items = list(attributes.items())
def sort_key(x):
# sort keys by their order in the dialect; anything not in there will
# be in arbitrary order at the end.
try:
return dialect['order'].index(x[0])
except ValueError:
return 1e6
if keep_order:
items.sort(key=sort_key)
for key, val in items:
# Multival sep is usually a comma:
if val:
if sort_attribute_values:
val = sorted(val)
val_str = dialect['multival separator'].join(val)
if val_str:
# Surround with quotes if needed
if dialect['quoted GFF2 values']:
val_str = '"%s"' % val_str
# Typically "=" for GFF3 or " " otherwise
part = dialect['keyval separator'].join([key, val_str])
else:
if dialect['fmt'] == 'gtf':
part = dialect['keyval separator'].join([key, '""'])
else:
part = key
parts.append(part)
# Typically ";" or "; "
parts_str = dialect['field separator'].join(parts)
# Sometimes need to add this
if dialect['trailing semicolon']:
parts_str += ';'
return parts_str |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 78; 2, function_name:create_db; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 3, 39; 3, 42; 3, 45; 3, 50; 3, 53; 3, 58; 3, 61; 3, 64; 3, 67; 3, 70; 3, 73; 3, 76; 4, identifier:data; 5, identifier:dbfn; 6, default_parameter; 6, 7; 6, 8; 7, identifier:id_spec; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:force; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:verbose; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:checklines; 17, integer:10; 18, default_parameter; 18, 19; 18, 20; 19, identifier:merge_strategy; 20, string:'error'; 21, default_parameter; 21, 22; 21, 23; 22, identifier:transform; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:gtf_transcript_key; 26, string:'transcript_id'; 27, default_parameter; 27, 28; 27, 29; 28, identifier:gtf_gene_key; 29, string:'gene_id'; 30, default_parameter; 30, 31; 30, 32; 31, identifier:gtf_subfeature; 32, string:'exon'; 33, default_parameter; 33, 34; 33, 35; 34, identifier:force_gff; 35, False; 36, default_parameter; 36, 37; 36, 38; 37, identifier:force_dialect_check; 38, False; 39, default_parameter; 39, 40; 39, 41; 40, identifier:from_string; 41, False; 42, default_parameter; 42, 43; 42, 44; 43, identifier:keep_order; 44, False; 45, default_parameter; 45, 46; 45, 47; 46, identifier:text_factory; 47, attribute; 47, 48; 47, 49; 48, identifier:sqlite3; 49, identifier:OptimizedUnicode; 50, default_parameter; 50, 51; 50, 52; 51, identifier:force_merge_fields; 52, None; 53, default_parameter; 53, 54; 53, 55; 54, identifier:pragmas; 55, attribute; 55, 56; 55, 57; 56, identifier:constants; 57, identifier:default_pragmas; 58, default_parameter; 58, 59; 58, 60; 59, identifier:sort_attribute_values; 60, False; 61, default_parameter; 61, 62; 61, 63; 62, identifier:dialect; 63, None; 64, default_parameter; 64, 65; 64, 66; 65, identifier:_keep_tempfiles; 66, False; 67, default_parameter; 67, 68; 67, 69; 68, identifier:infer_gene_extent; 69, True; 70, default_parameter; 70, 71; 70, 72; 71, identifier:disable_infer_genes; 72, False; 73, default_parameter; 73, 74; 73, 75; 74, identifier:disable_infer_transcripts; 75, False; 76, dictionary_splat_pattern; 76, 77; 77, identifier:kwargs; 78, block; 78, 79; 78, 81; 78, 87; 78, 88; 78, 93; 78, 109; 78, 110; 78, 111; 78, 112; 78, 113; 78, 123; 78, 131; 78, 142; 78, 143; 78, 144; 78, 145; 78, 146; 78, 147; 78, 148; 78, 149; 78, 150; 78, 158; 78, 166; 78, 167; 78, 173; 78, 243; 78, 251; 78, 257; 78, 265; 78, 271; 78, 322; 79, expression_statement; 79, 80; 80, comment; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:_locals; 84, call; 84, 85; 84, 86; 85, identifier:locals; 86, argument_list; 87, comment; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:deprecation_handler; 91, argument_list; 91, 92; 92, identifier:kwargs; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:kwargs; 96, call; 96, 97; 96, 98; 97, identifier:dict; 98, generator_expression; 98, 99; 98, 104; 99, tuple; 99, 100; 99, 101; 100, identifier:i; 101, subscript; 101, 102; 101, 103; 102, identifier:_locals; 103, identifier:i; 104, for_in_clause; 104, 105; 104, 106; 105, identifier:i; 106, attribute; 106, 107; 106, 108; 107, identifier:constants; 108, identifier:_iterator_kwargs; 109, comment; 110, comment; 111, comment; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:iterator; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:iterators; 119, identifier:DataIterator; 120, argument_list; 120, 121; 121, dictionary_splat; 121, 122; 122, identifier:kwargs; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:kwargs; 127, identifier:update; 128, argument_list; 128, 129; 129, dictionary_splat; 129, 130; 130, identifier:_locals; 131, if_statement; 131, 132; 131, 135; 132, comparison_operator:is; 132, 133; 132, 134; 133, identifier:dialect; 134, None; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:dialect; 139, attribute; 139, 140; 139, 141; 140, identifier:iterator; 141, identifier:dialect; 142, comment; 143, comment; 144, comment; 145, comment; 146, comment; 147, comment; 148, comment; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 155; 152, subscript; 152, 153; 152, 154; 153, identifier:kwargs; 154, string:'data'; 155, attribute; 155, 156; 155, 157; 156, identifier:iterator; 157, identifier:_iter; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:kwargs; 162, string:'directives'; 163, attribute; 163, 164; 163, 165; 164, identifier:iterator; 165, identifier:directives; 166, comment; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, subscript; 169, 170; 169, 171; 170, identifier:kwargs; 171, string:'checklines'; 172, integer:0; 173, if_statement; 173, 174; 173, 182; 173, 202; 174, boolean_operator:or; 174, 175; 174, 176; 175, identifier:force_gff; 176, parenthesized_expression; 176, 177; 177, comparison_operator:==; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:dialect; 180, string:'fmt'; 181, string:'gff3'; 182, block; 182, 183; 182, 187; 182, 193; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:cls; 186, identifier:_GFFDBCreator; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:id_spec; 190, boolean_operator:or; 190, 191; 190, 192; 191, identifier:id_spec; 192, string:'ID'; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:add_kwargs; 196, call; 196, 197; 196, 198; 197, identifier:dict; 198, argument_list; 198, 199; 199, keyword_argument; 199, 200; 199, 201; 200, identifier:id_spec; 201, identifier:id_spec; 202, elif_clause; 202, 203; 202, 208; 203, comparison_operator:==; 203, 204; 203, 207; 204, subscript; 204, 205; 204, 206; 205, identifier:dialect; 206, string:'fmt'; 207, string:'gtf'; 208, block; 208, 209; 208, 213; 208, 225; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:cls; 212, identifier:_GTFDBCreator; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:id_spec; 216, boolean_operator:or; 216, 217; 216, 218; 217, identifier:id_spec; 218, dictionary; 218, 219; 218, 222; 219, pair; 219, 220; 219, 221; 220, string:'gene'; 221, string:'gene_id'; 222, pair; 222, 223; 222, 224; 223, string:'transcript'; 224, string:'transcript_id'; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:add_kwargs; 228, call; 228, 229; 228, 230; 229, identifier:dict; 230, argument_list; 230, 231; 230, 234; 230, 237; 230, 240; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:transcript_key; 233, identifier:gtf_transcript_key; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:gene_key; 236, identifier:gtf_gene_key; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:subfeature; 239, identifier:gtf_subfeature; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:id_spec; 242, identifier:id_spec; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:kwargs; 247, identifier:update; 248, argument_list; 248, 249; 249, dictionary_splat; 249, 250; 250, identifier:add_kwargs; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 256; 253, subscript; 253, 254; 253, 255; 254, identifier:kwargs; 255, string:'dialect'; 256, identifier:dialect; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:c; 260, call; 260, 261; 260, 262; 261, identifier:cls; 262, argument_list; 262, 263; 263, dictionary_splat; 263, 264; 264, identifier:kwargs; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:c; 269, identifier:create; 270, argument_list; 271, if_statement; 271, 272; 271, 275; 271, 299; 272, comparison_operator:==; 272, 273; 272, 274; 273, identifier:dbfn; 274, string:':memory:'; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:db; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:interface; 282, identifier:FeatureDB; 283, argument_list; 283, 284; 283, 287; 283, 290; 283, 293; 283, 296; 284, attribute; 284, 285; 284, 286; 285, identifier:c; 286, identifier:conn; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:keep_order; 289, identifier:keep_order; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:pragmas; 292, identifier:pragmas; 293, keyword_argument; 293, 294; 293, 295; 294, identifier:sort_attribute_values; 295, identifier:sort_attribute_values; 296, keyword_argument; 296, 297; 296, 298; 297, identifier:text_factory; 298, identifier:text_factory; 299, else_clause; 299, 300; 300, block; 300, 301; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:db; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:interface; 307, identifier:FeatureDB; 308, argument_list; 308, 309; 308, 310; 308, 313; 308, 316; 308, 319; 309, identifier:c; 310, keyword_argument; 310, 311; 310, 312; 311, identifier:keep_order; 312, identifier:keep_order; 313, keyword_argument; 313, 314; 313, 315; 314, identifier:pragmas; 315, identifier:pragmas; 316, keyword_argument; 316, 317; 316, 318; 317, identifier:sort_attribute_values; 318, identifier:sort_attribute_values; 319, keyword_argument; 319, 320; 319, 321; 320, identifier:text_factory; 321, identifier:text_factory; 322, return_statement; 322, 323; 323, identifier:db | def create_db(data, dbfn, id_spec=None, force=False, verbose=False,
checklines=10, merge_strategy='error', transform=None,
gtf_transcript_key='transcript_id', gtf_gene_key='gene_id',
gtf_subfeature='exon', force_gff=False,
force_dialect_check=False, from_string=False, keep_order=False,
text_factory=sqlite3.OptimizedUnicode, force_merge_fields=None,
pragmas=constants.default_pragmas, sort_attribute_values=False,
dialect=None, _keep_tempfiles=False, infer_gene_extent=True,
disable_infer_genes=False, disable_infer_transcripts=False,
**kwargs):
"""
Create a database from a GFF or GTF file.
For more details on when and how to use the kwargs below, see the examples
in the online documentation (:ref:`examples`).
Parameters
----------
data : string or iterable
If a string (and `from_string` is False), then `data` is the path to
the original GFF or GTF file.
If a string and `from_string` is True, then assume `data` is the actual
data to use.
Otherwise, it's an iterable of Feature objects.
dbfn : string
Path to the database that will be created. Can be the special string
":memory:" to create an in-memory database.
id_spec : string, list, dict, callable, or None
This parameter guides what will be used as the primary key for the
database, which in turn determines how you will access individual
features by name from the database.
If `id_spec=None`, then auto-increment primary keys based on the
feature type (e.g., "gene_1", "gene_2"). This is also the fallback
behavior for the other values below.
If `id_spec` is a string, then look for this key in the attributes. If
it exists, then use its value as the primary key, otherwise
autoincrement based on the feature type. For many GFF3 files, "ID"
usually works well.
If `id_spec` is a list or tuple of keys, then check for each one in
order, using the first one found. For GFF3, this might be ["ID",
"Name"], which would use the ID if it exists, otherwise the Name,
otherwise autoincrement based on the feature type.
If `id_spec` is a dictionary, then it is a mapping of feature types to
what should be used as the ID. For example, for GTF files, `{'gene':
'gene_id', 'transcript': 'transcript_id'}` may be useful. The values
of this dictionary can also be a list, e.g., `{'gene': ['gene_id',
'geneID']}`
If `id_spec` is a callable object, then it accepts a dictionary from
the iterator and returns one of the following:
* None (in which case the feature type will be auto-incremented)
* string (which will be used as the primary key)
* special string starting with "autoincrement:X", where "X" is
a string that will be used for auto-incrementing. For example,
if "autoincrement:chr10", then the first feature will be
"chr10_1", the second "chr10_2", and so on.
force : bool
If `False` (default), then raise an exception if `dbfn` already exists.
Use `force=True` to overwrite any existing databases.
verbose : bool
Report percent complete and other feedback on how the db creation is
progressing.
In order to report percent complete, the entire file needs to be read
once to see how many items there are; for large files you may want to
use `verbose=False` to avoid this.
checklines : int
Number of lines to check the dialect.
merge_strategy : str
One of {merge, create_unique, error, warning, replace}.
This parameter specifies the behavior when two items have an identical
primary key.
Using `merge_strategy="merge"`, then there will be a single entry in
the database, but the attributes of all features with the same primary
key will be merged.
Using `merge_strategy="create_unique"`, then the first entry will use
the original primary key, but the second entry will have a unique,
autoincremented primary key assigned to it
Using `merge_strategy="error"`, a :class:`gffutils.DuplicateID`
exception will be raised. This means you will have to edit the file
yourself to fix the duplicated IDs.
Using `merge_strategy="warning"`, a warning will be printed to the
logger, and the duplicate feature will be skipped.
Using `merge_strategy="replace"` will replace the entire existing
feature with the new feature.
transform : callable
Function (or other callable object) that accepts a `Feature` object and
returns a (possibly modified) `Feature` object.
gtf_transcript_key, gtf_gene_key : string
Which attribute to use as the transcript ID and gene ID respectively
for GTF files. Default is `transcript_id` and `gene_id` according to
the GTF spec.
gtf_subfeature : string
Feature type to use as a "gene component" when inferring gene and
transcript extents for GTF files. Default is `exon` according to the
GTF spec.
force_gff : bool
If True, do not do automatic format detection -- only use GFF.
force_dialect_check : bool
If True, the dialect will be checkef for every feature (instead of just
`checklines` features). This can be slow, but may be necessary for
inconsistently-formatted input files.
from_string : bool
If True, then treat `data` as actual data (rather than the path to
a file).
keep_order : bool
If True, all features returned from this instance will have the
order of their attributes maintained. This can be turned on or off
database-wide by setting the `keep_order` attribute or with this
kwarg, or on a feature-by-feature basis by setting the `keep_order`
attribute of an individual feature.
Note that a single order of attributes will be used for all features.
Specifically, the order will be determined by the order of attribute
keys in the first `checklines` of the input data. See
helpers._choose_dialect for more information on this.
Default is False, since this includes a sorting step that can get
time-consuming for many features.
infer_gene_extent : bool
DEPRECATED in version 0.8.4. See `disable_infer_transcripts` and
`disable_infer_genes` for more granular control.
disable_infer_transcripts, disable_infer_genes : bool
Only used for GTF files. By default -- and according to the GTF spec --
we assume that there are no transcript or gene features in the file.
gffutils then infers the extent of each transcript based on its
constituent exons and infers the extent of each gene bases on its
constituent transcripts.
This default behavior is problematic if the input file already contains
transcript or gene features (like recent GENCODE GTF files for human),
since 1) the work to infer extents is unnecessary, and 2)
trying to insert an inferred feature back into the database triggers
gffutils' feature-merging routines, which can get time consuming.
The solution is to use `disable_infer_transcripts=True` if your GTF
already has transcripts in it, and/or `disable_infer_genes=True` if it
already has genes in it. This can result in dramatic (100x) speedup.
Prior to version 0.8.4, setting `infer_gene_extents=False` would
disable both transcript and gene inference simultaneously. As of
version 0.8.4, these argument allow more granular control.
force_merge_fields : list
If merge_strategy="merge", then features will only be merged if their
non-attribute values are identical (same chrom, source, start, stop,
score, strand, phase). Using `force_merge_fields`, you can override
this behavior to allow merges even when fields are different. This
list can contain one or more of ['seqid', 'source', 'featuretype',
'score', 'strand', 'frame']. The resulting merged fields will be
strings of comma-separated values. Note that 'start' and 'end' are not
available, since these fields need to be integers.
text_factory : callable
Text factory to use for the sqlite3 database. See
https://docs.python.org/2/library/\
sqlite3.html#sqlite3.Connection.text_factory
for details. The default sqlite3.OptimizedUnicode will return Unicode
objects only for non-ASCII data, and bytestrings otherwise.
pragmas : dict
Dictionary of pragmas used when creating the sqlite3 database. See
http://www.sqlite.org/pragma.html for a list of available pragmas. The
defaults are stored in constants.default_pragmas, which can be used as
a template for supplying a custom dictionary.
sort_attribute_values : bool
All features returned from the database will have their attribute
values sorted. Typically this is only useful for testing, since this
can get time-consuming for large numbers of features.
_keep_tempfiles : bool or string
False by default to clean up intermediate tempfiles created during GTF
import. If True, then keep these tempfile for testing or debugging.
If string, then keep the tempfile for testing, but also use the string
as the suffix fo the tempfile. This can be useful for testing in
parallel environments.
Returns
-------
New :class:`FeatureDB` object.
"""
_locals = locals()
# Check if any older kwargs made it in
deprecation_handler(kwargs)
kwargs = dict((i, _locals[i]) for i in constants._iterator_kwargs)
# First construct an iterator so that we can identify the file format.
# DataIterator figures out what kind of data was provided (string of lines,
# filename, or iterable of Features) and checks `checklines` lines to
# identify the dialect.
iterator = iterators.DataIterator(**kwargs)
kwargs.update(**_locals)
if dialect is None:
dialect = iterator.dialect
# However, a side-effect of this is that if `data` was a generator, then
# we've just consumed `checklines` items (see
# iterators.BaseIterator.__init__, which calls iterators.peek).
#
# But it also chains those consumed items back onto the beginning, and the
# result is available as as iterator._iter.
#
# That's what we should be using now for `data:
kwargs['data'] = iterator._iter
kwargs['directives'] = iterator.directives
# Since we've already checked lines, we don't want to do it again
kwargs['checklines'] = 0
if force_gff or (dialect['fmt'] == 'gff3'):
cls = _GFFDBCreator
id_spec = id_spec or 'ID'
add_kwargs = dict(
id_spec=id_spec,
)
elif dialect['fmt'] == 'gtf':
cls = _GTFDBCreator
id_spec = id_spec or {'gene': 'gene_id', 'transcript': 'transcript_id'}
add_kwargs = dict(
transcript_key=gtf_transcript_key,
gene_key=gtf_gene_key,
subfeature=gtf_subfeature,
id_spec=id_spec,
)
kwargs.update(**add_kwargs)
kwargs['dialect'] = dialect
c = cls(**kwargs)
c.create()
if dbfn == ':memory:':
db = interface.FeatureDB(c.conn,
keep_order=keep_order,
pragmas=pragmas,
sort_attribute_values=sort_attribute_values,
text_factory=text_factory)
else:
db = interface.FeatureDB(c,
keep_order=keep_order,
pragmas=pragmas,
sort_attribute_values=sort_attribute_values,
text_factory=text_factory)
return db |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_id_handler; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:f; 6, block; 6, 7; 6, 9; 6, 10; 6, 103; 6, 104; 6, 206; 6, 207; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 20; 10, 28; 10, 44; 10, 45; 10, 46; 10, 94; 10, 95; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:id_spec; 17, attribute; 17, 18; 17, 19; 18, identifier:six; 19, identifier:string_types; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:id_key; 24, list:[self.id_spec]; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:id_spec; 28, elif_clause; 28, 29; 28, 36; 29, call; 29, 30; 29, 31; 30, identifier:hasattr; 31, argument_list; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:id_spec; 35, string:'__call__'; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:id_key; 40, list:[self.id_spec]; 40, 41; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:id_spec; 44, comment; 45, comment; 46, elif_clause; 46, 47; 46, 54; 47, call; 47, 48; 47, 49; 48, identifier:isinstance; 49, argument_list; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:id_spec; 53, identifier:dict; 54, block; 54, 55; 55, try_statement; 55, 56; 55, 81; 55, 82; 56, block; 56, 57; 56, 67; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:id_key; 60, subscript; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:id_spec; 64, attribute; 64, 65; 64, 66; 65, identifier:f; 66, identifier:featuretype; 67, if_statement; 67, 68; 67, 75; 68, call; 68, 69; 68, 70; 69, identifier:isinstance; 70, argument_list; 70, 71; 70, 72; 71, identifier:id_key; 72, attribute; 72, 73; 72, 74; 73, identifier:six; 74, identifier:string_types; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:id_key; 79, list:[id_key]; 79, 80; 80, identifier:id_key; 81, comment; 82, except_clause; 82, 83; 82, 84; 83, identifier:KeyError; 84, block; 84, 85; 85, return_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_increment_featuretype_autoid; 90, argument_list; 90, 91; 91, attribute; 91, 92; 91, 93; 92, identifier:f; 93, identifier:featuretype; 94, comment; 95, else_clause; 95, 96; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:id_key; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:id_spec; 103, comment; 104, for_statement; 104, 105; 104, 106; 104, 107; 105, identifier:k; 106, identifier:id_key; 107, block; 107, 108; 108, if_statement; 108, 109; 108, 114; 108, 146; 109, call; 109, 110; 109, 111; 110, identifier:hasattr; 111, argument_list; 111, 112; 111, 113; 112, identifier:k; 113, string:'__call__'; 114, block; 114, 115; 114, 122; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:_id; 118, call; 118, 119; 118, 120; 119, identifier:k; 120, argument_list; 120, 121; 121, identifier:f; 122, if_statement; 122, 123; 122, 124; 123, identifier:_id; 124, block; 124, 125; 124, 144; 125, if_statement; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:_id; 129, identifier:startswith; 130, argument_list; 130, 131; 131, string:'autoincrement:'; 132, block; 132, 133; 133, return_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:_increment_featuretype_autoid; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 141; 140, identifier:_id; 141, slice; 141, 142; 141, 143; 142, integer:14; 143, colon; 144, return_statement; 144, 145; 145, identifier:_id; 146, else_clause; 146, 147; 146, 148; 146, 149; 147, comment; 148, comment; 149, block; 149, 150; 150, if_statement; 150, 151; 150, 173; 150, 174; 150, 175; 150, 188; 151, boolean_operator:and; 151, 152; 151, 166; 152, boolean_operator:and; 152, 153; 152, 160; 153, parenthesized_expression; 153, 154; 154, comparison_operator:>; 154, 155; 154, 159; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, identifier:k; 159, integer:3; 160, parenthesized_expression; 160, 161; 161, comparison_operator:==; 161, 162; 161, 165; 162, subscript; 162, 163; 162, 164; 163, identifier:k; 164, integer:0; 165, string:':'; 166, parenthesized_expression; 166, 167; 167, comparison_operator:==; 167, 168; 167, 172; 168, subscript; 168, 169; 168, 170; 169, identifier:k; 170, unary_operator:-; 170, 171; 171, integer:1; 172, string:':'; 173, comment; 174, comment; 175, block; 175, 176; 176, return_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:getattr; 179, argument_list; 179, 180; 179, 181; 180, identifier:f; 181, subscript; 181, 182; 181, 183; 182, identifier:k; 183, slice; 183, 184; 183, 185; 183, 186; 184, integer:1; 185, colon; 186, unary_operator:-; 186, 187; 187, integer:1; 188, else_clause; 188, 189; 189, block; 189, 190; 190, try_statement; 190, 191; 190, 200; 191, block; 191, 192; 192, return_statement; 192, 193; 193, subscript; 193, 194; 193, 199; 194, subscript; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:f; 197, identifier:attributes; 198, identifier:k; 199, integer:0; 200, except_clause; 200, 201; 200, 204; 201, tuple; 201, 202; 201, 203; 202, identifier:KeyError; 203, identifier:IndexError; 204, block; 204, 205; 205, pass_statement; 206, comment; 207, return_statement; 207, 208; 208, call; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:self; 211, identifier:_increment_featuretype_autoid; 212, argument_list; 212, 213; 213, attribute; 213, 214; 213, 215; 214, identifier:f; 215, identifier:featuretype | def _id_handler(self, f):
"""
Given a Feature from self.iterator, figure out what the ID should be.
This uses `self.id_spec` identify the ID.
"""
# If id_spec is a string, convert to iterable for later
if isinstance(self.id_spec, six.string_types):
id_key = [self.id_spec]
elif hasattr(self.id_spec, '__call__'):
id_key = [self.id_spec]
# If dict, then assume it's a feature -> attribute mapping, e.g.,
# {'gene': 'gene_id'} for GTF
elif isinstance(self.id_spec, dict):
try:
id_key = self.id_spec[f.featuretype]
if isinstance(id_key, six.string_types):
id_key = [id_key]
# Otherwise, use default auto-increment.
except KeyError:
return self._increment_featuretype_autoid(f.featuretype)
# Otherwise assume it's an iterable.
else:
id_key = self.id_spec
# Then try them in order, returning the first one that works:
for k in id_key:
if hasattr(k, '__call__'):
_id = k(f)
if _id:
if _id.startswith('autoincrement:'):
return self._increment_featuretype_autoid(_id[14:])
return _id
else:
# use GFF fields rather than attributes for cases like :seqid:
# or :strand:
if (len(k) > 3) and (k[0] == ':') and (k[-1] == ':'):
# No [0] here -- only attributes key/vals are forced into
# lists, not standard GFF fields.
return getattr(f, k[1:-1])
else:
try:
return f.attributes[k][0]
except (KeyError, IndexError):
pass
# If we get here, then default autoincrement
return self._increment_featuretype_autoid(f.featuretype) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:browser; 3, parameters; 3, 4; 3, 7; 3, 10; 4, default_parameter; 4, 5; 4, 6; 5, identifier:tags; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:proxy; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:other_caps; 12, None; 13, block; 13, 14; 13, 16; 13, 28; 13, 172; 13, 193; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:browser_name; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:os; 23, identifier:environ; 24, identifier:get; 25, argument_list; 25, 26; 25, 27; 26, string:'SELENIUM_BROWSER'; 27, string:'firefox'; 28, function_definition; 28, 29; 28, 30; 28, 31; 29, function_name:browser_check_func; 30, parameters; 31, block; 31, 32; 31, 34; 31, 35; 32, expression_statement; 32, 33; 33, comment; 34, comment; 35, try_statement; 35, 36; 35, 37; 35, 38; 35, 142; 36, comment; 37, comment; 38, block; 38, 39; 38, 85; 38, 86; 38, 98; 38, 99; 38, 100; 38, 132; 39, if_statement; 39, 40; 39, 44; 39, 56; 39, 73; 40, call; 40, 41; 40, 42; 41, identifier:_use_remote_browser; 42, argument_list; 42, 43; 43, identifier:SAUCE_ENV_VARS; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 51; 47, pattern_list; 47, 48; 47, 49; 47, 50; 48, identifier:browser_class; 49, identifier:browser_args; 50, identifier:browser_kwargs; 51, call; 51, 52; 51, 53; 52, identifier:_remote_browser_class; 53, argument_list; 53, 54; 53, 55; 54, identifier:SAUCE_ENV_VARS; 55, identifier:tags; 56, elif_clause; 56, 57; 56, 61; 57, call; 57, 58; 57, 59; 58, identifier:_use_remote_browser; 59, argument_list; 59, 60; 60, identifier:REMOTE_ENV_VARS; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 68; 64, pattern_list; 64, 65; 64, 66; 64, 67; 65, identifier:browser_class; 66, identifier:browser_args; 67, identifier:browser_kwargs; 68, call; 68, 69; 68, 70; 69, identifier:_remote_browser_class; 70, argument_list; 70, 71; 70, 72; 71, identifier:REMOTE_ENV_VARS; 72, identifier:tags; 73, else_clause; 73, 74; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 81; 77, pattern_list; 77, 78; 77, 79; 77, 80; 78, identifier:browser_class; 79, identifier:browser_args; 80, identifier:browser_kwargs; 81, call; 81, 82; 81, 83; 82, identifier:_local_browser_class; 83, argument_list; 83, 84; 84, identifier:browser_name; 85, comment; 86, if_statement; 86, 87; 86, 88; 87, identifier:proxy; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:browser_kwargs; 92, call; 92, 93; 92, 94; 93, identifier:_proxy_kwargs; 94, argument_list; 94, 95; 94, 96; 94, 97; 95, identifier:browser_name; 96, identifier:proxy; 97, identifier:browser_kwargs; 98, comment; 99, comment; 100, if_statement; 100, 101; 100, 106; 101, comparison_operator:==; 101, 102; 101, 103; 102, identifier:browser_class; 103, attribute; 103, 104; 103, 105; 104, identifier:webdriver; 105, identifier:Remote; 106, block; 106, 107; 106, 113; 106, 126; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:desired_caps; 110, boolean_operator:or; 110, 111; 110, 112; 111, identifier:other_caps; 112, dictionary; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:desired_caps; 117, identifier:update; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:browser_kwargs; 122, identifier:get; 123, argument_list; 123, 124; 123, 125; 124, string:'desired_capabilities'; 125, dictionary; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:browser_kwargs; 130, string:'desired_capabilities'; 131, identifier:desired_caps; 132, return_statement; 132, 133; 133, expression_list; 133, 134; 133, 135; 134, True; 135, call; 135, 136; 135, 137; 136, identifier:browser_class; 137, argument_list; 137, 138; 137, 140; 138, list_splat; 138, 139; 139, identifier:browser_args; 140, dictionary_splat; 140, 141; 141, identifier:browser_kwargs; 142, except_clause; 142, 143; 142, 151; 143, as_pattern; 143, 144; 143, 149; 144, tuple; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:socket; 147, identifier:error; 148, identifier:WebDriverException; 149, as_pattern_target; 149, 150; 150, identifier:err; 151, block; 151, 152; 151, 159; 151, 168; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:msg; 155, call; 155, 156; 155, 157; 156, identifier:str; 157, argument_list; 157, 158; 158, identifier:err; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:LOGGER; 163, identifier:debug; 164, argument_list; 164, 165; 165, binary_operator:+; 165, 166; 165, 167; 166, string:'Failed to instantiate browser: '; 167, identifier:msg; 168, return_statement; 168, 169; 169, expression_list; 169, 170; 169, 171; 170, False; 171, None; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:browser_instance; 175, call; 175, 176; 175, 192; 176, attribute; 176, 177; 176, 191; 177, call; 177, 178; 177, 179; 178, identifier:Promise; 179, argument_list; 179, 180; 179, 181; 179, 182; 179, 183; 179, 184; 179, 185; 179, 188; 180, comment; 181, comment; 182, comment; 183, identifier:browser_check_func; 184, string:"Browser is instantiated successfully."; 185, keyword_argument; 185, 186; 185, 187; 186, identifier:try_limit; 187, integer:3; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:timeout; 190, integer:95; 191, identifier:fulfill; 192, argument_list; 193, return_statement; 193, 194; 194, identifier:browser_instance | def browser(tags=None, proxy=None, other_caps=None):
"""
Interpret environment variables to configure Selenium.
Performs validation, logging, and sensible defaults.
There are three cases:
1. Local browsers: If the proper environment variables are not all set for the second case,
then we use a local browser.
* The environment variable `SELENIUM_BROWSER` can be set to specify which local browser to use. The default is \
Firefox.
* Additionally, if a proxy instance is passed and the browser choice is either Chrome or Firefox, then the \
browser will be initialized with the proxy server set.
* The environment variable `SELENIUM_FIREFOX_PATH` can be used for specifying a path to the Firefox binary. \
Default behavior is to use the system location.
* The environment variable `FIREFOX_PROFILE_PATH` can be used for specifying a path to the Firefox profile. \
Default behavior is to use a barebones default profile with a few useful preferences set.
2. Remote browser (not SauceLabs): Set all of the following environment variables, but not all of
the ones needed for SauceLabs:
* SELENIUM_BROWSER
* SELENIUM_HOST
* SELENIUM_PORT
3. SauceLabs: Set all of the following environment variables:
* SELENIUM_BROWSER
* SELENIUM_VERSION
* SELENIUM_PLATFORM
* SELENIUM_HOST
* SELENIUM_PORT
* SAUCE_USER_NAME
* SAUCE_API_KEY
**NOTE:** these are the environment variables set by the SauceLabs
Jenkins plugin.
Optionally provide Jenkins info, used to identify jobs to Sauce:
* JOB_NAME
* BUILD_NUMBER
`tags` is a list of string tags to apply to the SauceLabs
job. If not using SauceLabs, these will be ignored.
Keyword Args:
tags (list of str): Tags to apply to the SauceLabs job. If not using SauceLabs, these will be ignored.
proxy: A proxy instance.
other_caps (dict of str): Additional desired capabilities to provide to remote WebDriver instances. Note
that these values will be overwritten by environment variables described above. This is only used for
remote driver instances, where such info is usually used by services for additional configuration and
metadata.
Returns:
selenium.webdriver: The configured browser object used to drive tests
Raises:
BrowserConfigError: The environment variables are not correctly specified.
"""
browser_name = os.environ.get('SELENIUM_BROWSER', 'firefox')
def browser_check_func():
""" Instantiate the browser and return the browser instance """
# See https://openedx.atlassian.net/browse/TE-701
try:
# Get the class and kwargs required to instantiate the browser based on
# whether we are using a local or remote one.
if _use_remote_browser(SAUCE_ENV_VARS):
browser_class, browser_args, browser_kwargs = _remote_browser_class(
SAUCE_ENV_VARS, tags)
elif _use_remote_browser(REMOTE_ENV_VARS):
browser_class, browser_args, browser_kwargs = _remote_browser_class(
REMOTE_ENV_VARS, tags)
else:
browser_class, browser_args, browser_kwargs = _local_browser_class(
browser_name)
# If we are using a proxy, we need extra kwargs passed on intantiation.
if proxy:
browser_kwargs = _proxy_kwargs(browser_name, proxy, browser_kwargs)
# Load in user given desired caps but override with derived caps from above. This is to retain existing
# behavior. Only for remote drivers, where various testing services use this info for configuration.
if browser_class == webdriver.Remote:
desired_caps = other_caps or {}
desired_caps.update(browser_kwargs.get('desired_capabilities', {}))
browser_kwargs['desired_capabilities'] = desired_caps
return True, browser_class(*browser_args, **browser_kwargs)
except (socket.error, WebDriverException) as err:
msg = str(err)
LOGGER.debug('Failed to instantiate browser: ' + msg)
return False, None
browser_instance = Promise(
# There are cases where selenium takes 30s to return with a failure, so in order to try 3
# times, we set a long timeout. If there is a hang on the first try, the timeout will
# be enforced.
browser_check_func, "Browser is instantiated successfully.", try_limit=3, timeout=95).fulfill()
return browser_instance |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_local_browser_class; 3, parameters; 3, 4; 4, identifier:browser_name; 5, block; 5, 6; 5, 8; 5, 9; 5, 17; 5, 18; 5, 27; 5, 45; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:LOGGER; 13, identifier:info; 14, argument_list; 14, 15; 14, 16; 15, string:u"Using local browser: %s [Default is firefox]"; 16, identifier:browser_name; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:browser_class; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:BROWSERS; 24, identifier:get; 25, argument_list; 25, 26; 26, identifier:browser_name; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:headless; 30, comparison_operator:==; 30, 31; 30, 44; 31, call; 31, 32; 31, 43; 32, attribute; 32, 33; 32, 42; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:os; 37, identifier:environ; 38, identifier:get; 39, argument_list; 39, 40; 39, 41; 40, string:'BOKCHOY_HEADLESS'; 41, string:'false'; 42, identifier:lower; 43, argument_list; 44, string:'true'; 45, if_statement; 45, 46; 45, 49; 45, 77; 46, comparison_operator:is; 46, 47; 46, 48; 47, identifier:browser_class; 48, None; 49, block; 49, 50; 50, raise_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:BrowserConfigError; 53, argument_list; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, string:u"Invalid browser name {name}. Options are: {options}"; 57, identifier:format; 58, argument_list; 58, 59; 58, 62; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:name; 61, identifier:browser_name; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:options; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, string:", "; 67, identifier:join; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:list; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:BROWSERS; 75, identifier:keys; 76, argument_list; 77, else_clause; 77, 78; 78, block; 78, 79; 78, 296; 79, if_statement; 79, 80; 79, 83; 79, 84; 79, 238; 79, 286; 80, comparison_operator:==; 80, 81; 80, 82; 81, identifier:browser_name; 82, string:'firefox'; 83, comment; 84, block; 84, 85; 84, 101; 84, 118; 84, 124; 84, 132; 84, 141; 84, 145; 84, 157; 84, 168; 84, 179; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:log_path; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:os; 92, identifier:path; 93, identifier:join; 94, argument_list; 94, 95; 94, 100; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:os; 98, identifier:getcwd; 99, argument_list; 100, string:'geckodriver.log'; 101, if_statement; 101, 102; 101, 110; 102, call; 102, 103; 102, 108; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:os; 106, identifier:path; 107, identifier:exists; 108, argument_list; 108, 109; 109, identifier:log_path; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:os; 115, identifier:remove; 116, argument_list; 116, 117; 117, identifier:log_path; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:firefox_options; 121, call; 121, 122; 121, 123; 122, identifier:FirefoxOptions; 123, argument_list; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 131; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:firefox_options; 129, identifier:log; 130, identifier:level; 131, string:'trace'; 132, if_statement; 132, 133; 132, 134; 133, identifier:headless; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:firefox_options; 139, identifier:headless; 140, True; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:browser_args; 144, list:[]; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:browser_kwargs; 148, dictionary; 148, 149; 148, 154; 149, pair; 149, 150; 149, 151; 150, string:'firefox_profile'; 151, call; 151, 152; 151, 153; 152, identifier:_firefox_profile; 153, argument_list; 154, pair; 154, 155; 154, 156; 155, string:'options'; 156, identifier:firefox_options; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:firefox_path; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:os; 164, identifier:environ; 165, identifier:get; 166, argument_list; 166, 167; 167, string:'SELENIUM_FIREFOX_PATH'; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:firefox_log; 171, call; 171, 172; 171, 177; 172, attribute; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:os; 175, identifier:environ; 176, identifier:get; 177, argument_list; 177, 178; 178, string:'SELENIUM_FIREFOX_LOG'; 179, if_statement; 179, 180; 179, 183; 179, 202; 179, 220; 180, boolean_operator:and; 180, 181; 180, 182; 181, identifier:firefox_path; 182, identifier:firefox_log; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:browser_kwargs; 188, identifier:update; 189, argument_list; 189, 190; 190, dictionary; 190, 191; 191, pair; 191, 192; 191, 193; 192, string:'firefox_binary'; 193, call; 193, 194; 193, 195; 194, identifier:FirefoxBinary; 195, argument_list; 195, 196; 195, 199; 196, keyword_argument; 196, 197; 196, 198; 197, identifier:firefox_path; 198, identifier:firefox_path; 199, keyword_argument; 199, 200; 199, 201; 200, identifier:log_file; 201, identifier:firefox_log; 202, elif_clause; 202, 203; 202, 204; 203, identifier:firefox_path; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:browser_kwargs; 209, identifier:update; 210, argument_list; 210, 211; 211, dictionary; 211, 212; 212, pair; 212, 213; 212, 214; 213, string:'firefox_binary'; 214, call; 214, 215; 214, 216; 215, identifier:FirefoxBinary; 216, argument_list; 216, 217; 217, keyword_argument; 217, 218; 217, 219; 218, identifier:firefox_path; 219, identifier:firefox_path; 220, elif_clause; 220, 221; 220, 222; 221, identifier:firefox_log; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:browser_kwargs; 227, identifier:update; 228, argument_list; 228, 229; 229, dictionary; 229, 230; 230, pair; 230, 231; 230, 232; 231, string:'firefox_binary'; 232, call; 232, 233; 232, 234; 233, identifier:FirefoxBinary; 234, argument_list; 234, 235; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:log_file; 237, identifier:firefox_log; 238, elif_clause; 238, 239; 238, 242; 239, comparison_operator:==; 239, 240; 239, 241; 240, identifier:browser_name; 241, string:'chrome'; 242, block; 242, 243; 242, 249; 242, 258; 242, 259; 242, 266; 242, 267; 242, 268; 242, 275; 242, 279; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:chrome_options; 246, call; 246, 247; 246, 248; 247, identifier:ChromeOptions; 248, argument_list; 249, if_statement; 249, 250; 249, 251; 250, identifier:headless; 251, block; 251, 252; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:chrome_options; 256, identifier:headless; 257, True; 258, comment; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:chrome_options; 263, identifier:add_argument; 264, argument_list; 264, 265; 265, string:'--use-fake-device-for-media-stream'; 266, comment; 267, comment; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:chrome_options; 272, identifier:add_argument; 273, argument_list; 273, 274; 274, string:'--use-fake-ui-for-media-stream'; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:browser_args; 278, list:[]; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 281, identifier:browser_kwargs; 282, dictionary; 282, 283; 283, pair; 283, 284; 283, 285; 284, string:'options'; 285, identifier:chrome_options; 286, else_clause; 286, 287; 287, block; 287, 288; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 293; 290, pattern_list; 290, 291; 290, 292; 291, identifier:browser_args; 292, identifier:browser_kwargs; 293, expression_list; 293, 294; 293, 295; 294, list:[]; 295, dictionary; 296, return_statement; 296, 297; 297, expression_list; 297, 298; 297, 299; 297, 300; 298, identifier:browser_class; 299, identifier:browser_args; 300, identifier:browser_kwargs | def _local_browser_class(browser_name):
"""
Returns class, kwargs, and args needed to instantiate the local browser.
"""
# Log name of local browser
LOGGER.info(u"Using local browser: %s [Default is firefox]", browser_name)
# Get class of local browser based on name
browser_class = BROWSERS.get(browser_name)
headless = os.environ.get('BOKCHOY_HEADLESS', 'false').lower() == 'true'
if browser_class is None:
raise BrowserConfigError(
u"Invalid browser name {name}. Options are: {options}".format(
name=browser_name, options=", ".join(list(BROWSERS.keys()))))
else:
if browser_name == 'firefox':
# Remove geckodriver log data from previous test cases
log_path = os.path.join(os.getcwd(), 'geckodriver.log')
if os.path.exists(log_path):
os.remove(log_path)
firefox_options = FirefoxOptions()
firefox_options.log.level = 'trace'
if headless:
firefox_options.headless = True
browser_args = []
browser_kwargs = {
'firefox_profile': _firefox_profile(),
'options': firefox_options,
}
firefox_path = os.environ.get('SELENIUM_FIREFOX_PATH')
firefox_log = os.environ.get('SELENIUM_FIREFOX_LOG')
if firefox_path and firefox_log:
browser_kwargs.update({
'firefox_binary': FirefoxBinary(
firefox_path=firefox_path, log_file=firefox_log)
})
elif firefox_path:
browser_kwargs.update({
'firefox_binary': FirefoxBinary(firefox_path=firefox_path)
})
elif firefox_log:
browser_kwargs.update({
'firefox_binary': FirefoxBinary(log_file=firefox_log)
})
elif browser_name == 'chrome':
chrome_options = ChromeOptions()
if headless:
chrome_options.headless = True
# Emulate webcam and microphone for testing purposes
chrome_options.add_argument('--use-fake-device-for-media-stream')
# Bypasses the security prompt displayed by the browser when it attempts to
# access a media device (e.g., a webcam)
chrome_options.add_argument('--use-fake-ui-for-media-stream')
browser_args = []
browser_kwargs = {
'options': chrome_options,
}
else:
browser_args, browser_kwargs = [], {}
return browser_class, browser_args, browser_kwargs |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:filter; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filter_fn; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:desc; 10, None; 11, dictionary_splat_pattern; 11, 12; 12, identifier:kwargs; 13, block; 13, 14; 13, 16; 13, 28; 13, 41; 13, 88; 13, 97; 13, 126; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 22; 17, boolean_operator:and; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:filter_fn; 20, None; 21, identifier:kwargs; 22, block; 22, 23; 23, raise_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:TypeError; 26, argument_list; 26, 27; 27, string:'Must supply either a filter_fn or attribute filter parameters to filter(), but not both.'; 28, if_statement; 28, 29; 28, 35; 29, boolean_operator:and; 29, 30; 29, 33; 30, comparison_operator:is; 30, 31; 30, 32; 31, identifier:filter_fn; 32, None; 33, not_operator; 33, 34; 34, identifier:kwargs; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:TypeError; 39, argument_list; 39, 40; 40, string:'Must supply one of filter_fn or one or more attribute filter parameters to filter().'; 41, if_statement; 41, 42; 41, 45; 42, comparison_operator:is; 42, 43; 42, 44; 43, identifier:desc; 44, None; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 50; 46, 60; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:filter_fn; 49, None; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:desc; 54, call; 54, 55; 54, 56; 55, identifier:getattr; 56, argument_list; 56, 57; 56, 58; 56, 59; 57, identifier:filter_fn; 58, string:'__name__'; 59, string:''; 60, elif_clause; 60, 61; 60, 62; 61, identifier:kwargs; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:desc; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:u", "; 69, identifier:join; 70, argument_list; 70, 71; 71, list_comprehension; 71, 72; 71, 79; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, string:u"{}={!r}"; 75, identifier:format; 76, argument_list; 76, 77; 76, 78; 77, identifier:key; 78, identifier:value; 79, for_in_clause; 79, 80; 79, 83; 80, pattern_list; 80, 81; 80, 82; 81, identifier:key; 82, identifier:value; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:kwargs; 86, identifier:items; 87, argument_list; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:desc; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:u"filter({})"; 94, identifier:format; 95, argument_list; 95, 96; 96, identifier:desc; 97, if_statement; 97, 98; 97, 99; 98, identifier:kwargs; 99, block; 99, 100; 100, function_definition; 100, 101; 100, 102; 100, 104; 100, 105; 101, function_name:filter_fn; 102, parameters; 102, 103; 103, identifier:elem; 104, comment; 105, block; 105, 106; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:all; 109, generator_expression; 109, 110; 109, 117; 110, comparison_operator:==; 110, 111; 110, 116; 111, call; 111, 112; 111, 113; 112, identifier:getattr; 113, argument_list; 113, 114; 113, 115; 114, identifier:elem; 115, identifier:filter_key; 116, identifier:filter_value; 117, for_in_clause; 117, 118; 117, 121; 118, pattern_list; 118, 119; 118, 120; 119, identifier:filter_key; 120, identifier:filter_value; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:kwargs; 124, identifier:items; 125, argument_list; 126, return_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:transform; 131, argument_list; 131, 132; 131, 145; 132, lambda; 132, 133; 132, 135; 133, lambda_parameters; 133, 134; 134, identifier:xs; 135, generator_expression; 135, 136; 135, 137; 135, 140; 136, identifier:x; 137, for_in_clause; 137, 138; 137, 139; 138, identifier:x; 139, identifier:xs; 140, if_clause; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:filter_fn; 143, argument_list; 143, 144; 144, identifier:x; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:desc; 147, identifier:desc | def filter(self, filter_fn=None, desc=None, **kwargs):
"""
Return a copy of this query, with some values removed.
Example usages:
.. code:: python
# Returns a query that matches even numbers
q.filter(filter_fn=lambda x: x % 2)
# Returns a query that matches elements with el.description == "foo"
q.filter(description="foo")
Keyword Args:
filter_fn (callable): If specified, a function that accepts one argument (the element)
and returns a boolean indicating whether to include that element in the results.
kwargs: Specify attribute values that an element must have to be included in the results.
desc (str): A description of the filter, for use in log messages.
Defaults to the name of the filter function or attribute.
Raises:
TypeError: neither or both of `filter_fn` and `kwargs` are provided.
"""
if filter_fn is not None and kwargs:
raise TypeError('Must supply either a filter_fn or attribute filter parameters to filter(), but not both.')
if filter_fn is None and not kwargs:
raise TypeError('Must supply one of filter_fn or one or more attribute filter parameters to filter().')
if desc is None:
if filter_fn is not None:
desc = getattr(filter_fn, '__name__', '')
elif kwargs:
desc = u", ".join([u"{}={!r}".format(key, value) for key, value in kwargs.items()])
desc = u"filter({})".format(desc)
if kwargs:
def filter_fn(elem): # pylint: disable=function-redefined, missing-docstring
return all(
getattr(elem, filter_key) == filter_value
for filter_key, filter_value
in kwargs.items()
)
return self.transform(lambda xs: (x for x in xs if filter_fn(x)), desc=desc) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:order_by_on_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:objects; 5, identifier:order_field; 6, default_parameter; 6, 7; 6, 8; 7, identifier:is_desc; 8, False; 9, block; 9, 10; 9, 12; 9, 31; 9, 53; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:callable; 15, argument_list; 15, 16; 16, identifier:order_field; 17, block; 17, 18; 17, 30; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:objects; 22, identifier:sort; 23, argument_list; 23, 24; 23, 27; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:key; 26, identifier:order_field; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:reverse; 29, identifier:is_desc; 30, return_statement; 31, function_definition; 31, 32; 31, 33; 31, 35; 32, function_name:order_key; 33, parameters; 33, 34; 34, identifier:x; 35, block; 35, 36; 35, 44; 35, 51; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:v; 39, call; 39, 40; 39, 41; 40, identifier:getattr_path; 41, argument_list; 41, 42; 41, 43; 42, identifier:x; 43, identifier:order_field; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:v; 47, None; 48, block; 48, 49; 49, return_statement; 49, 50; 50, identifier:MIN; 51, return_statement; 51, 52; 52, identifier:v; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:objects; 57, identifier:sort; 58, argument_list; 58, 59; 58, 62; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:key; 61, identifier:order_key; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:reverse; 64, identifier:is_desc | def order_by_on_list(objects, order_field, is_desc=False):
"""
Utility function to sort objects django-style even for non-query set collections
:param objects: list of objects to sort
:param order_field: field name, follows django conventions, so "foo__bar" means `foo.bar`, can be a callable.
:param is_desc: reverse the sorting
:return:
"""
if callable(order_field):
objects.sort(key=order_field, reverse=is_desc)
return
def order_key(x):
v = getattr_path(x, order_field)
if v is None:
return MIN
return v
objects.sort(key=order_key, reverse=is_desc) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 42; 2, function_name:render_table; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 22; 3, 25; 3, 28; 3, 31; 3, 34; 4, identifier:request; 5, identifier:table; 6, default_parameter; 6, 7; 6, 8; 7, identifier:links; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:context; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:template; 14, string:'tri_table/list.html'; 15, default_parameter; 15, 16; 15, 17; 16, identifier:blank_on_empty; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:paginate_by; 20, integer:40; 21, comment; 22, default_parameter; 22, 23; 22, 24; 23, identifier:page; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:paginator; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:show_hits; 30, False; 31, default_parameter; 31, 32; 31, 33; 32, identifier:hit_label; 33, string:'Items'; 34, default_parameter; 34, 35; 34, 36; 35, identifier:post_bulk_edit; 36, lambda; 36, 37; 36, 41; 37, lambda_parameters; 37, 38; 37, 39; 37, 40; 38, identifier:table; 39, identifier:queryset; 40, identifier:updates; 41, None; 42, block; 42, 43; 42, 45; 42, 53; 42, 66; 42, 73; 42, 79; 42, 93; 42, 98; 42, 106; 42, 114; 42, 122; 42, 216; 42, 249; 42, 259; 42, 290; 43, expression_statement; 43, 44; 44, comment; 45, if_statement; 45, 46; 45, 48; 46, not_operator; 46, 47; 47, identifier:context; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:context; 52, dictionary; 53, if_statement; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:table; 58, identifier:Namespace; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:table; 63, call; 63, 64; 63, 65; 64, identifier:table; 65, argument_list; 66, assert_statement; 66, 67; 66, 72; 67, call; 67, 68; 67, 69; 68, identifier:isinstance; 69, argument_list; 69, 70; 69, 71; 70, identifier:table; 71, identifier:Table; 72, identifier:table; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:table; 77, identifier:request; 78, identifier:request; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 84; 81, pattern_list; 81, 82; 81, 83; 82, identifier:should_return; 83, identifier:dispatch_result; 84, call; 84, 85; 84, 86; 85, identifier:handle_dispatch; 86, argument_list; 86, 87; 86, 90; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:request; 89, identifier:request; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:obj; 92, identifier:table; 93, if_statement; 93, 94; 93, 95; 94, identifier:should_return; 95, block; 95, 96; 96, return_statement; 96, 97; 97, identifier:dispatch_result; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:context; 102, string:'bulk_form'; 103, attribute; 103, 104; 103, 105; 104, identifier:table; 105, identifier:bulk_form; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:context; 110, string:'query_form'; 111, attribute; 111, 112; 111, 113; 112, identifier:table; 113, identifier:query_form; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:context; 118, string:'tri_query_error'; 119, attribute; 119, 120; 119, 121; 120, identifier:table; 121, identifier:query_error; 122, if_statement; 122, 123; 122, 132; 123, boolean_operator:and; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:table; 126, identifier:bulk_form; 127, comparison_operator:==; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:request; 130, identifier:method; 131, string:'POST'; 132, block; 132, 133; 133, if_statement; 133, 134; 133, 141; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:table; 138, identifier:bulk_form; 139, identifier:is_valid; 140, argument_list; 141, block; 141, 142; 141, 150; 141, 186; 141, 194; 141, 207; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:queryset; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:table; 148, identifier:bulk_queryset; 149, argument_list; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:updates; 153, dictionary_comprehension; 153, 154; 153, 161; 153, 168; 154, pair; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:field; 157, identifier:name; 158, attribute; 158, 159; 158, 160; 159, identifier:field; 160, identifier:value; 161, for_in_clause; 161, 162; 161, 163; 162, identifier:field; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:table; 166, identifier:bulk_form; 167, identifier:fields; 168, if_clause; 168, 169; 169, boolean_operator:and; 169, 170; 169, 181; 170, boolean_operator:and; 170, 171; 170, 176; 171, comparison_operator:is; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:field; 174, identifier:value; 175, None; 176, comparison_operator:!=; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:field; 179, identifier:value; 180, string:''; 181, comparison_operator:is; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:field; 184, identifier:attr; 185, None; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:queryset; 190, identifier:update; 191, argument_list; 191, 192; 192, dictionary_splat; 192, 193; 193, identifier:updates; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 197; 196, identifier:post_bulk_edit; 197, argument_list; 197, 198; 197, 201; 197, 204; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:table; 200, identifier:table; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:queryset; 203, identifier:queryset; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:updates; 206, identifier:updates; 207, return_statement; 207, 208; 208, call; 208, 209; 208, 210; 209, identifier:HttpResponseRedirect; 210, argument_list; 210, 211; 211, subscript; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:request; 214, identifier:META; 215, string:'HTTP_REFERER'; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:table; 220, identifier:context; 221, call; 221, 222; 221, 223; 222, identifier:table_context; 223, argument_list; 223, 224; 223, 225; 223, 228; 223, 231; 223, 234; 223, 237; 223, 240; 223, 243; 223, 246; 224, identifier:request; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:table; 227, identifier:table; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:links; 230, identifier:links; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:paginate_by; 233, identifier:paginate_by; 234, keyword_argument; 234, 235; 234, 236; 235, identifier:page; 236, identifier:page; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:extra_context; 239, identifier:context; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:paginator; 242, identifier:paginator; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:show_hits; 245, identifier:show_hits; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:hit_label; 248, identifier:hit_label; 249, if_statement; 249, 250; 249, 256; 250, boolean_operator:and; 250, 251; 250, 255; 251, not_operator; 251, 252; 252, attribute; 252, 253; 252, 254; 253, identifier:table; 254, identifier:data; 255, identifier:blank_on_empty; 256, block; 256, 257; 257, return_statement; 257, 258; 258, string:''; 259, if_statement; 259, 260; 259, 272; 260, boolean_operator:and; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:table; 263, identifier:query_form; 264, not_operator; 264, 265; 265, call; 265, 266; 265, 271; 266, attribute; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:table; 269, identifier:query_form; 270, identifier:is_valid; 271, argument_list; 272, block; 272, 273; 272, 279; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:table; 277, identifier:data; 278, None; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 286; 281, subscript; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:table; 284, identifier:context; 285, string:'invalid_form_message'; 286, call; 286, 287; 286, 288; 287, identifier:mark_safe; 288, argument_list; 288, 289; 289, string:'<i class="fa fa-meh-o fa-5x" aria-hidden="true"></i>'; 290, return_statement; 290, 291; 291, call; 291, 292; 291, 293; 292, identifier:render_template; 293, argument_list; 293, 294; 293, 295; 293, 296; 294, identifier:request; 295, identifier:template; 296, attribute; 296, 297; 296, 298; 297, identifier:table; 298, identifier:context | def render_table(request,
table,
links=None,
context=None,
template='tri_table/list.html',
blank_on_empty=False,
paginate_by=40, # pragma: no mutate
page=None,
paginator=None,
show_hits=False,
hit_label='Items',
post_bulk_edit=lambda table, queryset, updates: None):
"""
Render a table. This automatically handles pagination, sorting, filtering and bulk operations.
:param request: the request object. This is set on the table object so that it is available for lambda expressions.
:param table: an instance of Table
:param links: a list of instances of Link
:param context: dict of extra context parameters
:param template: if you need to render the table differently you can override this parameter with either a name of a template to load or a `Template` instance.
:param blank_on_empty: turn off the displaying of `{{ empty_message }}` in the template when the list is empty
:param show_hits: Display how many items there are total in the paginator.
:param hit_label: Label for the show_hits display.
:return: a string with the rendered HTML table
"""
if not context:
context = {}
if isinstance(table, Namespace):
table = table()
assert isinstance(table, Table), table
table.request = request
should_return, dispatch_result = handle_dispatch(request=request, obj=table)
if should_return:
return dispatch_result
context['bulk_form'] = table.bulk_form
context['query_form'] = table.query_form
context['tri_query_error'] = table.query_error
if table.bulk_form and request.method == 'POST':
if table.bulk_form.is_valid():
queryset = table.bulk_queryset()
updates = {
field.name: field.value
for field in table.bulk_form.fields
if field.value is not None and field.value != '' and field.attr is not None
}
queryset.update(**updates)
post_bulk_edit(table=table, queryset=queryset, updates=updates)
return HttpResponseRedirect(request.META['HTTP_REFERER'])
table.context = table_context(
request,
table=table,
links=links,
paginate_by=paginate_by,
page=page,
extra_context=context,
paginator=paginator,
show_hits=show_hits,
hit_label=hit_label,
)
if not table.data and blank_on_empty:
return ''
if table.query_form and not table.query_form.is_valid():
table.data = None
table.context['invalid_form_message'] = mark_safe('<i class="fa fa-meh-o fa-5x" aria-hidden="true"></i>')
return render_template(request, template, table.context) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 2, function_name:create_network; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 4, identifier:self; 5, identifier:net_view_name; 6, identifier:cidr; 7, default_parameter; 7, 8; 7, 9; 8, identifier:nameservers; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:members; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:gateway_ip; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:dhcp_trel_ip; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:network_extattrs; 21, None; 22, block; 22, 23; 22, 25; 22, 36; 22, 40; 22, 65; 22, 87; 22, 112; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ipv4; 28, comparison_operator:==; 28, 29; 28, 35; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:ib_utils; 32, identifier:determine_ip_version; 33, argument_list; 33, 34; 34, identifier:cidr; 35, integer:4; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:options; 39, list:[]; 40, if_statement; 40, 41; 40, 42; 41, identifier:nameservers; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:options; 47, identifier:append; 48, argument_list; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:obj; 52, identifier:DhcpOption; 53, argument_list; 53, 54; 53, 57; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:name; 56, string:'domain-name-servers'; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:value; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, string:","; 62, identifier:join; 63, argument_list; 63, 64; 64, identifier:nameservers; 65, if_statement; 65, 66; 65, 69; 66, boolean_operator:and; 66, 67; 66, 68; 67, identifier:ipv4; 68, identifier:gateway_ip; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:options; 74, identifier:append; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:obj; 79, identifier:DhcpOption; 80, argument_list; 80, 81; 80, 84; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:name; 83, string:'routers'; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:value; 86, identifier:gateway_ip; 87, if_statement; 87, 88; 87, 91; 88, boolean_operator:and; 88, 89; 88, 90; 89, identifier:ipv4; 90, identifier:dhcp_trel_ip; 91, block; 91, 92; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:options; 96, identifier:append; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:obj; 101, identifier:DhcpOption; 102, argument_list; 102, 103; 102, 106; 102, 109; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:name; 105, string:'dhcp-server-identifier'; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:num; 108, integer:54; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:value; 111, identifier:dhcp_trel_ip; 112, return_statement; 112, 113; 113, call; 113, 114; 113, 119; 114, attribute; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:obj; 117, identifier:Network; 118, identifier:create; 119, argument_list; 119, 120; 119, 123; 119, 126; 119, 129; 119, 132; 119, 135; 119, 138; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:connector; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:network_view; 125, identifier:net_view_name; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:cidr; 128, identifier:cidr; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:members; 131, identifier:members; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:options; 134, identifier:options; 135, keyword_argument; 135, 136; 135, 137; 136, identifier:extattrs; 137, identifier:network_extattrs; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:check_if_exists; 140, False | def create_network(self, net_view_name, cidr, nameservers=None,
members=None, gateway_ip=None, dhcp_trel_ip=None,
network_extattrs=None):
"""Create NIOS Network and prepare DHCP options.
Some DHCP options are valid for IPv4 only, so just skip processing
them for IPv6 case.
:param net_view_name: network view name
:param cidr: network to allocate, example '172.23.23.0/24'
:param nameservers: list of name servers hosts/ip
:param members: list of objects.AnyMember objects that are expected
to serve dhcp for created network
:param gateway_ip: gateway ip for the network (valid for IPv4 only)
:param dhcp_trel_ip: ip address of dhcp relay (valid for IPv4 only)
:param network_extattrs: extensible attributes for network (instance of
objects.EA)
:returns: created network (instance of objects.Network)
"""
ipv4 = ib_utils.determine_ip_version(cidr) == 4
options = []
if nameservers:
options.append(obj.DhcpOption(name='domain-name-servers',
value=",".join(nameservers)))
if ipv4 and gateway_ip:
options.append(obj.DhcpOption(name='routers',
value=gateway_ip))
if ipv4 and dhcp_trel_ip:
options.append(obj.DhcpOption(name='dhcp-server-identifier',
num=54,
value=dhcp_trel_ip))
return obj.Network.create(self.connector,
network_view=net_view_name,
cidr=cidr,
members=members,
options=options,
extattrs=network_extattrs,
check_if_exists=False) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_run_pass; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 177; 6, expression_statement; 6, 7; 7, comment; 8, while_statement; 8, 9; 8, 10; 9, True; 10, block; 10, 11; 10, 45; 10, 54; 10, 104; 10, 114; 10, 146; 10, 156; 11, try_statement; 11, 12; 11, 24; 12, block; 12, 13; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:data; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_file; 21, identifier:read; 22, argument_list; 22, 23; 23, integer:4096; 24, except_clause; 24, 25; 24, 26; 24, 27; 25, identifier:IOError; 26, identifier:e; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 36; 29, comparison_operator:==; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:e; 32, identifier:errno; 33, attribute; 33, 34; 33, 35; 34, identifier:errno; 35, identifier:ESTALE; 36, block; 36, 37; 36, 43; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:active; 42, False; 43, return_statement; 43, 44; 44, False; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:lines; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_buffer_extract; 52, argument_list; 52, 53; 53, identifier:data; 54, if_statement; 54, 55; 54, 57; 54, 58; 55, not_operator; 55, 56; 56, identifier:lines; 57, comment; 58, block; 58, 59; 58, 103; 59, if_statement; 59, 60; 59, 75; 60, boolean_operator:and; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:_current_event; 64, comparison_operator:>; 64, 65; 64, 74; 65, binary_operator:-; 65, 66; 65, 71; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:time; 69, identifier:time; 70, argument_list; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_last_activity; 74, integer:1; 75, block; 75, 76; 75, 87; 75, 95; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:event; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:'\n'; 82, identifier:join; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_current_event; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 94; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_current_event; 93, identifier:clear; 94, argument_list; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:_callback_wrapper; 100, argument_list; 100, 101; 101, list:[event]; 101, 102; 102, identifier:event; 103, break_statement; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:_last_activity; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:time; 112, identifier:time; 113, argument_list; 114, if_statement; 114, 115; 114, 122; 114, 123; 114, 140; 115, boolean_operator:or; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_multiline_regex_after; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:_multiline_regex_before; 122, comment; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:events; 127, call; 127, 128; 127, 129; 128, identifier:multiline_merge; 129, argument_list; 129, 130; 129, 131; 129, 134; 129, 137; 130, identifier:lines; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_current_event; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_multiline_regex_after; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:_multiline_regex_before; 140, else_clause; 140, 141; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:events; 145, identifier:lines; 146, if_statement; 146, 147; 146, 148; 147, identifier:events; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:_callback_wrapper; 154, argument_list; 154, 155; 155, identifier:events; 156, if_statement; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_sincedb_path; 160, block; 160, 161; 160, 168; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:current_line_count; 164, call; 164, 165; 164, 166; 165, identifier:len; 166, argument_list; 166, 167; 167, identifier:lines; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:_sincedb_update_position; 173, argument_list; 173, 174; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:lines; 176, identifier:current_line_count; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:_sincedb_update_position; 182, argument_list | def _run_pass(self):
"""Read lines from a file and performs a callback against them"""
while True:
try:
data = self._file.read(4096)
except IOError, e:
if e.errno == errno.ESTALE:
self.active = False
return False
lines = self._buffer_extract(data)
if not lines:
# Before returning, check if an event (maybe partial) is waiting for too long.
if self._current_event and time.time() - self._last_activity > 1:
event = '\n'.join(self._current_event)
self._current_event.clear()
self._callback_wrapper([event])
break
self._last_activity = time.time()
if self._multiline_regex_after or self._multiline_regex_before:
# Multiline is enabled for this file.
events = multiline_merge(
lines,
self._current_event,
self._multiline_regex_after,
self._multiline_regex_before)
else:
events = lines
if events:
self._callback_wrapper(events)
if self._sincedb_path:
current_line_count = len(lines)
self._sincedb_update_position(lines=current_line_count)
self._sincedb_update_position() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:update_files; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 31; 5, 44; 5, 48; 5, 52; 5, 173; 5, 238; 5, 239; 5, 255; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 29; 9, boolean_operator:and; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_update_time; 13, comparison_operator:<; 13, 14; 13, 26; 14, binary_operator:-; 14, 15; 14, 23; 15, call; 15, 16; 15, 17; 16, identifier:int; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:time; 21, identifier:time; 22, argument_list; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:_update_time; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:_discover_interval; 29, block; 29, 30; 30, return_statement; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_update_time; 36, call; 36, 37; 36, 38; 37, identifier:int; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:time; 42, identifier:time; 43, argument_list; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:possible_files; 47, list:[]; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:files; 51, list:[]; 52, if_statement; 52, 53; 52, 66; 52, 135; 53, comparison_operator:>; 53, 54; 53, 65; 54, call; 54, 55; 54, 56; 55, identifier:len; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_beaver_config; 62, identifier:get; 63, argument_list; 63, 64; 64, string:'globs'; 65, integer:0; 66, block; 66, 67; 66, 73; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:extend_files; 70, attribute; 70, 71; 70, 72; 71, identifier:files; 72, identifier:extend; 73, for_statement; 73, 74; 73, 77; 73, 89; 74, pattern_list; 74, 75; 74, 76; 75, identifier:name; 76, identifier:exclude; 77, call; 77, 78; 77, 88; 78, attribute; 78, 79; 78, 87; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_beaver_config; 84, identifier:get; 85, argument_list; 85, 86; 86, string:'globs'; 87, identifier:items; 88, argument_list; 89, block; 89, 90; 89, 109; 89, 114; 89, 124; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:globbed; 93, list_comprehension; 93, 94; 93, 102; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:os; 98, identifier:path; 99, identifier:realpath; 100, argument_list; 100, 101; 101, identifier:filename; 102, for_in_clause; 102, 103; 102, 104; 103, identifier:filename; 104, call; 104, 105; 104, 106; 105, identifier:eglob; 106, argument_list; 106, 107; 106, 108; 107, identifier:name; 108, identifier:exclude; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:extend_files; 112, argument_list; 112, 113; 113, identifier:globbed; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:self; 119, identifier:_beaver_config; 120, identifier:addglob; 121, argument_list; 121, 122; 121, 123; 122, identifier:name; 123, identifier:globbed; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:_callback; 129, argument_list; 129, 130; 130, tuple; 130, 131; 130, 132; 131, string:"addglob"; 132, tuple; 132, 133; 132, 134; 133, identifier:name; 134, identifier:globbed; 135, else_clause; 135, 136; 136, block; 136, 137; 136, 143; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:append_files; 140, attribute; 140, 141; 140, 142; 141, identifier:files; 142, identifier:append; 143, for_statement; 143, 144; 143, 145; 143, 150; 144, identifier:name; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:self; 148, identifier:listdir; 149, argument_list; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 154; 153, identifier:append_files; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 161; 156, attribute; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:os; 159, identifier:path; 160, identifier:realpath; 161, argument_list; 161, 162; 162, call; 162, 163; 162, 168; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:os; 166, identifier:path; 167, identifier:join; 168, argument_list; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:self; 171, identifier:_folder; 172, identifier:name; 173, for_statement; 173, 174; 173, 175; 173, 176; 174, identifier:absname; 175, identifier:files; 176, block; 176, 177; 177, try_statement; 177, 178; 177, 188; 177, 202; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:st; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:os; 185, identifier:stat; 186, argument_list; 186, 187; 187, identifier:absname; 188, except_clause; 188, 189; 188, 190; 188, 191; 189, identifier:EnvironmentError; 190, identifier:err; 191, block; 191, 192; 192, if_statement; 192, 193; 192, 200; 193, comparison_operator:!=; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:err; 196, identifier:errno; 197, attribute; 197, 198; 197, 199; 198, identifier:errno; 199, identifier:ENOENT; 200, block; 200, 201; 201, raise_statement; 202, else_clause; 202, 203; 203, block; 203, 204; 203, 216; 203, 222; 203, 231; 204, if_statement; 204, 205; 204, 214; 205, not_operator; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:stat; 209, identifier:S_ISREG; 210, argument_list; 210, 211; 211, attribute; 211, 212; 211, 213; 212, identifier:st; 213, identifier:st_mode; 214, block; 214, 215; 215, continue_statement; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:append_possible_files; 219, attribute; 219, 220; 219, 221; 220, identifier:possible_files; 221, identifier:append; 222, expression_statement; 222, 223; 223, assignment; 223, 224; 223, 225; 224, identifier:fid; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:get_file_id; 229, argument_list; 229, 230; 230, identifier:st; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 234; 233, identifier:append_possible_files; 234, argument_list; 234, 235; 235, tuple; 235, 236; 235, 237; 236, identifier:fid; 237, identifier:absname; 238, comment; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:new_files; 242, list_comprehension; 242, 243; 242, 244; 242, 249; 243, identifier:fname; 244, for_in_clause; 244, 245; 244, 248; 245, pattern_list; 245, 246; 245, 247; 246, identifier:fid; 247, identifier:fname; 248, identifier:possible_files; 249, if_clause; 249, 250; 250, comparison_operator:not; 250, 251; 250, 252; 251, identifier:fid; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_tails; 255, expression_statement; 255, 256; 256, call; 256, 257; 256, 260; 257, attribute; 257, 258; 257, 259; 258, identifier:self; 259, identifier:watch; 260, argument_list; 260, 261; 261, identifier:new_files | def update_files(self):
"""Ensures all files are properly loaded.
Detects new files, file removals, file rotation, and truncation.
On non-linux platforms, it will also manually reload the file for tailing.
Note that this hack is necessary because EOF is cached on BSD systems.
"""
if self._update_time and int(time.time()) - self._update_time < self._discover_interval:
return
self._update_time = int(time.time())
possible_files = []
files = []
if len(self._beaver_config.get('globs')) > 0:
extend_files = files.extend
for name, exclude in self._beaver_config.get('globs').items():
globbed = [os.path.realpath(filename) for filename in eglob(name, exclude)]
extend_files(globbed)
self._beaver_config.addglob(name, globbed)
self._callback(("addglob", (name, globbed)))
else:
append_files = files.append
for name in self.listdir():
append_files(os.path.realpath(os.path.join(self._folder, name)))
for absname in files:
try:
st = os.stat(absname)
except EnvironmentError, err:
if err.errno != errno.ENOENT:
raise
else:
if not stat.S_ISREG(st.st_mode):
continue
append_possible_files = possible_files.append
fid = self.get_file_id(st)
append_possible_files((fid, absname))
# add new ones
new_files = [fname for fid, fname in possible_files if fid not in self._tails]
self.watch(new_files) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:gctx_file_path; 5, default_parameter; 5, 6; 5, 7; 6, identifier:convert_neg_666; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:rid; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cid; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:ridx; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:cidx; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:row_meta_only; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:col_meta_only; 25, False; 26, default_parameter; 26, 27; 26, 28; 27, identifier:make_multiindex; 28, False; 29, block; 29, 30; 29, 32; 29, 43; 29, 44; 29, 81; 29, 93; 29, 94; 29, 104; 30, expression_statement; 30, 31; 31, comment; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:full_path; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:os; 39, identifier:path; 40, identifier:expanduser; 41, argument_list; 41, 42; 42, identifier:gctx_file_path; 43, comment; 44, if_statement; 44, 45; 44, 54; 45, not_operator; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:os; 50, identifier:path; 51, identifier:exists; 52, argument_list; 52, 53; 53, identifier:full_path; 54, block; 54, 55; 54, 59; 54, 71; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:err_msg; 58, string:"The given path to the gctx file cannot be found. full_path: {}"; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:logger; 63, identifier:error; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:err_msg; 68, identifier:format; 69, argument_list; 69, 70; 70, identifier:full_path; 71, raise_statement; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:Exception; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:err_msg; 78, identifier:format; 79, argument_list; 79, 80; 80, identifier:full_path; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:logger; 85, identifier:info; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, string:"Reading GCTX: {}"; 90, identifier:format; 91, argument_list; 91, 92; 92, identifier:full_path; 93, comment; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:gctx_file; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:h5py; 100, identifier:File; 101, argument_list; 101, 102; 101, 103; 102, identifier:full_path; 103, string:"r"; 104, if_statement; 104, 105; 104, 106; 104, 107; 104, 155; 104, 206; 105, identifier:row_meta_only; 106, comment; 107, block; 107, 108; 107, 114; 107, 123; 107, 124; 107, 138; 107, 144; 107, 145; 107, 153; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:row_dset; 111, subscript; 111, 112; 111, 113; 112, identifier:gctx_file; 113, identifier:row_meta_group_node; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:row_meta; 117, call; 117, 118; 117, 119; 118, identifier:parse_metadata_df; 119, argument_list; 119, 120; 119, 121; 119, 122; 120, string:"row"; 121, identifier:row_dset; 122, identifier:convert_neg_666; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, tuple_pattern; 126, 127; 126, 128; 127, identifier:sorted_ridx; 128, identifier:sorted_cidx; 129, call; 129, 130; 129, 131; 130, identifier:check_and_order_id_inputs; 131, argument_list; 131, 132; 131, 133; 131, 134; 131, 135; 131, 136; 131, 137; 132, identifier:rid; 133, identifier:ridx; 134, identifier:cid; 135, identifier:cidx; 136, identifier:row_meta; 137, None; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:gctx_file; 142, identifier:close; 143, argument_list; 144, comment; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:row_meta; 148, subscript; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:row_meta; 151, identifier:iloc; 152, identifier:sorted_ridx; 153, return_statement; 153, 154; 154, identifier:row_meta; 155, elif_clause; 155, 156; 155, 157; 155, 158; 156, identifier:col_meta_only; 157, comment; 158, block; 158, 159; 158, 165; 158, 174; 158, 175; 158, 189; 158, 195; 158, 196; 158, 204; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:col_dset; 162, subscript; 162, 163; 162, 164; 163, identifier:gctx_file; 164, identifier:col_meta_group_node; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:col_meta; 168, call; 168, 169; 168, 170; 169, identifier:parse_metadata_df; 170, argument_list; 170, 171; 170, 172; 170, 173; 171, string:"col"; 172, identifier:col_dset; 173, identifier:convert_neg_666; 174, comment; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 180; 177, tuple_pattern; 177, 178; 177, 179; 178, identifier:sorted_ridx; 179, identifier:sorted_cidx; 180, call; 180, 181; 180, 182; 181, identifier:check_and_order_id_inputs; 182, argument_list; 182, 183; 182, 184; 182, 185; 182, 186; 182, 187; 182, 188; 183, identifier:rid; 184, identifier:ridx; 185, identifier:cid; 186, identifier:cidx; 187, None; 188, identifier:col_meta; 189, expression_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:gctx_file; 193, identifier:close; 194, argument_list; 195, comment; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:col_meta; 199, subscript; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:col_meta; 202, identifier:iloc; 203, identifier:sorted_cidx; 204, return_statement; 204, 205; 205, identifier:col_meta; 206, else_clause; 206, 207; 206, 208; 207, comment; 208, block; 208, 209; 208, 215; 208, 224; 208, 225; 208, 231; 208, 240; 208, 241; 208, 255; 208, 261; 208, 272; 208, 273; 208, 281; 208, 289; 208, 290; 208, 298; 208, 314; 208, 320; 208, 321; 208, 347; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:row_dset; 212, subscript; 212, 213; 212, 214; 213, identifier:gctx_file; 214, identifier:row_meta_group_node; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:row_meta; 218, call; 218, 219; 218, 220; 219, identifier:parse_metadata_df; 220, argument_list; 220, 221; 220, 222; 220, 223; 221, string:"row"; 222, identifier:row_dset; 223, identifier:convert_neg_666; 224, comment; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:col_dset; 228, subscript; 228, 229; 228, 230; 229, identifier:gctx_file; 230, identifier:col_meta_group_node; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:col_meta; 234, call; 234, 235; 234, 236; 235, identifier:parse_metadata_df; 236, argument_list; 236, 237; 236, 238; 236, 239; 237, string:"col"; 238, identifier:col_dset; 239, identifier:convert_neg_666; 240, comment; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 246; 243, tuple_pattern; 243, 244; 243, 245; 244, identifier:sorted_ridx; 245, identifier:sorted_cidx; 246, call; 246, 247; 246, 248; 247, identifier:check_and_order_id_inputs; 248, argument_list; 248, 249; 248, 250; 248, 251; 248, 252; 248, 253; 248, 254; 249, identifier:rid; 250, identifier:ridx; 251, identifier:cid; 252, identifier:cidx; 253, identifier:row_meta; 254, identifier:col_meta; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:data_dset; 258, subscript; 258, 259; 258, 260; 259, identifier:gctx_file; 260, identifier:data_node; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:data_df; 264, call; 264, 265; 264, 266; 265, identifier:parse_data_df; 266, argument_list; 266, 267; 266, 268; 266, 269; 266, 270; 266, 271; 267, identifier:data_dset; 268, identifier:sorted_ridx; 269, identifier:sorted_cidx; 270, identifier:row_meta; 271, identifier:col_meta; 272, comment; 273, expression_statement; 273, 274; 274, assignment; 274, 275; 274, 276; 275, identifier:row_meta; 276, subscript; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:row_meta; 279, identifier:iloc; 280, identifier:sorted_ridx; 281, expression_statement; 281, 282; 282, assignment; 282, 283; 282, 284; 283, identifier:col_meta; 284, subscript; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:col_meta; 287, identifier:iloc; 288, identifier:sorted_cidx; 289, comment; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:my_version; 293, subscript; 293, 294; 293, 297; 294, attribute; 294, 295; 294, 296; 295, identifier:gctx_file; 296, identifier:attrs; 297, identifier:version_node; 298, if_statement; 298, 299; 298, 307; 299, comparison_operator:==; 299, 300; 299, 304; 300, call; 300, 301; 300, 302; 301, identifier:type; 302, argument_list; 302, 303; 303, identifier:my_version; 304, attribute; 304, 305; 304, 306; 305, identifier:np; 306, identifier:ndarray; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:my_version; 311, subscript; 311, 312; 311, 313; 312, identifier:my_version; 313, integer:0; 314, expression_statement; 314, 315; 315, call; 315, 316; 315, 319; 316, attribute; 316, 317; 316, 318; 317, identifier:gctx_file; 318, identifier:close; 319, argument_list; 320, comment; 321, expression_statement; 321, 322; 322, assignment; 322, 323; 322, 324; 323, identifier:my_gctoo; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:GCToo; 327, identifier:GCToo; 328, argument_list; 328, 329; 328, 332; 328, 335; 328, 338; 328, 341; 328, 344; 329, keyword_argument; 329, 330; 329, 331; 330, identifier:data_df; 331, identifier:data_df; 332, keyword_argument; 332, 333; 332, 334; 333, identifier:row_metadata_df; 334, identifier:row_meta; 335, keyword_argument; 335, 336; 335, 337; 336, identifier:col_metadata_df; 337, identifier:col_meta; 338, keyword_argument; 338, 339; 338, 340; 339, identifier:src; 340, identifier:full_path; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:version; 343, identifier:my_version; 344, keyword_argument; 344, 345; 344, 346; 345, identifier:make_multiindex; 346, identifier:make_multiindex; 347, return_statement; 347, 348; 348, identifier:my_gctoo | def parse(gctx_file_path, convert_neg_666=True, rid=None, cid=None,
ridx=None, cidx=None, row_meta_only=False, col_meta_only=False, make_multiindex=False):
"""
Primary method of script. Reads in path to a gctx file and parses into GCToo object.
Input:
Mandatory:
- gctx_file_path (str): full path to gctx file you want to parse.
Optional:
- convert_neg_666 (bool): whether to convert -666 values to numpy.nan or not
(see Note below for more details on this). Default = False.
- rid (list of strings): list of row ids to specifically keep from gctx. Default=None.
- cid (list of strings): list of col ids to specifically keep from gctx. Default=None.
- ridx (list of integers): only read the rows corresponding to this
list of integer ids. Default=None.
- cidx (list of integers): only read the columns corresponding to this
list of integer ids. Default=None.
- row_meta_only (bool): Whether to load data + metadata (if False), or just row metadata (if True)
as pandas DataFrame
- col_meta_only (bool): Whether to load data + metadata (if False), or just col metadata (if True)
as pandas DataFrame
- make_multiindex (bool): whether to create a multi-index df combining
the 3 component dfs
Output:
- myGCToo (GCToo): A GCToo instance containing content of parsed gctx file. Note: if meta_only = True,
this will be a GCToo instance where the data_df is empty, i.e. data_df = pd.DataFrame(index=rids,
columns = cids)
Note: why does convert_neg_666 exist?
- In CMap--for somewhat obscure historical reasons--we use "-666" as our null value
for metadata. However (so that users can take full advantage of pandas' methods,
including those for filtering nan's etc) we provide the option of converting these
into numpy.NaN values, the pandas default.
"""
full_path = os.path.expanduser(gctx_file_path)
# Verify that the path exists
if not os.path.exists(full_path):
err_msg = "The given path to the gctx file cannot be found. full_path: {}"
logger.error(err_msg.format(full_path))
raise Exception(err_msg.format(full_path))
logger.info("Reading GCTX: {}".format(full_path))
# open file
gctx_file = h5py.File(full_path, "r")
if row_meta_only:
# read in row metadata
row_dset = gctx_file[row_meta_group_node]
row_meta = parse_metadata_df("row", row_dset, convert_neg_666)
# validate optional input ids & get indexes to subset by
(sorted_ridx, sorted_cidx) = check_and_order_id_inputs(rid, ridx, cid, cidx, row_meta, None)
gctx_file.close()
# subset if specified, then return
row_meta = row_meta.iloc[sorted_ridx]
return row_meta
elif col_meta_only:
# read in col metadata
col_dset = gctx_file[col_meta_group_node]
col_meta = parse_metadata_df("col", col_dset, convert_neg_666)
# validate optional input ids & get indexes to subset by
(sorted_ridx, sorted_cidx) = check_and_order_id_inputs(rid, ridx, cid, cidx, None, col_meta)
gctx_file.close()
# subset if specified, then return
col_meta = col_meta.iloc[sorted_cidx]
return col_meta
else:
# read in row metadata
row_dset = gctx_file[row_meta_group_node]
row_meta = parse_metadata_df("row", row_dset, convert_neg_666)
# read in col metadata
col_dset = gctx_file[col_meta_group_node]
col_meta = parse_metadata_df("col", col_dset, convert_neg_666)
# validate optional input ids & get indexes to subset by
(sorted_ridx, sorted_cidx) = check_and_order_id_inputs(rid, ridx, cid, cidx, row_meta, col_meta)
data_dset = gctx_file[data_node]
data_df = parse_data_df(data_dset, sorted_ridx, sorted_cidx, row_meta, col_meta)
# (if subsetting) subset metadata
row_meta = row_meta.iloc[sorted_ridx]
col_meta = col_meta.iloc[sorted_cidx]
# get version
my_version = gctx_file.attrs[version_node]
if type(my_version) == np.ndarray:
my_version = my_version[0]
gctx_file.close()
# make GCToo instance
my_gctoo = GCToo.GCToo(data_df=data_df, row_metadata_df=row_meta, col_metadata_df=col_meta,
src=full_path, version=my_version, make_multiindex=make_multiindex)
return my_gctoo |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:multi_index_df_to_component_dfs; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:multi_index_df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:rid; 7, string:"rid"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:cid; 10, string:"cid"; 11, block; 11, 12; 11, 14; 11, 15; 11, 29; 11, 43; 11, 44; 11, 45; 11, 139; 11, 140; 11, 234; 11, 235; 11, 268; 11, 301; 11, 334; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:rids; 18, call; 18, 19; 18, 20; 19, identifier:list; 20, argument_list; 20, 21; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:multi_index_df; 25, identifier:index; 26, identifier:get_level_values; 27, argument_list; 27, 28; 28, identifier:rid; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:cids; 32, call; 32, 33; 32, 34; 33, identifier:list; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:multi_index_df; 39, identifier:columns; 40, identifier:get_level_values; 41, argument_list; 41, 42; 42, identifier:cid; 43, comment; 44, comment; 45, if_statement; 45, 46; 45, 55; 45, 56; 45, 128; 45, 129; 46, call; 46, 47; 46, 48; 47, identifier:isinstance; 48, argument_list; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:multi_index_df; 51, identifier:index; 52, attribute; 52, 53; 52, 54; 53, identifier:pd; 54, identifier:MultiIndex; 55, comment; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 68; 57, 69; 57, 117; 57, 118; 58, comparison_operator:>; 58, 59; 58, 67; 59, call; 59, 60; 59, 61; 60, identifier:len; 61, argument_list; 61, 62; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:multi_index_df; 65, identifier:index; 66, identifier:names; 67, integer:1; 68, comment; 69, block; 69, 70; 69, 81; 69, 82; 69, 91; 69, 92; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:mi_df_index; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:multi_index_df; 77, identifier:index; 78, identifier:droplevel; 79, argument_list; 79, 80; 80, identifier:rid; 81, comment; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:rhds; 85, call; 85, 86; 85, 87; 86, identifier:list; 87, argument_list; 87, 88; 88, attribute; 88, 89; 88, 90; 89, identifier:mi_df_index; 90, identifier:names; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:row_metadata; 95, attribute; 95, 96; 95, 116; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:np; 99, identifier:array; 100, argument_list; 100, 101; 101, list_comprehension; 101, 102; 101, 110; 102, attribute; 102, 103; 102, 109; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:mi_df_index; 106, identifier:get_level_values; 107, argument_list; 107, 108; 108, identifier:level; 109, identifier:values; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:level; 112, call; 112, 113; 112, 114; 113, identifier:list; 114, argument_list; 114, 115; 115, identifier:rhds; 116, identifier:T; 117, comment; 118, else_clause; 118, 119; 119, block; 119, 120; 119, 124; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:rhds; 123, list:[]; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:row_metadata; 127, list:[]; 128, comment; 129, else_clause; 129, 130; 130, block; 130, 131; 130, 135; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:rhds; 134, list:[]; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:row_metadata; 138, list:[]; 139, comment; 140, if_statement; 140, 141; 140, 150; 140, 151; 140, 223; 140, 224; 141, call; 141, 142; 141, 143; 142, identifier:isinstance; 143, argument_list; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:multi_index_df; 146, identifier:columns; 147, attribute; 147, 148; 147, 149; 148, identifier:pd; 149, identifier:MultiIndex; 150, comment; 151, block; 151, 152; 152, if_statement; 152, 153; 152, 163; 152, 164; 152, 212; 152, 213; 153, comparison_operator:>; 153, 154; 153, 162; 154, call; 154, 155; 154, 156; 155, identifier:len; 156, argument_list; 156, 157; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:multi_index_df; 160, identifier:columns; 161, identifier:names; 162, integer:1; 163, comment; 164, block; 164, 165; 164, 176; 164, 177; 164, 186; 164, 187; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:mi_df_columns; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:multi_index_df; 172, identifier:columns; 173, identifier:droplevel; 174, argument_list; 174, 175; 175, identifier:cid; 176, comment; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:chds; 180, call; 180, 181; 180, 182; 181, identifier:list; 182, argument_list; 182, 183; 183, attribute; 183, 184; 183, 185; 184, identifier:mi_df_columns; 185, identifier:names; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:col_metadata; 190, attribute; 190, 191; 190, 211; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:np; 194, identifier:array; 195, argument_list; 195, 196; 196, list_comprehension; 196, 197; 196, 205; 197, attribute; 197, 198; 197, 204; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:mi_df_columns; 201, identifier:get_level_values; 202, argument_list; 202, 203; 203, identifier:level; 204, identifier:values; 205, for_in_clause; 205, 206; 205, 207; 206, identifier:level; 207, call; 207, 208; 207, 209; 208, identifier:list; 209, argument_list; 209, 210; 210, identifier:chds; 211, identifier:T; 212, comment; 213, else_clause; 213, 214; 214, block; 214, 215; 214, 219; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:chds; 218, list:[]; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:col_metadata; 222, list:[]; 223, comment; 224, else_clause; 224, 225; 225, block; 225, 226; 225, 230; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:chds; 229, list:[]; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:col_metadata; 233, list:[]; 234, comment; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:row_metadata_df; 238, call; 238, 239; 238, 244; 239, attribute; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:pd; 242, identifier:DataFrame; 243, identifier:from_records; 244, argument_list; 244, 245; 244, 246; 244, 257; 245, identifier:row_metadata; 246, keyword_argument; 246, 247; 246, 248; 247, identifier:index; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:pd; 251, identifier:Index; 252, argument_list; 252, 253; 252, 254; 253, identifier:rids; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:name; 256, string:"rid"; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:columns; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:pd; 262, identifier:Index; 263, argument_list; 263, 264; 263, 265; 264, identifier:rhds; 265, keyword_argument; 265, 266; 265, 267; 266, identifier:name; 267, string:"rhd"; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:col_metadata_df; 271, call; 271, 272; 271, 277; 272, attribute; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:pd; 275, identifier:DataFrame; 276, identifier:from_records; 277, argument_list; 277, 278; 277, 279; 277, 290; 278, identifier:col_metadata; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:index; 281, call; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:pd; 284, identifier:Index; 285, argument_list; 285, 286; 285, 287; 286, identifier:cids; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:name; 289, string:"cid"; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:columns; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:pd; 295, identifier:Index; 296, argument_list; 296, 297; 296, 298; 297, identifier:chds; 298, keyword_argument; 298, 299; 298, 300; 299, identifier:name; 300, string:"chd"; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:data_df; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:pd; 307, identifier:DataFrame; 308, argument_list; 308, 309; 308, 312; 308, 323; 309, attribute; 309, 310; 309, 311; 310, identifier:multi_index_df; 311, identifier:values; 312, keyword_argument; 312, 313; 312, 314; 313, identifier:index; 314, call; 314, 315; 314, 318; 315, attribute; 315, 316; 315, 317; 316, identifier:pd; 317, identifier:Index; 318, argument_list; 318, 319; 318, 320; 319, identifier:rids; 320, keyword_argument; 320, 321; 320, 322; 321, identifier:name; 322, string:"rid"; 323, keyword_argument; 323, 324; 323, 325; 324, identifier:columns; 325, call; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:pd; 328, identifier:Index; 329, argument_list; 329, 330; 329, 331; 330, identifier:cids; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:name; 333, string:"cid"; 334, return_statement; 334, 335; 335, expression_list; 335, 336; 335, 337; 335, 338; 336, identifier:data_df; 337, identifier:row_metadata_df; 338, identifier:col_metadata_df | def multi_index_df_to_component_dfs(multi_index_df, rid="rid", cid="cid"):
""" Convert a multi-index df into 3 component dfs. """
# Id level of the multiindex will become the index
rids = list(multi_index_df.index.get_level_values(rid))
cids = list(multi_index_df.columns.get_level_values(cid))
# It's possible that the index and/or columns of multi_index_df are not
# actually multi-index; need to check for this and there are more than one level in index(python3)
if isinstance(multi_index_df.index, pd.MultiIndex):
# check if there are more than one levels in index (python3)
if len(multi_index_df.index.names) > 1:
# If so, drop rid because it won't go into the body of the metadata
mi_df_index = multi_index_df.index.droplevel(rid)
# Names of the multiindex levels become the headers
rhds = list(mi_df_index.names)
# Assemble metadata values
row_metadata = np.array([mi_df_index.get_level_values(level).values for level in list(rhds)]).T
# if there is one level in index (python3), then rhds and row metadata should be empty
else:
rhds = []
row_metadata = []
# If the index is not multi-index, then rhds and row metadata should be empty
else:
rhds = []
row_metadata = []
# Check if columns of multi_index_df are in fact multi-index
if isinstance(multi_index_df.columns, pd.MultiIndex):
# Check if there are more than one levels in columns(python3)
if len(multi_index_df.columns.names) > 1:
# If so, drop cid because it won't go into the body of the metadata
mi_df_columns = multi_index_df.columns.droplevel(cid)
# Names of the multiindex levels become the headers
chds = list(mi_df_columns.names)
# Assemble metadata values
col_metadata = np.array([mi_df_columns.get_level_values(level).values for level in list(chds)]).T
# If there is one level in columns (python3), then rhds and row metadata should be empty
else:
chds = []
col_metadata = []
# If the columns are not multi-index, then rhds and row metadata should be empty
else:
chds = []
col_metadata = []
# Create component dfs
row_metadata_df = pd.DataFrame.from_records(row_metadata, index=pd.Index(rids, name="rid"), columns=pd.Index(rhds, name="rhd"))
col_metadata_df = pd.DataFrame.from_records(col_metadata, index=pd.Index(cids, name="cid"), columns=pd.Index(chds, name="chd"))
data_df = pd.DataFrame(multi_index_df.values, index=pd.Index(rids, name="rid"), columns=pd.Index(cids, name="cid"))
return data_df, row_metadata_df, col_metadata_df |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:file_path; 5, default_parameter; 5, 6; 5, 7; 6, identifier:convert_neg_666; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:rid; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cid; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:ridx; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:cidx; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:row_meta_only; 22, False; 23, default_parameter; 23, 24; 23, 25; 24, identifier:col_meta_only; 25, False; 26, default_parameter; 26, 27; 26, 28; 27, identifier:make_multiindex; 28, False; 29, block; 29, 30; 29, 32; 29, 132; 30, expression_statement; 30, 31; 31, comment; 32, if_statement; 32, 33; 32, 39; 32, 73; 32, 114; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:file_path; 36, identifier:endswith; 37, argument_list; 37, 38; 38, string:".gct"; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:out; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:parse_gct; 46, identifier:parse; 47, argument_list; 47, 48; 47, 49; 47, 52; 47, 55; 47, 58; 47, 61; 47, 64; 47, 67; 47, 70; 48, identifier:file_path; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:convert_neg_666; 51, identifier:convert_neg_666; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:rid; 54, identifier:rid; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:cid; 57, identifier:cid; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:ridx; 60, identifier:ridx; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:cidx; 63, identifier:cidx; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:row_meta_only; 66, identifier:row_meta_only; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:col_meta_only; 69, identifier:col_meta_only; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:make_multiindex; 72, identifier:make_multiindex; 73, elif_clause; 73, 74; 73, 80; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:file_path; 77, identifier:endswith; 78, argument_list; 78, 79; 79, string:".gctx"; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:out; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:parse_gctx; 87, identifier:parse; 88, argument_list; 88, 89; 88, 90; 88, 93; 88, 96; 88, 99; 88, 102; 88, 105; 88, 108; 88, 111; 89, identifier:file_path; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:convert_neg_666; 92, identifier:convert_neg_666; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:rid; 95, identifier:rid; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:cid; 98, identifier:cid; 99, keyword_argument; 99, 100; 99, 101; 100, identifier:ridx; 101, identifier:ridx; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:cidx; 104, identifier:cidx; 105, keyword_argument; 105, 106; 105, 107; 106, identifier:row_meta_only; 107, identifier:row_meta_only; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:col_meta_only; 110, identifier:col_meta_only; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:make_multiindex; 113, identifier:make_multiindex; 114, else_clause; 114, 115; 115, block; 115, 116; 115, 120; 115, 127; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:err_msg; 119, string:"File to parse must be .gct or .gctx!"; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:logger; 124, identifier:error; 125, argument_list; 125, 126; 126, identifier:err_msg; 127, raise_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:Exception; 130, argument_list; 130, 131; 131, identifier:err_msg; 132, return_statement; 132, 133; 133, identifier:out | def parse(file_path, convert_neg_666=True, rid=None, cid=None, ridx=None, cidx=None,
row_meta_only=False, col_meta_only=False, make_multiindex=False):
"""
Identifies whether file_path corresponds to a .gct or .gctx file and calls the
correct corresponding parse method.
Input:
Mandatory:
- gct(x)_file_path (str): full path to gct(x) file you want to parse.
Optional:
- convert_neg_666 (bool): whether to convert -666 values to numpy.nan or not
(see Note below for more details on this). Default = False.
- rid (list of strings): list of row ids to specifically keep from gctx. Default=None.
- cid (list of strings): list of col ids to specifically keep from gctx. Default=None.
- ridx (list of integers): only read the rows corresponding to this
list of integer ids. Default=None.
- cidx (list of integers): only read the columns corresponding to this
list of integer ids. Default=None.
- row_meta_only (bool): Whether to load data + metadata (if False), or just row metadata (if True)
as pandas DataFrame
- col_meta_only (bool): Whether to load data + metadata (if False), or just col metadata (if True)
as pandas DataFrame
- make_multiindex (bool): whether to create a multi-index df combining
the 3 component dfs
Output:
- out (GCToo object or pandas df): if row_meta_only or col_meta_only, then
out is a metadata df; otherwise, it's a GCToo instance containing
content of parsed gct(x) file
Note: why does convert_neg_666 exist?
- In CMap--for somewhat obscure historical reasons--we use "-666" as our null value
for metadata. However (so that users can take full advantage of pandas' methods,
including those for filtering nan's etc) we provide the option of converting these
into numpy.NaN values, the pandas default.
"""
if file_path.endswith(".gct"):
out = parse_gct.parse(file_path, convert_neg_666=convert_neg_666,
rid=rid, cid=cid, ridx=ridx, cidx=cidx,
row_meta_only=row_meta_only, col_meta_only=col_meta_only,
make_multiindex=make_multiindex)
elif file_path.endswith(".gctx"):
out = parse_gctx.parse(file_path, convert_neg_666=convert_neg_666,
rid=rid, cid=cid, ridx=ridx, cidx=cidx,
row_meta_only=row_meta_only, col_meta_only=col_meta_only,
make_multiindex=make_multiindex)
else:
err_msg = "File to parse must be .gct or .gctx!"
logger.error(err_msg)
raise Exception(err_msg)
return out |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:assemble_concatenated_meta; 3, parameters; 3, 4; 3, 5; 4, identifier:concated_meta_dfs; 5, identifier:remove_all_metadata_fields; 6, block; 6, 7; 6, 9; 6, 10; 6, 32; 6, 44; 6, 45; 6, 46; 6, 54; 6, 66; 6, 81; 6, 85; 6, 86; 6, 104; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, if_statement; 10, 11; 10, 12; 11, identifier:remove_all_metadata_fields; 12, block; 12, 13; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:df; 15, identifier:concated_meta_dfs; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:df; 21, identifier:drop; 22, argument_list; 22, 23; 22, 26; 22, 29; 23, attribute; 23, 24; 23, 25; 24, identifier:df; 25, identifier:columns; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:axis; 28, integer:1; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:inplace; 31, True; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:all_concated_meta_df; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:pd; 38, identifier:concat; 39, argument_list; 39, 40; 39, 41; 40, identifier:concated_meta_dfs; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:axis; 43, integer:0; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:n_rows; 49, subscript; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:all_concated_meta_df; 52, identifier:shape; 53, integer:0; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:logger; 58, identifier:debug; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, string:"all_concated_meta_df.shape[0]: {}"; 63, identifier:format; 64, argument_list; 64, 65; 65, identifier:n_rows; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:n_rows_cumulative; 69, call; 69, 70; 69, 71; 70, identifier:sum; 71, argument_list; 71, 72; 72, list_comprehension; 72, 73; 72, 78; 73, subscript; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:df; 76, identifier:shape; 77, integer:0; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:df; 80, identifier:concated_meta_dfs; 81, assert_statement; 81, 82; 82, comparison_operator:==; 82, 83; 82, 84; 83, identifier:n_rows; 84, identifier:n_rows_cumulative; 85, comment; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:all_concated_meta_df_sorted; 89, call; 89, 90; 89, 100; 90, attribute; 90, 91; 90, 99; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:all_concated_meta_df; 94, identifier:sort_index; 95, argument_list; 95, 96; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:axis; 98, integer:0; 99, identifier:sort_index; 100, argument_list; 100, 101; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:axis; 103, integer:1; 104, return_statement; 104, 105; 105, identifier:all_concated_meta_df_sorted | def assemble_concatenated_meta(concated_meta_dfs, remove_all_metadata_fields):
""" Assemble the concatenated metadata dfs together. For example,
if horizontally concatenating, the concatenated metadata dfs are the
column metadata dfs. Both indices are sorted.
Args:
concated_meta_dfs (list of pandas dfs)
Returns:
all_concated_meta_df_sorted (pandas df)
"""
# Concatenate the concated_meta_dfs
if remove_all_metadata_fields:
for df in concated_meta_dfs:
df.drop(df.columns, axis=1, inplace=True)
all_concated_meta_df = pd.concat(concated_meta_dfs, axis=0)
# Sanity check: the number of rows in all_concated_meta_df should correspond
# to the sum of the number of rows in the input dfs
n_rows = all_concated_meta_df.shape[0]
logger.debug("all_concated_meta_df.shape[0]: {}".format(n_rows))
n_rows_cumulative = sum([df.shape[0] for df in concated_meta_dfs])
assert n_rows == n_rows_cumulative
# Sort the index and columns
all_concated_meta_df_sorted = all_concated_meta_df.sort_index(axis=0).sort_index(axis=1)
return all_concated_meta_df_sorted |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:assemble_data; 3, parameters; 3, 4; 3, 5; 4, identifier:data_dfs; 5, identifier:concat_direction; 6, block; 6, 7; 6, 9; 6, 127; 6, 128; 6, 146; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 9, 14; 9, 68; 10, comparison_operator:==; 10, 11; 10, 12; 11, identifier:concat_direction; 12, string:"horiz"; 13, comment; 14, block; 14, 15; 14, 27; 14, 28; 14, 29; 14, 37; 14, 49; 14, 64; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:all_data_df; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:pd; 21, identifier:concat; 22, argument_list; 22, 23; 22, 24; 23, identifier:data_dfs; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:axis; 26, integer:1; 27, comment; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:n_cols; 32, subscript; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:all_data_df; 35, identifier:shape; 36, integer:1; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:logger; 41, identifier:debug; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:"all_data_df.shape[1]: {}"; 46, identifier:format; 47, argument_list; 47, 48; 48, identifier:n_cols; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:n_cols_cumulative; 52, call; 52, 53; 52, 54; 53, identifier:sum; 54, argument_list; 54, 55; 55, list_comprehension; 55, 56; 55, 61; 56, subscript; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:df; 59, identifier:shape; 60, integer:1; 61, for_in_clause; 61, 62; 61, 63; 62, identifier:df; 63, identifier:data_dfs; 64, assert_statement; 64, 65; 65, comparison_operator:==; 65, 66; 65, 67; 66, identifier:n_cols; 67, identifier:n_cols_cumulative; 68, elif_clause; 68, 69; 68, 72; 68, 73; 69, comparison_operator:==; 69, 70; 69, 71; 70, identifier:concat_direction; 71, string:"vert"; 72, comment; 73, block; 73, 74; 73, 86; 73, 87; 73, 88; 73, 96; 73, 108; 73, 123; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:all_data_df; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:pd; 80, identifier:concat; 81, argument_list; 81, 82; 81, 83; 82, identifier:data_dfs; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:axis; 85, integer:0; 86, comment; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:n_rows; 91, subscript; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:all_data_df; 94, identifier:shape; 95, integer:0; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:logger; 100, identifier:debug; 101, argument_list; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, string:"all_data_df.shape[0]: {}"; 105, identifier:format; 106, argument_list; 106, 107; 107, identifier:n_rows; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:n_rows_cumulative; 111, call; 111, 112; 111, 113; 112, identifier:sum; 113, argument_list; 113, 114; 114, list_comprehension; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:df; 118, identifier:shape; 119, integer:0; 120, for_in_clause; 120, 121; 120, 122; 121, identifier:df; 122, identifier:data_dfs; 123, assert_statement; 123, 124; 124, comparison_operator:==; 124, 125; 124, 126; 125, identifier:n_rows; 126, identifier:n_rows_cumulative; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:all_data_df_sorted; 131, call; 131, 132; 131, 142; 132, attribute; 132, 133; 132, 141; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:all_data_df; 136, identifier:sort_index; 137, argument_list; 137, 138; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:axis; 140, integer:0; 141, identifier:sort_index; 142, argument_list; 142, 143; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:axis; 145, integer:1; 146, return_statement; 146, 147; 147, identifier:all_data_df_sorted | def assemble_data(data_dfs, concat_direction):
""" Assemble the data dfs together. Both indices are sorted.
Args:
data_dfs (list of pandas dfs)
concat_direction (string): 'horiz' or 'vert'
Returns:
all_data_df_sorted (pandas df)
"""
if concat_direction == "horiz":
# Concatenate the data_dfs horizontally
all_data_df = pd.concat(data_dfs, axis=1)
# Sanity check: the number of columns in all_data_df should
# correspond to the sum of the number of columns in the input dfs
n_cols = all_data_df.shape[1]
logger.debug("all_data_df.shape[1]: {}".format(n_cols))
n_cols_cumulative = sum([df.shape[1] for df in data_dfs])
assert n_cols == n_cols_cumulative
elif concat_direction == "vert":
# Concatenate the data_dfs vertically
all_data_df = pd.concat(data_dfs, axis=0)
# Sanity check: the number of rows in all_data_df should
# correspond to the sum of the number of rows in the input dfs
n_rows = all_data_df.shape[0]
logger.debug("all_data_df.shape[0]: {}".format(n_rows))
n_rows_cumulative = sum([df.shape[0] for df in data_dfs])
assert n_rows == n_rows_cumulative
# Sort both indices
all_data_df_sorted = all_data_df.sort_index(axis=0).sort_index(axis=1)
return all_data_df_sorted |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:get_rows_to_keep; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:gctoo; 5, default_parameter; 5, 6; 5, 7; 6, identifier:rid; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:row_bool; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:ridx; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:exclude_rid; 16, None; 17, block; 17, 18; 17, 20; 17, 21; 17, 224; 17, 225; 17, 243; 18, expression_statement; 18, 19; 19, comment; 20, comment; 21, if_statement; 21, 22; 21, 25; 21, 85; 21, 86; 21, 135; 21, 136; 21, 211; 21, 212; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:rid; 24, None; 25, block; 25, 26; 25, 39; 25, 55; 25, 56; 25, 68; 26, assert_statement; 26, 27; 26, 33; 27, comparison_operator:==; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:type; 30, argument_list; 30, 31; 31, identifier:rid; 32, identifier:list; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, string:"rid must be a list. rid: {}"; 36, identifier:format; 37, argument_list; 37, 38; 38, identifier:rid; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:rows_to_keep; 42, list_comprehension; 42, 43; 42, 44; 42, 51; 43, identifier:gctoo_row; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:gctoo_row; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:gctoo; 49, identifier:data_df; 50, identifier:index; 51, if_clause; 51, 52; 52, comparison_operator:in; 52, 53; 52, 54; 53, identifier:gctoo_row; 54, identifier:rid; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:num_missing_rids; 59, binary_operator:-; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:rid; 64, call; 64, 65; 64, 66; 65, identifier:len; 66, argument_list; 66, 67; 67, identifier:rows_to_keep; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:!=; 69, 70; 69, 71; 70, identifier:num_missing_rids; 71, integer:0; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:logger; 77, identifier:info; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:"{} rids were not found in the GCT."; 82, identifier:format; 83, argument_list; 83, 84; 84, identifier:num_missing_rids; 85, comment; 86, elif_clause; 86, 87; 86, 90; 87, comparison_operator:is; 87, 88; 87, 89; 88, identifier:row_bool; 89, None; 90, block; 90, 91; 90, 123; 91, assert_statement; 91, 92; 91, 104; 92, comparison_operator:==; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:row_bool; 97, subscript; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:gctoo; 101, identifier:data_df; 102, identifier:shape; 103, integer:0; 104, parenthesized_expression; 104, 105; 105, binary_operator:+; 105, 106; 105, 107; 106, string:"row_bool must have length equal to gctoo.data_df.shape[0]. "; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, string:"len(row_bool): {}, gctoo.data_df.shape[0]: {}"; 110, identifier:format; 111, argument_list; 111, 112; 111, 116; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:row_bool; 116, subscript; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:gctoo; 120, identifier:data_df; 121, identifier:shape; 122, integer:0; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:rows_to_keep; 126, attribute; 126, 127; 126, 134; 127, subscript; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:gctoo; 131, identifier:data_df; 132, identifier:index; 133, identifier:row_bool; 134, identifier:values; 135, comment; 136, elif_clause; 136, 137; 136, 140; 137, comparison_operator:is; 137, 138; 137, 139; 138, identifier:ridx; 139, None; 140, block; 140, 141; 140, 167; 140, 199; 141, assert_statement; 141, 142; 141, 150; 142, comparison_operator:is; 142, 143; 142, 149; 143, call; 143, 144; 143, 145; 144, identifier:type; 145, argument_list; 145, 146; 146, subscript; 146, 147; 146, 148; 147, identifier:ridx; 148, integer:0; 149, identifier:int; 150, call; 150, 151; 150, 157; 151, attribute; 151, 152; 151, 156; 152, parenthesized_expression; 152, 153; 153, binary_operator:+; 153, 154; 153, 155; 154, string:"ridx must be a list of integers. ridx[0]: {}, "; 155, string:"type(ridx[0]): {}"; 156, identifier:format; 157, argument_list; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:ridx; 160, integer:0; 161, call; 161, 162; 161, 163; 162, identifier:type; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:ridx; 166, integer:0; 167, assert_statement; 167, 168; 167, 180; 168, comparison_operator:<=; 168, 169; 168, 173; 169, call; 169, 170; 169, 171; 170, identifier:max; 171, argument_list; 171, 172; 172, identifier:ridx; 173, subscript; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:gctoo; 177, identifier:data_df; 178, identifier:shape; 179, integer:0; 180, call; 180, 181; 180, 187; 181, attribute; 181, 182; 181, 186; 182, parenthesized_expression; 182, 183; 183, binary_operator:+; 183, 184; 183, 185; 184, string:"ridx contains an integer larger than the number of rows in "; 185, string:"the GCToo. max(ridx): {}, gctoo.data_df.shape[0]: {}"; 186, identifier:format; 187, argument_list; 187, 188; 187, 192; 188, call; 188, 189; 188, 190; 189, identifier:max; 190, argument_list; 190, 191; 191, identifier:ridx; 192, subscript; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:gctoo; 196, identifier:data_df; 197, identifier:shape; 198, integer:0; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:rows_to_keep; 202, attribute; 202, 203; 202, 210; 203, subscript; 203, 204; 203, 209; 204, attribute; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:gctoo; 207, identifier:data_df; 208, identifier:index; 209, identifier:ridx; 210, identifier:values; 211, comment; 212, else_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:rows_to_keep; 217, attribute; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:gctoo; 221, identifier:data_df; 222, identifier:index; 223, identifier:values; 224, comment; 225, if_statement; 225, 226; 225, 229; 225, 230; 226, comparison_operator:is; 226, 227; 226, 228; 227, identifier:exclude_rid; 228, None; 229, comment; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:rows_to_keep; 234, list_comprehension; 234, 235; 234, 236; 234, 239; 235, identifier:row_to_keep; 236, for_in_clause; 236, 237; 236, 238; 237, identifier:row_to_keep; 238, identifier:rows_to_keep; 239, if_clause; 239, 240; 240, comparison_operator:not; 240, 241; 240, 242; 241, identifier:row_to_keep; 242, identifier:exclude_rid; 243, return_statement; 243, 244; 244, identifier:rows_to_keep | def get_rows_to_keep(gctoo, rid=None, row_bool=None, ridx=None, exclude_rid=None):
""" Figure out based on the possible row inputs which rows to keep.
Args:
gctoo (GCToo object):
rid (list of strings):
row_bool (boolean array):
ridx (list of integers):
exclude_rid (list of strings):
Returns:
rows_to_keep (list of strings): row ids to be kept
"""
# Use rid if provided
if rid is not None:
assert type(rid) == list, "rid must be a list. rid: {}".format(rid)
rows_to_keep = [gctoo_row for gctoo_row in gctoo.data_df.index if gctoo_row in rid]
# Tell user if some rids not found
num_missing_rids = len(rid) - len(rows_to_keep)
if num_missing_rids != 0:
logger.info("{} rids were not found in the GCT.".format(num_missing_rids))
# Use row_bool if provided
elif row_bool is not None:
assert len(row_bool) == gctoo.data_df.shape[0], (
"row_bool must have length equal to gctoo.data_df.shape[0]. " +
"len(row_bool): {}, gctoo.data_df.shape[0]: {}".format(
len(row_bool), gctoo.data_df.shape[0]))
rows_to_keep = gctoo.data_df.index[row_bool].values
# Use ridx if provided
elif ridx is not None:
assert type(ridx[0]) is int, (
"ridx must be a list of integers. ridx[0]: {}, " +
"type(ridx[0]): {}").format(ridx[0], type(ridx[0]))
assert max(ridx) <= gctoo.data_df.shape[0], (
"ridx contains an integer larger than the number of rows in " +
"the GCToo. max(ridx): {}, gctoo.data_df.shape[0]: {}").format(
max(ridx), gctoo.data_df.shape[0])
rows_to_keep = gctoo.data_df.index[ridx].values
# If rid, row_bool, and ridx are all None, return all rows
else:
rows_to_keep = gctoo.data_df.index.values
# Use exclude_rid if provided
if exclude_rid is not None:
# Keep only those rows that are not in exclude_rid
rows_to_keep = [row_to_keep for row_to_keep in rows_to_keep if row_to_keep not in exclude_rid]
return rows_to_keep |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:get_cols_to_keep; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:gctoo; 5, default_parameter; 5, 6; 5, 7; 6, identifier:cid; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:col_bool; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:cidx; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:exclude_cid; 16, None; 17, block; 17, 18; 17, 20; 17, 21; 17, 224; 17, 225; 17, 243; 18, expression_statement; 18, 19; 19, comment; 20, comment; 21, if_statement; 21, 22; 21, 25; 21, 85; 21, 86; 21, 135; 21, 136; 21, 211; 21, 212; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:cid; 24, None; 25, block; 25, 26; 25, 39; 25, 55; 25, 56; 25, 68; 26, assert_statement; 26, 27; 26, 33; 27, comparison_operator:==; 27, 28; 27, 32; 28, call; 28, 29; 28, 30; 29, identifier:type; 30, argument_list; 30, 31; 31, identifier:cid; 32, identifier:list; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, string:"cid must be a list. cid: {}"; 36, identifier:format; 37, argument_list; 37, 38; 38, identifier:cid; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:cols_to_keep; 42, list_comprehension; 42, 43; 42, 44; 42, 51; 43, identifier:gctoo_col; 44, for_in_clause; 44, 45; 44, 46; 45, identifier:gctoo_col; 46, attribute; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:gctoo; 49, identifier:data_df; 50, identifier:columns; 51, if_clause; 51, 52; 52, comparison_operator:in; 52, 53; 52, 54; 53, identifier:gctoo_col; 54, identifier:cid; 55, comment; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:num_missing_cids; 59, binary_operator:-; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:len; 62, argument_list; 62, 63; 63, identifier:cid; 64, call; 64, 65; 64, 66; 65, identifier:len; 66, argument_list; 66, 67; 67, identifier:cols_to_keep; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:!=; 69, 70; 69, 71; 70, identifier:num_missing_cids; 71, integer:0; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:logger; 77, identifier:info; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, string:"{} cids were not found in the GCT."; 82, identifier:format; 83, argument_list; 83, 84; 84, identifier:num_missing_cids; 85, comment; 86, elif_clause; 86, 87; 86, 90; 87, comparison_operator:is; 87, 88; 87, 89; 88, identifier:col_bool; 89, None; 90, block; 90, 91; 90, 123; 91, assert_statement; 91, 92; 91, 104; 92, comparison_operator:==; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:len; 95, argument_list; 95, 96; 96, identifier:col_bool; 97, subscript; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:gctoo; 101, identifier:data_df; 102, identifier:shape; 103, integer:1; 104, parenthesized_expression; 104, 105; 105, binary_operator:+; 105, 106; 105, 107; 106, string:"col_bool must have length equal to gctoo.data_df.shape[1]. "; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, string:"len(col_bool): {}, gctoo.data_df.shape[1]: {}"; 110, identifier:format; 111, argument_list; 111, 112; 111, 116; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:col_bool; 116, subscript; 116, 117; 116, 122; 117, attribute; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:gctoo; 120, identifier:data_df; 121, identifier:shape; 122, integer:1; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:cols_to_keep; 126, attribute; 126, 127; 126, 134; 127, subscript; 127, 128; 127, 133; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:gctoo; 131, identifier:data_df; 132, identifier:columns; 133, identifier:col_bool; 134, identifier:values; 135, comment; 136, elif_clause; 136, 137; 136, 140; 137, comparison_operator:is; 137, 138; 137, 139; 138, identifier:cidx; 139, None; 140, block; 140, 141; 140, 167; 140, 199; 141, assert_statement; 141, 142; 141, 150; 142, comparison_operator:is; 142, 143; 142, 149; 143, call; 143, 144; 143, 145; 144, identifier:type; 145, argument_list; 145, 146; 146, subscript; 146, 147; 146, 148; 147, identifier:cidx; 148, integer:0; 149, identifier:int; 150, call; 150, 151; 150, 157; 151, attribute; 151, 152; 151, 156; 152, parenthesized_expression; 152, 153; 153, binary_operator:+; 153, 154; 153, 155; 154, string:"cidx must be a list of integers. cidx[0]: {}, "; 155, string:"type(cidx[0]): {}"; 156, identifier:format; 157, argument_list; 157, 158; 157, 161; 158, subscript; 158, 159; 158, 160; 159, identifier:cidx; 160, integer:0; 161, call; 161, 162; 161, 163; 162, identifier:type; 163, argument_list; 163, 164; 164, subscript; 164, 165; 164, 166; 165, identifier:cidx; 166, integer:0; 167, assert_statement; 167, 168; 167, 180; 168, comparison_operator:<=; 168, 169; 168, 173; 169, call; 169, 170; 169, 171; 170, identifier:max; 171, argument_list; 171, 172; 172, identifier:cidx; 173, subscript; 173, 174; 173, 179; 174, attribute; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:gctoo; 177, identifier:data_df; 178, identifier:shape; 179, integer:1; 180, call; 180, 181; 180, 187; 181, attribute; 181, 182; 181, 186; 182, parenthesized_expression; 182, 183; 183, binary_operator:+; 183, 184; 183, 185; 184, string:"cidx contains an integer larger than the number of columns in "; 185, string:"the GCToo. max(cidx): {}, gctoo.data_df.shape[1]: {}"; 186, identifier:format; 187, argument_list; 187, 188; 187, 192; 188, call; 188, 189; 188, 190; 189, identifier:max; 190, argument_list; 190, 191; 191, identifier:cidx; 192, subscript; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:gctoo; 196, identifier:data_df; 197, identifier:shape; 198, integer:1; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:cols_to_keep; 202, attribute; 202, 203; 202, 210; 203, subscript; 203, 204; 203, 209; 204, attribute; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:gctoo; 207, identifier:data_df; 208, identifier:columns; 209, identifier:cidx; 210, identifier:values; 211, comment; 212, else_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:cols_to_keep; 217, attribute; 217, 218; 217, 223; 218, attribute; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:gctoo; 221, identifier:data_df; 222, identifier:columns; 223, identifier:values; 224, comment; 225, if_statement; 225, 226; 225, 229; 225, 230; 226, comparison_operator:is; 226, 227; 226, 228; 227, identifier:exclude_cid; 228, None; 229, comment; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:cols_to_keep; 234, list_comprehension; 234, 235; 234, 236; 234, 239; 235, identifier:col_to_keep; 236, for_in_clause; 236, 237; 236, 238; 237, identifier:col_to_keep; 238, identifier:cols_to_keep; 239, if_clause; 239, 240; 240, comparison_operator:not; 240, 241; 240, 242; 241, identifier:col_to_keep; 242, identifier:exclude_cid; 243, return_statement; 243, 244; 244, identifier:cols_to_keep | def get_cols_to_keep(gctoo, cid=None, col_bool=None, cidx=None, exclude_cid=None):
""" Figure out based on the possible columns inputs which columns to keep.
Args:
gctoo (GCToo object):
cid (list of strings):
col_bool (boolean array):
cidx (list of integers):
exclude_cid (list of strings):
Returns:
cols_to_keep (list of strings): col ids to be kept
"""
# Use cid if provided
if cid is not None:
assert type(cid) == list, "cid must be a list. cid: {}".format(cid)
cols_to_keep = [gctoo_col for gctoo_col in gctoo.data_df.columns if gctoo_col in cid]
# Tell user if some cids not found
num_missing_cids = len(cid) - len(cols_to_keep)
if num_missing_cids != 0:
logger.info("{} cids were not found in the GCT.".format(num_missing_cids))
# Use col_bool if provided
elif col_bool is not None:
assert len(col_bool) == gctoo.data_df.shape[1], (
"col_bool must have length equal to gctoo.data_df.shape[1]. " +
"len(col_bool): {}, gctoo.data_df.shape[1]: {}".format(
len(col_bool), gctoo.data_df.shape[1]))
cols_to_keep = gctoo.data_df.columns[col_bool].values
# Use cidx if provided
elif cidx is not None:
assert type(cidx[0]) is int, (
"cidx must be a list of integers. cidx[0]: {}, " +
"type(cidx[0]): {}").format(cidx[0], type(cidx[0]))
assert max(cidx) <= gctoo.data_df.shape[1], (
"cidx contains an integer larger than the number of columns in " +
"the GCToo. max(cidx): {}, gctoo.data_df.shape[1]: {}").format(
max(cidx), gctoo.data_df.shape[1])
cols_to_keep = gctoo.data_df.columns[cidx].values
# If cid, col_bool, and cidx are all None, return all columns
else:
cols_to_keep = gctoo.data_df.columns.values
# Use exclude_cid if provided
if exclude_cid is not None:
# Keep only those columns that are not in exclude_cid
cols_to_keep = [col_to_keep for col_to_keep in cols_to_keep if col_to_keep not in exclude_cid]
return cols_to_keep |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:finish; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:queue_name; 5, identifier:task_id; 6, identifier:owner; 7, default_parameter; 7, 8; 7, 9; 8, identifier:error; 9, False; 10, block; 10, 11; 10, 13; 10, 22; 10, 51; 10, 73; 10, 85; 10, 94; 10, 106; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:task; 16, call; 16, 17; 16, 18; 17, identifier:_get_task_with_policy; 18, argument_list; 18, 19; 18, 20; 18, 21; 19, identifier:queue_name; 20, identifier:task_id; 21, identifier:owner; 22, if_statement; 22, 23; 22, 31; 23, not_operator; 23, 24; 24, comparison_operator:==; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:task; 27, identifier:status; 28, attribute; 28, 29; 28, 30; 29, identifier:WorkQueue; 30, identifier:LIVE; 31, block; 31, 32; 31, 49; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:logging; 36, identifier:warning; 37, argument_list; 37, 38; 37, 41; 37, 44; 37, 45; 37, 46; 38, concatenated_string; 38, 39; 38, 40; 39, string:'Finishing already dead task. queue=%r, task_id=%r, '; 40, string:'owner=%r, status=%r'; 41, attribute; 41, 42; 41, 43; 42, identifier:task; 43, identifier:queue_name; 44, identifier:task_id; 45, identifier:owner; 46, attribute; 46, 47; 46, 48; 47, identifier:task; 48, identifier:status; 49, return_statement; 49, 50; 50, False; 51, if_statement; 51, 52; 51, 54; 51, 63; 52, not_operator; 52, 53; 53, identifier:error; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:task; 59, identifier:status; 60, attribute; 60, 61; 60, 62; 61, identifier:WorkQueue; 62, identifier:DONE; 63, else_clause; 63, 64; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:task; 69, identifier:status; 70, attribute; 70, 71; 70, 72; 71, identifier:WorkQueue; 72, identifier:ERROR; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:task; 77, identifier:finished; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:datetime; 82, identifier:datetime; 83, identifier:utcnow; 84, argument_list; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:db; 90, identifier:session; 91, identifier:add; 92, argument_list; 92, 93; 93, identifier:task; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:signals; 99, identifier:task_updated; 100, identifier:send; 101, argument_list; 101, 102; 101, 103; 102, identifier:app; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:task; 105, identifier:task; 106, return_statement; 106, 107; 107, True | def finish(queue_name, task_id, owner, error=False):
"""Marks a work item on a queue as finished.
Args:
queue_name: Name of the queue the work item is on.
task_id: ID of the task that is finished.
owner: Who or what has the current lease on the task.
error: Defaults to false. True if this task's final state is an error.
Returns:
True if the task has been finished for the first time; False if the
task was already finished.
Raises:
TaskDoesNotExistError if the task does not exist.
LeaseExpiredError if the lease is no longer active.
NotOwnerError if the specified owner no longer owns the task.
"""
task = _get_task_with_policy(queue_name, task_id, owner)
if not task.status == WorkQueue.LIVE:
logging.warning('Finishing already dead task. queue=%r, task_id=%r, '
'owner=%r, status=%r',
task.queue_name, task_id, owner, task.status)
return False
if not error:
task.status = WorkQueue.DONE
else:
task.status = WorkQueue.ERROR
task.finished = datetime.datetime.utcnow()
db.session.add(task)
signals.task_updated.send(app, task=task)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:send_ready_for_review; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:build_id; 5, identifier:release_name; 6, identifier:release_number; 7, block; 7, 8; 7, 10; 7, 23; 7, 42; 7, 51; 7, 65; 7, 88; 7, 100; 7, 119; 7, 123; 7, 154; 7, 177; 7, 187; 7, 193; 7, 212; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:build; 13, call; 13, 14; 13, 21; 14, attribute; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:models; 18, identifier:Build; 19, identifier:query; 20, identifier:get; 21, argument_list; 21, 22; 22, identifier:build_id; 23, if_statement; 23, 24; 23, 28; 24, not_operator; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:build; 27, identifier:send_email; 28, block; 28, 29; 28, 41; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:logging; 33, identifier:debug; 34, argument_list; 34, 35; 34, 38; 35, concatenated_string; 35, 36; 35, 37; 36, string:'Not sending ready for review email because build does not have '; 37, string:'email enabled. build_id=%r'; 38, attribute; 38, 39; 38, 40; 39, identifier:build; 40, identifier:id; 41, return_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:ops; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:operations; 48, identifier:BuildOps; 49, argument_list; 49, 50; 50, identifier:build_id; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 58; 53, pattern_list; 53, 54; 53, 55; 53, 56; 53, 57; 54, identifier:release; 55, identifier:run_list; 56, identifier:stats_dict; 57, identifier:_; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:ops; 61, identifier:get_release; 62, argument_list; 62, 63; 62, 64; 63, identifier:release_name; 64, identifier:release_number; 65, if_statement; 65, 66; 65, 68; 66, not_operator; 66, 67; 67, identifier:run_list; 68, block; 68, 69; 68, 87; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:logging; 73, identifier:debug; 74, argument_list; 74, 75; 74, 78; 74, 81; 74, 84; 75, concatenated_string; 75, 76; 75, 77; 76, string:'Not sending ready for review email because there are '; 77, string:' no runs. build_id=%r, release_name=%r, release_number=%d'; 78, attribute; 78, 79; 78, 80; 79, identifier:build; 80, identifier:id; 81, attribute; 81, 82; 81, 83; 82, identifier:release; 83, identifier:name; 84, attribute; 84, 85; 84, 86; 85, identifier:release; 86, identifier:number; 87, return_statement; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:title; 91, binary_operator:%; 91, 92; 91, 93; 92, string:'%s: %s - Ready for review'; 93, tuple; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:build; 96, identifier:name; 97, attribute; 97, 98; 97, 99; 98, identifier:release; 99, identifier:name; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:email_body; 103, call; 103, 104; 103, 105; 104, identifier:render_template; 105, argument_list; 105, 106; 105, 107; 105, 110; 105, 113; 105, 116; 106, string:'email_ready_for_review.html'; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:build; 109, identifier:build; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:release; 112, identifier:release; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:run_list; 115, identifier:run_list; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:stats_dict; 118, identifier:stats_dict; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:recipients; 122, list:[]; 123, if_statement; 123, 124; 123, 127; 123, 137; 124, attribute; 124, 125; 124, 126; 125, identifier:build; 126, identifier:email_alias; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:recipients; 132, identifier:append; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:build; 136, identifier:email_alias; 137, else_clause; 137, 138; 138, block; 138, 139; 139, for_statement; 139, 140; 139, 141; 139, 144; 140, identifier:user; 141, attribute; 141, 142; 141, 143; 142, identifier:build; 143, identifier:owners; 144, block; 144, 145; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:recipients; 149, identifier:append; 150, argument_list; 150, 151; 151, attribute; 151, 152; 151, 153; 152, identifier:user; 153, identifier:email_address; 154, if_statement; 154, 155; 154, 157; 155, not_operator; 155, 156; 156, identifier:recipients; 157, block; 157, 158; 157, 176; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:logging; 162, identifier:debug; 163, argument_list; 163, 164; 163, 167; 163, 170; 163, 173; 164, concatenated_string; 164, 165; 164, 166; 165, string:'Not sending ready for review email because there are no '; 166, string:'recipients. build_id=%r, release_name=%r, release_number=%d'; 167, attribute; 167, 168; 167, 169; 168, identifier:build; 169, identifier:id; 170, attribute; 170, 171; 170, 172; 171, identifier:release; 172, identifier:name; 173, attribute; 173, 174; 173, 175; 174, identifier:release; 175, identifier:number; 176, return_statement; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:message; 180, call; 180, 181; 180, 182; 181, identifier:Message; 182, argument_list; 182, 183; 182, 184; 183, identifier:title; 184, keyword_argument; 184, 185; 184, 186; 185, identifier:recipients; 186, identifier:recipients; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:message; 191, identifier:html; 192, identifier:email_body; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:logging; 197, identifier:info; 198, argument_list; 198, 199; 198, 202; 198, 205; 198, 208; 198, 211; 199, concatenated_string; 199, 200; 199, 201; 200, string:'Sending ready for review email for build_id=%r, '; 201, string:'release_name=%r, release_number=%d to %r'; 202, attribute; 202, 203; 202, 204; 203, identifier:build; 204, identifier:id; 205, attribute; 205, 206; 205, 207; 206, identifier:release; 207, identifier:name; 208, attribute; 208, 209; 208, 210; 209, identifier:release; 210, identifier:number; 211, identifier:recipients; 212, return_statement; 212, 213; 213, call; 213, 214; 213, 215; 214, identifier:render_or_send; 215, argument_list; 215, 216; 215, 217; 216, identifier:send_ready_for_review; 217, identifier:message | def send_ready_for_review(build_id, release_name, release_number):
"""Sends an email indicating that the release is ready for review."""
build = models.Build.query.get(build_id)
if not build.send_email:
logging.debug(
'Not sending ready for review email because build does not have '
'email enabled. build_id=%r', build.id)
return
ops = operations.BuildOps(build_id)
release, run_list, stats_dict, _ = ops.get_release(
release_name, release_number)
if not run_list:
logging.debug(
'Not sending ready for review email because there are '
' no runs. build_id=%r, release_name=%r, release_number=%d',
build.id, release.name, release.number)
return
title = '%s: %s - Ready for review' % (build.name, release.name)
email_body = render_template(
'email_ready_for_review.html',
build=build,
release=release,
run_list=run_list,
stats_dict=stats_dict)
recipients = []
if build.email_alias:
recipients.append(build.email_alias)
else:
for user in build.owners:
recipients.append(user.email_address)
if not recipients:
logging.debug(
'Not sending ready for review email because there are no '
'recipients. build_id=%r, release_name=%r, release_number=%d',
build.id, release.name, release.number)
return
message = Message(title, recipients=recipients)
message.html = email_body
logging.info('Sending ready for review email for build_id=%r, '
'release_name=%r, release_number=%d to %r',
build.id, release.name, release.number, recipients)
return render_or_send(send_ready_for_review, message) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:claim_invitations; 3, parameters; 3, 4; 4, identifier:user; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 24; 5, 37; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:invitation_user_id; 13, binary_operator:%; 13, 14; 13, 15; 14, string:'%s:%s'; 15, tuple; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:models; 19, identifier:User; 20, identifier:EMAIL_INVITATION; 21, attribute; 21, 22; 21, 23; 22, identifier:user; 23, identifier:email_address; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:invitation_user; 27, call; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:models; 32, identifier:User; 33, identifier:query; 34, identifier:get; 35, argument_list; 35, 36; 36, identifier:invitation_user_id; 37, if_statement; 37, 38; 37, 39; 38, identifier:invitation_user; 39, block; 39, 40; 39, 49; 39, 54; 39, 63; 39, 76; 39, 153; 39, 162; 39, 170; 39, 171; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:invited_build_list; 43, call; 43, 44; 43, 45; 44, identifier:list; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:invitation_user; 48, identifier:builds; 49, if_statement; 49, 50; 49, 52; 50, not_operator; 50, 51; 51, identifier:invited_build_list; 52, block; 52, 53; 53, return_statement; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:db; 59, identifier:session; 60, identifier:add; 61, argument_list; 61, 62; 62, identifier:user; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:logging; 67, identifier:debug; 68, argument_list; 68, 69; 68, 70; 68, 74; 68, 75; 69, string:'Found %d build admin invitations for id=%r, user=%r'; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:invited_build_list; 74, identifier:invitation_user_id; 75, identifier:user; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:build; 78, identifier:invited_build_list; 79, block; 79, 80; 79, 89; 79, 144; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 87; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:build; 85, identifier:owners; 86, identifier:remove; 87, argument_list; 87, 88; 88, identifier:invitation_user; 89, if_statement; 89, 90; 89, 99; 89, 127; 90, not_operator; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:build; 94, identifier:is_owned_by; 95, argument_list; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:user; 98, identifier:id; 99, block; 99, 100; 99, 109; 99, 119; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:build; 105, identifier:owners; 106, identifier:append; 107, argument_list; 107, 108; 108, identifier:user; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:logging; 113, identifier:debug; 114, argument_list; 114, 115; 114, 116; 115, string:'Claiming invitation for build_id=%r'; 116, attribute; 116, 117; 116, 118; 117, identifier:build; 118, identifier:id; 119, expression_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:save_admin_log; 122, argument_list; 122, 123; 122, 124; 123, identifier:build; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:invite_accepted; 126, True; 127, else_clause; 127, 128; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:logging; 133, identifier:debug; 134, argument_list; 134, 135; 134, 138; 134, 141; 135, concatenated_string; 135, 136; 135, 137; 136, string:'User already owner of build. '; 137, string:'id=%r, build_id=%r'; 138, attribute; 138, 139; 138, 140; 139, identifier:user; 140, identifier:id; 141, attribute; 141, 142; 141, 143; 142, identifier:build; 143, identifier:id; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:db; 149, identifier:session; 150, identifier:add; 151, argument_list; 151, 152; 152, identifier:build; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:db; 158, identifier:session; 159, identifier:delete; 160, argument_list; 160, 161; 161, identifier:invitation_user; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:db; 167, identifier:session; 168, identifier:commit; 169, argument_list; 170, comment; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:db; 176, identifier:session; 177, identifier:add; 178, argument_list; 178, 179; 179, identifier:current_user | def claim_invitations(user):
"""Claims any pending invitations for the given user's email address."""
# See if there are any build invitations present for the user with this
# email address. If so, replace all those invitations with the real user.
invitation_user_id = '%s:%s' % (
models.User.EMAIL_INVITATION, user.email_address)
invitation_user = models.User.query.get(invitation_user_id)
if invitation_user:
invited_build_list = list(invitation_user.builds)
if not invited_build_list:
return
db.session.add(user)
logging.debug('Found %d build admin invitations for id=%r, user=%r',
len(invited_build_list), invitation_user_id, user)
for build in invited_build_list:
build.owners.remove(invitation_user)
if not build.is_owned_by(user.id):
build.owners.append(user)
logging.debug('Claiming invitation for build_id=%r', build.id)
save_admin_log(build, invite_accepted=True)
else:
logging.debug('User already owner of build. '
'id=%r, build_id=%r', user.id, build.id)
db.session.add(build)
db.session.delete(invitation_user)
db.session.commit()
# Re-add the user to the current session so we can query with it.
db.session.add(current_user) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_check_release_done_processing; 3, parameters; 3, 4; 4, identifier:release; 5, block; 5, 6; 5, 8; 5, 43; 5, 60; 5, 104; 5, 122; 5, 123; 5, 124; 5, 130; 5, 136; 5, 142; 5, 161; 5, 171; 5, 180; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 18; 8, 19; 8, 20; 8, 21; 8, 22; 9, comparison_operator:!=; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:release; 12, identifier:status; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:models; 16, identifier:Release; 17, identifier:PROCESSING; 18, comment; 19, comment; 20, comment; 21, comment; 22, block; 22, 23; 22, 41; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:logging; 27, identifier:info; 28, argument_list; 28, 29; 28, 32; 28, 35; 28, 38; 29, concatenated_string; 29, 30; 29, 31; 30, string:'Release not in processing state yet: build_id=%r, '; 31, string:'name=%r, number=%d'; 32, attribute; 32, 33; 32, 34; 33, identifier:release; 34, identifier:build_id; 35, attribute; 35, 36; 35, 37; 36, identifier:release; 37, identifier:name; 38, attribute; 38, 39; 38, 40; 39, identifier:release; 40, identifier:number; 41, return_statement; 41, 42; 42, False; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:query; 46, call; 46, 47; 46, 54; 47, attribute; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:models; 51, identifier:Run; 52, identifier:query; 53, identifier:filter_by; 54, argument_list; 54, 55; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:release_id; 57, attribute; 57, 58; 57, 59; 58, identifier:release; 59, identifier:id; 60, for_statement; 60, 61; 60, 62; 60, 63; 61, identifier:run; 62, identifier:query; 63, block; 63, 64; 63, 78; 63, 91; 64, if_statement; 64, 65; 64, 74; 64, 75; 65, comparison_operator:==; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:run; 68, identifier:status; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:models; 72, identifier:Run; 73, identifier:NEEDS_DIFF; 74, comment; 75, block; 75, 76; 76, return_statement; 76, 77; 77, False; 78, if_statement; 78, 79; 78, 87; 78, 88; 79, boolean_operator:and; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:run; 82, identifier:ref_config; 83, not_operator; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:run; 86, identifier:ref_image; 87, comment; 88, block; 88, 89; 89, return_statement; 89, 90; 90, False; 91, if_statement; 91, 92; 91, 100; 91, 101; 92, boolean_operator:and; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:run; 95, identifier:config; 96, not_operator; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:run; 99, identifier:image; 100, comment; 101, block; 101, 102; 102, return_statement; 102, 103; 103, False; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:logging; 108, identifier:info; 109, argument_list; 109, 110; 109, 113; 109, 116; 109, 119; 110, concatenated_string; 110, 111; 110, 112; 111, string:'Release done processing, now reviewing: build_id=%r, '; 112, string:'name=%r, number=%d'; 113, attribute; 113, 114; 113, 115; 114, identifier:release; 115, identifier:build_id; 116, attribute; 116, 117; 116, 118; 117, identifier:release; 118, identifier:name; 119, attribute; 119, 120; 119, 121; 120, identifier:release; 121, identifier:number; 122, comment; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:build_id; 127, attribute; 127, 128; 127, 129; 128, identifier:release; 129, identifier:build_id; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:release_name; 133, attribute; 133, 134; 133, 135; 134, identifier:release; 135, identifier:name; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:release_number; 139, attribute; 139, 140; 139, 141; 140, identifier:release; 141, identifier:number; 142, decorated_definition; 142, 143; 142, 147; 143, decorator; 143, 144; 144, attribute; 144, 145; 144, 146; 145, identifier:utils; 146, identifier:after_this_request; 147, function_definition; 147, 148; 147, 149; 147, 151; 148, function_name:send_notification_email; 149, parameters; 149, 150; 150, identifier:response; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:emails; 156, identifier:send_ready_for_review; 157, argument_list; 157, 158; 157, 159; 157, 160; 158, identifier:build_id; 159, identifier:release_name; 160, identifier:release_number; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:release; 165, identifier:status; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:models; 169, identifier:Release; 170, identifier:REVIEWING; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 178; 173, attribute; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:db; 176, identifier:session; 177, identifier:add; 178, argument_list; 178, 179; 179, identifier:release; 180, return_statement; 180, 181; 181, True | def _check_release_done_processing(release):
"""Moves a release candidate to reviewing if all runs are done."""
if release.status != models.Release.PROCESSING:
# NOTE: This statement also guards for situations where the user has
# prematurely specified that the release is good or bad. Once the user
# has done that, the system will not automatically move the release
# back into the 'reviewing' state or send the email notification below.
logging.info('Release not in processing state yet: build_id=%r, '
'name=%r, number=%d', release.build_id, release.name,
release.number)
return False
query = models.Run.query.filter_by(release_id=release.id)
for run in query:
if run.status == models.Run.NEEDS_DIFF:
# Still waiting for the diff to finish.
return False
if run.ref_config and not run.ref_image:
# Still waiting for the ref capture to process.
return False
if run.config and not run.image:
# Still waiting for the run capture to process.
return False
logging.info('Release done processing, now reviewing: build_id=%r, '
'name=%r, number=%d', release.build_id, release.name,
release.number)
# Send the email at the end of this request so we know it's only
# sent a single time (guarded by the release.status check above).
build_id = release.build_id
release_name = release.name
release_number = release.number
@utils.after_this_request
def send_notification_email(response):
emails.send_ready_for_review(build_id, release_name, release_number)
release.status = models.Release.REVIEWING
db.session.add(release)
return True |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:download; 3, parameters; 4, block; 4, 5; 4, 7; 4, 8; 4, 9; 4, 41; 4, 55; 4, 72; 4, 85; 4, 102; 4, 116; 4, 133; 4, 150; 4, 168; 4, 169; 4, 170; 4, 193; 4, 209; 4, 236; 5, expression_statement; 5, 6; 6, comment; 7, comment; 8, comment; 9, try_statement; 9, 10; 9, 20; 10, block; 10, 11; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:build; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:auth; 17, identifier:can_user_access_build; 18, argument_list; 18, 19; 19, string:'build_id'; 20, except_clause; 20, 21; 20, 22; 21, identifier:HTTPException; 22, block; 22, 23; 22, 30; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:logging; 27, identifier:debug; 28, argument_list; 28, 29; 29, string:'User access to artifact failed. Trying API key.'; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 35; 32, pattern_list; 32, 33; 32, 34; 33, identifier:_; 34, identifier:build; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:auth; 38, identifier:can_api_key_access_build; 39, argument_list; 39, 40; 40, string:'build_id'; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:sha1sum; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:request; 48, identifier:args; 49, identifier:get; 50, argument_list; 50, 51; 50, 52; 51, string:'sha1sum'; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:type; 54, identifier:str; 55, if_statement; 55, 56; 55, 58; 56, not_operator; 56, 57; 57, identifier:sha1sum; 58, block; 58, 59; 58, 67; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:logging; 63, identifier:debug; 64, argument_list; 64, 65; 64, 66; 65, string:'Artifact sha1sum=%r not supplied'; 66, identifier:sha1sum; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:abort; 70, argument_list; 70, 71; 71, integer:404; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:artifact; 75, call; 75, 76; 75, 83; 76, attribute; 76, 77; 76, 82; 77, attribute; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:models; 80, identifier:Artifact; 81, identifier:query; 82, identifier:get; 83, argument_list; 83, 84; 84, identifier:sha1sum; 85, if_statement; 85, 86; 85, 88; 86, not_operator; 86, 87; 87, identifier:artifact; 88, block; 88, 89; 88, 97; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:logging; 93, identifier:debug; 94, argument_list; 94, 95; 94, 96; 95, string:'Artifact sha1sum=%r does not exist'; 96, identifier:sha1sum; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:abort; 100, argument_list; 100, 101; 101, integer:404; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:build_id; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:request; 109, identifier:args; 110, identifier:get; 111, argument_list; 111, 112; 111, 113; 112, string:'build_id'; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:type; 115, identifier:int; 116, if_statement; 116, 117; 116, 119; 117, not_operator; 117, 118; 118, identifier:build_id; 119, block; 119, 120; 119, 128; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:logging; 124, identifier:debug; 125, argument_list; 125, 126; 125, 127; 126, string:'build_id missing for artifact sha1sum=%r'; 127, identifier:sha1sum; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 131; 130, identifier:abort; 131, argument_list; 131, 132; 132, integer:404; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:is_owned; 136, call; 136, 137; 136, 149; 137, attribute; 137, 138; 137, 148; 138, call; 138, 139; 138, 144; 139, attribute; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:artifact; 142, identifier:owners; 143, identifier:filter_by; 144, argument_list; 144, 145; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:id; 147, identifier:build_id; 148, identifier:first; 149, argument_list; 150, if_statement; 150, 151; 150, 153; 151, not_operator; 151, 152; 152, identifier:is_owned; 153, block; 153, 154; 153, 163; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:logging; 158, identifier:debug; 159, argument_list; 159, 160; 159, 161; 159, 162; 160, string:'build_id=%r not owner of artifact sha1sum=%r'; 161, identifier:build_id; 162, identifier:sha1sum; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:abort; 166, argument_list; 166, 167; 167, integer:403; 168, comment; 169, comment; 170, decorated_definition; 170, 171; 170, 175; 171, decorator; 171, 172; 172, attribute; 172, 173; 172, 174; 173, identifier:utils; 174, identifier:after_this_request; 175, function_definition; 175, 176; 175, 177; 175, 179; 176, function_name:no_session; 177, parameters; 177, 178; 178, identifier:response; 179, block; 179, 180; 180, if_statement; 180, 181; 180, 186; 181, comparison_operator:in; 181, 182; 181, 183; 182, string:'Set-Cookie'; 183, attribute; 183, 184; 183, 185; 184, identifier:response; 185, identifier:headers; 186, block; 186, 187; 187, delete_statement; 187, 188; 188, subscript; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:response; 191, identifier:headers; 192, string:'Set-Cookie'; 193, if_statement; 193, 194; 193, 200; 193, 201; 194, not_operator; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:utils; 198, identifier:is_production; 199, argument_list; 200, comment; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:time; 206, identifier:sleep; 207, argument_list; 207, 208; 208, float:1.5; 209, if_statement; 209, 210; 209, 222; 210, boolean_operator:and; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:request; 213, identifier:if_none_match; 214, call; 214, 215; 214, 220; 215, attribute; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:request; 218, identifier:if_none_match; 219, identifier:contains; 220, argument_list; 220, 221; 221, identifier:sha1sum; 222, block; 222, 223; 222, 234; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:response; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:flask; 229, identifier:Response; 230, argument_list; 230, 231; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:status; 233, integer:304; 234, return_statement; 234, 235; 235, identifier:response; 236, return_statement; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:_get_artifact_response; 239, argument_list; 239, 240; 240, identifier:artifact | def download():
"""Downloads an artifact by it's content hash."""
# Allow users with access to the build to download the file. Falls back
# to API keys with access to the build. Prefer user first for speed.
try:
build = auth.can_user_access_build('build_id')
except HTTPException:
logging.debug('User access to artifact failed. Trying API key.')
_, build = auth.can_api_key_access_build('build_id')
sha1sum = request.args.get('sha1sum', type=str)
if not sha1sum:
logging.debug('Artifact sha1sum=%r not supplied', sha1sum)
abort(404)
artifact = models.Artifact.query.get(sha1sum)
if not artifact:
logging.debug('Artifact sha1sum=%r does not exist', sha1sum)
abort(404)
build_id = request.args.get('build_id', type=int)
if not build_id:
logging.debug('build_id missing for artifact sha1sum=%r', sha1sum)
abort(404)
is_owned = artifact.owners.filter_by(id=build_id).first()
if not is_owned:
logging.debug('build_id=%r not owner of artifact sha1sum=%r',
build_id, sha1sum)
abort(403)
# Make sure there are no Set-Cookie headers on the response so this
# request is cachable by all HTTP frontends.
@utils.after_this_request
def no_session(response):
if 'Set-Cookie' in response.headers:
del response.headers['Set-Cookie']
if not utils.is_production():
# Insert a sleep to emulate how the page loading looks in production.
time.sleep(1.5)
if request.if_none_match and request.if_none_match.contains(sha1sum):
response = flask.Response(status=304)
return response
return _get_artifact_response(artifact) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_run; 3, parameters; 3, 4; 4, identifier:run; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 28; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 21; 12, comparison_operator:in; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:run; 15, identifier:status; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:models; 19, identifier:Run; 20, identifier:DIFF_NEEDED_STATES; 21, block; 21, 22; 22, return_statement; 22, 23; 23, tuple; 23, 24; 23, 25; 24, integer:0; 25, attribute; 25, 26; 25, 27; 26, identifier:run; 27, identifier:name; 28, return_statement; 28, 29; 29, tuple; 29, 30; 29, 31; 30, integer:1; 31, attribute; 31, 32; 31, 33; 32, identifier:run; 33, identifier:name | def sort_run(run):
"""Sort function for runs within a release."""
# Sort errors first, then by name. Also show errors that were manually
# approved, so the paging sort order stays the same even after users
# approve a diff on the run page.
if run.status in models.Run.DIFF_NEEDED_STATES:
return (0, run.name)
return (1, run.name) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:parse; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:obj; 5, default_parameter; 5, 6; 5, 7; 6, identifier:required_properties; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:additional_properties; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:ignore_optional_property_errors; 13, None; 14, block; 14, 15; 14, 17; 14, 47; 14, 51; 14, 140; 14, 155; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 25; 18, not_operator; 18, 19; 19, parenthesized_expression; 19, 20; 20, comparison_operator:is; 20, 21; 20, 22; 20, 23; 20, 24; 21, identifier:required_properties; 22, identifier:additional_properties; 23, identifier:ignore_optional_property_errors; 24, None; 25, block; 25, 26; 26, with_statement; 26, 27; 26, 41; 27, with_clause; 27, 28; 28, with_item; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:parsing; 31, argument_list; 31, 32; 31, 35; 31, 38; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:required_properties; 34, identifier:required_properties; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:additional_properties; 37, identifier:additional_properties; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:ignore_optional_property_errors; 40, identifier:ignore_optional_property_errors; 41, block; 41, 42; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:parse; 45, argument_list; 45, 46; 46, identifier:obj; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:validator; 50, None; 51, if_statement; 51, 52; 51, 57; 51, 62; 51, 82; 52, call; 52, 53; 52, 54; 53, identifier:isinstance; 54, argument_list; 54, 55; 54, 56; 55, identifier:obj; 56, identifier:Validator; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:validator; 61, identifier:obj; 62, elif_clause; 62, 63; 62, 75; 63, boolean_operator:and; 63, 64; 63, 70; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:inspect; 67, identifier:isclass; 68, argument_list; 68, 69; 69, identifier:obj; 70, call; 70, 71; 70, 72; 71, identifier:issubclass; 72, argument_list; 72, 73; 72, 74; 73, identifier:obj; 74, identifier:Validator; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:validator; 79, call; 79, 80; 79, 81; 80, identifier:obj; 81, argument_list; 82, else_clause; 82, 83; 83, block; 83, 84; 84, try_statement; 84, 85; 84, 92; 84, 114; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:validator; 89, subscript; 89, 90; 89, 91; 90, identifier:_NAMED_VALIDATORS; 91, identifier:obj; 92, except_clause; 92, 93; 92, 96; 93, tuple; 93, 94; 93, 95; 94, identifier:KeyError; 95, identifier:TypeError; 96, block; 96, 97; 97, for_statement; 97, 98; 97, 99; 97, 100; 98, identifier:factory; 99, identifier:_VALIDATOR_FACTORIES; 100, block; 100, 101; 100, 108; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:validator; 104, call; 104, 105; 104, 106; 105, identifier:factory; 106, argument_list; 106, 107; 107, identifier:obj; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:validator; 111, None; 112, block; 112, 113; 113, break_statement; 114, else_clause; 114, 115; 115, block; 115, 116; 116, if_statement; 116, 117; 116, 129; 117, boolean_operator:and; 117, 118; 117, 124; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:inspect; 121, identifier:isclass; 122, argument_list; 122, 123; 123, identifier:validator; 124, call; 124, 125; 124, 126; 125, identifier:issubclass; 126, argument_list; 126, 127; 126, 128; 127, identifier:validator; 128, identifier:Validator; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:_NAMED_VALIDATORS; 134, identifier:obj; 135, assignment; 135, 136; 135, 137; 136, identifier:validator; 137, call; 137, 138; 137, 139; 138, identifier:validator; 139, argument_list; 140, if_statement; 140, 141; 140, 147; 141, not_operator; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:isinstance; 144, argument_list; 144, 145; 144, 146; 145, identifier:validator; 146, identifier:Validator; 147, block; 147, 148; 148, raise_statement; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:SchemaError; 151, argument_list; 151, 152; 152, binary_operator:%; 152, 153; 152, 154; 153, string:"%r cannot be parsed as a Validator"; 154, identifier:obj; 155, return_statement; 155, 156; 156, identifier:validator | def parse(obj, required_properties=None, additional_properties=None,
ignore_optional_property_errors=None):
"""Try to parse the given ``obj`` as a validator instance.
:param obj: The object to be parsed. If it is a...:
- :py:class:`Validator` instance, return it.
- :py:class:`Validator` subclass, instantiate it without arguments and
return it.
- :py:attr:`~Validator.name` of a known :py:class:`Validator` subclass,
instantiate the subclass without arguments and return it.
- otherwise find the first registered :py:class:`Validator` factory that
can create it. The search order is the reverse of the factory registration
order. The caller is responsible for ensuring there are no ambiguous
values that can be parsed by more than one factory.
:param required_properties: Specifies for this parse call whether parsed
:py:class:`~valideer.validators.Object` properties are required or
optional by default. It can be:
- ``True`` for required.
- ``False`` for optional.
- ``None`` to use the value of the
:py:attr:`~valideer.validators.Object.REQUIRED_PROPERTIES` attribute.
:param additional_properties: Specifies for this parse call the schema of
all :py:class:`~valideer.validators.Object` properties that are not
explicitly defined as optional or required. It can also be:
- ``True`` to allow any value for additional properties.
- ``False`` to disallow any additional properties.
- :py:attr:`~valideer.validators.Object.REMOVE` to remove any additional
properties from the adapted object.
- ``None`` to use the value of the
:py:attr:`~valideer.validators.Object.ADDITIONAL_PROPERTIES` attribute.
:param ignore_optional_property_errors: Determines if invalid optional
properties are ignored:
- ``True`` to ignore invalid optional properties.
- ``False`` to raise ValidationError for invalid optional properties.
- ``None`` to use the value of the
:py:attr:`~valideer.validators.Object.IGNORE_OPTIONAL_PROPERTY_ERRORS`
attribute.
:raises SchemaError: If no appropriate validator could be found.
.. warning:: Passing ``required_properties`` and/or ``additional_properties``
with value other than ``None`` may be non intuitive for schemas that
involve nested validators. Take for example the following schema::
v = V.parse({
"x": "integer",
"child": V.Nullable({
"y": "integer"
})
}, required_properties=True)
Here the top-level properties 'x' and 'child' are required but the nested
'y' property is not. This is because by the time :py:meth:`parse` is called,
:py:class:`~valideer.validators.Nullable` has already parsed its argument
with the default value of ``required_properties``. Several other builtin
validators work similarly to :py:class:`~valideer.validators.Nullable`,
accepting one or more schemas to parse. In order to parse an arbitrarily
complex nested validator with the same value for ``required_properties``
and/or ``additional_properties``, use the :py:func:`parsing` context
manager instead::
with V.parsing(required_properties=True):
v = V.parse({
"x": "integer",
"child": V.Nullable({
"y": "integer"
})
})
"""
if not (required_properties is
additional_properties is
ignore_optional_property_errors is None):
with parsing(required_properties=required_properties,
additional_properties=additional_properties,
ignore_optional_property_errors=ignore_optional_property_errors):
return parse(obj)
validator = None
if isinstance(obj, Validator):
validator = obj
elif inspect.isclass(obj) and issubclass(obj, Validator):
validator = obj()
else:
try:
validator = _NAMED_VALIDATORS[obj]
except (KeyError, TypeError):
for factory in _VALIDATOR_FACTORIES:
validator = factory(obj)
if validator is not None:
break
else:
if inspect.isclass(validator) and issubclass(validator, Validator):
_NAMED_VALIDATORS[obj] = validator = validator()
if not isinstance(validator, Validator):
raise SchemaError("%r cannot be parsed as a Validator" % obj)
return validator |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:adapts; 3, parameters; 3, 4; 4, dictionary_splat_pattern; 4, 5; 5, identifier:schemas; 6, block; 6, 7; 6, 9; 6, 18; 6, 147; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:validate; 12, attribute; 12, 13; 12, 17; 13, call; 13, 14; 13, 15; 14, identifier:parse; 15, argument_list; 15, 16; 16, identifier:schemas; 17, identifier:validate; 18, decorated_definition; 18, 19; 18, 21; 19, decorator; 19, 20; 20, identifier:decorator; 21, function_definition; 21, 22; 21, 23; 21, 29; 22, function_name:adapting; 23, parameters; 23, 24; 23, 25; 23, 27; 24, identifier:func; 25, list_splat_pattern; 25, 26; 26, identifier:args; 27, dictionary_splat_pattern; 27, 28; 28, identifier:kwargs; 29, block; 29, 30; 29, 49; 29, 58; 29, 75; 29, 87; 29, 99; 29, 120; 29, 132; 29, 139; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:adapted; 33, call; 33, 34; 33, 35; 34, identifier:validate; 35, argument_list; 35, 36; 35, 46; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:inspect; 39, identifier:getcallargs; 40, argument_list; 40, 41; 40, 42; 40, 44; 41, identifier:func; 42, list_splat; 42, 43; 43, identifier:args; 44, dictionary_splat; 44, 45; 45, identifier:kwargs; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:adapt; 48, True; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:argspec; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:inspect; 55, identifier:getargspec; 56, argument_list; 56, 57; 57, identifier:func; 58, if_statement; 58, 59; 58, 67; 58, 68; 59, comparison_operator:is; 59, 60; 59, 63; 59, 66; 60, attribute; 60, 61; 60, 62; 61, identifier:argspec; 62, identifier:varargs; 63, attribute; 63, 64; 63, 65; 64, identifier:argspec; 65, identifier:keywords; 66, None; 67, comment; 68, block; 68, 69; 69, return_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:func; 72, argument_list; 72, 73; 73, dictionary_splat; 73, 74; 74, identifier:adapted; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:adapted_varargs; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:adapted; 81, identifier:pop; 82, argument_list; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:argspec; 85, identifier:varargs; 86, tuple; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:adapted_keywords; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:adapted; 93, identifier:pop; 94, argument_list; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:argspec; 97, identifier:keywords; 98, dictionary; 99, if_statement; 99, 100; 99, 102; 99, 103; 100, not_operator; 100, 101; 101, identifier:adapted_varargs; 102, comment; 103, block; 103, 104; 103, 114; 104, if_statement; 104, 105; 104, 106; 105, identifier:adapted_keywords; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:adapted; 111, identifier:update; 112, argument_list; 112, 113; 113, identifier:adapted_keywords; 114, return_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:func; 117, argument_list; 117, 118; 118, dictionary_splat; 118, 119; 119, identifier:adapted; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:adapted_posargs; 123, list_comprehension; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:adapted; 126, identifier:arg; 127, for_in_clause; 127, 128; 127, 129; 128, identifier:arg; 129, attribute; 129, 130; 129, 131; 130, identifier:argspec; 131, identifier:args; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:adapted_posargs; 136, identifier:extend; 137, argument_list; 137, 138; 138, identifier:adapted_varargs; 139, return_statement; 139, 140; 140, call; 140, 141; 140, 142; 141, identifier:func; 142, argument_list; 142, 143; 142, 145; 143, list_splat; 143, 144; 144, identifier:adapted_posargs; 145, dictionary_splat; 145, 146; 146, identifier:adapted_keywords; 147, return_statement; 147, 148; 148, identifier:adapting | def adapts(**schemas):
"""Create a decorator for validating and adapting function parameters.
Example::
@adapts(a="number", body={"+field_ids": [V.AdaptTo(int)], "is_ok": bool})
def f(a, body):
print (a, body.field_ids, body.is_ok)
:param schemas: The schema for adapting a given parameter.
"""
validate = parse(schemas).validate
@decorator
def adapting(func, *args, **kwargs):
adapted = validate(inspect.getcallargs(func, *args, **kwargs), adapt=True)
argspec = inspect.getargspec(func)
if argspec.varargs is argspec.keywords is None:
# optimization for the common no varargs, no keywords case
return func(**adapted)
adapted_varargs = adapted.pop(argspec.varargs, ())
adapted_keywords = adapted.pop(argspec.keywords, {})
if not adapted_varargs: # keywords only
if adapted_keywords:
adapted.update(adapted_keywords)
return func(**adapted)
adapted_posargs = [adapted[arg] for arg in argspec.args]
adapted_posargs.extend(adapted_varargs)
return func(*adapted_posargs, **adapted_keywords)
return adapting |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 27; 2, function_name:download; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 17; 3, 21; 3, 24; 4, identifier:self; 5, identifier:bundle_uuid; 6, identifier:replica; 7, default_parameter; 7, 8; 7, 9; 8, identifier:version; 9, string:""; 10, default_parameter; 10, 11; 10, 12; 11, identifier:download_dir; 12, string:""; 13, default_parameter; 13, 14; 13, 15; 14, identifier:metadata_files; 15, tuple; 15, 16; 16, string:'*'; 17, default_parameter; 17, 18; 17, 19; 18, identifier:data_files; 19, tuple; 19, 20; 20, string:'*'; 21, default_parameter; 21, 22; 21, 23; 22, identifier:num_retries; 23, integer:10; 24, default_parameter; 24, 25; 24, 26; 25, identifier:min_delay_seconds; 26, float:0.25; 27, block; 27, 28; 27, 30; 27, 34; 27, 134; 28, expression_statement; 28, 29; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:errors; 33, integer:0; 34, with_statement; 34, 35; 34, 50; 35, with_clause; 35, 36; 36, with_item; 36, 37; 37, as_pattern; 37, 38; 37, 48; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:concurrent; 42, identifier:futures; 43, identifier:ThreadPoolExecutor; 44, argument_list; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:threads; 48, as_pattern_target; 48, 49; 49, identifier:executor; 50, block; 50, 51; 50, 80; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:futures_to_dss_file; 54, dictionary_comprehension; 54, 55; 54, 63; 55, pair; 55, 56; 55, 62; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:executor; 59, identifier:submit; 60, argument_list; 60, 61; 61, identifier:task; 62, identifier:dss_file; 63, for_in_clause; 63, 64; 63, 67; 64, pattern_list; 64, 65; 64, 66; 65, identifier:dss_file; 66, identifier:task; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_download_tasks; 71, argument_list; 71, 72; 71, 73; 71, 74; 71, 75; 71, 76; 71, 77; 71, 78; 71, 79; 72, identifier:bundle_uuid; 73, identifier:replica; 74, identifier:version; 75, identifier:download_dir; 76, identifier:metadata_files; 77, identifier:data_files; 78, identifier:num_retries; 79, identifier:min_delay_seconds; 80, for_statement; 80, 81; 80, 82; 80, 90; 81, identifier:future; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:concurrent; 86, identifier:futures; 87, identifier:as_completed; 88, argument_list; 88, 89; 89, identifier:futures_to_dss_file; 90, block; 90, 91; 90, 97; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:dss_file; 94, subscript; 94, 95; 94, 96; 95, identifier:futures_to_dss_file; 96, identifier:future; 97, try_statement; 97, 98; 97, 105; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:future; 103, identifier:result; 104, argument_list; 105, except_clause; 105, 106; 105, 110; 106, as_pattern; 106, 107; 106, 108; 107, identifier:Exception; 108, as_pattern_target; 108, 109; 109, identifier:e; 110, block; 110, 111; 110, 115; 111, expression_statement; 111, 112; 112, augmented_assignment:+=; 112, 113; 112, 114; 113, identifier:errors; 114, integer:1; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:logger; 119, identifier:warning; 120, argument_list; 120, 121; 120, 122; 120, 125; 120, 128; 120, 131; 121, string:'Failed to download file %s version %s from replica %s'; 122, attribute; 122, 123; 122, 124; 123, identifier:dss_file; 124, identifier:uuid; 125, attribute; 125, 126; 125, 127; 126, identifier:dss_file; 127, identifier:version; 128, attribute; 128, 129; 128, 130; 129, identifier:dss_file; 130, identifier:replica; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:exc_info; 133, identifier:e; 134, if_statement; 134, 135; 134, 136; 135, identifier:errors; 136, block; 136, 137; 137, raise_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:RuntimeError; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, string:'{} file(s) failed to download'; 144, identifier:format; 145, argument_list; 145, 146; 146, identifier:errors | def download(self, bundle_uuid, replica, version="", download_dir="",
metadata_files=('*',), data_files=('*',),
num_retries=10, min_delay_seconds=0.25):
"""
Download a bundle and save it to the local filesystem as a directory.
:param str bundle_uuid: The uuid of the bundle to download
:param str replica: the replica to download from. The supported replicas are: `aws` for Amazon Web Services, and
`gcp` for Google Cloud Platform. [aws, gcp]
:param str version: The version to download, else if not specified, download the latest. The version is a
timestamp of bundle creation in RFC3339
:param str dest_name: The destination file path for the download
:param iterable metadata_files: one or more shell patterns against which all metadata files in the bundle will be
matched case-sensitively. A file is considered a metadata file if the `indexed` property in the manifest is
set. If and only if a metadata file matches any of the patterns in `metadata_files` will it be downloaded.
:param iterable data_files: one or more shell patterns against which all data files in the bundle will be matched
case-sensitively. A file is considered a data file if the `indexed` property in the manifest is not set. The
file will be downloaded only if a data file matches any of the patterns in `data_files` will it be
downloaded.
:param int num_retries: The initial quota of download failures to accept before exiting due to
failures. The number of retries increase and decrease as file chucks succeed and fail.
:param float min_delay_seconds: The minimum number of seconds to wait in between retries.
Download a bundle and save it to the local filesystem as a directory.
By default, all data and metadata files are downloaded. To disable the downloading of data files,
use `--data-files ''` if using the CLI (or `data_files=()` if invoking `download` programmatically).
Likewise for metadata files.
If a retryable exception occurs, we wait a bit and retry again. The delay increases each time we fail and
decreases each time we successfully read a block. We set a quota for the number of failures that goes up with
every successful block read and down with each failure.
"""
errors = 0
with concurrent.futures.ThreadPoolExecutor(self.threads) as executor:
futures_to_dss_file = {executor.submit(task): dss_file
for dss_file, task in self._download_tasks(bundle_uuid,
replica,
version,
download_dir,
metadata_files,
data_files,
num_retries,
min_delay_seconds)}
for future in concurrent.futures.as_completed(futures_to_dss_file):
dss_file = futures_to_dss_file[future]
try:
future.result()
except Exception as e:
errors += 1
logger.warning('Failed to download file %s version %s from replica %s',
dss_file.uuid, dss_file.version, dss_file.replica, exc_info=e)
if errors:
raise RuntimeError('{} file(s) failed to download'.format(errors)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_do_download_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:self; 5, identifier:dss_file; 6, identifier:fh; 7, identifier:num_retries; 8, identifier:min_delay_seconds; 9, block; 9, 10; 9, 12; 9, 20; 9, 24; 9, 28; 9, 385; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:hasher; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:hashlib; 18, identifier:sha256; 19, argument_list; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:delay; 23, identifier:min_delay_seconds; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:retries_left; 27, identifier:num_retries; 28, while_statement; 28, 29; 28, 30; 29, True; 30, block; 30, 31; 31, try_statement; 31, 32; 31, 342; 32, block; 32, 33; 32, 79; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:response; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:get_file; 41, identifier:_request; 42, argument_list; 42, 43; 42, 61; 42, 64; 43, call; 43, 44; 43, 45; 44, identifier:dict; 45, argument_list; 45, 46; 45, 51; 45, 56; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:uuid; 48, attribute; 48, 49; 48, 50; 49, identifier:dss_file; 50, identifier:uuid; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:version; 53, attribute; 53, 54; 53, 55; 54, identifier:dss_file; 55, identifier:version; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:replica; 58, attribute; 58, 59; 58, 60; 59, identifier:dss_file; 60, identifier:replica; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:stream; 63, True; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:headers; 66, dictionary; 66, 67; 67, pair; 67, 68; 67, 69; 68, string:'Range'; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, string:"bytes={}-"; 72, identifier:format; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:fh; 77, identifier:tell; 78, argument_list; 79, try_statement; 79, 80; 79, 334; 80, block; 80, 81; 80, 118; 80, 129; 80, 133; 80, 145; 80, 185; 80, 189; 80, 193; 80, 283; 80, 333; 81, if_statement; 81, 82; 81, 86; 82, not_operator; 82, 83; 83, attribute; 83, 84; 83, 85; 84, identifier:response; 85, identifier:ok; 86, block; 86, 87; 86, 102; 86, 117; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:logger; 91, identifier:error; 92, argument_list; 92, 93; 92, 94; 93, string:"%s"; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:"File {}: GET FAILED."; 97, identifier:format; 98, argument_list; 98, 99; 99, attribute; 99, 100; 99, 101; 100, identifier:dss_file; 101, identifier:uuid; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:logger; 106, identifier:error; 107, argument_list; 107, 108; 107, 109; 108, string:"%s"; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, string:"Response: {}"; 112, identifier:format; 113, argument_list; 113, 114; 114, attribute; 114, 115; 114, 116; 115, identifier:response; 116, identifier:text; 117, break_statement; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:consume_bytes; 121, call; 121, 122; 121, 123; 122, identifier:int; 123, argument_list; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:fh; 127, identifier:tell; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:server_start; 132, integer:0; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:content_range_header; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:response; 140, identifier:headers; 141, identifier:get; 142, argument_list; 142, 143; 142, 144; 143, string:'Content-Range'; 144, None; 145, if_statement; 145, 146; 145, 149; 146, comparison_operator:is; 146, 147; 146, 148; 147, identifier:content_range_header; 148, None; 149, block; 149, 150; 149, 159; 149, 168; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:cre; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:re; 156, identifier:compile; 157, argument_list; 157, 158; 158, string:"bytes (\d+)-(\d+)"; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:mo; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:cre; 165, identifier:search; 166, argument_list; 166, 167; 167, identifier:content_range_header; 168, if_statement; 168, 169; 168, 172; 169, comparison_operator:is; 169, 170; 169, 171; 170, identifier:mo; 171, None; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:server_start; 176, call; 176, 177; 176, 178; 177, identifier:int; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:mo; 182, identifier:group; 183, argument_list; 183, 184; 184, integer:1; 185, expression_statement; 185, 186; 186, augmented_assignment:-=; 186, 187; 186, 188; 187, identifier:consume_bytes; 188, identifier:server_start; 189, assert_statement; 189, 190; 190, comparison_operator:>=; 190, 191; 190, 192; 191, identifier:consume_bytes; 192, integer:0; 193, if_statement; 193, 194; 193, 201; 193, 218; 194, boolean_operator:and; 194, 195; 194, 198; 195, comparison_operator:>; 195, 196; 195, 197; 196, identifier:server_start; 197, integer:0; 198, comparison_operator:==; 198, 199; 198, 200; 199, identifier:consume_bytes; 200, integer:0; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:logger; 206, identifier:info; 207, argument_list; 207, 208; 207, 209; 208, string:"%s"; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, string:"File {}: Resuming at {}."; 212, identifier:format; 213, argument_list; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:dss_file; 216, identifier:uuid; 217, identifier:server_start; 218, elif_clause; 218, 219; 218, 222; 219, comparison_operator:>; 219, 220; 219, 221; 220, identifier:consume_bytes; 221, integer:0; 222, block; 222, 223; 222, 240; 223, expression_statement; 223, 224; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:logger; 227, identifier:info; 228, argument_list; 228, 229; 228, 230; 229, string:"%s"; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, string:"File {}: Resuming at {}. Dropping {} bytes to match"; 233, identifier:format; 234, argument_list; 234, 235; 234, 238; 234, 239; 235, attribute; 235, 236; 235, 237; 236, identifier:dss_file; 237, identifier:uuid; 238, identifier:server_start; 239, identifier:consume_bytes; 240, while_statement; 240, 241; 240, 244; 241, comparison_operator:>; 241, 242; 241, 243; 242, identifier:consume_bytes; 243, integer:0; 244, block; 244, 245; 244, 255; 244, 266; 244, 273; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:bytes_to_read; 248, call; 248, 249; 248, 250; 249, identifier:min; 250, argument_list; 250, 251; 250, 252; 251, identifier:consume_bytes; 252, binary_operator:*; 252, 253; 252, 254; 253, integer:1024; 254, integer:1024; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 258; 257, identifier:content; 258, call; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:response; 261, identifier:iter_content; 262, argument_list; 262, 263; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:chunk_size; 265, identifier:bytes_to_read; 266, expression_statement; 266, 267; 267, assignment; 267, 268; 267, 269; 268, identifier:chunk; 269, call; 269, 270; 269, 271; 270, identifier:next; 271, argument_list; 271, 272; 272, identifier:content; 273, if_statement; 273, 274; 273, 275; 274, identifier:chunk; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, augmented_assignment:-=; 277, 278; 277, 279; 278, identifier:consume_bytes; 279, call; 279, 280; 279, 281; 280, identifier:len; 281, argument_list; 281, 282; 282, identifier:chunk; 283, for_statement; 283, 284; 283, 285; 283, 295; 284, identifier:chunk; 285, call; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:response; 288, identifier:iter_content; 289, argument_list; 289, 290; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:chunk_size; 292, binary_operator:*; 292, 293; 292, 294; 293, integer:1024; 294, integer:1024; 295, block; 295, 296; 296, if_statement; 296, 297; 296, 298; 297, identifier:chunk; 298, block; 298, 299; 298, 306; 298, 313; 298, 323; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:fh; 303, identifier:write; 304, argument_list; 304, 305; 305, identifier:chunk; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:hasher; 310, identifier:update; 311, argument_list; 311, 312; 312, identifier:chunk; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:retries_left; 316, call; 316, 317; 316, 318; 317, identifier:min; 318, argument_list; 318, 319; 318, 322; 319, binary_operator:+; 319, 320; 319, 321; 320, identifier:retries_left; 321, integer:1; 322, identifier:num_retries; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:delay; 326, call; 326, 327; 326, 328; 327, identifier:max; 328, argument_list; 328, 329; 328, 332; 329, binary_operator:/; 329, 330; 329, 331; 330, identifier:delay; 331, integer:2; 332, identifier:min_delay_seconds; 333, break_statement; 334, finally_clause; 334, 335; 335, block; 335, 336; 336, expression_statement; 336, 337; 337, call; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:response; 340, identifier:close; 341, argument_list; 342, except_clause; 342, 343; 342, 347; 343, tuple; 343, 344; 343, 345; 343, 346; 344, identifier:ChunkedEncodingError; 345, identifier:ConnectionError; 346, identifier:ReadTimeout; 347, block; 347, 348; 347, 384; 348, if_statement; 348, 349; 348, 352; 349, comparison_operator:>; 349, 350; 349, 351; 350, identifier:retries_left; 351, integer:0; 352, block; 352, 353; 352, 368; 352, 375; 352, 379; 352, 383; 353, expression_statement; 353, 354; 354, call; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:logger; 357, identifier:info; 358, argument_list; 358, 359; 358, 360; 359, string:"%s"; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, string:"File {}: GET FAILED. Attempting to resume."; 363, identifier:format; 364, argument_list; 364, 365; 365, attribute; 365, 366; 365, 367; 366, identifier:dss_file; 367, identifier:uuid; 368, expression_statement; 368, 369; 369, call; 369, 370; 369, 373; 370, attribute; 370, 371; 370, 372; 371, identifier:time; 372, identifier:sleep; 373, argument_list; 373, 374; 374, identifier:delay; 375, expression_statement; 375, 376; 376, augmented_assignment:*=; 376, 377; 376, 378; 377, identifier:delay; 378, integer:2; 379, expression_statement; 379, 380; 380, augmented_assignment:-=; 380, 381; 380, 382; 381, identifier:retries_left; 382, integer:1; 383, continue_statement; 384, raise_statement; 385, return_statement; 385, 386; 386, call; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:hasher; 389, identifier:hexdigest; 390, argument_list | def _do_download_file(self, dss_file, fh, num_retries, min_delay_seconds):
"""
Abstracts away complications for downloading a file, handles retries and delays, and computes its hash
"""
hasher = hashlib.sha256()
delay = min_delay_seconds
retries_left = num_retries
while True:
try:
response = self.get_file._request(
dict(uuid=dss_file.uuid, version=dss_file.version, replica=dss_file.replica),
stream=True,
headers={
'Range': "bytes={}-".format(fh.tell())
},
)
try:
if not response.ok:
logger.error("%s", "File {}: GET FAILED.".format(dss_file.uuid))
logger.error("%s", "Response: {}".format(response.text))
break
consume_bytes = int(fh.tell())
server_start = 0
content_range_header = response.headers.get('Content-Range', None)
if content_range_header is not None:
cre = re.compile("bytes (\d+)-(\d+)")
mo = cre.search(content_range_header)
if mo is not None:
server_start = int(mo.group(1))
consume_bytes -= server_start
assert consume_bytes >= 0
if server_start > 0 and consume_bytes == 0:
logger.info("%s", "File {}: Resuming at {}.".format(
dss_file.uuid, server_start))
elif consume_bytes > 0:
logger.info("%s", "File {}: Resuming at {}. Dropping {} bytes to match".format(
dss_file.uuid, server_start, consume_bytes))
while consume_bytes > 0:
bytes_to_read = min(consume_bytes, 1024*1024)
content = response.iter_content(chunk_size=bytes_to_read)
chunk = next(content)
if chunk:
consume_bytes -= len(chunk)
for chunk in response.iter_content(chunk_size=1024*1024):
if chunk:
fh.write(chunk)
hasher.update(chunk)
retries_left = min(retries_left + 1, num_retries)
delay = max(delay / 2, min_delay_seconds)
break
finally:
response.close()
except (ChunkedEncodingError, ConnectionError, ReadTimeout):
if retries_left > 0:
logger.info("%s", "File {}: GET FAILED. Attempting to resume.".format(dss_file.uuid))
time.sleep(delay)
delay *= 2
retries_left -= 1
continue
raise
return hasher.hexdigest() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:output; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:output; 6, default_parameter; 6, 7; 6, 8; 7, identifier:status; 8, None; 9, block; 9, 10; 9, 12; 9, 194; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 14; 13, identifier:output; 14, block; 14, 15; 14, 27; 14, 36; 14, 40; 14, 44; 14, 56; 14, 162; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:size; 18, call; 18, 19; 18, 26; 19, attribute; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:cli; 24, identifier:output; 25, identifier:get_size; 26, argument_list; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:margin; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:get_output_margin; 34, argument_list; 34, 35; 35, identifier:status; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:fits; 39, True; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:buf; 43, list:[]; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:output_via_pager; 47, boolean_operator:and; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:explicit_pager; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:special; 54, identifier:is_pager_enabled; 55, argument_list; 56, for_statement; 56, 57; 56, 60; 56, 65; 57, pattern_list; 57, 58; 57, 59; 58, identifier:i; 59, identifier:line; 60, call; 60, 61; 60, 62; 61, identifier:enumerate; 62, argument_list; 62, 63; 62, 64; 63, identifier:output; 64, integer:1; 65, block; 65, 66; 65, 73; 65, 80; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:special; 70, identifier:write_tee; 71, argument_list; 71, 72; 72, identifier:line; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:special; 77, identifier:write_once; 78, argument_list; 78, 79; 79, identifier:line; 80, if_statement; 80, 81; 80, 84; 80, 85; 80, 153; 81, boolean_operator:or; 81, 82; 81, 83; 82, identifier:fits; 83, identifier:output_via_pager; 84, comment; 85, block; 85, 86; 85, 93; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:buf; 90, identifier:append; 91, argument_list; 91, 92; 92, identifier:line; 93, if_statement; 93, 94; 93, 111; 94, boolean_operator:or; 94, 95; 94, 103; 95, comparison_operator:>; 95, 96; 95, 100; 96, call; 96, 97; 96, 98; 97, identifier:len; 98, argument_list; 98, 99; 99, identifier:line; 100, attribute; 100, 101; 100, 102; 101, identifier:size; 102, identifier:columns; 103, comparison_operator:>; 103, 104; 103, 105; 104, identifier:i; 105, parenthesized_expression; 105, 106; 106, binary_operator:-; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:size; 109, identifier:rows; 110, identifier:margin; 111, block; 111, 112; 111, 116; 111, 133; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:fits; 115, False; 116, if_statement; 116, 117; 116, 127; 116, 128; 117, boolean_operator:and; 117, 118; 117, 122; 118, not_operator; 118, 119; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:explicit_pager; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:special; 125, identifier:is_pager_enabled; 126, argument_list; 127, comment; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:output_via_pager; 132, True; 133, if_statement; 133, 134; 133, 136; 133, 137; 134, not_operator; 134, 135; 135, identifier:output_via_pager; 136, comment; 137, block; 137, 138; 137, 149; 138, for_statement; 138, 139; 138, 140; 138, 141; 139, identifier:line; 140, identifier:buf; 141, block; 141, 142; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:click; 146, identifier:secho; 147, argument_list; 147, 148; 148, identifier:line; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:buf; 152, list:[]; 153, else_clause; 153, 154; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:click; 159, identifier:secho; 160, argument_list; 160, 161; 161, identifier:line; 162, if_statement; 162, 163; 162, 164; 163, identifier:buf; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 167; 165, 168; 165, 181; 166, identifier:output_via_pager; 167, comment; 168, block; 168, 169; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:click; 173, identifier:echo_via_pager; 174, argument_list; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, string:"\n"; 178, identifier:join; 179, argument_list; 179, 180; 180, identifier:buf; 181, else_clause; 181, 182; 182, block; 182, 183; 183, for_statement; 183, 184; 183, 185; 183, 186; 184, identifier:line; 185, identifier:buf; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:click; 191, identifier:secho; 192, argument_list; 192, 193; 193, identifier:line; 194, if_statement; 194, 195; 194, 196; 195, identifier:status; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:click; 201, identifier:secho; 202, argument_list; 202, 203; 203, identifier:status | def output(self, output, status=None):
"""Output text to stdout or a pager command.
The status text is not outputted to pager or files.
The message will be logged in the audit log, if enabled. The
message will be written to the tee file, if enabled. The
message will be written to the output file, if enabled.
"""
if output:
size = self.cli.output.get_size()
margin = self.get_output_margin(status)
fits = True
buf = []
output_via_pager = self.explicit_pager and special.is_pager_enabled()
for i, line in enumerate(output, 1):
special.write_tee(line)
special.write_once(line)
if fits or output_via_pager:
# buffering
buf.append(line)
if len(line) > size.columns or i > (size.rows - margin):
fits = False
if not self.explicit_pager and special.is_pager_enabled():
# doesn't fit, use pager
output_via_pager = True
if not output_via_pager:
# doesn't fit, flush buffer
for line in buf:
click.secho(line)
buf = []
else:
click.secho(line)
if buf:
if output_via_pager:
# sadly click.echo_via_pager doesn't accept generators
click.echo_via_pager("\n".join(buf))
else:
for line in buf:
click.secho(line)
if status:
click.secho(status) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:find_matches; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:text; 5, identifier:collection; 6, default_parameter; 6, 7; 6, 8; 7, identifier:start_only; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:fuzzy; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:casing; 14, None; 15, block; 15, 16; 15, 18; 15, 28; 15, 36; 15, 40; 15, 161; 15, 182; 15, 204; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:last; 21, call; 21, 22; 21, 23; 22, identifier:last_word; 23, argument_list; 23, 24; 23, 25; 24, identifier:text; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:include; 27, string:'most_punctuations'; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:text; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:last; 34, identifier:lower; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:completions; 39, list:[]; 40, if_statement; 40, 41; 40, 42; 40, 109; 41, identifier:fuzzy; 42, block; 42, 43; 42, 56; 42, 65; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:regex; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, string:'.*?'; 49, identifier:join; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:map; 53, argument_list; 53, 54; 53, 55; 54, identifier:escape; 55, identifier:text; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:pat; 59, call; 59, 60; 59, 61; 60, identifier:compile; 61, argument_list; 61, 62; 62, binary_operator:%; 62, 63; 62, 64; 63, string:'(%s)'; 64, identifier:regex; 65, for_statement; 65, 66; 65, 67; 65, 71; 66, identifier:item; 67, call; 67, 68; 67, 69; 68, identifier:sorted; 69, argument_list; 69, 70; 70, identifier:collection; 71, block; 71, 72; 71, 85; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:r; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:pat; 78, identifier:search; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:item; 83, identifier:lower; 84, argument_list; 85, if_statement; 85, 86; 85, 87; 86, identifier:r; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:completions; 92, identifier:append; 93, argument_list; 93, 94; 94, tuple; 94, 95; 94, 103; 94, 108; 95, call; 95, 96; 95, 97; 96, identifier:len; 97, argument_list; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:r; 101, identifier:group; 102, argument_list; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:r; 106, identifier:start; 107, argument_list; 108, identifier:item; 109, else_clause; 109, 110; 110, block; 110, 111; 110, 121; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:match_end_limit; 114, conditional_expression:if; 114, 115; 114, 119; 114, 120; 115, call; 115, 116; 115, 117; 116, identifier:len; 117, argument_list; 117, 118; 118, identifier:text; 119, identifier:start_only; 120, None; 121, for_statement; 121, 122; 121, 123; 121, 127; 122, identifier:item; 123, call; 123, 124; 123, 125; 124, identifier:sorted; 125, argument_list; 125, 126; 126, identifier:collection; 127, block; 127, 128; 127, 143; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:match_point; 131, call; 131, 132; 131, 139; 132, attribute; 132, 133; 132, 138; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:item; 136, identifier:lower; 137, argument_list; 138, identifier:find; 139, argument_list; 139, 140; 139, 141; 139, 142; 140, identifier:text; 141, integer:0; 142, identifier:match_end_limit; 143, if_statement; 143, 144; 143, 147; 144, comparison_operator:>=; 144, 145; 144, 146; 145, identifier:match_point; 146, integer:0; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:completions; 152, identifier:append; 153, argument_list; 153, 154; 154, tuple; 154, 155; 154, 159; 154, 160; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, identifier:text; 159, identifier:match_point; 160, identifier:item; 161, if_statement; 161, 162; 161, 165; 162, comparison_operator:==; 162, 163; 162, 164; 163, identifier:casing; 164, string:'auto'; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:casing; 169, conditional_expression:if; 169, 170; 169, 171; 169, 181; 170, string:'lower'; 171, boolean_operator:and; 171, 172; 171, 173; 172, identifier:last; 173, call; 173, 174; 173, 180; 174, attribute; 174, 175; 174, 179; 175, subscript; 175, 176; 175, 177; 176, identifier:last; 177, unary_operator:-; 177, 178; 178, integer:1; 179, identifier:islower; 180, argument_list; 181, string:'upper'; 182, function_definition; 182, 183; 182, 184; 182, 186; 183, function_name:apply_case; 184, parameters; 184, 185; 185, identifier:kw; 186, block; 186, 187; 186, 198; 187, if_statement; 187, 188; 187, 191; 188, comparison_operator:==; 188, 189; 188, 190; 189, identifier:casing; 190, string:'upper'; 191, block; 191, 192; 192, return_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:kw; 196, identifier:upper; 197, argument_list; 198, return_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:kw; 202, identifier:lower; 203, argument_list; 204, return_statement; 204, 205; 205, generator_expression; 205, 206; 205, 223; 206, call; 206, 207; 206, 208; 207, identifier:Completion; 208, argument_list; 208, 209; 208, 218; 209, conditional_expression:if; 209, 210; 209, 211; 209, 214; 210, identifier:z; 211, comparison_operator:is; 211, 212; 211, 213; 212, identifier:casing; 213, None; 214, call; 214, 215; 214, 216; 215, identifier:apply_case; 216, argument_list; 216, 217; 217, identifier:z; 218, unary_operator:-; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:len; 221, argument_list; 221, 222; 222, identifier:text; 223, for_in_clause; 223, 224; 223, 228; 224, pattern_list; 224, 225; 224, 226; 224, 227; 225, identifier:x; 226, identifier:y; 227, identifier:z; 228, call; 228, 229; 228, 230; 229, identifier:sorted; 230, argument_list; 230, 231; 231, identifier:completions | def find_matches(text, collection, start_only=False, fuzzy=True, casing=None):
"""Find completion matches for the given text.
Given the user's input text and a collection of available
completions, find completions matching the last word of the
text.
If `start_only` is True, the text will match an available
completion only at the beginning. Otherwise, a completion is
considered a match if the text appears anywhere within it.
yields prompt_toolkit Completion instances for any matches found
in the collection of available completions.
"""
last = last_word(text, include='most_punctuations')
text = last.lower()
completions = []
if fuzzy:
regex = '.*?'.join(map(escape, text))
pat = compile('(%s)' % regex)
for item in sorted(collection):
r = pat.search(item.lower())
if r:
completions.append((len(r.group()), r.start(), item))
else:
match_end_limit = len(text) if start_only else None
for item in sorted(collection):
match_point = item.lower().find(text, 0, match_end_limit)
if match_point >= 0:
completions.append((len(text), match_point, item))
if casing == 'auto':
casing = 'lower' if last and last[-1].islower() else 'upper'
def apply_case(kw):
if casing == 'upper':
return kw.upper()
return kw.lower()
return (Completion(z if casing is None else apply_case(z), -len(text))
for x, y, z in sorted(completions)) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:cli_bindings; 3, parameters; 4, block; 4, 5; 4, 7; 4, 28; 4, 75; 4, 118; 4, 177; 4, 235; 4, 293; 4, 346; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:key_binding_manager; 10, call; 10, 11; 10, 12; 11, identifier:KeyBindingManager; 12, argument_list; 12, 13; 12, 16; 12, 19; 12, 22; 12, 25; 13, keyword_argument; 13, 14; 13, 15; 14, identifier:enable_open_in_editor; 15, True; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:enable_system_bindings; 18, True; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:enable_auto_suggest_bindings; 21, True; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:enable_search; 24, True; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:enable_abort_and_exit_bindings; 27, True; 28, decorated_definition; 28, 29; 28, 40; 29, decorator; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:key_binding_manager; 34, identifier:registry; 35, identifier:add_binding; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:Keys; 39, identifier:F2; 40, function_definition; 40, 41; 40, 42; 40, 44; 41, function_name:_; 42, parameters; 42, 43; 43, identifier:event; 44, block; 44, 45; 44, 47; 44, 54; 44, 62; 45, expression_statement; 45, 46; 46, comment; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:_logger; 51, identifier:debug; 52, argument_list; 52, 53; 53, string:'Detected F2 key.'; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:buf; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:event; 60, identifier:cli; 61, identifier:current_buffer; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:buf; 67, identifier:completer; 68, identifier:smart_completion; 69, not_operator; 69, 70; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:buf; 73, identifier:completer; 74, identifier:smart_completion; 75, decorated_definition; 75, 76; 75, 87; 76, decorator; 76, 77; 77, call; 77, 78; 77, 83; 78, attribute; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:key_binding_manager; 81, identifier:registry; 82, identifier:add_binding; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:Keys; 86, identifier:F3; 87, function_definition; 87, 88; 87, 89; 87, 91; 88, function_name:_; 89, parameters; 89, 90; 90, identifier:event; 91, block; 91, 92; 91, 94; 91, 101; 91, 109; 92, expression_statement; 92, 93; 93, comment; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:_logger; 98, identifier:debug; 99, argument_list; 99, 100; 100, string:'Detected F3 key.'; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:buf; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:event; 107, identifier:cli; 108, identifier:current_buffer; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:buf; 113, identifier:always_multiline; 114, not_operator; 114, 115; 115, attribute; 115, 116; 115, 117; 116, identifier:buf; 117, identifier:always_multiline; 118, decorated_definition; 118, 119; 118, 130; 119, decorator; 119, 120; 120, call; 120, 121; 120, 126; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:key_binding_manager; 124, identifier:registry; 125, identifier:add_binding; 126, argument_list; 126, 127; 127, attribute; 127, 128; 127, 129; 128, identifier:Keys; 129, identifier:F4; 130, function_definition; 130, 131; 130, 132; 130, 134; 131, function_name:_; 132, parameters; 132, 133; 133, identifier:event; 134, block; 134, 135; 134, 137; 134, 144; 135, expression_statement; 135, 136; 136, comment; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:_logger; 141, identifier:debug; 142, argument_list; 142, 143; 143, string:'Detected F4 key.'; 144, if_statement; 144, 145; 144, 154; 144, 165; 145, comparison_operator:==; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:event; 149, identifier:cli; 150, identifier:editing_mode; 151, attribute; 151, 152; 151, 153; 152, identifier:EditingMode; 153, identifier:VI; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:event; 160, identifier:cli; 161, identifier:editing_mode; 162, attribute; 162, 163; 162, 164; 163, identifier:EditingMode; 164, identifier:EMACS; 165, else_clause; 165, 166; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:event; 172, identifier:cli; 173, identifier:editing_mode; 174, attribute; 174, 175; 174, 176; 175, identifier:EditingMode; 176, identifier:VI; 177, decorated_definition; 177, 178; 177, 189; 178, decorator; 178, 179; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:key_binding_manager; 183, identifier:registry; 184, identifier:add_binding; 185, argument_list; 185, 186; 186, attribute; 186, 187; 186, 188; 187, identifier:Keys; 188, identifier:Tab; 189, function_definition; 189, 190; 189, 191; 189, 193; 190, function_name:_; 191, parameters; 191, 192; 192, identifier:event; 193, block; 193, 194; 193, 196; 193, 203; 193, 211; 194, expression_statement; 194, 195; 195, comment; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:_logger; 200, identifier:debug; 201, argument_list; 201, 202; 202, string:'Detected <Tab> key.'; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:b; 206, attribute; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:event; 209, identifier:cli; 210, identifier:current_buffer; 211, if_statement; 211, 212; 211, 215; 211, 222; 212, attribute; 212, 213; 212, 214; 213, identifier:b; 214, identifier:complete_state; 215, block; 215, 216; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:b; 220, identifier:complete_next; 221, argument_list; 222, else_clause; 222, 223; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 231; 226, attribute; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:event; 229, identifier:cli; 230, identifier:start_completion; 231, argument_list; 231, 232; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:select_first; 234, True; 235, decorated_definition; 235, 236; 235, 247; 236, decorator; 236, 237; 237, call; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:key_binding_manager; 241, identifier:registry; 242, identifier:add_binding; 243, argument_list; 243, 244; 244, attribute; 244, 245; 244, 246; 245, identifier:Keys; 246, identifier:ControlSpace; 247, function_definition; 247, 248; 247, 249; 247, 251; 248, function_name:_; 249, parameters; 249, 250; 250, identifier:event; 251, block; 251, 252; 251, 254; 251, 261; 251, 269; 252, expression_statement; 252, 253; 253, comment; 254, expression_statement; 254, 255; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:_logger; 258, identifier:debug; 259, argument_list; 259, 260; 260, string:'Detected <C-Space> key.'; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:b; 264, attribute; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:event; 267, identifier:cli; 268, identifier:current_buffer; 269, if_statement; 269, 270; 269, 273; 269, 280; 270, attribute; 270, 271; 270, 272; 271, identifier:b; 272, identifier:complete_state; 273, block; 273, 274; 274, expression_statement; 274, 275; 275, call; 275, 276; 275, 279; 276, attribute; 276, 277; 276, 278; 277, identifier:b; 278, identifier:complete_next; 279, argument_list; 280, else_clause; 280, 281; 281, block; 281, 282; 282, expression_statement; 282, 283; 283, call; 283, 284; 283, 289; 284, attribute; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:event; 287, identifier:cli; 288, identifier:start_completion; 289, argument_list; 289, 290; 290, keyword_argument; 290, 291; 290, 292; 291, identifier:select_first; 292, False; 293, decorated_definition; 293, 294; 293, 310; 294, decorator; 294, 295; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:key_binding_manager; 299, identifier:registry; 300, identifier:add_binding; 301, argument_list; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:Keys; 304, identifier:ControlJ; 305, keyword_argument; 305, 306; 305, 307; 306, identifier:filter; 307, call; 307, 308; 307, 309; 308, identifier:HasSelectedCompletion; 309, argument_list; 310, function_definition; 310, 311; 310, 312; 310, 314; 311, function_name:_; 312, parameters; 312, 313; 313, identifier:event; 314, block; 314, 315; 314, 317; 314, 324; 314, 332; 314, 340; 315, expression_statement; 315, 316; 316, comment; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:_logger; 321, identifier:debug; 322, argument_list; 322, 323; 323, string:'Detected <C-J> key.'; 324, expression_statement; 324, 325; 325, assignment; 325, 326; 325, 331; 326, attribute; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:event; 329, identifier:current_buffer; 330, identifier:complete_state; 331, None; 332, expression_statement; 332, 333; 333, assignment; 333, 334; 333, 335; 334, identifier:b; 335, attribute; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:event; 338, identifier:cli; 339, identifier:current_buffer; 340, expression_statement; 340, 341; 341, assignment; 341, 342; 341, 345; 342, attribute; 342, 343; 342, 344; 343, identifier:b; 344, identifier:complete_state; 345, None; 346, return_statement; 346, 347; 347, identifier:key_binding_manager | def cli_bindings():
"""
Custom key bindings for cli.
"""
key_binding_manager = KeyBindingManager(
enable_open_in_editor=True,
enable_system_bindings=True,
enable_auto_suggest_bindings=True,
enable_search=True,
enable_abort_and_exit_bindings=True)
@key_binding_manager.registry.add_binding(Keys.F2)
def _(event):
"""
Enable/Disable SmartCompletion Mode.
"""
_logger.debug('Detected F2 key.')
buf = event.cli.current_buffer
buf.completer.smart_completion = not buf.completer.smart_completion
@key_binding_manager.registry.add_binding(Keys.F3)
def _(event):
"""
Enable/Disable Multiline Mode.
"""
_logger.debug('Detected F3 key.')
buf = event.cli.current_buffer
buf.always_multiline = not buf.always_multiline
@key_binding_manager.registry.add_binding(Keys.F4)
def _(event):
"""
Toggle between Vi and Emacs mode.
"""
_logger.debug('Detected F4 key.')
if event.cli.editing_mode == EditingMode.VI:
event.cli.editing_mode = EditingMode.EMACS
else:
event.cli.editing_mode = EditingMode.VI
@key_binding_manager.registry.add_binding(Keys.Tab)
def _(event):
"""
Force autocompletion at cursor.
"""
_logger.debug('Detected <Tab> key.')
b = event.cli.current_buffer
if b.complete_state:
b.complete_next()
else:
event.cli.start_completion(select_first=True)
@key_binding_manager.registry.add_binding(Keys.ControlSpace)
def _(event):
"""
Initialize autocompletion at cursor.
If the autocompletion menu is not showing, display it with the
appropriate completions for the context.
If the menu is showing, select the next completion.
"""
_logger.debug('Detected <C-Space> key.')
b = event.cli.current_buffer
if b.complete_state:
b.complete_next()
else:
event.cli.start_completion(select_first=False)
@key_binding_manager.registry.add_binding(Keys.ControlJ, filter=HasSelectedCompletion())
def _(event):
"""
Makes the enter key work as the tab key only when showing the menu.
"""
_logger.debug('Detected <C-J> key.')
event.current_buffer.complete_state = None
b = event.cli.current_buffer
b.complete_state = None
return key_binding_manager |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_get_thumbnail_options; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:context; 6, identifier:instance; 7, block; 7, 8; 7, 10; 7, 18; 7, 22; 7, 32; 7, 42; 7, 57; 7, 82; 7, 93; 7, 209; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, pattern_list; 12, 13; 12, 14; 13, identifier:width; 14, identifier:height; 15, expression_list; 15, 16; 15, 17; 16, None; 17, None; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:subject_location; 21, False; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:placeholder_width; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:context; 28, identifier:get; 29, argument_list; 29, 30; 29, 31; 30, string:'width'; 31, None; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:placeholder_height; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:context; 38, identifier:get; 39, argument_list; 39, 40; 39, 41; 40, string:'height'; 41, None; 42, if_statement; 42, 43; 42, 48; 42, 49; 43, boolean_operator:and; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:instance; 46, identifier:use_autoscale; 47, identifier:placeholder_width; 48, comment; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:width; 53, call; 53, 54; 53, 55; 54, identifier:int; 55, argument_list; 55, 56; 56, identifier:placeholder_width; 57, if_statement; 57, 58; 57, 63; 57, 71; 58, boolean_operator:and; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:instance; 61, identifier:use_autoscale; 62, identifier:placeholder_height; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:height; 67, call; 67, 68; 67, 69; 68, identifier:int; 69, argument_list; 69, 70; 70, identifier:placeholder_height; 71, elif_clause; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:instance; 74, identifier:width; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:width; 79, attribute; 79, 80; 79, 81; 80, identifier:instance; 81, identifier:width; 82, if_statement; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:instance; 85, identifier:height; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:height; 90, attribute; 90, 91; 90, 92; 91, identifier:instance; 92, identifier:height; 93, if_statement; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:instance; 96, identifier:image; 97, block; 97, 98; 97, 113; 97, 148; 97, 183; 97, 196; 98, if_statement; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:instance; 102, identifier:image; 103, identifier:subject_location; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:subject_location; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:instance; 111, identifier:image; 112, identifier:subject_location; 113, if_statement; 113, 114; 113, 118; 113, 119; 114, boolean_operator:and; 114, 115; 114, 117; 115, not_operator; 115, 116; 116, identifier:height; 117, identifier:width; 118, comment; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:height; 123, call; 123, 124; 123, 125; 124, identifier:int; 125, argument_list; 125, 126; 126, binary_operator:/; 126, 127; 126, 140; 127, binary_operator:*; 127, 128; 127, 132; 128, call; 128, 129; 128, 130; 129, identifier:float; 130, argument_list; 130, 131; 131, identifier:width; 132, call; 132, 133; 132, 134; 133, identifier:float; 134, argument_list; 134, 135; 135, attribute; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:instance; 138, identifier:image; 139, identifier:height; 140, call; 140, 141; 140, 142; 141, identifier:float; 142, argument_list; 142, 143; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:instance; 146, identifier:image; 147, identifier:width; 148, if_statement; 148, 149; 148, 153; 148, 154; 149, boolean_operator:and; 149, 150; 149, 152; 150, not_operator; 150, 151; 151, identifier:width; 152, identifier:height; 153, comment; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:width; 158, call; 158, 159; 158, 160; 159, identifier:int; 160, argument_list; 160, 161; 161, binary_operator:/; 161, 162; 161, 175; 162, binary_operator:*; 162, 163; 162, 167; 163, call; 163, 164; 163, 165; 164, identifier:float; 165, argument_list; 165, 166; 166, identifier:height; 167, call; 167, 168; 167, 169; 168, identifier:float; 169, argument_list; 169, 170; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:instance; 173, identifier:image; 174, identifier:width; 175, call; 175, 176; 175, 177; 176, identifier:float; 177, argument_list; 177, 178; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:instance; 181, identifier:image; 182, identifier:height; 183, if_statement; 183, 184; 183, 186; 183, 187; 184, not_operator; 184, 185; 185, identifier:width; 186, comment; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:width; 191, attribute; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:instance; 194, identifier:image; 195, identifier:width; 196, if_statement; 196, 197; 196, 199; 196, 200; 197, not_operator; 197, 198; 198, identifier:height; 199, comment; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:height; 204, attribute; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:instance; 207, identifier:image; 208, identifier:height; 209, return_statement; 209, 210; 210, dictionary; 210, 211; 210, 216; 211, pair; 211, 212; 211, 213; 212, string:'size'; 213, tuple; 213, 214; 213, 215; 214, identifier:width; 215, identifier:height; 216, pair; 216, 217; 216, 218; 217, string:'subject_location'; 218, identifier:subject_location | def _get_thumbnail_options(self, context, instance):
"""
Return the size and options of the thumbnail that should be inserted
"""
width, height = None, None
subject_location = False
placeholder_width = context.get('width', None)
placeholder_height = context.get('height', None)
if instance.use_autoscale and placeholder_width:
# use the placeholder width as a hint for sizing
width = int(placeholder_width)
if instance.use_autoscale and placeholder_height:
height = int(placeholder_height)
elif instance.width:
width = instance.width
if instance.height:
height = instance.height
if instance.image:
if instance.image.subject_location:
subject_location = instance.image.subject_location
if not height and width:
# height was not externally defined: use ratio to scale it by the width
height = int(float(width) * float(instance.image.height) / float(instance.image.width))
if not width and height:
# width was not externally defined: use ratio to scale it by the height
width = int(float(height) * float(instance.image.width) / float(instance.image.height))
if not width:
# width is still not defined. fallback the actual image width
width = instance.image.width
if not height:
# height is still not defined. fallback the actual image height
height = instance.image.height
return {'size': (width, height),
'subject_location': subject_location} |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:group_and_sort_statements; 3, parameters; 3, 4; 3, 5; 4, identifier:stmt_list; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ev_totals; 7, None; 8, block; 8, 9; 8, 11; 8, 38; 8, 45; 8, 53; 8, 61; 8, 144; 8, 145; 8, 277; 8, 298; 9, expression_statement; 9, 10; 10, comment; 11, function_definition; 11, 12; 11, 13; 11, 15; 12, function_name:_count; 13, parameters; 13, 14; 14, identifier:stmt; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 20; 16, 28; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:ev_totals; 19, None; 20, block; 20, 21; 21, return_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, attribute; 25, 26; 25, 27; 26, identifier:stmt; 27, identifier:evidence; 28, else_clause; 28, 29; 29, block; 29, 30; 30, return_statement; 30, 31; 31, subscript; 31, 32; 31, 33; 32, identifier:ev_totals; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:stmt; 36, identifier:get_hash; 37, argument_list; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:stmt_rows; 41, call; 41, 42; 41, 43; 42, identifier:defaultdict; 43, argument_list; 43, 44; 44, identifier:list; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:stmt_counts; 48, call; 48, 49; 48, 50; 49, identifier:defaultdict; 50, argument_list; 50, 51; 51, lambda; 51, 52; 52, integer:0; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:arg_counts; 56, call; 56, 57; 56, 58; 57, identifier:defaultdict; 58, argument_list; 58, 59; 59, lambda; 59, 60; 60, integer:0; 61, for_statement; 61, 62; 61, 65; 61, 69; 61, 70; 62, pattern_list; 62, 63; 62, 64; 63, identifier:key; 64, identifier:s; 65, call; 65, 66; 65, 67; 66, identifier:_get_keyed_stmts; 67, argument_list; 67, 68; 68, identifier:stmt_list; 69, comment; 70, block; 70, 71; 70, 80; 70, 81; 70, 82; 70, 91; 70, 92; 70, 93; 70, 94; 70, 95; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, subscript; 74, 75; 74, 76; 75, identifier:stmt_rows; 76, identifier:key; 77, identifier:append; 78, argument_list; 78, 79; 79, identifier:s; 80, comment; 81, comment; 82, expression_statement; 82, 83; 83, augmented_assignment:+=; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:stmt_counts; 86, identifier:key; 87, call; 87, 88; 87, 89; 88, identifier:_count; 89, argument_list; 89, 90; 90, identifier:s; 91, comment; 92, comment; 93, comment; 94, comment; 95, if_statement; 95, 96; 95, 101; 95, 129; 96, comparison_operator:==; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:key; 99, integer:0; 100, string:'Conversion'; 101, block; 101, 102; 101, 108; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:subj; 105, subscript; 105, 106; 105, 107; 106, identifier:key; 107, integer:1; 108, for_statement; 108, 109; 108, 110; 108, 117; 109, identifier:obj; 110, binary_operator:+; 110, 111; 110, 114; 111, subscript; 111, 112; 111, 113; 112, identifier:key; 113, integer:2; 114, subscript; 114, 115; 114, 116; 115, identifier:key; 116, integer:3; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, augmented_assignment:+=; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 122; 121, identifier:arg_counts; 122, tuple; 122, 123; 122, 124; 123, identifier:subj; 124, identifier:obj; 125, call; 125, 126; 125, 127; 126, identifier:_count; 127, argument_list; 127, 128; 128, identifier:s; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, augmented_assignment:+=; 132, 133; 132, 140; 133, subscript; 133, 134; 133, 135; 134, identifier:arg_counts; 135, subscript; 135, 136; 135, 137; 136, identifier:key; 137, slice; 137, 138; 137, 139; 138, integer:1; 139, colon; 140, call; 140, 141; 140, 142; 141, identifier:_count; 142, argument_list; 142, 143; 143, identifier:s; 144, comment; 145, function_definition; 145, 146; 145, 147; 145, 149; 146, function_name:process_rows; 147, parameters; 147, 148; 148, identifier:stmt_rows; 149, block; 149, 150; 150, for_statement; 150, 151; 150, 154; 150, 159; 151, pattern_list; 151, 152; 151, 153; 152, identifier:key; 153, identifier:stmts; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:stmt_rows; 157, identifier:items; 158, argument_list; 159, block; 159, 160; 159, 166; 159, 174; 159, 180; 159, 186; 159, 230; 159, 238; 159, 271; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:verb; 163, subscript; 163, 164; 163, 165; 164, identifier:key; 165, integer:0; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:inps; 169, subscript; 169, 170; 169, 171; 170, identifier:key; 171, slice; 171, 172; 171, 173; 172, integer:1; 173, colon; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:sub_count; 177, subscript; 177, 178; 177, 179; 178, identifier:stmt_counts; 179, identifier:key; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:arg_count; 183, subscript; 183, 184; 183, 185; 184, identifier:arg_counts; 185, identifier:inps; 186, if_statement; 186, 187; 186, 201; 187, boolean_operator:and; 187, 188; 187, 195; 188, boolean_operator:and; 188, 189; 188, 192; 189, comparison_operator:==; 189, 190; 189, 191; 190, identifier:verb; 191, string:'Complex'; 192, comparison_operator:==; 192, 193; 192, 194; 193, identifier:sub_count; 194, identifier:arg_count; 195, comparison_operator:<=; 195, 196; 195, 200; 196, call; 196, 197; 196, 198; 197, identifier:len; 198, argument_list; 198, 199; 199, identifier:inps; 200, integer:2; 201, block; 201, 202; 202, if_statement; 202, 203; 202, 228; 203, call; 203, 204; 203, 205; 204, identifier:all; 205, argument_list; 205, 206; 206, list_comprehension; 206, 207; 206, 225; 207, comparison_operator:>; 207, 208; 207, 224; 208, call; 208, 209; 208, 210; 209, identifier:len; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:set; 213, generator_expression; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:ag; 216, identifier:name; 217, for_in_clause; 217, 218; 217, 219; 218, identifier:ag; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:s; 222, identifier:agent_list; 223, argument_list; 224, integer:2; 225, for_in_clause; 225, 226; 225, 227; 226, identifier:s; 227, identifier:stmts; 228, block; 228, 229; 229, continue_statement; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:new_key; 233, tuple; 233, 234; 233, 235; 233, 236; 233, 237; 234, identifier:arg_count; 235, identifier:inps; 236, identifier:sub_count; 237, identifier:verb; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:stmts; 241, call; 241, 242; 241, 243; 242, identifier:sorted; 243, argument_list; 243, 244; 243, 245; 243, 268; 244, identifier:stmts; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:key; 247, lambda; 247, 248; 247, 250; 248, lambda_parameters; 248, 249; 249, identifier:s; 250, binary_operator:+; 250, 251; 250, 255; 251, call; 251, 252; 251, 253; 252, identifier:_count; 253, argument_list; 253, 254; 254, identifier:s; 255, binary_operator:/; 255, 256; 255, 257; 256, integer:1; 257, parenthesized_expression; 257, 258; 258, binary_operator:+; 258, 259; 258, 260; 259, integer:1; 260, call; 260, 261; 260, 262; 261, identifier:len; 262, argument_list; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:s; 266, identifier:agent_list; 267, argument_list; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:reverse; 270, True; 271, expression_statement; 271, 272; 272, yield; 272, 273; 273, expression_list; 273, 274; 273, 275; 273, 276; 274, identifier:new_key; 275, identifier:verb; 276, identifier:stmts; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 280; 279, identifier:sorted_groups; 280, call; 280, 281; 280, 282; 281, identifier:sorted; 282, argument_list; 282, 283; 282, 287; 282, 295; 283, call; 283, 284; 283, 285; 284, identifier:process_rows; 285, argument_list; 285, 286; 286, identifier:stmt_rows; 287, keyword_argument; 287, 288; 287, 289; 288, identifier:key; 289, lambda; 289, 290; 289, 292; 290, lambda_parameters; 290, 291; 291, identifier:tpl; 292, subscript; 292, 293; 292, 294; 293, identifier:tpl; 294, integer:0; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:reverse; 297, True; 298, return_statement; 298, 299; 299, identifier:sorted_groups | def group_and_sort_statements(stmt_list, ev_totals=None):
"""Group statements by type and arguments, and sort by prevalence.
Parameters
----------
stmt_list : list[Statement]
A list of INDRA statements.
ev_totals : dict{int: int}
A dictionary, keyed by statement hash (shallow) with counts of total
evidence as the values. Including this will allow statements to be
better sorted.
Returns
-------
sorted_groups : list[tuple]
A list of tuples containing a sort key, the statement type, and a list
of statements, also sorted by evidence count, for that key and type.
The sort key contains a count of statements with those argument, the
arguments (normalized strings), the count of statements with those
arguements and type, and then the statement type.
"""
def _count(stmt):
if ev_totals is None:
return len(stmt.evidence)
else:
return ev_totals[stmt.get_hash()]
stmt_rows = defaultdict(list)
stmt_counts = defaultdict(lambda: 0)
arg_counts = defaultdict(lambda: 0)
for key, s in _get_keyed_stmts(stmt_list):
# Update the counts, and add key if needed.
stmt_rows[key].append(s)
# Keep track of the total evidence counts for this statement and the
# arguments.
stmt_counts[key] += _count(s)
# Add up the counts for the arguments, pairwise for Complexes and
# Conversions. This allows, for example, a complex between MEK, ERK,
# and something else to lend weight to the interactions between MEK
# and ERK.
if key[0] == 'Conversion':
subj = key[1]
for obj in key[2] + key[3]:
arg_counts[(subj, obj)] += _count(s)
else:
arg_counts[key[1:]] += _count(s)
# Sort the rows by count and agent names.
def process_rows(stmt_rows):
for key, stmts in stmt_rows.items():
verb = key[0]
inps = key[1:]
sub_count = stmt_counts[key]
arg_count = arg_counts[inps]
if verb == 'Complex' and sub_count == arg_count and len(inps) <= 2:
if all([len(set(ag.name for ag in s.agent_list())) > 2
for s in stmts]):
continue
new_key = (arg_count, inps, sub_count, verb)
stmts = sorted(stmts,
key=lambda s: _count(s) + 1/(1+len(s.agent_list())),
reverse=True)
yield new_key, verb, stmts
sorted_groups = sorted(process_rows(stmt_rows),
key=lambda tpl: tpl[0], reverse=True)
return sorted_groups |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:make_stmt_from_sort_key; 3, parameters; 3, 4; 3, 5; 4, identifier:key; 5, identifier:verb; 6, block; 6, 7; 6, 9; 6, 30; 6, 37; 6, 46; 6, 146; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:make_agent; 11, parameters; 11, 12; 12, identifier:name; 13, block; 13, 14; 13, 25; 14, if_statement; 14, 15; 14, 22; 15, boolean_operator:or; 15, 16; 15, 19; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:name; 18, string:'None'; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:name; 21, None; 22, block; 22, 23; 23, return_statement; 23, 24; 24, None; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:Agent; 28, argument_list; 28, 29; 29, identifier:name; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:StmtClass; 33, call; 33, 34; 33, 35; 34, identifier:get_statement_by_name; 35, argument_list; 35, 36; 36, identifier:verb; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:inps; 40, call; 40, 41; 40, 42; 41, identifier:list; 42, argument_list; 42, 43; 43, subscript; 43, 44; 43, 45; 44, identifier:key; 45, integer:1; 46, if_statement; 46, 47; 46, 50; 46, 65; 46, 102; 46, 129; 47, comparison_operator:==; 47, 48; 47, 49; 48, identifier:verb; 49, string:'Complex'; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:stmt; 54, call; 54, 55; 54, 56; 55, identifier:StmtClass; 56, argument_list; 56, 57; 57, list_comprehension; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:make_agent; 60, argument_list; 60, 61; 61, identifier:name; 62, for_in_clause; 62, 63; 62, 64; 63, identifier:name; 64, identifier:inps; 65, elif_clause; 65, 66; 65, 69; 66, comparison_operator:==; 66, 67; 66, 68; 67, identifier:verb; 68, string:'Conversion'; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:stmt; 73, call; 73, 74; 73, 75; 74, identifier:StmtClass; 75, argument_list; 75, 76; 75, 82; 75, 92; 76, call; 76, 77; 76, 78; 77, identifier:make_agent; 78, argument_list; 78, 79; 79, subscript; 79, 80; 79, 81; 80, identifier:inps; 81, integer:0; 82, list_comprehension; 82, 83; 82, 87; 83, call; 83, 84; 83, 85; 84, identifier:make_agent; 85, argument_list; 85, 86; 86, identifier:name; 87, for_in_clause; 87, 88; 87, 89; 88, identifier:name; 89, subscript; 89, 90; 89, 91; 90, identifier:inps; 91, integer:1; 92, list_comprehension; 92, 93; 92, 97; 93, call; 93, 94; 93, 95; 94, identifier:make_agent; 95, argument_list; 95, 96; 96, identifier:name; 97, for_in_clause; 97, 98; 97, 99; 98, identifier:name; 99, subscript; 99, 100; 99, 101; 100, identifier:inps; 101, integer:2; 102, elif_clause; 102, 103; 102, 110; 103, boolean_operator:or; 103, 104; 103, 107; 104, comparison_operator:==; 104, 105; 104, 106; 105, identifier:verb; 106, string:'ActiveForm'; 107, comparison_operator:==; 107, 108; 107, 109; 108, identifier:verb; 109, string:'HasActivity'; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:stmt; 114, call; 114, 115; 114, 116; 115, identifier:StmtClass; 116, argument_list; 116, 117; 116, 123; 116, 126; 117, call; 117, 118; 117, 119; 118, identifier:make_agent; 119, argument_list; 119, 120; 120, subscript; 120, 121; 120, 122; 121, identifier:inps; 122, integer:0; 123, subscript; 123, 124; 123, 125; 124, identifier:inps; 125, integer:1; 126, subscript; 126, 127; 126, 128; 127, identifier:inps; 128, integer:2; 129, else_clause; 129, 130; 130, block; 130, 131; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:stmt; 134, call; 134, 135; 134, 136; 135, identifier:StmtClass; 136, argument_list; 136, 137; 137, list_splat; 137, 138; 138, list_comprehension; 138, 139; 138, 143; 139, call; 139, 140; 139, 141; 140, identifier:make_agent; 141, argument_list; 141, 142; 142, identifier:name; 143, for_in_clause; 143, 144; 143, 145; 144, identifier:name; 145, identifier:inps; 146, return_statement; 146, 147; 147, identifier:stmt | def make_stmt_from_sort_key(key, verb):
"""Make a Statement from the sort key.
Specifically, the sort key used by `group_and_sort_statements`.
"""
def make_agent(name):
if name == 'None' or name is None:
return None
return Agent(name)
StmtClass = get_statement_by_name(verb)
inps = list(key[1])
if verb == 'Complex':
stmt = StmtClass([make_agent(name) for name in inps])
elif verb == 'Conversion':
stmt = StmtClass(make_agent(inps[0]),
[make_agent(name) for name in inps[1]],
[make_agent(name) for name in inps[2]])
elif verb == 'ActiveForm' or verb == 'HasActivity':
stmt = StmtClass(make_agent(inps[0]), inps[1], inps[2])
else:
stmt = StmtClass(*[make_agent(name) for name in inps])
return stmt |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:print_boolean_net; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:out_file; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 43; 8, 47; 8, 261; 8, 269; 8, 293; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:init_str; 14, string:''; 15, for_statement; 15, 16; 15, 17; 15, 24; 16, identifier:node_key; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:graph; 22, identifier:nodes; 23, argument_list; 24, block; 24, 25; 24, 37; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:node_name; 28, subscript; 28, 29; 28, 36; 29, subscript; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:graph; 34, identifier:node; 35, identifier:node_key; 36, string:'name'; 37, expression_statement; 37, 38; 38, augmented_assignment:+=; 38, 39; 38, 40; 39, identifier:init_str; 40, binary_operator:%; 40, 41; 40, 42; 41, string:'%s = False\n'; 42, identifier:node_name; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:rule_str; 46, string:''; 47, for_statement; 47, 48; 47, 49; 47, 56; 48, identifier:node_key; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:graph; 54, identifier:nodes; 55, argument_list; 56, block; 56, 57; 56, 69; 56, 80; 56, 85; 56, 95; 56, 115; 56, 133; 56, 151; 56, 155; 56, 174; 56, 183; 56, 187; 56, 206; 56, 215; 56, 249; 56, 257; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:node_name; 60, subscript; 60, 61; 60, 68; 61, subscript; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:graph; 66, identifier:node; 67, identifier:node_key; 68, string:'name'; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:in_edges; 72, call; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:graph; 77, identifier:in_edges; 78, argument_list; 78, 79; 79, identifier:node_key; 80, if_statement; 80, 81; 80, 83; 81, not_operator; 81, 82; 82, identifier:in_edges; 83, block; 83, 84; 84, continue_statement; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:parents; 88, list_comprehension; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:e; 91, integer:0; 92, for_in_clause; 92, 93; 92, 94; 93, identifier:e; 94, identifier:in_edges; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:polarities; 98, list_comprehension; 98, 99; 98, 112; 99, subscript; 99, 100; 99, 111; 100, subscript; 100, 101; 100, 110; 101, subscript; 101, 102; 101, 107; 102, attribute; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:graph; 106, identifier:edge; 107, subscript; 107, 108; 107, 109; 108, identifier:e; 109, integer:0; 110, identifier:node_key; 111, string:'polarity'; 112, for_in_clause; 112, 113; 112, 114; 113, identifier:e; 114, identifier:in_edges; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:pos_parents; 118, list_comprehension; 118, 119; 118, 120; 118, 129; 119, identifier:par; 120, for_in_clause; 120, 121; 120, 124; 121, pattern_list; 121, 122; 121, 123; 122, identifier:par; 123, identifier:pol; 124, call; 124, 125; 124, 126; 125, identifier:zip; 126, argument_list; 126, 127; 126, 128; 127, identifier:parents; 128, identifier:polarities; 129, if_clause; 129, 130; 130, comparison_operator:==; 130, 131; 130, 132; 131, identifier:pol; 132, string:'positive'; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:neg_parents; 136, list_comprehension; 136, 137; 136, 138; 136, 147; 137, identifier:par; 138, for_in_clause; 138, 139; 138, 142; 139, pattern_list; 139, 140; 139, 141; 140, identifier:par; 141, identifier:pol; 142, call; 142, 143; 142, 144; 143, identifier:zip; 144, argument_list; 144, 145; 144, 146; 145, identifier:parents; 146, identifier:polarities; 147, if_clause; 147, 148; 148, comparison_operator:==; 148, 149; 148, 150; 149, identifier:pol; 150, string:'negative'; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:rhs_pos_parts; 154, list:[]; 155, for_statement; 155, 156; 155, 157; 155, 158; 156, identifier:par; 157, identifier:pos_parents; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:rhs_pos_parts; 163, identifier:append; 164, argument_list; 164, 165; 165, subscript; 165, 166; 165, 173; 166, subscript; 166, 167; 166, 172; 167, attribute; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:self; 170, identifier:graph; 171, identifier:node; 172, identifier:par; 173, string:'name'; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:rhs_pos_str; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, string:' or '; 180, identifier:join; 181, argument_list; 181, 182; 182, identifier:rhs_pos_parts; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:rhs_neg_parts; 186, list:[]; 187, for_statement; 187, 188; 187, 189; 187, 190; 188, identifier:par; 189, identifier:neg_parents; 190, block; 190, 191; 191, expression_statement; 191, 192; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:rhs_neg_parts; 195, identifier:append; 196, argument_list; 196, 197; 197, subscript; 197, 198; 197, 205; 198, subscript; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:graph; 203, identifier:node; 204, identifier:par; 205, string:'name'; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:rhs_neg_str; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, string:' or '; 212, identifier:join; 213, argument_list; 213, 214; 214, identifier:rhs_neg_parts; 215, if_statement; 215, 216; 215, 217; 215, 239; 216, identifier:rhs_pos_str; 217, block; 217, 218; 218, if_statement; 218, 219; 218, 220; 218, 233; 219, identifier:rhs_neg_str; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:rhs_str; 224, binary_operator:+; 224, 225; 224, 232; 225, binary_operator:+; 225, 226; 225, 231; 226, binary_operator:+; 226, 227; 226, 230; 227, binary_operator:+; 227, 228; 227, 229; 228, string:'('; 229, identifier:rhs_pos_str; 230, string:') and not ('; 231, identifier:rhs_neg_str; 232, string:')'; 233, else_clause; 233, 234; 234, block; 234, 235; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:rhs_str; 238, identifier:rhs_pos_str; 239, else_clause; 239, 240; 240, block; 240, 241; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:rhs_str; 244, binary_operator:+; 244, 245; 244, 248; 245, binary_operator:+; 245, 246; 245, 247; 246, string:'not ('; 247, identifier:rhs_neg_str; 248, string:')'; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:node_eq; 252, binary_operator:%; 252, 253; 252, 254; 253, string:'%s* = %s\n'; 254, tuple; 254, 255; 254, 256; 255, identifier:node_name; 256, identifier:rhs_str; 257, expression_statement; 257, 258; 258, augmented_assignment:+=; 258, 259; 258, 260; 259, identifier:rule_str; 260, identifier:node_eq; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:full_str; 264, binary_operator:+; 264, 265; 264, 268; 265, binary_operator:+; 265, 266; 265, 267; 266, identifier:init_str; 267, string:'\n'; 268, identifier:rule_str; 269, if_statement; 269, 270; 269, 273; 270, comparison_operator:is; 270, 271; 270, 272; 271, identifier:out_file; 272, None; 273, block; 273, 274; 274, with_statement; 274, 275; 274, 285; 275, with_clause; 275, 276; 276, with_item; 276, 277; 277, as_pattern; 277, 278; 277, 283; 278, call; 278, 279; 278, 280; 279, identifier:open; 280, argument_list; 280, 281; 280, 282; 281, identifier:out_file; 282, string:'wt'; 283, as_pattern_target; 283, 284; 284, identifier:fh; 285, block; 285, 286; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 291; 288, attribute; 288, 289; 288, 290; 289, identifier:fh; 290, identifier:write; 291, argument_list; 291, 292; 292, identifier:full_str; 293, return_statement; 293, 294; 294, identifier:full_str | def print_boolean_net(self, out_file=None):
"""Return a Boolean network from the assembled graph.
See https://github.com/ialbert/booleannet for details about
the format used to encode the Boolean rules.
Parameters
----------
out_file : Optional[str]
A file name in which the Boolean network is saved.
Returns
-------
full_str : str
The string representing the Boolean network.
"""
init_str = ''
for node_key in self.graph.nodes():
node_name = self.graph.node[node_key]['name']
init_str += '%s = False\n' % node_name
rule_str = ''
for node_key in self.graph.nodes():
node_name = self.graph.node[node_key]['name']
in_edges = self.graph.in_edges(node_key)
if not in_edges:
continue
parents = [e[0] for e in in_edges]
polarities = [self.graph.edge[e[0]][node_key]['polarity']
for e in in_edges]
pos_parents = [par for par, pol in zip(parents, polarities) if
pol == 'positive']
neg_parents = [par for par, pol in zip(parents, polarities) if
pol == 'negative']
rhs_pos_parts = []
for par in pos_parents:
rhs_pos_parts.append(self.graph.node[par]['name'])
rhs_pos_str = ' or '.join(rhs_pos_parts)
rhs_neg_parts = []
for par in neg_parents:
rhs_neg_parts.append(self.graph.node[par]['name'])
rhs_neg_str = ' or '.join(rhs_neg_parts)
if rhs_pos_str:
if rhs_neg_str:
rhs_str = '(' + rhs_pos_str + \
') and not (' + rhs_neg_str + ')'
else:
rhs_str = rhs_pos_str
else:
rhs_str = 'not (' + rhs_neg_str + ')'
node_eq = '%s* = %s\n' % (node_name, rhs_str)
rule_str += node_eq
full_str = init_str + '\n' + rule_str
if out_file is not None:
with open(out_file, 'wt') as fh:
fh.write(full_str)
return full_str |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_complex_agents; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:complex_id; 6, block; 6, 7; 6, 9; 6, 13; 6, 22; 6, 155; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:agents; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:components; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_recursively_lookup_complex; 20, argument_list; 20, 21; 21, identifier:complex_id; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:c; 24, identifier:components; 25, block; 25, 26; 25, 30; 25, 39; 25, 76; 25, 82; 25, 137; 25, 142; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:db_refs; 29, dictionary; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:name; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:uniprot_client; 36, identifier:get_gene_name; 37, argument_list; 37, 38; 38, identifier:c; 39, if_statement; 39, 40; 39, 43; 39, 50; 40, comparison_operator:is; 40, 41; 40, 42; 41, identifier:name; 42, None; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, subscript; 46, 47; 46, 48; 47, identifier:db_refs; 48, string:'SIGNOR'; 49, identifier:c; 50, else_clause; 50, 51; 51, block; 51, 52; 51, 58; 51, 67; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:db_refs; 56, string:'UP'; 57, identifier:c; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:hgnc_id; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:hgnc_client; 64, identifier:get_hgnc_id; 65, argument_list; 65, 66; 66, identifier:name; 67, if_statement; 67, 68; 67, 69; 68, identifier:hgnc_id; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:db_refs; 74, string:'HGNC'; 75, identifier:hgnc_id; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:famplex_key; 79, tuple; 79, 80; 79, 81; 80, string:'SIGNOR'; 81, identifier:c; 82, if_statement; 82, 83; 82, 86; 82, 107; 83, comparison_operator:in; 83, 84; 83, 85; 84, identifier:famplex_key; 85, identifier:famplex_map; 86, block; 86, 87; 86, 95; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:db_refs; 91, string:'FPLX'; 92, subscript; 92, 93; 92, 94; 93, identifier:famplex_map; 94, identifier:famplex_key; 95, if_statement; 95, 96; 95, 98; 96, not_operator; 96, 97; 97, identifier:name; 98, block; 98, 99; 98, 105; 98, 106; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:name; 102, subscript; 102, 103; 102, 104; 103, identifier:db_refs; 104, string:'FPLX'; 105, comment; 106, comment; 107, elif_clause; 107, 108; 107, 110; 107, 111; 108, not_operator; 108, 109; 109, identifier:name; 110, comment; 111, block; 111, 112; 111, 123; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:logger; 116, identifier:info; 117, argument_list; 117, 118; 118, binary_operator:+; 118, 119; 118, 122; 119, binary_operator:+; 119, 120; 119, 121; 120, string:'Have neither a Uniprot nor Famplex grounding '; 121, string:'for '; 122, identifier:c; 123, if_statement; 123, 124; 123, 126; 124, not_operator; 124, 125; 125, identifier:name; 126, block; 126, 127; 126, 133; 126, 134; 126, 135; 126, 136; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:name; 130, subscript; 130, 131; 130, 132; 131, identifier:db_refs; 132, string:'SIGNOR'; 133, comment; 134, comment; 135, comment; 136, comment; 137, assert_statement; 137, 138; 138, parenthesized_expression; 138, 139; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:name; 141, None; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:agents; 146, identifier:append; 147, argument_list; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:Agent; 150, argument_list; 150, 151; 150, 152; 151, identifier:name; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:db_refs; 154, identifier:db_refs; 155, return_statement; 155, 156; 156, identifier:agents | def _get_complex_agents(self, complex_id):
"""Returns a list of agents corresponding to each of the constituents
in a SIGNOR complex."""
agents = []
components = self._recursively_lookup_complex(complex_id)
for c in components:
db_refs = {}
name = uniprot_client.get_gene_name(c)
if name is None:
db_refs['SIGNOR'] = c
else:
db_refs['UP'] = c
hgnc_id = hgnc_client.get_hgnc_id(name)
if hgnc_id:
db_refs['HGNC'] = hgnc_id
famplex_key = ('SIGNOR', c)
if famplex_key in famplex_map:
db_refs['FPLX'] = famplex_map[famplex_key]
if not name:
name = db_refs['FPLX'] # Set agent name to Famplex name if
# the Uniprot name is not available
elif not name:
# We neither have a Uniprot nor Famplex grounding
logger.info('Have neither a Uniprot nor Famplex grounding ' + \
'for ' + c)
if not name:
name = db_refs['SIGNOR'] # Set the agent name to the
# Signor name if neither the
# Uniprot nor Famplex names are
# available
assert(name is not None)
agents.append(Agent(name, db_refs=db_refs))
return agents |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_args; 3, parameters; 3, 4; 4, identifier:node; 5, block; 5, 6; 5, 8; 5, 12; 5, 42; 5, 83; 5, 84; 5, 165; 5, 166; 5, 175; 5, 196; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:arg_roles; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:args; 15, binary_operator:+; 15, 16; 15, 22; 15, 23; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:node; 19, identifier:findall; 20, argument_list; 20, 21; 21, string:'arg'; 22, line_continuation:\; 23, list:[node.find('arg1'), node.find('arg2'), node.find('arg3')]; 23, 24; 23, 30; 23, 36; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:node; 27, identifier:find; 28, argument_list; 28, 29; 29, string:'arg1'; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:node; 33, identifier:find; 34, argument_list; 34, 35; 35, string:'arg2'; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:node; 39, identifier:find; 40, argument_list; 40, 41; 41, string:'arg3'; 42, for_statement; 42, 43; 42, 44; 42, 45; 43, identifier:arg; 44, identifier:args; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 50; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:arg; 49, None; 50, block; 50, 51; 50, 62; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:id; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:arg; 58, identifier:attrib; 59, identifier:get; 60, argument_list; 60, 61; 61, string:'id'; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:id; 65, None; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 76; 69, subscript; 69, 70; 69, 71; 70, identifier:arg_roles; 71, subscript; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:arg; 74, identifier:attrib; 75, string:'role'; 76, tuple; 76, 77; 76, 82; 77, subscript; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:arg; 80, identifier:attrib; 81, string:'id'; 82, identifier:arg; 83, comment; 84, if_statement; 84, 85; 84, 93; 85, comparison_operator:is; 85, 86; 85, 92; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:node; 89, identifier:find; 90, argument_list; 90, 91; 91, string:'features'; 92, None; 93, block; 93, 94; 93, 103; 93, 126; 93, 142; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:inevents; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:node; 100, identifier:findall; 101, argument_list; 101, 102; 102, string:'features/inevent'; 103, for_statement; 103, 104; 103, 105; 103, 106; 104, identifier:inevent; 105, identifier:inevents; 106, block; 106, 107; 107, if_statement; 107, 108; 107, 113; 108, comparison_operator:in; 108, 109; 108, 110; 109, string:'id'; 110, attribute; 110, 111; 110, 112; 111, identifier:inevent; 112, identifier:attrib; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:arg_roles; 118, string:'inevent'; 119, tuple; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:inevent; 123, identifier:attrib; 124, string:'id'; 125, identifier:inevent; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:ptms; 129, binary_operator:+; 129, 130; 129, 136; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:node; 133, identifier:findall; 134, argument_list; 134, 135; 135, string:'features/ptm'; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:node; 139, identifier:findall; 140, argument_list; 140, 141; 141, string:'features/no-ptm'; 142, for_statement; 142, 143; 142, 144; 142, 145; 143, identifier:ptm; 144, identifier:ptms; 145, block; 145, 146; 146, if_statement; 146, 147; 146, 152; 147, comparison_operator:in; 147, 148; 147, 149; 148, string:'id'; 149, attribute; 149, 150; 149, 151; 150, identifier:inevent; 151, identifier:attrib; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:arg_roles; 157, string:'ptm'; 158, tuple; 158, 159; 158, 164; 159, subscript; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:inevent; 162, identifier:attrib; 163, string:'id'; 164, identifier:ptm; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:aw; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:node; 172, identifier:find; 173, argument_list; 173, 174; 174, string:'assoc-with'; 175, if_statement; 175, 176; 175, 179; 176, comparison_operator:is; 176, 177; 176, 178; 177, identifier:aw; 178, None; 179, block; 179, 180; 179, 188; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:aw_id; 183, subscript; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:aw; 186, identifier:attrib; 187, string:'id'; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 193; 190, subscript; 190, 191; 190, 192; 191, identifier:arg_roles; 192, string:'assoc-with'; 193, tuple; 193, 194; 193, 195; 194, identifier:aw_id; 195, identifier:aw; 196, return_statement; 196, 197; 197, identifier:arg_roles | def get_args(node):
"""Return the arguments of a node in the event graph."""
arg_roles = {}
args = node.findall('arg') + \
[node.find('arg1'), node.find('arg2'), node.find('arg3')]
for arg in args:
if arg is not None:
id = arg.attrib.get('id')
if id is not None:
arg_roles[arg.attrib['role']] = (arg.attrib['id'], arg)
# Now look at possible inevent links
if node.find('features') is not None:
inevents = node.findall('features/inevent')
for inevent in inevents:
if 'id' in inevent.attrib:
arg_roles['inevent'] = (inevent.attrib['id'], inevent)
ptms = node.findall('features/ptm') + node.findall('features/no-ptm')
for ptm in ptms:
if 'id' in inevent.attrib:
arg_roles['ptm'] = (inevent.attrib['id'], ptm)
# And also look for assoc-with links
aw = node.find('assoc-with')
if aw is not None:
aw_id = aw.attrib['id']
arg_roles['assoc-with'] = (aw_id, aw)
return arg_roles |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:map_statements; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 13; 9, identifier:stmt; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:statements; 13, block; 13, 14; 14, for_statement; 14, 15; 14, 16; 14, 21; 15, identifier:agent; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:stmt; 19, identifier:agent_list; 20, argument_list; 21, block; 21, 22; 21, 28; 21, 32; 21, 73; 22, if_statement; 22, 23; 22, 26; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:agent; 25, None; 26, block; 26, 27; 27, continue_statement; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:all_mappings; 31, list:[]; 32, for_statement; 32, 33; 32, 36; 32, 43; 33, pattern_list; 33, 34; 33, 35; 34, identifier:db_name; 35, identifier:db_id; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:agent; 40, identifier:db_refs; 41, identifier:items; 42, argument_list; 43, block; 43, 44; 43, 59; 43, 69; 44, if_statement; 44, 45; 44, 50; 45, call; 45, 46; 45, 47; 46, identifier:isinstance; 47, argument_list; 47, 48; 47, 49; 48, identifier:db_id; 49, identifier:list; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:db_id; 54, subscript; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:db_id; 57, integer:0; 58, integer:0; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:mappings; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_map_id; 66, argument_list; 66, 67; 66, 68; 67, identifier:db_name; 68, identifier:db_id; 69, expression_statement; 69, 70; 70, augmented_assignment:+=; 70, 71; 70, 72; 71, identifier:all_mappings; 72, identifier:mappings; 73, for_statement; 73, 74; 73, 79; 73, 80; 74, pattern_list; 74, 75; 74, 76; 74, 77; 74, 78; 75, identifier:map_db_name; 76, identifier:map_db_id; 77, identifier:score; 78, identifier:orig_db_name; 79, identifier:all_mappings; 80, block; 80, 81; 80, 89; 81, if_statement; 81, 82; 81, 87; 82, comparison_operator:in; 82, 83; 82, 84; 83, identifier:map_db_name; 84, attribute; 84, 85; 84, 86; 85, identifier:agent; 86, identifier:db_refs; 87, block; 87, 88; 88, continue_statement; 89, if_statement; 89, 90; 89, 93; 89, 94; 89, 95; 89, 96; 89, 132; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:scored; 93, comment; 94, comment; 95, comment; 96, block; 96, 97; 96, 118; 97, try_statement; 97, 98; 97, 111; 98, block; 98, 99; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:orig_score; 102, subscript; 102, 103; 102, 110; 103, subscript; 103, 104; 103, 109; 104, subscript; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:agent; 107, identifier:db_refs; 108, identifier:orig_db_name; 109, integer:0; 110, integer:1; 111, except_clause; 111, 112; 111, 113; 112, identifier:Exception; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:orig_score; 117, float:1.0; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 125; 119, 126; 120, subscript; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:agent; 123, identifier:db_refs; 124, identifier:map_db_name; 125, line_continuation:\; 126, list:[(map_db_id, score * orig_score)]; 126, 127; 127, tuple; 127, 128; 127, 129; 128, identifier:map_db_id; 129, binary_operator:*; 129, 130; 129, 131; 130, identifier:score; 131, identifier:orig_score; 132, else_clause; 132, 133; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 140; 134, 152; 135, comparison_operator:in; 135, 136; 135, 137; 136, identifier:map_db_name; 137, tuple; 137, 138; 137, 139; 138, string:'UN'; 139, string:'HUME'; 140, block; 140, 141; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 148; 143, subscript; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:agent; 146, identifier:db_refs; 147, identifier:map_db_name; 148, list:[(map_db_id, 1.0)]; 148, 149; 149, tuple; 149, 150; 149, 151; 150, identifier:map_db_id; 151, float:1.0; 152, else_clause; 152, 153; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 161; 156, subscript; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:agent; 159, identifier:db_refs; 160, identifier:map_db_name; 161, identifier:map_db_id | def map_statements(self):
"""Run the ontology mapping on the statements."""
for stmt in self.statements:
for agent in stmt.agent_list():
if agent is None:
continue
all_mappings = []
for db_name, db_id in agent.db_refs.items():
if isinstance(db_id, list):
db_id = db_id[0][0]
mappings = self._map_id(db_name, db_id)
all_mappings += mappings
for map_db_name, map_db_id, score, orig_db_name in all_mappings:
if map_db_name in agent.db_refs:
continue
if self.scored:
# If the original one is a scored grounding,
# we take that score and multiply it with the mapping
# score. Otherwise we assume the original score is 1.
try:
orig_score = agent.db_refs[orig_db_name][0][1]
except Exception:
orig_score = 1.0
agent.db_refs[map_db_name] = \
[(map_db_id, score * orig_score)]
else:
if map_db_name in ('UN', 'HUME'):
agent.db_refs[map_db_name] = [(map_db_id, 1.0)]
else:
agent.db_refs[map_db_name] = map_db_id |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:agent_texts_with_grounding; 3, parameters; 3, 4; 4, identifier:stmts; 5, block; 5, 6; 5, 8; 5, 15; 5, 16; 5, 17; 5, 48; 5, 65; 5, 72; 5, 93; 5, 94; 5, 113; 5, 114; 5, 118; 5, 261; 5, 262; 5, 279; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:allag; 11, call; 11, 12; 11, 13; 12, identifier:all_agents; 13, argument_list; 13, 14; 14, identifier:stmts; 15, comment; 16, comment; 17, for_statement; 17, 18; 17, 19; 17, 20; 18, identifier:ag; 19, identifier:allag; 20, block; 20, 21; 20, 32; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:pfam_def; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:ag; 28, identifier:db_refs; 29, identifier:get; 30, argument_list; 30, 31; 31, string:'PFAM-DEF'; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:pfam_def; 35, None; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 44; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:ag; 42, identifier:db_refs; 43, string:'PFAM-DEF'; 44, call; 44, 45; 44, 46; 45, identifier:tuple; 46, argument_list; 46, 47; 47, identifier:pfam_def; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:refs; 51, list_comprehension; 51, 52; 51, 62; 52, call; 52, 53; 52, 54; 53, identifier:tuple; 54, argument_list; 54, 55; 55, call; 55, 56; 55, 61; 56, attribute; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:ag; 59, identifier:db_refs; 60, identifier:items; 61, argument_list; 62, for_in_clause; 62, 63; 62, 64; 63, identifier:ag; 64, identifier:allag; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:refs_counter; 68, call; 68, 69; 68, 70; 69, identifier:Counter; 70, argument_list; 70, 71; 71, identifier:refs; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:refs_counter_dict; 75, list_comprehension; 75, 76; 75, 86; 76, tuple; 76, 77; 76, 83; 77, call; 77, 78; 77, 79; 78, identifier:dict; 79, argument_list; 79, 80; 80, subscript; 80, 81; 80, 82; 81, identifier:entry; 82, integer:0; 83, subscript; 83, 84; 83, 85; 84, identifier:entry; 85, integer:1; 86, for_in_clause; 86, 87; 86, 88; 87, identifier:entry; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:refs_counter; 91, identifier:items; 92, argument_list; 93, comment; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:refs_counter_dict; 98, identifier:sort; 99, argument_list; 99, 100; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:key; 102, lambda; 102, 103; 102, 105; 103, lambda_parameters; 103, 104; 104, identifier:x; 105, call; 105, 106; 105, 111; 106, attribute; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:x; 109, integer:0; 110, identifier:get; 111, argument_list; 111, 112; 112, string:'TEXT'; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:grouped_by_text; 117, list:[]; 118, for_statement; 118, 119; 118, 122; 118, 139; 118, 140; 119, pattern_list; 119, 120; 119, 121; 120, identifier:k; 121, identifier:g; 122, call; 122, 123; 122, 124; 123, identifier:groupby; 124, argument_list; 124, 125; 124, 126; 125, identifier:refs_counter_dict; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:key; 128, lambda; 128, 129; 128, 131; 129, lambda_parameters; 129, 130; 130, identifier:x; 131, call; 131, 132; 131, 137; 132, attribute; 132, 133; 132, 136; 133, subscript; 133, 134; 133, 135; 134, identifier:x; 135, integer:0; 136, identifier:get; 137, argument_list; 137, 138; 138, string:'TEXT'; 139, comment; 140, block; 140, 141; 140, 145; 140, 150; 140, 154; 140, 217; 140, 218; 140, 242; 140, 243; 140, 250; 140, 251; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:total; 144, integer:0; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:entry; 148, list:[k]; 148, 149; 149, identifier:k; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:db_ref_list; 153, list:[]; 154, for_statement; 154, 155; 154, 158; 154, 159; 154, 160; 155, pattern_list; 155, 156; 155, 157; 156, identifier:db_refs; 157, identifier:count; 158, identifier:g; 159, comment; 160, block; 160, 161; 160, 184; 160, 185; 160, 213; 161, if_statement; 161, 162; 161, 173; 162, comparison_operator:==; 162, 163; 162, 171; 163, call; 163, 164; 163, 165; 164, identifier:list; 165, argument_list; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:db_refs; 169, identifier:keys; 170, argument_list; 171, list:['TEXT']; 171, 172; 172, string:'TEXT'; 173, block; 173, 174; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:db_ref_list; 178, identifier:append; 179, argument_list; 179, 180; 180, tuple; 180, 181; 180, 182; 180, 183; 181, None; 182, None; 183, identifier:count; 184, comment; 185, for_statement; 185, 186; 185, 189; 185, 194; 186, pattern_list; 186, 187; 186, 188; 187, identifier:db; 188, identifier:db_id; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:db_refs; 192, identifier:items; 193, argument_list; 194, block; 194, 195; 195, if_statement; 195, 196; 195, 199; 195, 201; 196, comparison_operator:==; 196, 197; 196, 198; 197, identifier:db; 198, string:'TEXT'; 199, block; 199, 200; 200, continue_statement; 201, else_clause; 201, 202; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:db_ref_list; 207, identifier:append; 208, argument_list; 208, 209; 209, tuple; 209, 210; 209, 211; 209, 212; 210, identifier:db; 211, identifier:db_id; 212, identifier:count; 213, expression_statement; 213, 214; 214, augmented_assignment:+=; 214, 215; 214, 216; 215, identifier:total; 216, identifier:count; 217, comment; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:entry; 222, identifier:append; 223, argument_list; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:tuple; 226, argument_list; 226, 227; 227, call; 227, 228; 227, 229; 228, identifier:sorted; 229, argument_list; 229, 230; 229, 231; 229, 239; 230, identifier:db_ref_list; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:key; 233, lambda; 233, 234; 233, 236; 234, lambda_parameters; 234, 235; 235, identifier:x; 236, subscript; 236, 237; 236, 238; 237, identifier:x; 238, integer:2; 239, keyword_argument; 239, 240; 239, 241; 240, identifier:reverse; 241, True; 242, comment; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:entry; 247, identifier:append; 248, argument_list; 248, 249; 249, identifier:total; 250, comment; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:grouped_by_text; 255, identifier:append; 256, argument_list; 256, 257; 257, call; 257, 258; 257, 259; 258, identifier:tuple; 259, argument_list; 259, 260; 260, identifier:entry; 261, comment; 262, expression_statement; 262, 263; 263, call; 263, 264; 263, 267; 264, attribute; 264, 265; 264, 266; 265, identifier:grouped_by_text; 266, identifier:sort; 267, argument_list; 267, 268; 267, 276; 268, keyword_argument; 268, 269; 268, 270; 269, identifier:key; 270, lambda; 270, 271; 270, 273; 271, lambda_parameters; 271, 272; 272, identifier:x; 273, subscript; 273, 274; 273, 275; 274, identifier:x; 275, integer:2; 276, keyword_argument; 276, 277; 276, 278; 277, identifier:reverse; 278, True; 279, return_statement; 279, 280; 280, identifier:grouped_by_text | def agent_texts_with_grounding(stmts):
"""Return agent text groundings in a list of statements with their counts
Parameters
----------
stmts: list of :py:class:`indra.statements.Statement`
Returns
-------
list of tuple
List of tuples of the form
(text: str, ((name_space: str, ID: str, count: int)...),
total_count: int)
Where the counts within the tuple of groundings give the number of
times an agent with the given agent_text appears grounded with the
particular name space and ID. The total_count gives the total number
of times an agent with text appears in the list of statements.
"""
allag = all_agents(stmts)
# Convert PFAM-DEF lists into tuples so that they are hashable and can
# be tabulated with a Counter
for ag in allag:
pfam_def = ag.db_refs.get('PFAM-DEF')
if pfam_def is not None:
ag.db_refs['PFAM-DEF'] = tuple(pfam_def)
refs = [tuple(ag.db_refs.items()) for ag in allag]
refs_counter = Counter(refs)
refs_counter_dict = [(dict(entry[0]), entry[1])
for entry in refs_counter.items()]
# First, sort by text so that we can do a groupby
refs_counter_dict.sort(key=lambda x: x[0].get('TEXT'))
# Then group by text
grouped_by_text = []
for k, g in groupby(refs_counter_dict, key=lambda x: x[0].get('TEXT')):
# Total occurrences of this agent text
total = 0
entry = [k]
db_ref_list = []
for db_refs, count in g:
# Check if TEXT is our only key, indicating no grounding
if list(db_refs.keys()) == ['TEXT']:
db_ref_list.append((None, None, count))
# Add any other db_refs (not TEXT)
for db, db_id in db_refs.items():
if db == 'TEXT':
continue
else:
db_ref_list.append((db, db_id, count))
total += count
# Sort the db_ref_list by the occurrences of each grounding
entry.append(tuple(sorted(db_ref_list, key=lambda x: x[2],
reverse=True)))
# Now add the total frequency to the entry
entry.append(total)
# And add the entry to the overall list
grouped_by_text.append(tuple(entry))
# Sort the list by the total number of occurrences of each unique key
grouped_by_text.sort(key=lambda x: x[2], reverse=True)
return grouped_by_text |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:ungrounded_texts; 3, parameters; 3, 4; 4, identifier:stmts; 5, block; 5, 6; 5, 8; 5, 45; 5, 52; 5, 60; 5, 78; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:ungrounded; 11, list_comprehension; 11, 12; 11, 17; 11, 20; 11, 27; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:ag; 15, identifier:db_refs; 16, string:'TEXT'; 17, for_in_clause; 17, 18; 17, 19; 18, identifier:s; 19, identifier:stmts; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:ag; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:s; 25, identifier:agent_list; 26, argument_list; 27, if_clause; 27, 28; 28, boolean_operator:and; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:ag; 31, None; 32, comparison_operator:==; 32, 33; 32, 43; 33, call; 33, 34; 33, 35; 34, identifier:list; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:ag; 40, identifier:db_refs; 41, identifier:keys; 42, argument_list; 43, list:['TEXT']; 43, 44; 44, string:'TEXT'; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:ungroundc; 48, call; 48, 49; 48, 50; 49, identifier:Counter; 50, argument_list; 50, 51; 51, identifier:ungrounded; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:ungroundc; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:ungroundc; 58, identifier:items; 59, argument_list; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:ungroundc; 63, call; 63, 64; 63, 65; 64, identifier:sorted; 65, argument_list; 65, 66; 65, 67; 65, 75; 66, identifier:ungroundc; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:key; 69, lambda; 69, 70; 69, 72; 70, lambda_parameters; 70, 71; 71, identifier:x; 72, subscript; 72, 73; 72, 74; 73, identifier:x; 74, integer:1; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:reverse; 77, True; 78, return_statement; 78, 79; 79, identifier:ungroundc | def ungrounded_texts(stmts):
"""Return a list of all ungrounded entities ordered by number of mentions
Parameters
----------
stmts : list of :py:class:`indra.statements.Statement`
Returns
-------
ungroundc : list of tuple
list of tuples of the form (text: str, count: int) sorted in descending
order by count.
"""
ungrounded = [ag.db_refs['TEXT']
for s in stmts
for ag in s.agent_list()
if ag is not None and list(ag.db_refs.keys()) == ['TEXT']]
ungroundc = Counter(ungrounded)
ungroundc = ungroundc.items()
ungroundc = sorted(ungroundc, key=lambda x: x[1], reverse=True)
return ungroundc |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:protein_map_from_twg; 3, parameters; 3, 4; 4, identifier:twg; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 20; 5, 27; 5, 150; 5, 159; 5, 168; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:protein_map; 11, dictionary; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:unmatched; 15, integer:0; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:matched; 19, integer:0; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:logger; 24, identifier:info; 25, argument_list; 25, 26; 26, string:'Building grounding map for human proteins'; 27, for_statement; 27, 28; 27, 32; 27, 33; 27, 34; 27, 35; 28, pattern_list; 28, 29; 28, 30; 28, 31; 29, identifier:agent_text; 30, identifier:grounding_list; 31, identifier:_; 32, identifier:twg; 33, comment; 34, comment; 35, block; 35, 36; 35, 48; 35, 49; 35, 65; 35, 66; 36, if_statement; 36, 37; 36, 46; 37, comparison_operator:not; 37, 38; 37, 39; 38, string:'UP'; 39, list_comprehension; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:entry; 42, integer:0; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:entry; 45, identifier:grounding_list; 46, block; 46, 47; 47, continue_statement; 48, comment; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:uniprot_ids; 52, list_comprehension; 52, 53; 52, 56; 52, 59; 53, subscript; 53, 54; 53, 55; 54, identifier:entry; 55, integer:1; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:entry; 58, identifier:grounding_list; 59, if_clause; 59, 60; 60, comparison_operator:==; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:entry; 63, integer:0; 64, string:'UP'; 65, comment; 66, for_statement; 66, 67; 66, 68; 66, 69; 66, 70; 67, identifier:uniprot_id; 68, identifier:uniprot_ids; 69, comment; 70, block; 70, 71; 70, 80; 70, 94; 70, 95; 70, 96; 70, 105; 70, 115; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:mnemonic; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:uniprot_client; 77, identifier:get_mnemonic; 78, argument_list; 78, 79; 79, identifier:uniprot_id; 80, if_statement; 80, 81; 80, 92; 81, boolean_operator:or; 81, 82; 81, 85; 82, comparison_operator:is; 82, 83; 82, 84; 83, identifier:mnemonic; 84, None; 85, not_operator; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:mnemonic; 89, identifier:endswith; 90, argument_list; 90, 91; 91, string:'_HUMAN'; 92, block; 92, 93; 93, continue_statement; 94, comment; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:gene_name; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:uniprot_client; 102, identifier:get_gene_name; 103, argument_list; 103, 104; 104, identifier:uniprot_id; 105, if_statement; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:gene_name; 108, None; 109, block; 109, 110; 109, 114; 110, expression_statement; 110, 111; 111, augmented_assignment:+=; 111, 112; 111, 113; 112, identifier:unmatched; 113, integer:1; 114, continue_statement; 115, if_statement; 115, 116; 115, 127; 115, 144; 116, comparison_operator:==; 116, 117; 116, 122; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:agent_text; 120, identifier:upper; 121, argument_list; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:gene_name; 125, identifier:upper; 126, argument_list; 127, block; 127, 128; 127, 132; 128, expression_statement; 128, 129; 129, augmented_assignment:+=; 129, 130; 129, 131; 130, identifier:matched; 131, integer:1; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:protein_map; 136, identifier:agent_text; 137, dictionary; 137, 138; 137, 141; 138, pair; 138, 139; 138, 140; 139, string:'TEXT'; 140, identifier:agent_text; 141, pair; 141, 142; 141, 143; 142, string:'UP'; 143, identifier:uniprot_id; 144, else_clause; 144, 145; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, augmented_assignment:+=; 147, 148; 147, 149; 148, identifier:unmatched; 149, integer:1; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:logger; 154, identifier:info; 155, argument_list; 155, 156; 156, binary_operator:%; 156, 157; 156, 158; 157, string:'Exact matches for %d proteins'; 158, identifier:matched; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:logger; 163, identifier:info; 164, argument_list; 164, 165; 165, binary_operator:%; 165, 166; 165, 167; 166, string:'No match (or no gene name) for %d proteins'; 167, identifier:unmatched; 168, return_statement; 168, 169; 169, identifier:protein_map | def protein_map_from_twg(twg):
"""Build map of entity texts to validate protein grounding.
Looks at the grounding of the entity texts extracted from the statements
and finds proteins where there is grounding to a human protein that maps to
an HGNC name that is an exact match to the entity text. Returns a dict that
can be used to update/expand the grounding map.
Parameters
----------
twg : list of tuple
list of tuples of the form output by agent_texts_with_grounding
Returns
-------
protein_map : dict
dict keyed on agent text with associated values
{'TEXT': agent_text, 'UP': uniprot_id}. Entries are for agent texts
where the grounding map was able to find human protein grounded to
this agent_text in Uniprot.
"""
protein_map = {}
unmatched = 0
matched = 0
logger.info('Building grounding map for human proteins')
for agent_text, grounding_list, _ in twg:
# If 'UP' (Uniprot) not one of the grounding entries for this text,
# then we skip it.
if 'UP' not in [entry[0] for entry in grounding_list]:
continue
# Otherwise, collect all the Uniprot IDs for this protein.
uniprot_ids = [entry[1] for entry in grounding_list
if entry[0] == 'UP']
# For each Uniprot ID, look up the species
for uniprot_id in uniprot_ids:
# If it's not a human protein, skip it
mnemonic = uniprot_client.get_mnemonic(uniprot_id)
if mnemonic is None or not mnemonic.endswith('_HUMAN'):
continue
# Otherwise, look up the gene name in HGNC and match against the
# agent text
gene_name = uniprot_client.get_gene_name(uniprot_id)
if gene_name is None:
unmatched += 1
continue
if agent_text.upper() == gene_name.upper():
matched += 1
protein_map[agent_text] = {'TEXT': agent_text,
'UP': uniprot_id}
else:
unmatched += 1
logger.info('Exact matches for %d proteins' % matched)
logger.info('No match (or no gene name) for %d proteins' % unmatched)
return protein_map |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:save_sentences; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:twg; 5, identifier:stmts; 6, identifier:filename; 7, default_parameter; 7, 8; 7, 9; 8, identifier:agent_limit; 9, integer:300; 10, block; 10, 11; 10, 13; 10, 17; 10, 27; 10, 31; 10, 40; 10, 76; 10, 77; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:sentences; 16, list:[]; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:unmapped_texts; 20, list_comprehension; 20, 21; 20, 24; 21, subscript; 21, 22; 21, 23; 22, identifier:t; 23, integer:0; 24, for_in_clause; 24, 25; 24, 26; 25, identifier:t; 26, identifier:twg; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:counter; 30, integer:0; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:logger; 35, identifier:info; 36, argument_list; 36, 37; 37, binary_operator:%; 37, 38; 37, 39; 38, string:'Getting sentences for top %d unmapped agent texts.'; 39, identifier:agent_limit; 40, for_statement; 40, 41; 40, 42; 40, 43; 41, identifier:text; 42, identifier:unmapped_texts; 43, block; 43, 44; 43, 52; 43, 66; 43, 70; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:agent_sentences; 47, call; 47, 48; 47, 49; 48, identifier:get_sentences_for_agent; 49, argument_list; 49, 50; 49, 51; 50, identifier:text; 51, identifier:stmts; 52, expression_statement; 52, 53; 53, augmented_assignment:+=; 53, 54; 53, 55; 54, identifier:sentences; 55, call; 55, 56; 55, 57; 56, identifier:map; 57, argument_list; 57, 58; 57, 65; 58, lambda; 58, 59; 58, 61; 59, lambda_parameters; 59, 60; 60, identifier:tup; 61, binary_operator:+; 61, 62; 61, 64; 62, tuple; 62, 63; 63, identifier:text; 64, identifier:tup; 65, identifier:agent_sentences; 66, expression_statement; 66, 67; 67, augmented_assignment:+=; 67, 68; 67, 69; 68, identifier:counter; 69, integer:1; 70, if_statement; 70, 71; 70, 74; 71, comparison_operator:>=; 71, 72; 71, 73; 72, identifier:counter; 73, identifier:agent_limit; 74, block; 74, 75; 75, break_statement; 76, comment; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:write_unicode_csv; 80, argument_list; 80, 81; 80, 82; 80, 83; 80, 86; 80, 89; 80, 94; 81, identifier:filename; 82, identifier:sentences; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:delimiter; 85, string:','; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:quotechar; 88, string:'"'; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:quoting; 91, attribute; 91, 92; 91, 93; 92, identifier:csv; 93, identifier:QUOTE_MINIMAL; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:lineterminator; 96, string:'\r\n' | def save_sentences(twg, stmts, filename, agent_limit=300):
"""Write evidence sentences for stmts with ungrounded agents to csv file.
Parameters
----------
twg: list of tuple
list of tuples of ungrounded agent_texts with counts of the
number of times they are mentioned in the list of statements.
Should be sorted in descending order by the counts.
This is of the form output by the function ungrounded texts.
stmts: list of :py:class:`indra.statements.Statement`
filename : str
Path to output file
agent_limit : Optional[int]
Number of agents to include in output file. Takes the top agents
by count.
"""
sentences = []
unmapped_texts = [t[0] for t in twg]
counter = 0
logger.info('Getting sentences for top %d unmapped agent texts.' %
agent_limit)
for text in unmapped_texts:
agent_sentences = get_sentences_for_agent(text, stmts)
sentences += map(lambda tup: (text,) + tup, agent_sentences)
counter += 1
if counter >= agent_limit:
break
# Write sentences to CSV file
write_unicode_csv(filename, sentences, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL, lineterminator='\r\n') |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_text_for_grounding; 3, parameters; 3, 4; 3, 5; 4, identifier:stmt; 5, identifier:agent_text; 6, block; 6, 7; 6, 9; 6, 13; 6, 14; 6, 107; 6, 108; 6, 155; 6, 156; 6, 180; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:text; 12, None; 13, comment; 14, try_statement; 14, 15; 14, 94; 15, block; 15, 16; 15, 24; 15, 31; 15, 41; 15, 42; 15, 63; 15, 72; 15, 79; 15, 89; 16, import_from_statement; 16, 17; 16, 21; 16, 22; 17, dotted_name; 17, 18; 17, 19; 17, 20; 18, identifier:indra_db; 19, identifier:util; 20, identifier:content_scripts; 21, line_continuation:\; 22, dotted_name; 22, 23; 23, identifier:get_text_content_from_text_refs; 24, import_from_statement; 24, 25; 24, 29; 25, dotted_name; 25, 26; 25, 27; 25, 28; 26, identifier:indra; 27, identifier:literature; 28, identifier:deft_tools; 29, dotted_name; 29, 30; 30, identifier:universal_extract_text; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:refs; 34, attribute; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:stmt; 38, identifier:evidence; 39, integer:0; 40, identifier:text_refs; 41, comment; 42, if_statement; 42, 43; 42, 50; 43, attribute; 43, 44; 43, 49; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:stmt; 47, identifier:evidence; 48, integer:0; 49, identifier:pmid; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:refs; 55, string:'PMID'; 56, attribute; 56, 57; 56, 62; 57, subscript; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:stmt; 60, identifier:evidence; 61, integer:0; 62, identifier:pmid; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:logger; 67, identifier:info; 68, argument_list; 68, 69; 69, binary_operator:%; 69, 70; 69, 71; 70, string:'Obtaining text for disambiguation with refs: %s'; 71, identifier:refs; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:content; 75, call; 75, 76; 75, 77; 76, identifier:get_text_content_from_text_refs; 77, argument_list; 77, 78; 78, identifier:refs; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:text; 82, call; 82, 83; 82, 84; 83, identifier:universal_extract_text; 84, argument_list; 84, 85; 84, 86; 85, identifier:content; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:contains; 88, identifier:agent_text; 89, if_statement; 89, 90; 89, 91; 90, identifier:text; 91, block; 91, 92; 92, return_statement; 92, 93; 93, identifier:text; 94, except_clause; 94, 95; 94, 99; 95, as_pattern; 95, 96; 95, 97; 96, identifier:Exception; 97, as_pattern_target; 97, 98; 98, identifier:e; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:logger; 104, identifier:info; 105, argument_list; 105, 106; 106, string:'Could not get text for disambiguation from DB.'; 107, comment; 108, if_statement; 108, 109; 108, 112; 109, comparison_operator:is; 109, 110; 109, 111; 110, identifier:text; 111, None; 112, block; 112, 113; 112, 119; 112, 129; 113, import_from_statement; 113, 114; 113, 117; 114, dotted_name; 114, 115; 114, 116; 115, identifier:indra; 116, identifier:literature; 117, dotted_name; 117, 118; 118, identifier:pubmed_client; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:pmid; 122, attribute; 122, 123; 122, 128; 123, subscript; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:stmt; 126, identifier:evidence; 127, integer:0; 128, identifier:pmid; 129, if_statement; 129, 130; 129, 131; 130, identifier:pmid; 131, block; 131, 132; 131, 141; 131, 150; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:logger; 136, identifier:info; 137, argument_list; 137, 138; 138, binary_operator:%; 138, 139; 138, 140; 139, string:'Obtaining abstract for disambiguation for PMID%s'; 140, identifier:pmid; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:text; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:pubmed_client; 147, identifier:get_abstract; 148, argument_list; 148, 149; 149, identifier:pmid; 150, if_statement; 150, 151; 150, 152; 151, identifier:text; 152, block; 152, 153; 153, return_statement; 153, 154; 154, identifier:text; 155, comment; 156, if_statement; 156, 157; 156, 160; 157, comparison_operator:is; 157, 158; 157, 159; 158, identifier:text; 159, None; 160, block; 160, 161; 160, 168; 160, 178; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:logger; 165, identifier:info; 166, argument_list; 166, 167; 167, string:'Falling back on sentence-based disambiguation'; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:text; 171, attribute; 171, 172; 171, 177; 172, subscript; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:stmt; 175, identifier:evidence; 176, integer:0; 177, identifier:text; 178, return_statement; 178, 179; 179, identifier:text; 180, return_statement; 180, 181; 181, None | def _get_text_for_grounding(stmt, agent_text):
"""Get text context for Deft disambiguation
If the INDRA database is available, attempts to get the fulltext from
which the statement was extracted. If the fulltext is not available, the
abstract is returned. If the indra database is not available, uses the
pubmed client to get the abstract. If no abstract can be found, falls back
on returning the evidence text for the statement.
Parameters
----------
stmt : py:class:`indra.statements.Statement`
Statement with agent we seek to disambiguate.
agent_text : str
Agent text that needs to be disambiguated
Returns
-------
text : str
Text for Feft disambiguation
"""
text = None
# First we will try to get content from the DB
try:
from indra_db.util.content_scripts \
import get_text_content_from_text_refs
from indra.literature.deft_tools import universal_extract_text
refs = stmt.evidence[0].text_refs
# Prioritize the pmid attribute if given
if stmt.evidence[0].pmid:
refs['PMID'] = stmt.evidence[0].pmid
logger.info('Obtaining text for disambiguation with refs: %s' %
refs)
content = get_text_content_from_text_refs(refs)
text = universal_extract_text(content, contains=agent_text)
if text:
return text
except Exception as e:
logger.info('Could not get text for disambiguation from DB.')
# If that doesn't work, we try PubMed next
if text is None:
from indra.literature import pubmed_client
pmid = stmt.evidence[0].pmid
if pmid:
logger.info('Obtaining abstract for disambiguation for PMID%s' %
pmid)
text = pubmed_client.get_abstract(pmid)
if text:
return text
# Finally, falling back on the evidence sentence
if text is None:
logger.info('Falling back on sentence-based disambiguation')
text = stmt.evidence[0].text
return text
return None |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:map_agents_for_stmt; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:stmt; 6, default_parameter; 6, 7; 6, 8; 7, identifier:do_rename; 8, True; 9, block; 9, 10; 9, 12; 9, 19; 9, 20; 9, 21; 9, 29; 9, 153; 9, 160; 9, 161; 9, 199; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:mapped_stmt; 15, call; 15, 16; 15, 17; 16, identifier:deepcopy; 17, argument_list; 17, 18; 18, identifier:stmt; 19, comment; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:agent_list; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:mapped_stmt; 27, identifier:agent_list; 28, argument_list; 29, for_statement; 29, 30; 29, 33; 29, 37; 30, pattern_list; 30, 31; 30, 32; 31, identifier:idx; 32, identifier:agent; 33, call; 33, 34; 33, 35; 34, identifier:enumerate; 35, argument_list; 35, 36; 36, identifier:agent_list; 37, block; 37, 38; 37, 44; 37, 55; 37, 61; 37, 73; 37, 74; 37, 116; 37, 123; 37, 124; 37, 125; 37, 147; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:is; 39, 40; 39, 41; 40, identifier:agent; 41, None; 42, block; 42, 43; 43, continue_statement; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:agent_txt; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:agent; 51, identifier:db_refs; 52, identifier:get; 53, argument_list; 53, 54; 54, string:'TEXT'; 55, if_statement; 55, 56; 55, 59; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:agent_txt; 58, None; 59, block; 59, 60; 60, continue_statement; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, pattern_list; 63, 64; 63, 65; 64, identifier:new_agent; 65, identifier:maps_to_none; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:map_agent; 70, argument_list; 70, 71; 70, 72; 71, identifier:agent; 72, identifier:do_rename; 73, comment; 74, if_statement; 74, 75; 74, 82; 75, boolean_operator:and; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:use_deft; 79, comparison_operator:in; 79, 80; 79, 81; 80, identifier:agent_txt; 81, identifier:deft_disambiguators; 82, block; 82, 83; 83, try_statement; 83, 84; 83, 94; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:run_deft_disambiguation; 88, argument_list; 88, 89; 88, 90; 88, 91; 88, 92; 88, 93; 89, identifier:mapped_stmt; 90, identifier:agent_list; 91, identifier:idx; 92, identifier:new_agent; 93, identifier:agent_txt; 94, except_clause; 94, 95; 94, 99; 95, as_pattern; 95, 96; 95, 97; 96, identifier:Exception; 97, as_pattern_target; 97, 98; 98, identifier:e; 99, block; 99, 100; 99, 109; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:logger; 104, identifier:error; 105, argument_list; 105, 106; 106, concatenated_string; 106, 107; 106, 108; 107, string:'There was an error during Deft'; 108, string:' disambiguation.'; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:logger; 113, identifier:error; 114, argument_list; 114, 115; 115, identifier:e; 116, if_statement; 116, 117; 116, 118; 116, 119; 116, 120; 117, identifier:maps_to_none; 118, comment; 119, comment; 120, block; 120, 121; 121, return_statement; 121, 122; 122, None; 123, comment; 124, comment; 125, if_statement; 125, 126; 125, 138; 126, boolean_operator:and; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:new_agent; 129, None; 130, comparison_operator:==; 130, 131; 130, 137; 131, call; 131, 132; 131, 133; 132, identifier:len; 133, argument_list; 133, 134; 134, attribute; 134, 135; 134, 136; 135, identifier:new_agent; 136, identifier:bound_conditions; 137, integer:0; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:new_agent; 143, identifier:bound_conditions; 144, attribute; 144, 145; 144, 146; 145, identifier:agent; 146, identifier:bound_conditions; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 152; 149, subscript; 149, 150; 149, 151; 150, identifier:agent_list; 151, identifier:idx; 152, identifier:new_agent; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:mapped_stmt; 157, identifier:set_agent_list; 158, argument_list; 158, 159; 159, identifier:agent_list; 160, comment; 161, for_statement; 161, 162; 161, 163; 161, 164; 162, identifier:agent; 163, identifier:agent_list; 164, block; 164, 165; 165, if_statement; 165, 166; 165, 169; 166, comparison_operator:is; 166, 167; 166, 168; 167, identifier:agent; 168, None; 169, block; 169, 170; 170, for_statement; 170, 171; 170, 172; 170, 175; 171, identifier:bc; 172, attribute; 172, 173; 172, 174; 173, identifier:agent; 174, identifier:bound_conditions; 175, block; 175, 176; 175, 192; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 183; 178, pattern_list; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:bc; 181, identifier:agent; 182, identifier:maps_to_none; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:self; 186, identifier:map_agent; 187, argument_list; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:bc; 190, identifier:agent; 191, identifier:do_rename; 192, if_statement; 192, 193; 192, 194; 192, 195; 192, 196; 193, identifier:maps_to_none; 194, comment; 195, comment; 196, block; 196, 197; 197, return_statement; 197, 198; 198, None; 199, return_statement; 199, 200; 200, identifier:mapped_stmt | def map_agents_for_stmt(self, stmt, do_rename=True):
"""Return a new Statement whose agents have been grounding mapped.
Parameters
----------
stmt : :py:class:`indra.statements.Statement`
The Statement whose agents need mapping.
do_rename: Optional[bool]
If True, the Agent name is updated based on the mapped grounding.
If do_rename is True the priority for setting the name is
FamPlex ID, HGNC symbol, then the gene name
from Uniprot. Default: True
Returns
-------
mapped_stmt : :py:class:`indra.statements.Statement`
The mapped Statement.
"""
mapped_stmt = deepcopy(stmt)
# Iterate over the agents
# Update agents directly participating in the statement
agent_list = mapped_stmt.agent_list()
for idx, agent in enumerate(agent_list):
if agent is None:
continue
agent_txt = agent.db_refs.get('TEXT')
if agent_txt is None:
continue
new_agent, maps_to_none = self.map_agent(agent, do_rename)
# Check if a deft model exists for agent text
if self.use_deft and agent_txt in deft_disambiguators:
try:
run_deft_disambiguation(mapped_stmt, agent_list, idx,
new_agent, agent_txt)
except Exception as e:
logger.error('There was an error during Deft'
' disambiguation.')
logger.error(e)
if maps_to_none:
# Skip the entire statement if the agent maps to None in the
# grounding map
return None
# If the old agent had bound conditions, but the new agent does
# not, copy the bound conditions over
if new_agent is not None and len(new_agent.bound_conditions) == 0:
new_agent.bound_conditions = agent.bound_conditions
agent_list[idx] = new_agent
mapped_stmt.set_agent_list(agent_list)
# Update agents in the bound conditions
for agent in agent_list:
if agent is not None:
for bc in agent.bound_conditions:
bc.agent, maps_to_none = self.map_agent(bc.agent,
do_rename)
if maps_to_none:
# Skip the entire statement if the agent maps to None
# in the grounding map
return None
return mapped_stmt |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:map_agent; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:agent; 6, identifier:do_rename; 7, block; 7, 8; 7, 10; 7, 21; 7, 32; 7, 51; 7, 52; 7, 53; 7, 78; 7, 79; 7, 80; 7, 113; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:agent_text; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:agent; 17, identifier:db_refs; 18, identifier:get; 19, argument_list; 19, 20; 20, string:'TEXT'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:mapped_to_agent_json; 24, call; 24, 25; 24, 30; 25, attribute; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:agent_map; 29, identifier:get; 30, argument_list; 30, 31; 31, identifier:agent_text; 32, if_statement; 32, 33; 32, 34; 33, identifier:mapped_to_agent_json; 34, block; 34, 35; 34, 47; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 36, 39; 37, identifier:mapped_to_agent; 38, line_continuation:\; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:Agent; 42, identifier:_from_json; 43, argument_list; 43, 44; 44, subscript; 44, 45; 44, 46; 45, identifier:mapped_to_agent_json; 46, string:'agent'; 47, return_statement; 47, 48; 48, expression_list; 48, 49; 48, 50; 49, identifier:mapped_to_agent; 50, False; 51, comment; 52, comment; 53, if_statement; 53, 54; 53, 63; 53, 72; 54, comparison_operator:in; 54, 55; 54, 56; 55, identifier:agent_text; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:gm; 61, identifier:keys; 62, argument_list; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:map_db_refs; 67, subscript; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:gm; 71, identifier:agent_text; 72, else_clause; 72, 73; 73, block; 73, 74; 74, return_statement; 74, 75; 75, expression_list; 75, 76; 75, 77; 76, identifier:agent; 77, False; 78, comment; 79, comment; 80, if_statement; 80, 81; 80, 84; 80, 85; 80, 86; 80, 100; 80, 101; 81, comparison_operator:is; 81, 82; 81, 83; 82, identifier:map_db_refs; 83, None; 84, comment; 85, comment; 86, block; 86, 87; 86, 96; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:logger; 91, identifier:debug; 92, argument_list; 92, 93; 93, binary_operator:%; 93, 94; 93, 95; 94, string:"Skipping %s"; 95, identifier:agent_text; 96, return_statement; 96, 97; 97, expression_list; 97, 98; 97, 99; 98, None; 99, True; 100, comment; 101, else_clause; 101, 102; 101, 103; 102, comment; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:update_agent_db_refs; 109, argument_list; 109, 110; 109, 111; 109, 112; 110, identifier:agent; 111, identifier:agent_text; 112, identifier:do_rename; 113, return_statement; 113, 114; 114, expression_list; 114, 115; 114, 116; 115, identifier:agent; 116, False | def map_agent(self, agent, do_rename):
"""Return the given Agent with its grounding mapped.
This function grounds a single agent. It returns the new Agent object
(which might be a different object if we load a new agent state
from json) or the same object otherwise.
Parameters
----------
agent : :py:class:`indra.statements.Agent`
The Agent to map.
do_rename: bool
If True, the Agent name is updated based on the mapped grounding.
If do_rename is True the priority for setting the name is
FamPlex ID, HGNC symbol, then the gene name
from Uniprot.
Returns
-------
grounded_agent : :py:class:`indra.statements.Agent`
The grounded Agent.
maps_to_none : bool
True if the Agent is in the grounding map and maps to None.
"""
agent_text = agent.db_refs.get('TEXT')
mapped_to_agent_json = self.agent_map.get(agent_text)
if mapped_to_agent_json:
mapped_to_agent = \
Agent._from_json(mapped_to_agent_json['agent'])
return mapped_to_agent, False
# Look this string up in the grounding map
# If not in the map, leave agent alone and continue
if agent_text in self.gm.keys():
map_db_refs = self.gm[agent_text]
else:
return agent, False
# If it's in the map but it maps to None, then filter out
# this statement by skipping it
if map_db_refs is None:
# Increase counter if this statement has not already
# been skipped via another agent
logger.debug("Skipping %s" % agent_text)
return None, True
# If it has a value that's not None, map it and add it
else:
# Otherwise, update the agent's db_refs field
self.update_agent_db_refs(agent, agent_text, do_rename)
return agent, False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:rename_agents; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:stmts; 6, block; 6, 7; 6, 9; 6, 10; 6, 17; 6, 18; 6, 133; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:mapped_stmts; 13, call; 13, 14; 13, 15; 14, identifier:deepcopy; 15, argument_list; 15, 16; 16, identifier:stmts; 17, comment; 18, for_statement; 18, 19; 18, 22; 18, 26; 18, 27; 19, pattern_list; 19, 20; 19, 21; 20, identifier:_; 21, identifier:stmt; 22, call; 22, 23; 22, 24; 23, identifier:enumerate; 24, argument_list; 24, 25; 25, identifier:mapped_stmts; 26, comment; 27, block; 27, 28; 28, for_statement; 28, 29; 28, 30; 28, 35; 29, identifier:agent; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:stmt; 33, identifier:agent_list; 34, argument_list; 35, block; 35, 36; 35, 42; 35, 43; 35, 130; 35, 131; 35, 132; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:agent; 39, None; 40, block; 40, 41; 41, continue_statement; 42, comment; 43, if_statement; 43, 44; 43, 52; 43, 66; 43, 67; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:agent; 48, identifier:db_refs; 49, identifier:get; 50, argument_list; 50, 51; 51, string:'FPLX'; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:agent; 57, identifier:name; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:agent; 62, identifier:db_refs; 63, identifier:get; 64, argument_list; 64, 65; 65, string:'FPLX'; 66, comment; 67, elif_clause; 67, 68; 67, 76; 67, 77; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:agent; 72, identifier:db_refs; 73, identifier:get; 74, argument_list; 74, 75; 75, string:'UP'; 76, comment; 77, block; 77, 78; 77, 97; 77, 126; 77, 127; 77, 128; 77, 129; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:gene_name; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:uniprot_client; 84, identifier:get_gene_name; 85, argument_list; 85, 86; 85, 94; 86, call; 86, 87; 86, 92; 87, attribute; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:agent; 90, identifier:db_refs; 91, identifier:get; 92, argument_list; 92, 93; 93, string:'UP'; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:web_fallback; 96, False; 97, if_statement; 97, 98; 97, 99; 98, identifier:gene_name; 99, block; 99, 100; 99, 106; 99, 115; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:agent; 104, identifier:name; 105, identifier:gene_name; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:hgnc_id; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:hgnc_client; 112, identifier:get_hgnc_id; 113, argument_list; 113, 114; 114, identifier:gene_name; 115, if_statement; 115, 116; 115, 117; 116, identifier:hgnc_id; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 125; 120, subscript; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:agent; 123, identifier:db_refs; 124, string:'HGNC'; 125, identifier:hgnc_id; 126, comment; 127, comment; 128, comment; 129, comment; 130, comment; 131, comment; 132, comment; 133, return_statement; 133, 134; 134, identifier:mapped_stmts | def rename_agents(self, stmts):
"""Return a list of mapped statements with updated agent names.
Creates a new list of statements without modifying the original list.
The agents in a statement should be renamed if the grounding map has
updated their db_refs. If an agent contains a FamPlex grounding, the
FamPlex ID is used as a name. Otherwise if it contains a Uniprot ID,
an attempt is made to find the associated HGNC gene name. If one can
be found it is used as the agent name and the associated HGNC ID is
added as an entry to the db_refs. If neither a FamPlex ID or HGNC name
can be found, falls back to the original name.
Parameters
----------
stmts : list of :py:class:`indra.statements.Statement`
List of statements whose Agents need their names updated.
Returns
-------
mapped_stmts : list of :py:class:`indra.statements.Statement`
A new list of Statements with updated Agent names
"""
# Make a copy of the stmts
mapped_stmts = deepcopy(stmts)
# Iterate over the statements
for _, stmt in enumerate(mapped_stmts):
# Iterate over the agents
for agent in stmt.agent_list():
if agent is None:
continue
# If there's a FamPlex ID, prefer that for the name
if agent.db_refs.get('FPLX'):
agent.name = agent.db_refs.get('FPLX')
# Take a HGNC name from Uniprot next
elif agent.db_refs.get('UP'):
# Try for the gene name
gene_name = uniprot_client.get_gene_name(
agent.db_refs.get('UP'),
web_fallback=False)
if gene_name:
agent.name = gene_name
hgnc_id = hgnc_client.get_hgnc_id(gene_name)
if hgnc_id:
agent.db_refs['HGNC'] = hgnc_id
# Take the text string
#if agent.db_refs.get('TEXT'):
# agent.name = agent.db_refs.get('TEXT')
# If this fails, then we continue with no change
# Fall back to the text string
#elif agent.db_refs.get('TEXT'):
# agent.name = agent.db_refs.get('TEXT')
return mapped_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_ptms; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:ptm_df; 6, block; 6, 7; 6, 9; 6, 16; 6, 17; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:logger; 13, identifier:info; 14, argument_list; 14, 15; 15, string:'Processing PTMs...'; 16, comment; 17, for_statement; 17, 18; 17, 21; 17, 26; 17, 27; 17, 28; 18, pattern_list; 18, 19; 18, 20; 19, identifier:ix; 20, identifier:row; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:ptm_df; 24, identifier:iterrows; 25, argument_list; 26, comment; 27, comment; 28, block; 28, 29; 28, 37; 28, 43; 28, 44; 28, 45; 28, 61; 28, 62; 28, 68; 28, 77; 28, 86; 28, 95; 28, 104; 28, 128; 28, 129; 28, 130; 28, 131; 28, 133; 28, 135; 28, 148; 28, 149; 28, 171; 28, 184; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:ptm_class; 32, subscript; 32, 33; 32, 34; 33, identifier:_ptm_map; 34, subscript; 34, 35; 34, 36; 35, identifier:row; 36, string:'MOD_TYPE'; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:ptm_class; 40, None; 41, block; 41, 42; 42, continue_statement; 43, comment; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:sub_ag; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:_make_agent; 52, argument_list; 52, 53; 52, 56; 53, subscript; 53, 54; 53, 55; 54, identifier:row; 55, string:'HPRD_ID'; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:refseq_id; 58, subscript; 58, 59; 58, 60; 59, identifier:row; 60, string:'REFSEQ_PROTEIN'; 61, comment; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:sub_ag; 65, None; 66, block; 66, 67; 67, continue_statement; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:enz_id; 71, call; 71, 72; 71, 73; 72, identifier:_nan_to_none; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:row; 76, string:'ENZ_HPRD_ID'; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:enz_ag; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_make_agent; 84, argument_list; 84, 85; 85, identifier:enz_id; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:res; 89, call; 89, 90; 89, 91; 90, identifier:_nan_to_none; 91, argument_list; 91, 92; 92, subscript; 92, 93; 92, 94; 93, identifier:row; 94, string:'RESIDUE'; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:pos; 98, call; 98, 99; 98, 100; 99, identifier:_nan_to_none; 100, argument_list; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:row; 103, string:'POSITION'; 104, if_statement; 104, 105; 104, 112; 105, boolean_operator:and; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:pos; 108, None; 109, comparison_operator:in; 109, 110; 109, 111; 110, string:';'; 111, identifier:pos; 112, block; 112, 113; 112, 124; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, pattern_list; 115, 116; 115, 117; 116, identifier:pos; 117, identifier:dash; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:pos; 121, identifier:split; 122, argument_list; 122, 123; 123, string:';'; 124, assert_statement; 124, 125; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:dash; 127, string:'-'; 128, comment; 129, comment; 130, comment; 131, assert_statement; 131, 132; 132, identifier:res; 133, assert_statement; 133, 134; 134, identifier:pos; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:motif_dict; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_get_seq_motif; 142, argument_list; 142, 143; 142, 146; 142, 147; 143, subscript; 143, 144; 143, 145; 144, identifier:row; 145, string:'REFSEQ_PROTEIN'; 146, identifier:res; 147, identifier:pos; 148, comment; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:ev_list; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_get_evidence; 156, argument_list; 156, 157; 156, 160; 156, 163; 156, 166; 156, 169; 156, 170; 157, subscript; 157, 158; 157, 159; 158, identifier:row; 159, string:'HPRD_ID'; 160, subscript; 160, 161; 160, 162; 161, identifier:row; 162, string:'HPRD_ISOFORM'; 163, subscript; 163, 164; 163, 165; 164, identifier:row; 165, string:'PMIDS'; 166, subscript; 166, 167; 166, 168; 167, identifier:row; 168, string:'EVIDENCE'; 169, string:'ptms'; 170, identifier:motif_dict; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:stmt; 174, call; 174, 175; 174, 176; 175, identifier:ptm_class; 176, argument_list; 176, 177; 176, 178; 176, 179; 176, 180; 176, 181; 177, identifier:enz_ag; 178, identifier:sub_ag; 179, identifier:res; 180, identifier:pos; 181, keyword_argument; 181, 182; 181, 183; 182, identifier:evidence; 183, identifier:ev_list; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 191; 186, attribute; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:statements; 190, identifier:append; 191, argument_list; 191, 192; 192, identifier:stmt | def get_ptms(self, ptm_df):
"""Generate Modification statements from the HPRD PTM data.
Parameters
----------
ptm_df : pandas.DataFrame
DataFrame loaded from the POST_TRANSLATIONAL_MODIFICATIONS.txt file.
"""
logger.info('Processing PTMs...')
# Iterate over the rows of the dataframe
for ix, row in ptm_df.iterrows():
# Check the modification type; if we can't make an INDRA statement
# for it, then skip it
ptm_class = _ptm_map[row['MOD_TYPE']]
if ptm_class is None:
continue
# Use the Refseq protein ID for the substrate to make sure that
# we get the right Uniprot ID for the isoform
sub_ag = self._make_agent(row['HPRD_ID'],
refseq_id=row['REFSEQ_PROTEIN'])
# If we couldn't get the substrate, skip the statement
if sub_ag is None:
continue
enz_id = _nan_to_none(row['ENZ_HPRD_ID'])
enz_ag = self._make_agent(enz_id)
res = _nan_to_none(row['RESIDUE'])
pos = _nan_to_none(row['POSITION'])
if pos is not None and ';' in pos:
pos, dash = pos.split(';')
assert dash == '-'
# As a fallback for later site mapping, we also get the protein
# sequence information in case there was a problem with the
# RefSeq->Uniprot mapping
assert res
assert pos
motif_dict = self._get_seq_motif(row['REFSEQ_PROTEIN'], res, pos)
# Get evidence
ev_list = self._get_evidence(
row['HPRD_ID'], row['HPRD_ISOFORM'], row['PMIDS'],
row['EVIDENCE'], 'ptms', motif_dict)
stmt = ptm_class(enz_ag, sub_ag, res, pos, evidence=ev_list)
self.statements.append(stmt) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_process_interaction; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:self; 5, identifier:source_id; 6, identifier:interaction; 7, identifier:text; 8, identifier:pmid; 9, identifier:extra_annotations; 10, block; 10, 11; 10, 13; 10, 23; 10, 30; 10, 37; 10, 38; 10, 39; 10, 48; 10, 57; 10, 58; 10, 65; 10, 81; 10, 87; 10, 93; 10, 117; 10, 118; 10, 119; 10, 120; 10, 124; 10, 147; 10, 156; 10, 160; 10, 205; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:verb; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, subscript; 18, 19; 18, 20; 19, identifier:interaction; 20, integer:0; 21, identifier:lower; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:subj; 26, subscript; 26, 27; 26, 28; 27, identifier:interaction; 28, unary_operator:-; 28, 29; 29, integer:2; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:obj; 33, subscript; 33, 34; 33, 35; 34, identifier:interaction; 35, unary_operator:-; 35, 36; 36, integer:1; 37, comment; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:subj; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:_make_agent; 46, argument_list; 46, 47; 47, identifier:subj; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:obj; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:_make_agent; 55, argument_list; 55, 56; 56, identifier:obj; 57, comment; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:annotations; 61, call; 61, 62; 61, 63; 62, identifier:deepcopy; 63, argument_list; 63, 64; 64, identifier:extra_annotations; 65, if_statement; 65, 66; 65, 69; 66, comparison_operator:in; 66, 67; 66, 68; 67, string:'interaction'; 68, identifier:extra_annotations; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:logger; 74, identifier:warning; 75, argument_list; 75, 76; 76, binary_operator:+; 76, 77; 76, 80; 77, binary_operator:+; 77, 78; 77, 79; 78, string:"'interaction' key of extra_annotations ignored"; 79, string:" since this is reserved for storing the raw ISI "; 80, string:"input."; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, subscript; 83, 84; 83, 85; 84, identifier:annotations; 85, string:'source_id'; 86, identifier:source_id; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:annotations; 91, string:'interaction'; 92, identifier:interaction; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:ev; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:ist; 99, identifier:Evidence; 100, argument_list; 100, 101; 100, 104; 100, 107; 100, 114; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:source_api; 103, string:'isi'; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:pmid; 106, identifier:pmid; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:text; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:text; 112, identifier:rstrip; 113, argument_list; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:annotations; 116, identifier:annotations; 117, comment; 118, comment; 119, comment; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:cataylst_specified; 123, False; 124, if_statement; 124, 125; 124, 131; 125, comparison_operator:==; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, identifier:interaction; 130, integer:4; 131, block; 131, 132; 131, 138; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:catalyst; 135, subscript; 135, 136; 135, 137; 136, identifier:interaction; 137, integer:1; 138, if_statement; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:catalyst; 141, None; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:cataylst_specified; 146, True; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:self; 152, identifier:verbs; 153, identifier:add; 154, argument_list; 154, 155; 155, identifier:verb; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:statement; 159, None; 160, if_statement; 160, 161; 160, 164; 161, comparison_operator:in; 161, 162; 161, 163; 162, identifier:verb; 163, identifier:verb_to_statement_type; 164, block; 164, 165; 164, 171; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:statement_class; 168, subscript; 168, 169; 168, 170; 169, identifier:verb_to_statement_type; 170, identifier:verb; 171, if_statement; 171, 172; 171, 177; 171, 192; 172, comparison_operator:==; 172, 173; 172, 174; 173, identifier:statement_class; 174, attribute; 174, 175; 174, 176; 175, identifier:ist; 176, identifier:Complex; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:statement; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:ist; 184, identifier:Complex; 185, argument_list; 185, 186; 185, 189; 186, list:[subj, obj]; 186, 187; 186, 188; 187, identifier:subj; 188, identifier:obj; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:evidence; 191, identifier:ev; 192, else_clause; 192, 193; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:statement; 197, call; 197, 198; 197, 199; 198, identifier:statement_class; 199, argument_list; 199, 200; 199, 201; 199, 202; 200, identifier:subj; 201, identifier:obj; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:evidence; 204, identifier:ev; 205, if_statement; 205, 206; 205, 209; 205, 210; 205, 211; 205, 212; 205, 213; 205, 214; 206, comparison_operator:is; 206, 207; 206, 208; 207, identifier:statement; 208, None; 209, comment; 210, comment; 211, comment; 212, comment; 213, comment; 214, block; 214, 215; 214, 219; 214, 322; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:already_have; 218, False; 219, if_statement; 219, 220; 219, 228; 220, comparison_operator:==; 220, 221; 220, 225; 221, call; 221, 222; 221, 223; 222, identifier:type; 223, argument_list; 223, 224; 224, identifier:statement; 225, attribute; 225, 226; 225, 227; 226, identifier:ist; 227, identifier:Complex; 228, block; 228, 229; 229, for_statement; 229, 230; 229, 231; 229, 234; 230, identifier:old_s; 231, attribute; 231, 232; 231, 233; 232, identifier:self; 233, identifier:statements; 234, block; 234, 235; 234, 245; 234, 255; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:old_id; 238, attribute; 238, 239; 238, 244; 239, subscript; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:statement; 242, identifier:evidence; 243, integer:0; 244, identifier:source_id; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:new_id; 248, attribute; 248, 249; 248, 254; 249, subscript; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:old_s; 252, identifier:evidence; 253, integer:0; 254, identifier:source_id; 255, if_statement; 255, 256; 255, 268; 256, boolean_operator:and; 256, 257; 256, 265; 257, comparison_operator:==; 257, 258; 257, 262; 258, call; 258, 259; 258, 260; 259, identifier:type; 260, argument_list; 260, 261; 261, identifier:old_s; 262, attribute; 262, 263; 262, 264; 263, identifier:ist; 264, identifier:Complex; 265, comparison_operator:==; 265, 266; 265, 267; 266, identifier:old_id; 267, identifier:new_id; 268, block; 268, 269; 268, 284; 268, 291; 268, 305; 268, 312; 269, expression_statement; 269, 270; 270, assignment; 270, 271; 270, 272; 270, 273; 271, identifier:old_statement_members; 272, line_continuation:\; 273, list_comprehension; 273, 274; 273, 279; 274, subscript; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:m; 277, identifier:db_refs; 278, string:'TEXT'; 279, for_in_clause; 279, 280; 279, 281; 280, identifier:m; 281, attribute; 281, 282; 281, 283; 282, identifier:old_s; 283, identifier:members; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:old_statement_members; 287, call; 287, 288; 287, 289; 288, identifier:sorted; 289, argument_list; 289, 290; 290, identifier:old_statement_members; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:new_statement_members; 294, list_comprehension; 294, 295; 294, 300; 295, subscript; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:m; 298, identifier:db_refs; 299, string:'TEXT'; 300, for_in_clause; 300, 301; 300, 302; 301, identifier:m; 302, attribute; 302, 303; 302, 304; 303, identifier:statement; 304, identifier:members; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 308; 307, identifier:new_statement_members; 308, call; 308, 309; 308, 310; 309, identifier:sorted; 310, argument_list; 310, 311; 311, identifier:new_statement_members; 312, if_statement; 312, 313; 312, 316; 313, comparison_operator:==; 313, 314; 313, 315; 314, identifier:old_statement_members; 315, identifier:new_statement_members; 316, block; 316, 317; 316, 321; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:already_have; 320, True; 321, break_statement; 322, if_statement; 322, 323; 322, 325; 323, not_operator; 323, 324; 324, identifier:already_have; 325, block; 325, 326; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 333; 328, attribute; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:self; 331, identifier:statements; 332, identifier:append; 333, argument_list; 333, 334; 334, identifier:statement | def _process_interaction(self, source_id, interaction, text, pmid,
extra_annotations):
"""Process an interaction JSON tuple from the ISI output, and adds up
to one statement to the list of extracted statements.
Parameters
----------
source_id : str
the JSON key corresponding to the sentence in the ISI output
interaction: the JSON list with subject/verb/object information
about the event in the ISI output
text : str
the text of the sentence
pmid : str
the PMID of the article from which the information was extracted
extra_annotations : dict
Additional annotations to add to the statement's evidence,
potentially containing metadata about the source. Annotations
with the key "interaction" will be overridden by the JSON
interaction tuple from the ISI output
"""
verb = interaction[0].lower()
subj = interaction[-2]
obj = interaction[-1]
# Make ungrounded agent objects for the subject and object
# Grounding will happen after all statements are extracted in __init__
subj = self._make_agent(subj)
obj = self._make_agent(obj)
# Make an evidence object
annotations = deepcopy(extra_annotations)
if 'interaction' in extra_annotations:
logger.warning("'interaction' key of extra_annotations ignored" +
" since this is reserved for storing the raw ISI " +
"input.")
annotations['source_id'] = source_id
annotations['interaction'] = interaction
ev = ist.Evidence(source_api='isi',
pmid=pmid,
text=text.rstrip(),
annotations=annotations)
# For binding time interactions, it is said that a catayst might be
# specified. We don't use this for now, but extract in case we want
# to in the future
cataylst_specified = False
if len(interaction) == 4:
catalyst = interaction[1]
if catalyst is not None:
cataylst_specified = True
self.verbs.add(verb)
statement = None
if verb in verb_to_statement_type:
statement_class = verb_to_statement_type[verb]
if statement_class == ist.Complex:
statement = ist.Complex([subj, obj], evidence=ev)
else:
statement = statement_class(subj, obj, evidence=ev)
if statement is not None:
# For Complex statements, the ISI reader produces two events:
# binds(A, B) and binds(B, A)
# We want only one Complex statement for each sentence, so check
# to see if we already have a Complex for this source_id with the
# same members
already_have = False
if type(statement) == ist.Complex:
for old_s in self.statements:
old_id = statement.evidence[0].source_id
new_id = old_s.evidence[0].source_id
if type(old_s) == ist.Complex and old_id == new_id:
old_statement_members = \
[m.db_refs['TEXT'] for m in old_s.members]
old_statement_members = sorted(old_statement_members)
new_statement_members = [m.db_refs['TEXT']
for m in statement.members]
new_statement_members = sorted(new_statement_members)
if old_statement_members == new_statement_members:
already_have = True
break
if not already_have:
self.statements.append(statement) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_regulate_amounts; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 23; 5, 35; 5, 50; 5, 54; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:pb; 14, identifier:controlsExpressionWithTemplateReac; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:s; 19, call; 19, 20; 19, 21; 20, identifier:_bpp; 21, argument_list; 21, 22; 22, string:'Searcher'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:res; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:s; 29, identifier:searchPlain; 30, argument_list; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:model; 34, identifier:p; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:res_array; 38, list_comprehension; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:_match_to_array; 41, argument_list; 41, 42; 42, identifier:m; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:m; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:res; 48, identifier:toArray; 49, argument_list; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:stmts; 53, list:[]; 54, for_statement; 54, 55; 54, 56; 54, 57; 54, 58; 54, 59; 54, 60; 55, identifier:res; 56, identifier:res_array; 57, comment; 58, comment; 59, comment; 60, block; 60, 61; 60, 63; 60, 64; 60, 65; 60, 66; 60, 67; 60, 68; 60, 69; 60, 70; 60, 71; 60, 72; 60, 73; 60, 74; 60, 75; 60, 86; 60, 92; 60, 101; 60, 107; 60, 115; 60, 143; 60, 144; 60, 145; 60, 146; 60, 158; 60, 178; 60, 184; 60, 192; 60, 203; 60, 212; 61, expression_statement; 61, 62; 62, string:'''
controller_er = res[p.indexOf('controller ER')]
generic_controller_er = res[p.indexOf('generic controller ER')]
controller_simple_pe = res[p.indexOf('controller simple PE')]
controller_pe = res[p.indexOf('controller PE')]
control = res[p.indexOf('Control')]
conversion = res[p.indexOf('Conversion')]
input_pe = res[p.indexOf('input PE')]
input_simple_pe = res[p.indexOf('input simple PE')]
changed_generic_er = res[p.indexOf('changed generic ER')]
output_pe = res[p.indexOf('output PE')]
output_simple_pe = res[p.indexOf('output simple PE')]
changed_er = res[p.indexOf('changed ER')]
'''; 63, comment; 64, comment; 65, comment; 66, comment; 67, comment; 68, comment; 69, comment; 70, comment; 71, comment; 72, comment; 73, comment; 74, comment; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:controller; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_get_agents_from_entity; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:res; 85, integer:2; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:controlled_pe; 89, subscript; 89, 90; 89, 91; 90, identifier:res; 91, integer:6; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:controlled; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_get_agents_from_entity; 99, argument_list; 99, 100; 100, identifier:controlled_pe; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:conversion; 104, subscript; 104, 105; 104, 106; 105, identifier:res; 106, integer:5; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:direction; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:conversion; 113, identifier:getTemplateDirection; 114, argument_list; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:is; 116, 117; 116, 118; 117, identifier:direction; 118, None; 119, block; 119, 120; 119, 128; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:direction; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:direction; 126, identifier:name; 127, argument_list; 128, if_statement; 128, 129; 128, 132; 129, comparison_operator:!=; 129, 130; 129, 131; 130, identifier:direction; 131, string:'FORWARD'; 132, block; 132, 133; 132, 142; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:logger; 137, identifier:warning; 138, argument_list; 138, 139; 139, binary_operator:%; 139, 140; 139, 141; 140, string:'Unhandled conversion direction %s'; 141, identifier:direction; 142, continue_statement; 143, comment; 144, comment; 145, comment; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:int_type; 149, call; 149, 150; 149, 157; 150, attribute; 150, 151; 150, 156; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:conversion; 154, identifier:getInteractionType; 155, argument_list; 156, identifier:toArray; 157, argument_list; 158, if_statement; 158, 159; 158, 160; 159, identifier:int_type; 160, block; 160, 161; 161, for_statement; 161, 162; 161, 163; 161, 164; 162, identifier:it; 163, identifier:int_type; 164, block; 164, 165; 165, for_statement; 165, 166; 165, 167; 165, 176; 166, identifier:term; 167, call; 167, 168; 167, 175; 168, attribute; 168, 169; 168, 174; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:it; 172, identifier:getTerm; 173, argument_list; 174, identifier:toArray; 175, argument_list; 176, block; 176, 177; 177, pass_statement; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:control; 181, subscript; 181, 182; 181, 183; 182, identifier:res; 183, integer:4; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:control_type; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:control; 190, identifier:getControlType; 191, argument_list; 192, if_statement; 192, 193; 192, 194; 193, identifier:control_type; 194, block; 194, 195; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:control_type; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:control_type; 201, identifier:name; 202, argument_list; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:ev; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:_get_evidence; 210, argument_list; 210, 211; 211, identifier:control; 212, for_statement; 212, 213; 212, 216; 212, 229; 213, pattern_list; 213, 214; 213, 215; 214, identifier:subj; 215, identifier:obj; 216, call; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:itertools; 219, identifier:product; 220, argument_list; 220, 221; 220, 225; 221, call; 221, 222; 221, 223; 222, identifier:_listify; 223, argument_list; 223, 224; 224, identifier:controller; 225, call; 225, 226; 225, 227; 226, identifier:_listify; 227, argument_list; 227, 228; 228, identifier:controlled; 229, block; 229, 230; 229, 238; 229, 244; 229, 288; 229, 298; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:subj_act; 233, call; 233, 234; 233, 235; 234, identifier:ActivityCondition; 235, argument_list; 235, 236; 235, 237; 236, string:'transcription'; 237, True; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:subj; 242, identifier:activity; 243, identifier:subj_act; 244, if_statement; 244, 245; 244, 248; 244, 260; 244, 276; 245, comparison_operator:==; 245, 246; 245, 247; 246, identifier:control_type; 247, string:'ACTIVATION'; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:st; 252, call; 252, 253; 252, 254; 253, identifier:IncreaseAmount; 254, argument_list; 254, 255; 254, 256; 254, 257; 255, identifier:subj; 256, identifier:obj; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:evidence; 259, identifier:ev; 260, elif_clause; 260, 261; 260, 264; 261, comparison_operator:==; 261, 262; 261, 263; 262, identifier:control_type; 263, string:'INHIBITION'; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:st; 268, call; 268, 269; 268, 270; 269, identifier:DecreaseAmount; 270, argument_list; 270, 271; 270, 272; 270, 273; 271, identifier:subj; 272, identifier:obj; 273, keyword_argument; 273, 274; 273, 275; 274, identifier:evidence; 275, identifier:ev; 276, else_clause; 276, 277; 277, block; 277, 278; 277, 287; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:logger; 282, identifier:warning; 283, argument_list; 283, 284; 284, binary_operator:%; 284, 285; 284, 286; 285, string:'Unhandled control type %s'; 286, identifier:control_type; 287, continue_statement; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:st_dec; 291, call; 291, 292; 291, 293; 292, identifier:decode_obj; 293, argument_list; 293, 294; 293, 295; 294, identifier:st; 295, keyword_argument; 295, 296; 295, 297; 296, identifier:encoding; 297, string:'utf-8'; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 305; 300, attribute; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:self; 303, identifier:statements; 304, identifier:append; 305, argument_list; 305, 306; 306, identifier:st_dec | def get_regulate_amounts(self):
"""Extract INDRA RegulateAmount Statements from the BioPAX model.
This method extracts IncreaseAmount/DecreaseAmount Statements from
the BioPAX model. It fully reuses BioPAX Pattern's
org.biopax.paxtools.pattern.PatternBox.controlsExpressionWithTemplateReac
pattern to find TemplateReactions which control the expression of
a protein.
"""
p = pb.controlsExpressionWithTemplateReac()
s = _bpp('Searcher')
res = s.searchPlain(self.model, p)
res_array = [_match_to_array(m) for m in res.toArray()]
stmts = []
for res in res_array:
# FIXME: for some reason labels are not accessible
# for these queries. It would be more reliable
# to get results by label instead of index.
'''
controller_er = res[p.indexOf('controller ER')]
generic_controller_er = res[p.indexOf('generic controller ER')]
controller_simple_pe = res[p.indexOf('controller simple PE')]
controller_pe = res[p.indexOf('controller PE')]
control = res[p.indexOf('Control')]
conversion = res[p.indexOf('Conversion')]
input_pe = res[p.indexOf('input PE')]
input_simple_pe = res[p.indexOf('input simple PE')]
changed_generic_er = res[p.indexOf('changed generic ER')]
output_pe = res[p.indexOf('output PE')]
output_simple_pe = res[p.indexOf('output simple PE')]
changed_er = res[p.indexOf('changed ER')]
'''
# TODO: here, res[3] is the complex physical entity
# for instance http://pathwaycommons.org/pc2/
# Complex_43c6b8330562c1b411d21e9d1185bae9
# consists of 3 components: JUN, FOS and NFAT
# where NFAT further contains 3 member physical entities.
#
# However, res[2] iterates over all 5 member physical entities
# of the complex which doesn't represent the underlying
# structure faithfully. It would be better to use res[3]
# (the complex itself) and look at components and then
# members. However, then, it would not be clear how to
# construct an INDRA Agent for the controller.
controller = self._get_agents_from_entity(res[2])
controlled_pe = res[6]
controlled = self._get_agents_from_entity(controlled_pe)
conversion = res[5]
direction = conversion.getTemplateDirection()
if direction is not None:
direction = direction.name()
if direction != 'FORWARD':
logger.warning('Unhandled conversion direction %s' %
direction)
continue
# Sometimes interaction type is annotated as
# term=='TRANSCRIPTION'. Other times this is not
# annotated.
int_type = conversion.getInteractionType().toArray()
if int_type:
for it in int_type:
for term in it.getTerm().toArray():
pass
control = res[4]
control_type = control.getControlType()
if control_type:
control_type = control_type.name()
ev = self._get_evidence(control)
for subj, obj in itertools.product(_listify(controller),
_listify(controlled)):
subj_act = ActivityCondition('transcription', True)
subj.activity = subj_act
if control_type == 'ACTIVATION':
st = IncreaseAmount(subj, obj, evidence=ev)
elif control_type == 'INHIBITION':
st = DecreaseAmount(subj, obj, evidence=ev)
else:
logger.warning('Unhandled control type %s' % control_type)
continue
st_dec = decode_obj(st, encoding='utf-8')
self.statements.append(st_dec) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_gef; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 23; 5, 35; 5, 50; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_gef_gap_base; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:s; 19, call; 19, 20; 19, 21; 20, identifier:_bpp; 21, argument_list; 21, 22; 22, string:'Searcher'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:res; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:s; 29, identifier:searchPlain; 30, argument_list; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:model; 34, identifier:p; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:res_array; 38, list_comprehension; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:_match_to_array; 41, argument_list; 41, 42; 42, identifier:m; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:m; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:res; 48, identifier:toArray; 49, argument_list; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:r; 52, identifier:res_array; 53, block; 53, 54; 53, 65; 53, 76; 53, 87; 53, 98; 53, 109; 53, 120; 53, 131; 53, 132; 53, 133; 53, 134; 53, 141; 53, 150; 53, 159; 53, 167; 53, 168; 53, 169; 53, 170; 53, 171; 53, 180; 53, 181; 53, 185; 53, 206; 53, 210; 53, 231; 53, 239; 53, 248; 53, 257; 53, 266; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:controller_pe; 57, subscript; 57, 58; 57, 59; 58, identifier:r; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:p; 62, identifier:indexOf; 63, argument_list; 63, 64; 64, string:'controller PE'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:input_pe; 68, subscript; 68, 69; 68, 70; 69, identifier:r; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:p; 73, identifier:indexOf; 74, argument_list; 74, 75; 75, string:'input PE'; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:input_spe; 79, subscript; 79, 80; 79, 81; 80, identifier:r; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:p; 84, identifier:indexOf; 85, argument_list; 85, 86; 86, string:'input simple PE'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:output_pe; 90, subscript; 90, 91; 90, 92; 91, identifier:r; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:p; 95, identifier:indexOf; 96, argument_list; 96, 97; 97, string:'output PE'; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:output_spe; 101, subscript; 101, 102; 101, 103; 102, identifier:r; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:p; 106, identifier:indexOf; 107, argument_list; 107, 108; 108, string:'output simple PE'; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:reaction; 112, subscript; 112, 113; 112, 114; 113, identifier:r; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:p; 117, identifier:indexOf; 118, argument_list; 118, 119; 119, string:'Conversion'; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:control; 123, subscript; 123, 124; 123, 125; 124, identifier:r; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:p; 128, identifier:indexOf; 129, argument_list; 129, 130; 130, string:'Control'; 131, comment; 132, comment; 133, comment; 134, if_statement; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:_is_complex; 137, argument_list; 137, 138; 138, identifier:controller_pe; 139, block; 139, 140; 140, continue_statement; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:members_in; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_get_complex_members; 148, argument_list; 148, 149; 149, identifier:input_pe; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:members_out; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_get_complex_members; 157, argument_list; 157, 158; 158, identifier:output_pe; 159, if_statement; 159, 160; 159, 165; 160, not_operator; 160, 161; 161, parenthesized_expression; 161, 162; 162, boolean_operator:and; 162, 163; 162, 164; 163, identifier:members_in; 164, identifier:members_out; 165, block; 165, 166; 166, continue_statement; 167, comment; 168, comment; 169, comment; 170, comment; 171, if_statement; 171, 172; 171, 178; 172, comparison_operator:!=; 172, 173; 172, 177; 173, call; 173, 174; 173, 175; 174, identifier:len; 175, argument_list; 175, 176; 176, identifier:members_out; 177, integer:2; 178, block; 178, 179; 179, continue_statement; 180, comment; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:gdp_in; 184, False; 185, for_statement; 185, 186; 185, 187; 185, 188; 186, identifier:member; 187, identifier:members_in; 188, block; 188, 189; 189, if_statement; 189, 190; 189, 201; 190, boolean_operator:and; 190, 191; 190, 196; 191, call; 191, 192; 191, 193; 192, identifier:isinstance; 193, argument_list; 193, 194; 193, 195; 194, identifier:member; 195, identifier:Agent; 196, comparison_operator:==; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:member; 199, identifier:name; 200, string:'GDP'; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:gdp_in; 205, True; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:gtp_out; 209, False; 210, for_statement; 210, 211; 210, 212; 210, 213; 211, identifier:member; 212, identifier:members_out; 213, block; 213, 214; 214, if_statement; 214, 215; 214, 226; 215, boolean_operator:and; 215, 216; 215, 221; 216, call; 216, 217; 216, 218; 217, identifier:isinstance; 218, argument_list; 218, 219; 218, 220; 219, identifier:member; 220, identifier:Agent; 221, comparison_operator:==; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:member; 224, identifier:name; 225, string:'GTP'; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:gtp_out; 230, True; 231, if_statement; 231, 232; 231, 237; 232, not_operator; 232, 233; 233, parenthesized_expression; 233, 234; 234, boolean_operator:and; 234, 235; 234, 236; 235, identifier:gdp_in; 236, identifier:gtp_out; 237, block; 237, 238; 238, continue_statement; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:ras_list; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:_get_agents_from_entity; 246, argument_list; 246, 247; 247, identifier:input_spe; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:gef_list; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_get_agents_from_entity; 255, argument_list; 255, 256; 256, identifier:controller_pe; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:ev; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:_get_evidence; 264, argument_list; 264, 265; 265, identifier:control; 266, for_statement; 266, 267; 266, 270; 266, 283; 267, pattern_list; 267, 268; 267, 269; 268, identifier:gef; 269, identifier:ras; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:itertools; 273, identifier:product; 274, argument_list; 274, 275; 274, 279; 275, call; 275, 276; 275, 277; 276, identifier:_listify; 277, argument_list; 277, 278; 278, identifier:gef_list; 279, call; 279, 280; 279, 281; 280, identifier:_listify; 281, argument_list; 281, 282; 282, identifier:ras_list; 283, block; 283, 284; 283, 295; 283, 305; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:st; 287, call; 287, 288; 287, 289; 288, identifier:Gef; 289, argument_list; 289, 290; 289, 291; 289, 292; 290, identifier:gef; 291, identifier:ras; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:evidence; 294, identifier:ev; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:st_dec; 298, call; 298, 299; 298, 300; 299, identifier:decode_obj; 300, argument_list; 300, 301; 300, 302; 301, identifier:st; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:encoding; 304, string:'utf-8'; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:statements; 311, identifier:append; 312, argument_list; 312, 313; 313, identifier:st_dec | def get_gef(self):
"""Extract Gef INDRA Statements from the BioPAX model.
This method uses a custom BioPAX Pattern
(one that is not implemented PatternBox) to query for controlled
BiochemicalReactions in which the same protein is in complex with
GDP on the left hand side and in complex with GTP on the
right hand side. This implies that the controller is a GEF for the
GDP/GTP-bound protein.
"""
p = self._gef_gap_base()
s = _bpp('Searcher')
res = s.searchPlain(self.model, p)
res_array = [_match_to_array(m) for m in res.toArray()]
for r in res_array:
controller_pe = r[p.indexOf('controller PE')]
input_pe = r[p.indexOf('input PE')]
input_spe = r[p.indexOf('input simple PE')]
output_pe = r[p.indexOf('output PE')]
output_spe = r[p.indexOf('output simple PE')]
reaction = r[p.indexOf('Conversion')]
control = r[p.indexOf('Control')]
# Make sure the GEF is not a complex
# TODO: it could be possible to extract certain complexes here, for
# instance ones that only have a single protein
if _is_complex(controller_pe):
continue
members_in = self._get_complex_members(input_pe)
members_out = self._get_complex_members(output_pe)
if not (members_in and members_out):
continue
# Make sure the outgoing complex has exactly 2 members
# TODO: by finding matching proteins on either side, in principle
# it would be possible to find Gef relationships in complexes
# with more members
if len(members_out) != 2:
continue
# Make sure complex starts with GDP that becomes GTP
gdp_in = False
for member in members_in:
if isinstance(member, Agent) and member.name == 'GDP':
gdp_in = True
gtp_out = False
for member in members_out:
if isinstance(member, Agent) and member.name == 'GTP':
gtp_out = True
if not (gdp_in and gtp_out):
continue
ras_list = self._get_agents_from_entity(input_spe)
gef_list = self._get_agents_from_entity(controller_pe)
ev = self._get_evidence(control)
for gef, ras in itertools.product(_listify(gef_list),
_listify(ras_list)):
st = Gef(gef, ras, evidence=ev)
st_dec = decode_obj(st, encoding='utf-8')
self.statements.append(st_dec) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_gap; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 23; 5, 35; 5, 50; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_gef_gap_base; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:s; 19, call; 19, 20; 19, 21; 20, identifier:_bpp; 21, argument_list; 21, 22; 22, string:'Searcher'; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:res; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:s; 29, identifier:searchPlain; 30, argument_list; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:model; 34, identifier:p; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:res_array; 38, list_comprehension; 38, 39; 38, 43; 39, call; 39, 40; 39, 41; 40, identifier:_match_to_array; 41, argument_list; 41, 42; 42, identifier:m; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:m; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:res; 48, identifier:toArray; 49, argument_list; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:r; 52, identifier:res_array; 53, block; 53, 54; 53, 65; 53, 76; 53, 87; 53, 98; 53, 109; 53, 120; 53, 131; 53, 132; 53, 133; 53, 134; 53, 141; 53, 150; 53, 159; 53, 167; 53, 168; 53, 169; 53, 170; 53, 171; 53, 180; 53, 181; 53, 185; 53, 206; 53, 210; 53, 231; 53, 239; 53, 248; 53, 257; 53, 266; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:controller_pe; 57, subscript; 57, 58; 57, 59; 58, identifier:r; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:p; 62, identifier:indexOf; 63, argument_list; 63, 64; 64, string:'controller PE'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:input_pe; 68, subscript; 68, 69; 68, 70; 69, identifier:r; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:p; 73, identifier:indexOf; 74, argument_list; 74, 75; 75, string:'input PE'; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:input_spe; 79, subscript; 79, 80; 79, 81; 80, identifier:r; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:p; 84, identifier:indexOf; 85, argument_list; 85, 86; 86, string:'input simple PE'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:output_pe; 90, subscript; 90, 91; 90, 92; 91, identifier:r; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:p; 95, identifier:indexOf; 96, argument_list; 96, 97; 97, string:'output PE'; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:output_spe; 101, subscript; 101, 102; 101, 103; 102, identifier:r; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:p; 106, identifier:indexOf; 107, argument_list; 107, 108; 108, string:'output simple PE'; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:reaction; 112, subscript; 112, 113; 112, 114; 113, identifier:r; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:p; 117, identifier:indexOf; 118, argument_list; 118, 119; 119, string:'Conversion'; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:control; 123, subscript; 123, 124; 123, 125; 124, identifier:r; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:p; 128, identifier:indexOf; 129, argument_list; 129, 130; 130, string:'Control'; 131, comment; 132, comment; 133, comment; 134, if_statement; 134, 135; 134, 139; 135, call; 135, 136; 135, 137; 136, identifier:_is_complex; 137, argument_list; 137, 138; 138, identifier:controller_pe; 139, block; 139, 140; 140, continue_statement; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:members_in; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:_get_complex_members; 148, argument_list; 148, 149; 149, identifier:input_pe; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:members_out; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_get_complex_members; 157, argument_list; 157, 158; 158, identifier:output_pe; 159, if_statement; 159, 160; 159, 165; 160, not_operator; 160, 161; 161, parenthesized_expression; 161, 162; 162, boolean_operator:and; 162, 163; 162, 164; 163, identifier:members_in; 164, identifier:members_out; 165, block; 165, 166; 166, continue_statement; 167, comment; 168, comment; 169, comment; 170, comment; 171, if_statement; 171, 172; 171, 178; 172, comparison_operator:!=; 172, 173; 172, 177; 173, call; 173, 174; 173, 175; 174, identifier:len; 175, argument_list; 175, 176; 176, identifier:members_out; 177, integer:2; 178, block; 178, 179; 179, continue_statement; 180, comment; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:gtp_in; 184, False; 185, for_statement; 185, 186; 185, 187; 185, 188; 186, identifier:member; 187, identifier:members_in; 188, block; 188, 189; 189, if_statement; 189, 190; 189, 201; 190, boolean_operator:and; 190, 191; 190, 196; 191, call; 191, 192; 191, 193; 192, identifier:isinstance; 193, argument_list; 193, 194; 193, 195; 194, identifier:member; 195, identifier:Agent; 196, comparison_operator:==; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:member; 199, identifier:name; 200, string:'GTP'; 201, block; 201, 202; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 205; 204, identifier:gtp_in; 205, True; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:gdp_out; 209, False; 210, for_statement; 210, 211; 210, 212; 210, 213; 211, identifier:member; 212, identifier:members_out; 213, block; 213, 214; 214, if_statement; 214, 215; 214, 226; 215, boolean_operator:and; 215, 216; 215, 221; 216, call; 216, 217; 216, 218; 217, identifier:isinstance; 218, argument_list; 218, 219; 218, 220; 219, identifier:member; 220, identifier:Agent; 221, comparison_operator:==; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:member; 224, identifier:name; 225, string:'GDP'; 226, block; 226, 227; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 230; 229, identifier:gdp_out; 230, True; 231, if_statement; 231, 232; 231, 237; 232, not_operator; 232, 233; 233, parenthesized_expression; 233, 234; 234, boolean_operator:and; 234, 235; 234, 236; 235, identifier:gtp_in; 236, identifier:gdp_out; 237, block; 237, 238; 238, continue_statement; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:ras_list; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:_get_agents_from_entity; 246, argument_list; 246, 247; 247, identifier:input_spe; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 251; 250, identifier:gap_list; 251, call; 251, 252; 251, 255; 252, attribute; 252, 253; 252, 254; 253, identifier:self; 254, identifier:_get_agents_from_entity; 255, argument_list; 255, 256; 256, identifier:controller_pe; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:ev; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:_get_evidence; 264, argument_list; 264, 265; 265, identifier:control; 266, for_statement; 266, 267; 266, 270; 266, 283; 267, pattern_list; 267, 268; 267, 269; 268, identifier:gap; 269, identifier:ras; 270, call; 270, 271; 270, 274; 271, attribute; 271, 272; 271, 273; 272, identifier:itertools; 273, identifier:product; 274, argument_list; 274, 275; 274, 279; 275, call; 275, 276; 275, 277; 276, identifier:_listify; 277, argument_list; 277, 278; 278, identifier:gap_list; 279, call; 279, 280; 279, 281; 280, identifier:_listify; 281, argument_list; 281, 282; 282, identifier:ras_list; 283, block; 283, 284; 283, 295; 283, 305; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:st; 287, call; 287, 288; 287, 289; 288, identifier:Gap; 289, argument_list; 289, 290; 289, 291; 289, 292; 290, identifier:gap; 291, identifier:ras; 292, keyword_argument; 292, 293; 292, 294; 293, identifier:evidence; 294, identifier:ev; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:st_dec; 298, call; 298, 299; 298, 300; 299, identifier:decode_obj; 300, argument_list; 300, 301; 300, 302; 301, identifier:st; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:encoding; 304, string:'utf-8'; 305, expression_statement; 305, 306; 306, call; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:statements; 311, identifier:append; 312, argument_list; 312, 313; 313, identifier:st_dec | def get_gap(self):
"""Extract Gap INDRA Statements from the BioPAX model.
This method uses a custom BioPAX Pattern
(one that is not implemented PatternBox) to query for controlled
BiochemicalReactions in which the same protein is in complex with
GTP on the left hand side and in complex with GDP on the
right hand side. This implies that the controller is a GAP for the
GDP/GTP-bound protein.
"""
p = self._gef_gap_base()
s = _bpp('Searcher')
res = s.searchPlain(self.model, p)
res_array = [_match_to_array(m) for m in res.toArray()]
for r in res_array:
controller_pe = r[p.indexOf('controller PE')]
input_pe = r[p.indexOf('input PE')]
input_spe = r[p.indexOf('input simple PE')]
output_pe = r[p.indexOf('output PE')]
output_spe = r[p.indexOf('output simple PE')]
reaction = r[p.indexOf('Conversion')]
control = r[p.indexOf('Control')]
# Make sure the GAP is not a complex
# TODO: it could be possible to extract certain complexes here, for
# instance ones that only have a single protein
if _is_complex(controller_pe):
continue
members_in = self._get_complex_members(input_pe)
members_out = self._get_complex_members(output_pe)
if not (members_in and members_out):
continue
# Make sure the outgoing complex has exactly 2 members
# TODO: by finding matching proteins on either side, in principle
# it would be possible to find Gap relationships in complexes
# with more members
if len(members_out) != 2:
continue
# Make sure complex starts with GDP that becomes GTP
gtp_in = False
for member in members_in:
if isinstance(member, Agent) and member.name == 'GTP':
gtp_in = True
gdp_out = False
for member in members_out:
if isinstance(member, Agent) and member.name == 'GDP':
gdp_out = True
if not (gtp_in and gdp_out):
continue
ras_list = self._get_agents_from_entity(input_spe)
gap_list = self._get_agents_from_entity(controller_pe)
ev = self._get_evidence(control)
for gap, ras in itertools.product(_listify(gap_list),
_listify(ras_list)):
st = Gap(gap, ras, evidence=ev)
st_dec = decode_obj(st, encoding='utf-8')
self.statements.append(st_dec) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_generic_modification; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:mod_class; 6, block; 6, 7; 6, 9; 6, 15; 6, 42; 6, 50; 6, 51; 6, 59; 6, 72; 6, 79; 6, 91; 6, 106; 6, 110; 6, 383; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:mod_type; 12, subscript; 12, 13; 12, 14; 13, identifier:modclass_to_modtype; 14, identifier:mod_class; 15, if_statement; 15, 16; 15, 21; 15, 34; 16, call; 16, 17; 16, 18; 17, identifier:issubclass; 18, argument_list; 18, 19; 18, 20; 19, identifier:mod_class; 20, identifier:RemoveModification; 21, block; 21, 22; 21, 28; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:mod_gain_const; 25, attribute; 25, 26; 25, 27; 26, identifier:mcct; 27, identifier:LOSS; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:mod_type; 31, subscript; 31, 32; 31, 33; 32, identifier:modtype_to_inverse; 33, identifier:mod_type; 34, else_clause; 34, 35; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:mod_gain_const; 39, attribute; 39, 40; 39, 41; 40, identifier:mcct; 41, identifier:GAIN; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:mod_filter; 45, subscript; 45, 46; 45, 47; 46, identifier:mod_type; 47, slice; 47, 48; 47, 49; 48, colon; 49, integer:5; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:p; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:BiopaxProcessor; 57, identifier:_construct_modification_pattern; 58, argument_list; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:p; 63, identifier:add; 64, argument_list; 64, 65; 64, 70; 64, 71; 65, call; 65, 66; 65, 67; 66, identifier:mcc; 67, argument_list; 67, 68; 67, 69; 68, identifier:mod_gain_const; 69, identifier:mod_filter; 70, string:"input simple PE"; 71, string:"output simple PE"; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:s; 75, call; 75, 76; 75, 77; 76, identifier:_bpp; 77, argument_list; 77, 78; 78, string:'Searcher'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:res; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:s; 85, identifier:searchPlain; 86, argument_list; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:model; 90, identifier:p; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:res_array; 94, list_comprehension; 94, 95; 94, 99; 95, call; 95, 96; 95, 97; 96, identifier:_match_to_array; 97, argument_list; 97, 98; 98, identifier:m; 99, for_in_clause; 99, 100; 99, 101; 100, identifier:m; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:res; 104, identifier:toArray; 105, argument_list; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:stmts; 109, list:[]; 110, for_statement; 110, 111; 110, 112; 110, 113; 111, identifier:r; 112, identifier:res_array; 113, block; 113, 114; 113, 125; 113, 136; 113, 147; 113, 158; 113, 169; 113, 180; 113, 188; 113, 196; 113, 224; 113, 233; 113, 238; 113, 240; 113, 252; 113, 261; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:controller_pe; 117, subscript; 117, 118; 117, 119; 118, identifier:r; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:p; 122, identifier:indexOf; 123, argument_list; 123, 124; 124, string:'controller PE'; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:input_pe; 128, subscript; 128, 129; 128, 130; 129, identifier:r; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:p; 133, identifier:indexOf; 134, argument_list; 134, 135; 135, string:'input PE'; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:input_spe; 139, subscript; 139, 140; 139, 141; 140, identifier:r; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:p; 144, identifier:indexOf; 145, argument_list; 145, 146; 146, string:'input simple PE'; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:output_spe; 150, subscript; 150, 151; 150, 152; 151, identifier:r; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:p; 155, identifier:indexOf; 156, argument_list; 156, 157; 157, string:'output simple PE'; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:reaction; 161, subscript; 161, 162; 161, 163; 162, identifier:r; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:p; 166, identifier:indexOf; 167, argument_list; 167, 168; 168, string:'Conversion'; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:control; 172, subscript; 172, 173; 172, 174; 173, identifier:r; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:p; 177, identifier:indexOf; 178, argument_list; 178, 179; 179, string:'Control'; 180, if_statement; 180, 181; 180, 186; 181, not_operator; 181, 182; 182, call; 182, 183; 182, 184; 183, identifier:_is_catalysis; 184, argument_list; 184, 185; 185, identifier:control; 186, block; 186, 187; 187, continue_statement; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:cat_dir; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:control; 194, identifier:getCatalysisDirection; 195, argument_list; 196, if_statement; 196, 197; 196, 208; 197, boolean_operator:and; 197, 198; 197, 201; 198, comparison_operator:is; 198, 199; 198, 200; 199, identifier:cat_dir; 200, None; 201, comparison_operator:!=; 201, 202; 201, 207; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:cat_dir; 205, identifier:name; 206, argument_list; 207, string:'LEFT_TO_RIGHT'; 208, block; 208, 209; 208, 223; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:logger; 213, identifier:debug; 214, argument_list; 214, 215; 215, binary_operator:%; 215, 216; 215, 217; 215, 218; 216, string:'Unexpected catalysis direction: %s.'; 217, line_continuation:\; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:control; 221, identifier:getCatalysisDirection; 222, argument_list; 223, continue_statement; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:enzs; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:BiopaxProcessor; 230, identifier:_get_primary_controller; 231, argument_list; 231, 232; 232, identifier:controller_pe; 233, if_statement; 233, 234; 233, 236; 234, not_operator; 234, 235; 235, identifier:enzs; 236, block; 236, 237; 237, continue_statement; 238, expression_statement; 238, 239; 239, string:'''
if _is_complex(input_pe):
sub_members_in = self._get_complex_members(input_pe)
sub_members_out = self._get_complex_members(output_pe)
# TODO: It is possible to find which member of the complex is
# actually modified. That member will be the substrate and
# all other members of the complex will be bound to it.
logger.info('Cannot handle complex substrates.')
continue
'''; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:subs; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:BiopaxProcessor; 246, identifier:_get_agents_from_entity; 247, argument_list; 247, 248; 247, 249; 248, identifier:input_spe; 249, keyword_argument; 249, 250; 249, 251; 250, identifier:expand_pe; 251, False; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:ev; 255, call; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:self; 258, identifier:_get_evidence; 259, argument_list; 259, 260; 260, identifier:control; 261, for_statement; 261, 262; 261, 265; 261, 278; 261, 279; 262, pattern_list; 262, 263; 262, 264; 263, identifier:enz; 264, identifier:sub; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:itertools; 268, identifier:product; 269, argument_list; 269, 270; 269, 274; 270, call; 270, 271; 270, 272; 271, identifier:_listify; 272, argument_list; 272, 273; 273, identifier:enzs; 274, call; 274, 275; 274, 276; 275, identifier:_listify; 276, argument_list; 276, 277; 277, identifier:subs; 278, comment; 279, block; 279, 280; 279, 290; 279, 300; 279, 310; 279, 335; 280, expression_statement; 280, 281; 281, assignment; 281, 282; 281, 283; 281, 284; 282, identifier:mod_in; 283, line_continuation:\; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:BiopaxProcessor; 287, identifier:_get_entity_mods; 288, argument_list; 288, 289; 289, identifier:input_spe; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 291, 294; 292, identifier:mod_out; 293, line_continuation:\; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:BiopaxProcessor; 297, identifier:_get_entity_mods; 298, argument_list; 298, 299; 299, identifier:output_spe; 300, expression_statement; 300, 301; 301, assignment; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:sub; 304, identifier:mods; 305, call; 305, 306; 305, 307; 306, identifier:_get_mod_intersection; 307, argument_list; 307, 308; 307, 309; 308, identifier:mod_in; 309, identifier:mod_out; 310, if_statement; 310, 311; 310, 316; 310, 325; 311, call; 311, 312; 311, 313; 312, identifier:issubclass; 313, argument_list; 313, 314; 313, 315; 314, identifier:mod_class; 315, identifier:AddModification; 316, block; 316, 317; 317, expression_statement; 317, 318; 318, assignment; 318, 319; 318, 320; 319, identifier:gained_mods; 320, call; 320, 321; 320, 322; 321, identifier:_get_mod_difference; 322, argument_list; 322, 323; 322, 324; 323, identifier:mod_out; 324, identifier:mod_in; 325, else_clause; 325, 326; 326, block; 326, 327; 327, expression_statement; 327, 328; 328, assignment; 328, 329; 328, 330; 329, identifier:gained_mods; 330, call; 330, 331; 330, 332; 331, identifier:_get_mod_difference; 332, argument_list; 332, 333; 332, 334; 333, identifier:mod_in; 334, identifier:mod_out; 335, for_statement; 335, 336; 335, 337; 335, 338; 335, 339; 335, 340; 336, identifier:mod; 337, identifier:gained_mods; 338, comment; 339, comment; 340, block; 340, 341; 340, 353; 340, 370; 341, if_statement; 341, 342; 341, 351; 342, comparison_operator:not; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:mod; 345, identifier:mod_type; 346, tuple; 346, 347; 346, 348; 347, identifier:mod_type; 348, subscript; 348, 349; 348, 350; 349, identifier:modtype_to_inverse; 350, identifier:mod_type; 351, block; 351, 352; 352, continue_statement; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:stmt; 356, call; 356, 357; 356, 358; 357, identifier:mod_class; 358, argument_list; 358, 359; 358, 360; 358, 361; 358, 364; 358, 367; 359, identifier:enz; 360, identifier:sub; 361, attribute; 361, 362; 361, 363; 362, identifier:mod; 363, identifier:residue; 364, attribute; 364, 365; 364, 366; 365, identifier:mod; 366, identifier:position; 367, keyword_argument; 367, 368; 367, 369; 368, identifier:evidence; 369, identifier:ev; 370, expression_statement; 370, 371; 371, call; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:stmts; 374, identifier:append; 375, argument_list; 375, 376; 376, call; 376, 377; 376, 378; 377, identifier:decode_obj; 378, argument_list; 378, 379; 378, 380; 379, identifier:stmt; 380, keyword_argument; 380, 381; 380, 382; 381, identifier:encoding; 382, string:'utf-8'; 383, return_statement; 383, 384; 384, identifier:stmts | def _get_generic_modification(self, mod_class):
"""Get all modification reactions given a Modification class."""
mod_type = modclass_to_modtype[mod_class]
if issubclass(mod_class, RemoveModification):
mod_gain_const = mcct.LOSS
mod_type = modtype_to_inverse[mod_type]
else:
mod_gain_const = mcct.GAIN
mod_filter = mod_type[:5]
# Start with a generic modification pattern
p = BiopaxProcessor._construct_modification_pattern()
p.add(mcc(mod_gain_const, mod_filter),
"input simple PE", "output simple PE")
s = _bpp('Searcher')
res = s.searchPlain(self.model, p)
res_array = [_match_to_array(m) for m in res.toArray()]
stmts = []
for r in res_array:
controller_pe = r[p.indexOf('controller PE')]
input_pe = r[p.indexOf('input PE')]
input_spe = r[p.indexOf('input simple PE')]
output_spe = r[p.indexOf('output simple PE')]
reaction = r[p.indexOf('Conversion')]
control = r[p.indexOf('Control')]
if not _is_catalysis(control):
continue
cat_dir = control.getCatalysisDirection()
if cat_dir is not None and cat_dir.name() != 'LEFT_TO_RIGHT':
logger.debug('Unexpected catalysis direction: %s.' % \
control.getCatalysisDirection())
continue
enzs = BiopaxProcessor._get_primary_controller(controller_pe)
if not enzs:
continue
'''
if _is_complex(input_pe):
sub_members_in = self._get_complex_members(input_pe)
sub_members_out = self._get_complex_members(output_pe)
# TODO: It is possible to find which member of the complex is
# actually modified. That member will be the substrate and
# all other members of the complex will be bound to it.
logger.info('Cannot handle complex substrates.')
continue
'''
subs = BiopaxProcessor._get_agents_from_entity(input_spe,
expand_pe=False)
ev = self._get_evidence(control)
for enz, sub in itertools.product(_listify(enzs), _listify(subs)):
# Get the modifications
mod_in = \
BiopaxProcessor._get_entity_mods(input_spe)
mod_out = \
BiopaxProcessor._get_entity_mods(output_spe)
sub.mods = _get_mod_intersection(mod_in, mod_out)
if issubclass(mod_class, AddModification):
gained_mods = _get_mod_difference(mod_out, mod_in)
else:
gained_mods = _get_mod_difference(mod_in, mod_out)
for mod in gained_mods:
# Is it guaranteed that these are all modifications
# of the type we are extracting?
if mod.mod_type not in (mod_type,
modtype_to_inverse[mod_type]):
continue
stmt = mod_class(enz, sub, mod.residue, mod.position,
evidence=ev)
stmts.append(decode_obj(stmt, encoding='utf-8'))
return stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_extract_mod_from_feature; 3, parameters; 3, 4; 4, identifier:mf; 5, block; 5, 6; 5, 8; 5, 9; 5, 17; 5, 24; 5, 36; 5, 40; 5, 79; 5, 99; 5, 105; 5, 106; 5, 107; 5, 108; 5, 109; 5, 117; 5, 215; 5, 225; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:mf_type; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:mf; 15, identifier:getModificationType; 16, argument_list; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:mf_type; 20, None; 21, block; 21, 22; 22, return_statement; 22, 23; 23, None; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:mf_type_terms; 27, call; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:mf_type; 32, identifier:getTerm; 33, argument_list; 34, identifier:toArray; 35, argument_list; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:known_mf_type; 39, None; 40, for_statement; 40, 41; 40, 42; 40, 43; 41, identifier:t; 42, identifier:mf_type_terms; 43, block; 43, 44; 43, 60; 43, 69; 44, if_statement; 44, 45; 44, 51; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:t; 48, identifier:startswith; 49, argument_list; 49, 50; 50, string:'MOD_RES '; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:t; 55, subscript; 55, 56; 55, 57; 56, identifier:t; 57, slice; 57, 58; 57, 59; 58, integer:8; 59, colon; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:mf_type_indra; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:_mftype_dict; 66, identifier:get; 67, argument_list; 67, 68; 68, identifier:t; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:mf_type_indra; 72, None; 73, block; 73, 74; 73, 78; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:known_mf_type; 77, identifier:mf_type_indra; 78, break_statement; 79, if_statement; 79, 80; 79, 82; 80, not_operator; 80, 81; 81, identifier:known_mf_type; 82, block; 82, 83; 82, 97; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:logger; 87, identifier:debug; 88, argument_list; 88, 89; 89, binary_operator:%; 89, 90; 89, 91; 90, string:'Skipping modification with unknown terms: %s'; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, string:', '; 94, identifier:join; 95, argument_list; 95, 96; 96, identifier:mf_type_terms; 97, return_statement; 97, 98; 98, None; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 104; 101, pattern_list; 101, 102; 101, 103; 102, identifier:mod_type; 103, identifier:residue; 104, identifier:known_mf_type; 105, comment; 106, comment; 107, comment; 108, comment; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:mf_pos; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:mf; 115, identifier:getFeatureLocation; 116, argument_list; 117, if_statement; 117, 118; 117, 121; 117, 122; 117, 209; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:mf_pos; 120, None; 121, comment; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 134; 123, 139; 124, not_operator; 124, 125; 125, comparison_operator:==; 125, 126; 125, 133; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:mf_pos; 130, identifier:modelInterface; 131, identifier:getName; 132, argument_list; 133, string:'org.biopax.paxtools.model.level3.SequenceSite'; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:mod_pos; 138, None; 139, else_clause; 139, 140; 140, block; 140, 141; 140, 152; 140, 160; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:mf_site; 144, call; 144, 145; 144, 146; 145, identifier:cast; 146, argument_list; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:_bp; 149, argument_list; 149, 150; 150, string:'SequenceSite'; 151, identifier:mf_pos; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:mf_pos_status; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:mf_site; 158, identifier:getPositionStatus; 159, argument_list; 160, if_statement; 160, 161; 160, 164; 160, 169; 160, 193; 161, comparison_operator:is; 161, 162; 161, 163; 162, identifier:mf_pos_status; 163, None; 164, block; 164, 165; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:mod_pos; 168, None; 169, elif_clause; 169, 170; 169, 179; 170, boolean_operator:and; 170, 171; 170, 172; 171, identifier:mf_pos_status; 172, comparison_operator:!=; 172, 173; 172, 178; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:mf_pos_status; 176, identifier:toString; 177, argument_list; 178, string:'EQUAL'; 179, block; 179, 180; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:logger; 184, identifier:debug; 185, argument_list; 185, 186; 186, binary_operator:%; 186, 187; 186, 188; 187, string:'Modification site position is %s'; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:mf_pos_status; 191, identifier:toString; 192, argument_list; 193, else_clause; 193, 194; 194, block; 194, 195; 194, 203; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:mod_pos; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:mf_site; 201, identifier:getSequencePosition; 202, argument_list; 203, expression_statement; 203, 204; 204, assignment; 204, 205; 204, 206; 205, identifier:mod_pos; 206, binary_operator:%; 206, 207; 206, 208; 207, string:'%s'; 208, identifier:mod_pos; 209, else_clause; 209, 210; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:mod_pos; 214, None; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:mc; 218, call; 218, 219; 218, 220; 219, identifier:ModCondition; 220, argument_list; 220, 221; 220, 222; 220, 223; 220, 224; 221, identifier:mod_type; 222, identifier:residue; 223, identifier:mod_pos; 224, True; 225, return_statement; 225, 226; 226, identifier:mc | def _extract_mod_from_feature(mf):
"""Extract the type of modification and the position from
a ModificationFeature object in the INDRA format."""
# ModificationFeature / SequenceModificationVocabulary
mf_type = mf.getModificationType()
if mf_type is None:
return None
mf_type_terms = mf_type.getTerm().toArray()
known_mf_type = None
for t in mf_type_terms:
if t.startswith('MOD_RES '):
t = t[8:]
mf_type_indra = _mftype_dict.get(t)
if mf_type_indra is not None:
known_mf_type = mf_type_indra
break
if not known_mf_type:
logger.debug('Skipping modification with unknown terms: %s' %
', '.join(mf_type_terms))
return None
mod_type, residue = known_mf_type
# getFeatureLocation returns SequenceLocation, which is the
# generic parent class of SequenceSite and SequenceInterval.
# Here we need to cast to SequenceSite in order to get to
# the sequence position.
mf_pos = mf.getFeatureLocation()
if mf_pos is not None:
# If it is not a SequenceSite we can't handle it
if not mf_pos.modelInterface.getName() == \
'org.biopax.paxtools.model.level3.SequenceSite':
mod_pos = None
else:
mf_site = cast(_bp('SequenceSite'), mf_pos)
mf_pos_status = mf_site.getPositionStatus()
if mf_pos_status is None:
mod_pos = None
elif mf_pos_status and mf_pos_status.toString() != 'EQUAL':
logger.debug('Modification site position is %s' %
mf_pos_status.toString())
else:
mod_pos = mf_site.getSequencePosition()
mod_pos = '%s' % mod_pos
else:
mod_pos = None
mc = ModCondition(mod_type, residue, mod_pos, True)
return mc |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_activations; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 19; 5, 30; 5, 41; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:act_events; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:tree; 16, identifier:findall; 17, argument_list; 17, 18; 18, string:"EVENT/[type='ONT::ACTIVATE']"; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:inact_events; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:tree; 27, identifier:findall; 28, argument_list; 28, 29; 29, string:"EVENT/[type='ONT::DEACTIVATE']"; 30, expression_statement; 30, 31; 31, augmented_assignment:+=; 31, 32; 31, 33; 32, identifier:inact_events; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:tree; 38, identifier:findall; 39, argument_list; 39, 40; 40, string:"EVENT/[type='ONT::INHIBIT']"; 41, for_statement; 41, 42; 41, 43; 41, 47; 42, identifier:event; 43, parenthesized_expression; 43, 44; 44, binary_operator:+; 44, 45; 44, 46; 45, identifier:act_events; 46, identifier:inact_events; 47, block; 47, 48; 47, 56; 47, 64; 47, 65; 47, 74; 47, 80; 47, 91; 47, 104; 47, 114; 47, 120; 47, 121; 47, 130; 47, 143; 47, 154; 47, 167; 47, 177; 47, 190; 47, 194; 47, 259; 47, 268; 47, 277; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:event_id; 51, subscript; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:event; 54, identifier:attrib; 55, string:'id'; 56, if_statement; 56, 57; 56, 62; 57, comparison_operator:in; 57, 58; 57, 59; 58, identifier:event_id; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_static_events; 62, block; 62, 63; 63, continue_statement; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:agent; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:event; 71, identifier:find; 72, argument_list; 72, 73; 73, string:".//*[@role=':AGENT']"; 74, if_statement; 74, 75; 74, 78; 75, comparison_operator:is; 75, 76; 75, 77; 76, identifier:agent; 77, None; 78, block; 78, 79; 79, continue_statement; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:agent_id; 83, call; 83, 84; 83, 89; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:agent; 87, identifier:attrib; 88, identifier:get; 89, argument_list; 89, 90; 90, string:'id'; 91, if_statement; 91, 92; 91, 95; 92, comparison_operator:is; 92, 93; 92, 94; 93, identifier:agent_id; 94, None; 95, block; 95, 96; 95, 103; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:logger; 100, identifier:debug; 101, argument_list; 101, 102; 102, string:'Skipping activation with missing activator agent'; 103, continue_statement; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:activator_agent; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:_get_agent_by_id; 111, argument_list; 111, 112; 111, 113; 112, identifier:agent_id; 113, identifier:event_id; 114, if_statement; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:activator_agent; 117, None; 118, block; 118, 119; 119, continue_statement; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:affected; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:event; 127, identifier:find; 128, argument_list; 128, 129; 129, string:".//*[@role=':AFFECTED']"; 130, if_statement; 130, 131; 130, 134; 131, comparison_operator:is; 131, 132; 131, 133; 132, identifier:affected; 133, None; 134, block; 134, 135; 134, 142; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:logger; 139, identifier:debug; 140, argument_list; 140, 141; 141, string:'Skipping activation with missing affected agent'; 142, continue_statement; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:affected_id; 146, call; 146, 147; 146, 152; 147, attribute; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:affected; 150, identifier:attrib; 151, identifier:get; 152, argument_list; 152, 153; 153, string:'id'; 154, if_statement; 154, 155; 154, 158; 155, comparison_operator:is; 155, 156; 155, 157; 156, identifier:affected_id; 157, None; 158, block; 158, 159; 158, 166; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:logger; 163, identifier:debug; 164, argument_list; 164, 165; 165, string:'Skipping activation with missing affected agent'; 166, continue_statement; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:affected_agent; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:_get_agent_by_id; 174, argument_list; 174, 175; 174, 176; 175, identifier:affected_id; 176, identifier:event_id; 177, if_statement; 177, 178; 177, 181; 178, comparison_operator:is; 178, 179; 178, 180; 179, identifier:affected_agent; 180, None; 181, block; 181, 182; 181, 189; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:logger; 186, identifier:debug; 187, argument_list; 187, 188; 188, string:'Skipping activation with missing affected agent'; 189, continue_statement; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:is_activation; 193, True; 194, if_statement; 194, 195; 194, 200; 194, 213; 194, 236; 195, call; 195, 196; 195, 197; 196, identifier:_is_type; 197, argument_list; 197, 198; 197, 199; 198, identifier:event; 199, string:'ONT::ACTIVATE'; 200, block; 200, 201; 201, expression_statement; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:_add_extracted; 206, argument_list; 206, 207; 206, 208; 207, string:'ONT::ACTIVATE'; 208, subscript; 208, 209; 208, 212; 209, attribute; 209, 210; 209, 211; 210, identifier:event; 211, identifier:attrib; 212, string:'id'; 213, elif_clause; 213, 214; 213, 219; 214, call; 214, 215; 214, 216; 215, identifier:_is_type; 216, argument_list; 216, 217; 216, 218; 217, identifier:event; 218, string:'ONT::INHIBIT'; 219, block; 219, 220; 219, 224; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 223; 222, identifier:is_activation; 223, False; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:self; 228, identifier:_add_extracted; 229, argument_list; 229, 230; 229, 231; 230, string:'ONT::INHIBIT'; 231, subscript; 231, 232; 231, 235; 232, attribute; 232, 233; 232, 234; 233, identifier:event; 234, identifier:attrib; 235, string:'id'; 236, elif_clause; 236, 237; 236, 242; 237, call; 237, 238; 237, 239; 238, identifier:_is_type; 239, argument_list; 239, 240; 239, 241; 240, identifier:event; 241, string:'ONT::DEACTIVATE'; 242, block; 242, 243; 242, 247; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:is_activation; 246, False; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:_add_extracted; 252, argument_list; 252, 253; 252, 254; 253, string:'ONT::DEACTIVATE'; 254, subscript; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:event; 257, identifier:attrib; 258, string:'id'; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:ev; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:self; 265, identifier:_get_evidence; 266, argument_list; 266, 267; 267, identifier:event; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:location; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:_get_event_location; 275, argument_list; 275, 276; 276, identifier:event; 277, for_statement; 277, 278; 277, 281; 277, 287; 278, pattern_list; 278, 279; 278, 280; 279, identifier:a1; 280, identifier:a2; 281, call; 281, 282; 281, 283; 282, identifier:_agent_list_product; 283, argument_list; 283, 284; 284, tuple; 284, 285; 284, 286; 285, identifier:activator_agent; 286, identifier:affected_agent; 287, block; 287, 288; 287, 323; 287, 329; 288, if_statement; 288, 289; 288, 290; 288, 306; 289, identifier:is_activation; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, assignment; 292, 293; 292, 294; 293, identifier:st; 294, call; 294, 295; 294, 296; 295, identifier:Activation; 296, argument_list; 296, 297; 296, 298; 296, 299; 297, identifier:a1; 298, identifier:a2; 299, keyword_argument; 299, 300; 299, 301; 300, identifier:evidence; 301, list:[deepcopy(ev)]; 301, 302; 302, call; 302, 303; 302, 304; 303, identifier:deepcopy; 304, argument_list; 304, 305; 305, identifier:ev; 306, else_clause; 306, 307; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:st; 311, call; 311, 312; 311, 313; 312, identifier:Inhibition; 313, argument_list; 313, 314; 313, 315; 313, 316; 314, identifier:a1; 315, identifier:a2; 316, keyword_argument; 316, 317; 316, 318; 317, identifier:evidence; 318, list:[deepcopy(ev)]; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:deepcopy; 321, argument_list; 321, 322; 322, identifier:ev; 323, expression_statement; 323, 324; 324, call; 324, 325; 324, 326; 325, identifier:_stmt_location_to_agents; 326, argument_list; 326, 327; 326, 328; 327, identifier:st; 328, identifier:location; 329, expression_statement; 329, 330; 330, call; 330, 331; 330, 336; 331, attribute; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:self; 334, identifier:statements; 335, identifier:append; 336, argument_list; 336, 337; 337, identifier:st | def get_activations(self):
"""Extract direct Activation INDRA Statements."""
act_events = self.tree.findall("EVENT/[type='ONT::ACTIVATE']")
inact_events = self.tree.findall("EVENT/[type='ONT::DEACTIVATE']")
inact_events += self.tree.findall("EVENT/[type='ONT::INHIBIT']")
for event in (act_events + inact_events):
event_id = event.attrib['id']
if event_id in self._static_events:
continue
# Get the activating agent in the event
agent = event.find(".//*[@role=':AGENT']")
if agent is None:
continue
agent_id = agent.attrib.get('id')
if agent_id is None:
logger.debug(
'Skipping activation with missing activator agent')
continue
activator_agent = self._get_agent_by_id(agent_id, event_id)
if activator_agent is None:
continue
# Get the activated agent in the event
affected = event.find(".//*[@role=':AFFECTED']")
if affected is None:
logger.debug(
'Skipping activation with missing affected agent')
continue
affected_id = affected.attrib.get('id')
if affected_id is None:
logger.debug(
'Skipping activation with missing affected agent')
continue
affected_agent = self._get_agent_by_id(affected_id, event_id)
if affected_agent is None:
logger.debug(
'Skipping activation with missing affected agent')
continue
is_activation = True
if _is_type(event, 'ONT::ACTIVATE'):
self._add_extracted('ONT::ACTIVATE', event.attrib['id'])
elif _is_type(event, 'ONT::INHIBIT'):
is_activation = False
self._add_extracted('ONT::INHIBIT', event.attrib['id'])
elif _is_type(event, 'ONT::DEACTIVATE'):
is_activation = False
self._add_extracted('ONT::DEACTIVATE', event.attrib['id'])
ev = self._get_evidence(event)
location = self._get_event_location(event)
for a1, a2 in _agent_list_product((activator_agent,
affected_agent)):
if is_activation:
st = Activation(a1, a2, evidence=[deepcopy(ev)])
else:
st = Inhibition(a1, a2, evidence=[deepcopy(ev)])
_stmt_location_to_agents(st, location)
self.statements.append(st) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_activations_causal; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 20; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:ccs; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:tree; 17, identifier:findall; 18, argument_list; 18, 19; 19, string:"CC/[type='ONT::CAUSE']"; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:cc; 22, identifier:ccs; 23, block; 23, 24; 23, 33; 23, 42; 23, 43; 23, 53; 23, 64; 23, 65; 23, 66; 23, 79; 23, 90; 23, 91; 23, 92; 23, 105; 23, 115; 23, 124; 23, 125; 23, 138; 23, 148; 23, 154; 23, 163; 23, 169; 23, 170; 23, 179; 23, 187; 23, 196; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:factor; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:cc; 30, identifier:find; 31, argument_list; 31, 32; 32, string:"arg/[@role=':FACTOR']"; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:outcome; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:cc; 39, identifier:find; 40, argument_list; 40, 41; 41, string:"arg/[@role=':OUTCOME']"; 42, comment; 43, if_statement; 43, 44; 43, 51; 44, boolean_operator:or; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:factor; 47, None; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:outcome; 50, None; 51, block; 51, 52; 52, continue_statement; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:factor_id; 56, call; 56, 57; 56, 62; 57, attribute; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:factor; 60, identifier:attrib; 61, identifier:get; 62, argument_list; 62, 63; 63, string:'id'; 64, comment; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:factor_term; 69, call; 69, 70; 69, 75; 70, attribute; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:tree; 74, identifier:find; 75, argument_list; 75, 76; 76, binary_operator:%; 76, 77; 76, 78; 77, string:"TERM/[@id='%s']"; 78, identifier:factor_id; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:outcome_id; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:outcome; 86, identifier:attrib; 87, identifier:get; 88, argument_list; 88, 89; 89, string:'id'; 90, comment; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:outcome_event; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:tree; 100, identifier:find; 101, argument_list; 101, 102; 102, binary_operator:%; 102, 103; 102, 104; 103, string:"EVENT/[@id='%s']"; 104, identifier:outcome_id; 105, if_statement; 105, 106; 105, 113; 106, boolean_operator:or; 106, 107; 106, 110; 107, comparison_operator:is; 107, 108; 107, 109; 108, identifier:factor_term; 109, None; 110, comparison_operator:is; 110, 111; 110, 112; 111, identifier:outcome_event; 112, None; 113, block; 113, 114; 114, continue_statement; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:factor_term_type; 118, call; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:factor_term; 121, identifier:find; 122, argument_list; 122, 123; 123, string:'type'; 124, comment; 125, if_statement; 125, 126; 125, 136; 126, boolean_operator:or; 126, 127; 126, 130; 126, 131; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:factor_term_type; 129, None; 130, line_continuation:\; 131, comparison_operator:not; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:factor_term_type; 134, identifier:text; 135, identifier:molecule_types; 136, block; 136, 137; 137, continue_statement; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:factor_agent; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:_get_agent_by_id; 145, argument_list; 145, 146; 145, 147; 146, identifier:factor_id; 147, None; 148, if_statement; 148, 149; 148, 152; 149, comparison_operator:is; 149, 150; 149, 151; 150, identifier:factor_agent; 151, None; 152, block; 152, 153; 153, continue_statement; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:outcome_event_type; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:outcome_event; 160, identifier:find; 161, argument_list; 161, 162; 162, string:'type'; 163, if_statement; 163, 164; 163, 167; 164, comparison_operator:is; 164, 165; 164, 166; 165, identifier:outcome_event_type; 166, None; 167, block; 167, 168; 168, continue_statement; 169, comment; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:ev; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:self; 176, identifier:_get_evidence; 177, argument_list; 177, 178; 178, identifier:cc; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 186; 181, subscript; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:ev; 184, identifier:epistemics; 185, string:'direct'; 186, False; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:location; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:_get_event_location; 194, argument_list; 194, 195; 195, identifier:outcome_event; 196, if_statement; 196, 197; 196, 205; 197, comparison_operator:in; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:outcome_event_type; 200, identifier:text; 201, list:['ONT::ACTIVATE', 'ONT::ACTIVITY',
'ONT::DEACTIVATE']; 201, 202; 201, 203; 201, 204; 202, string:'ONT::ACTIVATE'; 203, string:'ONT::ACTIVITY'; 204, string:'ONT::DEACTIVATE'; 205, block; 205, 206; 205, 240; 205, 257; 205, 271; 205, 277; 205, 294; 206, if_statement; 206, 207; 206, 214; 206, 224; 207, comparison_operator:in; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:outcome_event_type; 210, identifier:text; 211, list:['ONT::ACTIVATE',
'ONT::DEACTIVATE']; 211, 212; 211, 213; 212, string:'ONT::ACTIVATE'; 213, string:'ONT::DEACTIVATE'; 214, block; 214, 215; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:agent_tag; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:outcome_event; 221, identifier:find; 222, argument_list; 222, 223; 223, string:".//*[@role=':AFFECTED']"; 224, elif_clause; 224, 225; 224, 230; 225, comparison_operator:==; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:outcome_event_type; 228, identifier:text; 229, string:'ONT::ACTIVITY'; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:agent_tag; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:outcome_event; 237, identifier:find; 238, argument_list; 238, 239; 239, string:".//*[@role=':AGENT']"; 240, if_statement; 240, 241; 240, 255; 241, boolean_operator:or; 241, 242; 241, 245; 242, comparison_operator:is; 242, 243; 242, 244; 243, identifier:agent_tag; 244, None; 245, comparison_operator:is; 245, 246; 245, 254; 246, call; 246, 247; 246, 252; 247, attribute; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:agent_tag; 250, identifier:attrib; 251, identifier:get; 252, argument_list; 252, 253; 253, string:'id'; 254, None; 255, block; 255, 256; 256, continue_statement; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:outcome_agent; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:self; 263, identifier:_get_agent_by_id; 264, argument_list; 264, 265; 264, 270; 265, subscript; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:agent_tag; 268, identifier:attrib; 269, string:'id'; 270, identifier:outcome_id; 271, if_statement; 271, 272; 271, 275; 272, comparison_operator:is; 272, 273; 272, 274; 273, identifier:outcome_agent; 274, None; 275, block; 275, 276; 276, continue_statement; 277, if_statement; 277, 278; 277, 283; 277, 288; 278, comparison_operator:==; 278, 279; 278, 282; 279, attribute; 279, 280; 279, 281; 280, identifier:outcome_event_type; 281, identifier:text; 282, string:'ONT::DEACTIVATE'; 283, block; 283, 284; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:is_activation; 287, False; 288, else_clause; 288, 289; 289, block; 289, 290; 290, expression_statement; 290, 291; 291, assignment; 291, 292; 291, 293; 292, identifier:is_activation; 293, True; 294, for_statement; 294, 295; 294, 298; 294, 304; 295, pattern_list; 295, 296; 295, 297; 296, identifier:a1; 297, identifier:a2; 298, call; 298, 299; 298, 300; 299, identifier:_agent_list_product; 300, argument_list; 300, 301; 301, tuple; 301, 302; 301, 303; 302, identifier:factor_agent; 303, identifier:outcome_agent; 304, block; 304, 305; 304, 340; 304, 346; 305, if_statement; 305, 306; 305, 307; 305, 323; 306, identifier:is_activation; 307, block; 307, 308; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:st; 311, call; 311, 312; 311, 313; 312, identifier:Activation; 313, argument_list; 313, 314; 313, 315; 313, 316; 314, identifier:a1; 315, identifier:a2; 316, keyword_argument; 316, 317; 316, 318; 317, identifier:evidence; 318, list:[deepcopy(ev)]; 318, 319; 319, call; 319, 320; 319, 321; 320, identifier:deepcopy; 321, argument_list; 321, 322; 322, identifier:ev; 323, else_clause; 323, 324; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:st; 328, call; 328, 329; 328, 330; 329, identifier:Inhibition; 330, argument_list; 330, 331; 330, 332; 330, 333; 331, identifier:a1; 332, identifier:a2; 333, keyword_argument; 333, 334; 333, 335; 334, identifier:evidence; 335, list:[deepcopy(ev)]; 335, 336; 336, call; 336, 337; 336, 338; 337, identifier:deepcopy; 338, argument_list; 338, 339; 339, identifier:ev; 340, expression_statement; 340, 341; 341, call; 341, 342; 341, 343; 342, identifier:_stmt_location_to_agents; 343, argument_list; 343, 344; 343, 345; 344, identifier:st; 345, identifier:location; 346, expression_statement; 346, 347; 347, call; 347, 348; 347, 353; 348, attribute; 348, 349; 348, 352; 349, attribute; 349, 350; 349, 351; 350, identifier:self; 351, identifier:statements; 352, identifier:append; 353, argument_list; 353, 354; 354, identifier:st | def get_activations_causal(self):
"""Extract causal Activation INDRA Statements."""
# Search for causal connectives of type ONT::CAUSE
ccs = self.tree.findall("CC/[type='ONT::CAUSE']")
for cc in ccs:
factor = cc.find("arg/[@role=':FACTOR']")
outcome = cc.find("arg/[@role=':OUTCOME']")
# If either the factor or the outcome is missing, skip
if factor is None or outcome is None:
continue
factor_id = factor.attrib.get('id')
# Here, implicitly, we require that the factor is a TERM
# and not an EVENT
factor_term = self.tree.find("TERM/[@id='%s']" % factor_id)
outcome_id = outcome.attrib.get('id')
# Here it is implicit that the outcome is an event not
# a TERM
outcome_event = self.tree.find("EVENT/[@id='%s']" % outcome_id)
if factor_term is None or outcome_event is None:
continue
factor_term_type = factor_term.find('type')
# The factor term must be a molecular entity
if factor_term_type is None or \
factor_term_type.text not in molecule_types:
continue
factor_agent = self._get_agent_by_id(factor_id, None)
if factor_agent is None:
continue
outcome_event_type = outcome_event.find('type')
if outcome_event_type is None:
continue
# Construct evidence
ev = self._get_evidence(cc)
ev.epistemics['direct'] = False
location = self._get_event_location(outcome_event)
if outcome_event_type.text in ['ONT::ACTIVATE', 'ONT::ACTIVITY',
'ONT::DEACTIVATE']:
if outcome_event_type.text in ['ONT::ACTIVATE',
'ONT::DEACTIVATE']:
agent_tag = outcome_event.find(".//*[@role=':AFFECTED']")
elif outcome_event_type.text == 'ONT::ACTIVITY':
agent_tag = outcome_event.find(".//*[@role=':AGENT']")
if agent_tag is None or agent_tag.attrib.get('id') is None:
continue
outcome_agent = self._get_agent_by_id(agent_tag.attrib['id'],
outcome_id)
if outcome_agent is None:
continue
if outcome_event_type.text == 'ONT::DEACTIVATE':
is_activation = False
else:
is_activation = True
for a1, a2 in _agent_list_product((factor_agent,
outcome_agent)):
if is_activation:
st = Activation(a1, a2, evidence=[deepcopy(ev)])
else:
st = Inhibition(a1, a2, evidence=[deepcopy(ev)])
_stmt_location_to_agents(st, location)
self.statements.append(st) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_activations_stimulate; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 11; 5, 12; 5, 23; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:stim_events; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:tree; 20, identifier:findall; 21, argument_list; 21, 22; 22, string:"EVENT/[type='ONT::STIMULATE']"; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:event; 25, identifier:stim_events; 26, block; 26, 27; 26, 38; 26, 46; 26, 55; 26, 64; 26, 65; 26, 75; 26, 86; 26, 87; 26, 88; 26, 101; 26, 112; 26, 113; 26, 114; 26, 127; 26, 137; 26, 146; 26, 147; 26, 160; 26, 170; 26, 176; 26, 185; 26, 191; 26, 192; 26, 201; 26, 209; 26, 218; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:event_id; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:event; 34, identifier:attrib; 35, identifier:get; 36, argument_list; 36, 37; 37, string:'id'; 38, if_statement; 38, 39; 38, 44; 39, comparison_operator:in; 39, 40; 39, 41; 40, identifier:event_id; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_static_events; 44, block; 44, 45; 45, continue_statement; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:controller; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:event; 52, identifier:find; 53, argument_list; 53, 54; 54, string:"arg1/[@role=':AGENT']"; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:affected; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:event; 61, identifier:find; 62, argument_list; 62, 63; 63, string:"arg2/[@role=':AFFECTED']"; 64, comment; 65, if_statement; 65, 66; 65, 73; 66, boolean_operator:or; 66, 67; 66, 70; 67, comparison_operator:is; 67, 68; 67, 69; 68, identifier:controller; 69, None; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:affected; 72, None; 73, block; 73, 74; 74, continue_statement; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:controller_id; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:controller; 82, identifier:attrib; 83, identifier:get; 84, argument_list; 84, 85; 85, string:'id'; 86, comment; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:controller_term; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:tree; 96, identifier:find; 97, argument_list; 97, 98; 98, binary_operator:%; 98, 99; 98, 100; 99, string:"TERM/[@id='%s']"; 100, identifier:controller_id; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:affected_id; 104, call; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:affected; 108, identifier:attrib; 109, identifier:get; 110, argument_list; 110, 111; 111, string:'id'; 112, comment; 113, comment; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:affected_event; 117, call; 117, 118; 117, 123; 118, attribute; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:tree; 122, identifier:find; 123, argument_list; 123, 124; 124, binary_operator:%; 124, 125; 124, 126; 125, string:"EVENT/[@id='%s']"; 126, identifier:affected_id; 127, if_statement; 127, 128; 127, 135; 128, boolean_operator:or; 128, 129; 128, 132; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:controller_term; 131, None; 132, comparison_operator:is; 132, 133; 132, 134; 133, identifier:affected_event; 134, None; 135, block; 135, 136; 136, continue_statement; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:controller_term_type; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:controller_term; 143, identifier:find; 144, argument_list; 144, 145; 145, string:'type'; 146, comment; 147, if_statement; 147, 148; 147, 158; 148, boolean_operator:or; 148, 149; 148, 152; 148, 153; 149, comparison_operator:is; 149, 150; 149, 151; 150, identifier:controller_term_type; 151, None; 152, line_continuation:\; 153, comparison_operator:not; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:controller_term_type; 156, identifier:text; 157, identifier:molecule_types; 158, block; 158, 159; 159, continue_statement; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:controller_agent; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:_get_agent_by_id; 167, argument_list; 167, 168; 167, 169; 168, identifier:controller_id; 169, None; 170, if_statement; 170, 171; 170, 174; 171, comparison_operator:is; 171, 172; 171, 173; 172, identifier:controller_agent; 173, None; 174, block; 174, 175; 175, continue_statement; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:affected_event_type; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:affected_event; 182, identifier:find; 183, argument_list; 183, 184; 184, string:'type'; 185, if_statement; 185, 186; 185, 189; 186, comparison_operator:is; 186, 187; 186, 188; 187, identifier:affected_event_type; 188, None; 189, block; 189, 190; 190, continue_statement; 191, comment; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:ev; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:self; 198, identifier:_get_evidence; 199, argument_list; 199, 200; 200, identifier:event; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 208; 203, subscript; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:ev; 206, identifier:epistemics; 207, string:'direct'; 208, False; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:location; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:self; 215, identifier:_get_event_location; 216, argument_list; 216, 217; 217, identifier:affected_event; 218, if_statement; 218, 219; 218, 224; 218, 301; 219, comparison_operator:==; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:affected_event_type; 222, identifier:text; 223, string:'ONT::ACTIVATE'; 224, block; 224, 225; 224, 234; 224, 240; 224, 254; 224, 260; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:affected; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:affected_event; 231, identifier:find; 232, argument_list; 232, 233; 233, string:".//*[@role=':AFFECTED']"; 234, if_statement; 234, 235; 234, 238; 235, comparison_operator:is; 235, 236; 235, 237; 236, identifier:affected; 237, None; 238, block; 238, 239; 239, continue_statement; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:affected_agent; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:self; 246, identifier:_get_agent_by_id; 247, argument_list; 247, 248; 247, 253; 248, subscript; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, identifier:affected; 251, identifier:attrib; 252, string:'id'; 253, identifier:affected_id; 254, if_statement; 254, 255; 254, 258; 255, comparison_operator:is; 255, 256; 255, 257; 256, identifier:affected_agent; 257, None; 258, block; 258, 259; 259, continue_statement; 260, for_statement; 260, 261; 260, 264; 260, 270; 261, pattern_list; 261, 262; 261, 263; 262, identifier:a1; 263, identifier:a2; 264, call; 264, 265; 264, 266; 265, identifier:_agent_list_product; 266, argument_list; 266, 267; 267, tuple; 267, 268; 267, 269; 268, identifier:controller_agent; 269, identifier:affected_agent; 270, block; 270, 271; 270, 286; 270, 292; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:st; 274, call; 274, 275; 274, 276; 275, identifier:Activation; 276, argument_list; 276, 277; 276, 278; 276, 279; 277, identifier:a1; 278, identifier:a2; 279, keyword_argument; 279, 280; 279, 281; 280, identifier:evidence; 281, list:[deepcopy(ev)]; 281, 282; 282, call; 282, 283; 282, 284; 283, identifier:deepcopy; 284, argument_list; 284, 285; 285, identifier:ev; 286, expression_statement; 286, 287; 287, call; 287, 288; 287, 289; 288, identifier:_stmt_location_to_agents; 289, argument_list; 289, 290; 289, 291; 290, identifier:st; 291, identifier:location; 292, expression_statement; 292, 293; 293, call; 293, 294; 293, 299; 294, attribute; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:self; 297, identifier:statements; 298, identifier:append; 299, argument_list; 299, 300; 300, identifier:st; 301, elif_clause; 301, 302; 301, 307; 302, comparison_operator:==; 302, 303; 302, 306; 303, attribute; 303, 304; 303, 305; 304, identifier:affected_event_type; 305, identifier:text; 306, string:'ONT::ACTIVITY'; 307, block; 307, 308; 307, 317; 307, 323; 307, 337; 307, 343; 308, expression_statement; 308, 309; 309, assignment; 309, 310; 309, 311; 310, identifier:agent_tag; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:affected_event; 314, identifier:find; 315, argument_list; 315, 316; 316, string:".//*[@role=':AGENT']"; 317, if_statement; 317, 318; 317, 321; 318, comparison_operator:is; 318, 319; 318, 320; 319, identifier:agent_tag; 320, None; 321, block; 321, 322; 322, continue_statement; 323, expression_statement; 323, 324; 324, assignment; 324, 325; 324, 326; 325, identifier:affected_agent; 326, call; 326, 327; 326, 330; 327, attribute; 327, 328; 327, 329; 328, identifier:self; 329, identifier:_get_agent_by_id; 330, argument_list; 330, 331; 330, 336; 331, subscript; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:agent_tag; 334, identifier:attrib; 335, string:'id'; 336, identifier:affected_id; 337, if_statement; 337, 338; 337, 341; 338, comparison_operator:is; 338, 339; 338, 340; 339, identifier:affected_agent; 340, None; 341, block; 341, 342; 342, continue_statement; 343, for_statement; 343, 344; 343, 347; 343, 353; 344, pattern_list; 344, 345; 344, 346; 345, identifier:a1; 346, identifier:a2; 347, call; 347, 348; 347, 349; 348, identifier:_agent_list_product; 349, argument_list; 349, 350; 350, tuple; 350, 351; 350, 352; 351, identifier:controller_agent; 352, identifier:affected_agent; 353, block; 353, 354; 353, 369; 353, 375; 354, expression_statement; 354, 355; 355, assignment; 355, 356; 355, 357; 356, identifier:st; 357, call; 357, 358; 357, 359; 358, identifier:Activation; 359, argument_list; 359, 360; 359, 361; 359, 362; 360, identifier:a1; 361, identifier:a2; 362, keyword_argument; 362, 363; 362, 364; 363, identifier:evidence; 364, list:[deepcopy(ev)]; 364, 365; 365, call; 365, 366; 365, 367; 366, identifier:deepcopy; 367, argument_list; 367, 368; 368, identifier:ev; 369, expression_statement; 369, 370; 370, call; 370, 371; 370, 372; 371, identifier:_stmt_location_to_agents; 372, argument_list; 372, 373; 372, 374; 373, identifier:st; 374, identifier:location; 375, expression_statement; 375, 376; 376, call; 376, 377; 376, 382; 377, attribute; 377, 378; 377, 381; 378, attribute; 378, 379; 378, 380; 379, identifier:self; 380, identifier:statements; 381, identifier:append; 382, argument_list; 382, 383; 383, identifier:st | def get_activations_stimulate(self):
"""Extract Activation INDRA Statements via stimulation."""
# TODO: extract to other patterns:
# - Stimulation by EGF activates ERK
# - Stimulation by EGF leads to ERK activation
# Search for stimulation event
stim_events = self.tree.findall("EVENT/[type='ONT::STIMULATE']")
for event in stim_events:
event_id = event.attrib.get('id')
if event_id in self._static_events:
continue
controller = event.find("arg1/[@role=':AGENT']")
affected = event.find("arg2/[@role=':AFFECTED']")
# If either the controller or the affected is missing, skip
if controller is None or affected is None:
continue
controller_id = controller.attrib.get('id')
# Here, implicitly, we require that the controller is a TERM
# and not an EVENT
controller_term = self.tree.find("TERM/[@id='%s']" % controller_id)
affected_id = affected.attrib.get('id')
# Here it is implicit that the affected is an event not
# a TERM
affected_event = self.tree.find("EVENT/[@id='%s']" % affected_id)
if controller_term is None or affected_event is None:
continue
controller_term_type = controller_term.find('type')
# The controller term must be a molecular entity
if controller_term_type is None or \
controller_term_type.text not in molecule_types:
continue
controller_agent = self._get_agent_by_id(controller_id, None)
if controller_agent is None:
continue
affected_event_type = affected_event.find('type')
if affected_event_type is None:
continue
# Construct evidence
ev = self._get_evidence(event)
ev.epistemics['direct'] = False
location = self._get_event_location(affected_event)
if affected_event_type.text == 'ONT::ACTIVATE':
affected = affected_event.find(".//*[@role=':AFFECTED']")
if affected is None:
continue
affected_agent = self._get_agent_by_id(affected.attrib['id'],
affected_id)
if affected_agent is None:
continue
for a1, a2 in _agent_list_product((controller_agent,
affected_agent)):
st = Activation(a1, a2, evidence=[deepcopy(ev)])
_stmt_location_to_agents(st, location)
self.statements.append(st)
elif affected_event_type.text == 'ONT::ACTIVITY':
agent_tag = affected_event.find(".//*[@role=':AGENT']")
if agent_tag is None:
continue
affected_agent = self._get_agent_by_id(agent_tag.attrib['id'],
affected_id)
if affected_agent is None:
continue
for a1, a2 in _agent_list_product((controller_agent,
affected_agent)):
st = Activation(a1, a2, evidence=[deepcopy(ev)])
_stmt_location_to_agents(st, location)
self.statements.append(st) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_degradations; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 19; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:deg_events; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:tree; 16, identifier:findall; 17, argument_list; 17, 18; 18, string:"EVENT/[type='ONT::CONSUME']"; 19, for_statement; 19, 20; 19, 21; 19, 22; 20, identifier:event; 21, identifier:deg_events; 22, block; 22, 23; 22, 35; 22, 44; 22, 61; 22, 62; 22, 63; 22, 64; 22, 65; 22, 66; 22, 67; 22, 78; 22, 91; 22, 105; 22, 118; 22, 127; 22, 174; 22, 183; 22, 192; 22, 233; 23, if_statement; 23, 24; 23, 33; 24, comparison_operator:in; 24, 25; 24, 30; 25, subscript; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:event; 28, identifier:attrib; 29, string:'id'; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_static_events; 33, block; 33, 34; 34, continue_statement; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:affected; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:event; 41, identifier:find; 42, argument_list; 42, 43; 43, string:".//*[@role=':AFFECTED']"; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:is; 45, 46; 45, 47; 46, identifier:affected; 47, None; 48, block; 48, 49; 48, 53; 48, 60; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:msg; 52, string:'Skipping degradation event with no affected term.'; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:logger; 57, identifier:debug; 58, argument_list; 58, 59; 59, identifier:msg; 60, continue_statement; 61, comment; 62, comment; 63, comment; 64, comment; 65, comment; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:affected_id; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:affected; 74, identifier:attrib; 75, identifier:get; 76, argument_list; 76, 77; 77, string:'id'; 78, if_statement; 78, 79; 78, 82; 79, comparison_operator:is; 79, 80; 79, 81; 80, identifier:affected_id; 81, None; 82, block; 82, 83; 82, 90; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:logger; 87, identifier:debug; 88, argument_list; 88, 89; 89, string:'Skipping degradation event with missing affected agent'; 90, continue_statement; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:affected_agent; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:_get_agent_by_id; 98, argument_list; 98, 99; 98, 100; 99, identifier:affected_id; 100, subscript; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:event; 103, identifier:attrib; 104, string:'id'; 105, if_statement; 105, 106; 105, 109; 106, comparison_operator:is; 106, 107; 106, 108; 107, identifier:affected_agent; 108, None; 109, block; 109, 110; 109, 117; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:logger; 114, identifier:debug; 115, argument_list; 115, 116; 116, string:'Skipping degradation event with missing affected agent'; 117, continue_statement; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:agent; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:event; 124, identifier:find; 125, argument_list; 125, 126; 126, string:".//*[@role=':AGENT']"; 127, if_statement; 127, 128; 127, 131; 127, 136; 128, comparison_operator:is; 128, 129; 128, 130; 129, identifier:agent; 130, None; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:agent_agent; 135, None; 136, else_clause; 136, 137; 137, block; 137, 138; 137, 149; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:agent_id; 141, call; 141, 142; 141, 147; 142, attribute; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:agent; 145, identifier:attrib; 146, identifier:get; 147, argument_list; 147, 148; 148, string:'id'; 149, if_statement; 149, 150; 149, 153; 149, 158; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:agent_id; 152, None; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:agent_agent; 157, None; 158, else_clause; 158, 159; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:agent_agent; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:_get_agent_by_id; 167, argument_list; 167, 168; 167, 169; 168, identifier:agent_id; 169, subscript; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:event; 172, identifier:attrib; 173, string:'id'; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:ev; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:_get_evidence; 181, argument_list; 181, 182; 182, identifier:event; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:location; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:self; 189, identifier:_get_event_location; 190, argument_list; 190, 191; 191, identifier:event; 192, for_statement; 192, 193; 192, 196; 192, 197; 192, 203; 193, pattern_list; 193, 194; 193, 195; 194, identifier:subj; 195, identifier:obj; 196, line_continuation:\; 197, call; 197, 198; 197, 199; 198, identifier:_agent_list_product; 199, argument_list; 199, 200; 200, tuple; 200, 201; 200, 202; 201, identifier:agent_agent; 202, identifier:affected_agent; 203, block; 203, 204; 203, 218; 203, 224; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:st; 207, call; 207, 208; 207, 209; 208, identifier:DecreaseAmount; 209, argument_list; 209, 210; 209, 211; 209, 212; 210, identifier:subj; 211, identifier:obj; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:evidence; 214, call; 214, 215; 214, 216; 215, identifier:deepcopy; 216, argument_list; 216, 217; 217, identifier:ev; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 221; 220, identifier:_stmt_location_to_agents; 221, argument_list; 221, 222; 221, 223; 222, identifier:st; 223, identifier:location; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 231; 226, attribute; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:statements; 230, identifier:append; 231, argument_list; 231, 232; 232, identifier:st; 233, expression_statement; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, identifier:self; 237, identifier:_add_extracted; 238, argument_list; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:_get_type; 241, argument_list; 241, 242; 242, identifier:event; 243, subscript; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:event; 246, identifier:attrib; 247, string:'id' | def get_degradations(self):
"""Extract Degradation INDRA Statements."""
deg_events = self.tree.findall("EVENT/[type='ONT::CONSUME']")
for event in deg_events:
if event.attrib['id'] in self._static_events:
continue
affected = event.find(".//*[@role=':AFFECTED']")
if affected is None:
msg = 'Skipping degradation event with no affected term.'
logger.debug(msg)
continue
# Make sure the degradation is affecting a molecule type
# Temporarily removed for CwC compatibility with no type tag
#affected_type = affected.find('type')
#if affected_type is None or \
# affected_type.text not in molecule_types:
# continue
affected_id = affected.attrib.get('id')
if affected_id is None:
logger.debug(
'Skipping degradation event with missing affected agent')
continue
affected_agent = self._get_agent_by_id(affected_id,
event.attrib['id'])
if affected_agent is None:
logger.debug(
'Skipping degradation event with missing affected agent')
continue
agent = event.find(".//*[@role=':AGENT']")
if agent is None:
agent_agent = None
else:
agent_id = agent.attrib.get('id')
if agent_id is None:
agent_agent = None
else:
agent_agent = self._get_agent_by_id(agent_id,
event.attrib['id'])
ev = self._get_evidence(event)
location = self._get_event_location(event)
for subj, obj in \
_agent_list_product((agent_agent, affected_agent)):
st = DecreaseAmount(subj, obj, evidence=deepcopy(ev))
_stmt_location_to_agents(st, location)
self.statements.append(st)
self._add_extracted(_get_type(event), event.attrib['id']) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_complexes; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 19; 5, 30; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:bind_events; 11, call; 11, 12; 11, 17; 12, attribute; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:tree; 16, identifier:findall; 17, argument_list; 17, 18; 18, string:"EVENT/[type='ONT::BIND']"; 19, expression_statement; 19, 20; 20, augmented_assignment:+=; 20, 21; 20, 22; 21, identifier:bind_events; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:tree; 27, identifier:findall; 28, argument_list; 28, 29; 29, string:"EVENT/[type='ONT::INTERACT']"; 30, for_statement; 30, 31; 30, 32; 30, 33; 31, identifier:event; 32, identifier:bind_events; 33, block; 33, 34; 33, 46; 33, 55; 33, 64; 33, 65; 33, 107; 33, 149; 33, 167; 33, 185; 33, 202; 33, 203; 33, 204; 33, 205; 33, 207; 33, 216; 33, 225; 33, 266; 34, if_statement; 34, 35; 34, 44; 35, comparison_operator:in; 35, 36; 35, 41; 36, subscript; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:event; 39, identifier:attrib; 40, string:'id'; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_static_events; 44, block; 44, 45; 45, continue_statement; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:arg1; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:event; 52, identifier:find; 53, argument_list; 53, 54; 54, string:"arg1"; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:arg2; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:event; 61, identifier:find; 62, argument_list; 62, 63; 63, string:"arg2"; 64, comment; 65, if_statement; 65, 66; 65, 73; 66, boolean_operator:and; 66, 67; 66, 70; 67, comparison_operator:is; 67, 68; 67, 69; 68, identifier:arg1; 69, None; 70, comparison_operator:is; 70, 71; 70, 72; 71, identifier:arg2; 72, None; 73, block; 73, 74; 73, 86; 73, 95; 73, 101; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:args; 77, call; 77, 78; 77, 79; 78, identifier:list; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:event; 83, identifier:findall; 84, argument_list; 84, 85; 85, string:'arg'; 86, if_statement; 86, 87; 86, 93; 87, comparison_operator:<; 87, 88; 87, 92; 88, call; 88, 89; 88, 90; 89, identifier:len; 90, argument_list; 90, 91; 91, identifier:args; 92, integer:2; 93, block; 93, 94; 94, continue_statement; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:arg1; 98, subscript; 98, 99; 98, 100; 99, identifier:args; 100, integer:0; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:arg2; 104, subscript; 104, 105; 104, 106; 105, identifier:args; 106, integer:1; 107, if_statement; 107, 108; 107, 140; 108, boolean_operator:or; 108, 109; 108, 124; 108, 125; 109, parenthesized_expression; 109, 110; 110, boolean_operator:or; 110, 111; 110, 114; 111, comparison_operator:is; 111, 112; 111, 113; 112, identifier:arg1; 113, None; 114, comparison_operator:is; 114, 115; 114, 123; 115, call; 115, 116; 115, 121; 116, attribute; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:arg1; 119, identifier:attrib; 120, identifier:get; 121, argument_list; 121, 122; 122, string:'id'; 123, None; 124, line_continuation:\; 125, parenthesized_expression; 125, 126; 126, boolean_operator:or; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:arg2; 129, None; 130, comparison_operator:is; 130, 131; 130, 139; 131, call; 131, 132; 131, 137; 132, attribute; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:arg2; 135, identifier:attrib; 136, identifier:get; 137, argument_list; 137, 138; 138, string:'id'; 139, None; 140, block; 140, 141; 140, 148; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:logger; 145, identifier:debug; 146, argument_list; 146, 147; 147, string:'Skipping complex with less than 2 members'; 148, continue_statement; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:agent1; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_get_agent_by_id; 156, argument_list; 156, 157; 156, 162; 157, subscript; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:arg1; 160, identifier:attrib; 161, string:'id'; 162, subscript; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:event; 165, identifier:attrib; 166, string:'id'; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:agent2; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:_get_agent_by_id; 174, argument_list; 174, 175; 174, 180; 175, subscript; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:arg2; 178, identifier:attrib; 179, string:'id'; 180, subscript; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:event; 183, identifier:attrib; 184, string:'id'; 185, if_statement; 185, 186; 185, 193; 186, boolean_operator:or; 186, 187; 186, 190; 187, comparison_operator:is; 187, 188; 187, 189; 188, identifier:agent1; 189, None; 190, comparison_operator:is; 190, 191; 190, 192; 191, identifier:agent2; 192, None; 193, block; 193, 194; 193, 201; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:logger; 198, identifier:debug; 199, argument_list; 199, 200; 200, string:'Skipping complex with less than 2 members'; 201, continue_statement; 202, comment; 203, comment; 204, comment; 205, expression_statement; 205, 206; 206, string:'''
site_feature = self._find_in_term(arg1.attrib['id'], 'features/site')
if site_feature is not None:
sites, positions = self._get_site_by_id(site_id)
print sites, positions
site_feature = self._find_in_term(arg2.attrib['id'], 'features/site')
if site_feature is not None:
sites, positions = self._get_site_by_id(site_id)
print sites, positions
site = event.find("site")
if site is not None:
sites, positions = self._get_site_by_id(site.attrib['id'])
print sites, positions
'''; 207, expression_statement; 207, 208; 208, assignment; 208, 209; 208, 210; 209, identifier:ev; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:self; 213, identifier:_get_evidence; 214, argument_list; 214, 215; 215, identifier:event; 216, expression_statement; 216, 217; 217, assignment; 217, 218; 217, 219; 218, identifier:location; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:_get_event_location; 223, argument_list; 223, 224; 224, identifier:event; 225, for_statement; 225, 226; 225, 229; 225, 235; 226, pattern_list; 226, 227; 226, 228; 227, identifier:a1; 228, identifier:a2; 229, call; 229, 230; 229, 231; 230, identifier:_agent_list_product; 231, argument_list; 231, 232; 232, tuple; 232, 233; 232, 234; 233, identifier:agent1; 234, identifier:agent2; 235, block; 235, 236; 235, 251; 235, 257; 236, expression_statement; 236, 237; 237, assignment; 237, 238; 237, 239; 238, identifier:st; 239, call; 239, 240; 239, 241; 240, identifier:Complex; 241, argument_list; 241, 242; 241, 245; 242, list:[a1, a2]; 242, 243; 242, 244; 243, identifier:a1; 244, identifier:a2; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:evidence; 247, call; 247, 248; 247, 249; 248, identifier:deepcopy; 249, argument_list; 249, 250; 250, identifier:ev; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 254; 253, identifier:_stmt_location_to_agents; 254, argument_list; 254, 255; 254, 256; 255, identifier:st; 256, identifier:location; 257, expression_statement; 257, 258; 258, call; 258, 259; 258, 264; 259, attribute; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:self; 262, identifier:statements; 263, identifier:append; 264, argument_list; 264, 265; 265, identifier:st; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_add_extracted; 271, argument_list; 271, 272; 271, 276; 272, call; 272, 273; 272, 274; 273, identifier:_get_type; 274, argument_list; 274, 275; 275, identifier:event; 276, subscript; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:event; 279, identifier:attrib; 280, string:'id' | def get_complexes(self):
"""Extract Complex INDRA Statements."""
bind_events = self.tree.findall("EVENT/[type='ONT::BIND']")
bind_events += self.tree.findall("EVENT/[type='ONT::INTERACT']")
for event in bind_events:
if event.attrib['id'] in self._static_events:
continue
arg1 = event.find("arg1")
arg2 = event.find("arg2")
# EKB-AGENT
if arg1 is None and arg2 is None:
args = list(event.findall('arg'))
if len(args) < 2:
continue
arg1 = args[0]
arg2 = args[1]
if (arg1 is None or arg1.attrib.get('id') is None) or \
(arg2 is None or arg2.attrib.get('id') is None):
logger.debug('Skipping complex with less than 2 members')
continue
agent1 = self._get_agent_by_id(arg1.attrib['id'],
event.attrib['id'])
agent2 = self._get_agent_by_id(arg2.attrib['id'],
event.attrib['id'])
if agent1 is None or agent2 is None:
logger.debug('Skipping complex with less than 2 members')
continue
# Information on binding site is either attached to the agent term
# in a features/site tag or attached to the event itself in
# a site tag
'''
site_feature = self._find_in_term(arg1.attrib['id'], 'features/site')
if site_feature is not None:
sites, positions = self._get_site_by_id(site_id)
print sites, positions
site_feature = self._find_in_term(arg2.attrib['id'], 'features/site')
if site_feature is not None:
sites, positions = self._get_site_by_id(site_id)
print sites, positions
site = event.find("site")
if site is not None:
sites, positions = self._get_site_by_id(site.attrib['id'])
print sites, positions
'''
ev = self._get_evidence(event)
location = self._get_event_location(event)
for a1, a2 in _agent_list_product((agent1, agent2)):
st = Complex([a1, a2], evidence=deepcopy(ev))
_stmt_location_to_agents(st, location)
self.statements.append(st)
self._add_extracted(_get_type(event), event.attrib['id']) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_modifications_indirect; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 20; 5, 21; 5, 26; 5, 133; 5, 240; 5, 247; 5, 254; 5, 255; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:mod_event_types; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:ont_to_mod_type; 18, identifier:keys; 19, argument_list; 20, comment; 21, expression_statement; 21, 22; 22, augmented_assignment:+=; 22, 23; 22, 24; 23, identifier:mod_event_types; 24, list:['ONT::PTM']; 24, 25; 25, string:'ONT::PTM'; 26, function_definition; 26, 27; 26, 28; 26, 30; 27, function_name:get_increase_events; 28, parameters; 28, 29; 29, identifier:mod_event_types; 30, block; 30, 31; 30, 35; 30, 46; 30, 131; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:mod_events; 34, list:[]; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:events; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:tree; 43, identifier:findall; 44, argument_list; 44, 45; 45, string:"EVENT/[type='ONT::INCREASE']"; 46, for_statement; 46, 47; 46, 48; 46, 49; 47, identifier:event; 48, identifier:events; 49, block; 49, 50; 49, 59; 49, 65; 49, 76; 49, 81; 49, 87; 49, 98; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:affected; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:event; 56, identifier:find; 57, argument_list; 57, 58; 58, string:".//*[@role=':AFFECTED']"; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:affected; 62, None; 63, block; 63, 64; 64, continue_statement; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:affected_id; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:affected; 72, identifier:attrib; 73, identifier:get; 74, argument_list; 74, 75; 75, string:'id'; 76, if_statement; 76, 77; 76, 79; 77, not_operator; 77, 78; 78, identifier:affected_id; 79, block; 79, 80; 80, continue_statement; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:pattern; 84, binary_operator:%; 84, 85; 84, 86; 85, string:"EVENT/[@id='%s']"; 86, identifier:affected_id; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:affected_event; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:tree; 95, identifier:find; 96, argument_list; 96, 97; 97, identifier:pattern; 98, if_statement; 98, 99; 98, 102; 99, comparison_operator:is; 99, 100; 99, 101; 100, identifier:affected_event; 101, None; 102, block; 102, 103; 102, 112; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:affected_type; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:affected_event; 109, identifier:find; 110, argument_list; 110, 111; 111, string:'type'; 112, if_statement; 112, 113; 112, 123; 113, boolean_operator:and; 113, 114; 113, 117; 113, 118; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:affected_type; 116, None; 117, line_continuation:\; 118, comparison_operator:in; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:affected_type; 121, identifier:text; 122, identifier:mod_event_types; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:mod_events; 128, identifier:append; 129, argument_list; 129, 130; 130, identifier:event; 131, return_statement; 131, 132; 132, identifier:mod_events; 133, function_definition; 133, 134; 133, 135; 133, 137; 134, function_name:get_cause_events; 135, parameters; 135, 136; 136, identifier:mod_event_types; 137, block; 137, 138; 137, 142; 137, 153; 137, 238; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:mod_events; 141, list:[]; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:ccs; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:tree; 150, identifier:findall; 151, argument_list; 151, 152; 152, string:"CC/[type='ONT::CAUSE']"; 153, for_statement; 153, 154; 153, 155; 153, 156; 154, identifier:cc; 155, identifier:ccs; 156, block; 156, 157; 156, 166; 156, 172; 156, 183; 156, 188; 156, 194; 156, 205; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:outcome; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:cc; 163, identifier:find; 164, argument_list; 164, 165; 165, string:".//*[@role=':OUTCOME']"; 166, if_statement; 166, 167; 166, 170; 167, comparison_operator:is; 167, 168; 167, 169; 168, identifier:outcome; 169, None; 170, block; 170, 171; 171, continue_statement; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:outcome_id; 175, call; 175, 176; 175, 181; 176, attribute; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:outcome; 179, identifier:attrib; 180, identifier:get; 181, argument_list; 181, 182; 182, string:'id'; 183, if_statement; 183, 184; 183, 186; 184, not_operator; 184, 185; 185, identifier:outcome_id; 186, block; 186, 187; 187, continue_statement; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:pattern; 191, binary_operator:%; 191, 192; 191, 193; 192, string:"EVENT/[@id='%s']"; 193, identifier:outcome_id; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:outcome_event; 197, call; 197, 198; 197, 203; 198, attribute; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:tree; 202, identifier:find; 203, argument_list; 203, 204; 204, identifier:pattern; 205, if_statement; 205, 206; 205, 209; 206, comparison_operator:is; 206, 207; 206, 208; 207, identifier:outcome_event; 208, None; 209, block; 209, 210; 209, 219; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:outcome_type; 213, call; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:outcome_event; 216, identifier:find; 217, argument_list; 217, 218; 218, string:'type'; 219, if_statement; 219, 220; 219, 230; 220, boolean_operator:and; 220, 221; 220, 224; 220, 225; 221, comparison_operator:is; 221, 222; 221, 223; 222, identifier:outcome_type; 223, None; 224, line_continuation:\; 225, comparison_operator:in; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:outcome_type; 228, identifier:text; 229, identifier:mod_event_types; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:mod_events; 235, identifier:append; 236, argument_list; 236, 237; 237, identifier:cc; 238, return_statement; 238, 239; 239, identifier:mod_events; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:mod_events; 243, call; 243, 244; 243, 245; 244, identifier:get_increase_events; 245, argument_list; 245, 246; 246, identifier:mod_event_types; 247, expression_statement; 247, 248; 248, augmented_assignment:+=; 248, 249; 248, 250; 249, identifier:mod_events; 250, call; 250, 251; 250, 252; 251, identifier:get_cause_events; 252, argument_list; 252, 253; 253, identifier:mod_event_types; 254, comment; 255, for_statement; 255, 256; 255, 257; 255, 258; 256, identifier:event; 257, identifier:mod_events; 258, block; 258, 259; 258, 267; 258, 275; 258, 282; 258, 283; 258, 292; 258, 312; 258, 323; 258, 329; 258, 339; 258, 348; 258, 368; 258, 379; 258, 384; 258, 397; 258, 403; 258, 404; 258, 499; 258, 511; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 262; 261, identifier:event_id; 262, subscript; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:event; 265, identifier:attrib; 266, string:'id'; 267, if_statement; 267, 268; 267, 273; 268, comparison_operator:in; 268, 269; 268, 270; 269, identifier:event_id; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:_static_events; 273, block; 273, 274; 274, continue_statement; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 278; 277, identifier:event_type; 278, call; 278, 279; 278, 280; 279, identifier:_get_type; 280, argument_list; 280, 281; 281, identifier:event; 282, comment; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 286; 285, identifier:enzyme; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:event; 289, identifier:find; 290, argument_list; 290, 291; 291, string:".//*[@role=':AGENT']"; 292, if_statement; 292, 293; 292, 296; 293, comparison_operator:is; 293, 294; 293, 295; 294, identifier:enzyme; 295, None; 296, block; 296, 297; 296, 306; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:enzyme; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:event; 303, identifier:find; 304, argument_list; 304, 305; 305, string:".//*[@role=':FACTOR']"; 306, if_statement; 306, 307; 306, 310; 307, comparison_operator:is; 307, 308; 307, 309; 308, identifier:enzyme; 309, None; 310, block; 310, 311; 311, return_statement; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:enzyme_id; 315, call; 315, 316; 315, 321; 316, attribute; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:enzyme; 319, identifier:attrib; 320, identifier:get; 321, argument_list; 321, 322; 322, string:'id'; 323, if_statement; 323, 324; 323, 327; 324, comparison_operator:is; 324, 325; 324, 326; 325, identifier:enzyme_id; 326, None; 327, block; 327, 328; 328, continue_statement; 329, expression_statement; 329, 330; 330, assignment; 330, 331; 330, 332; 331, identifier:enzyme_agent; 332, call; 332, 333; 332, 336; 333, attribute; 333, 334; 333, 335; 334, identifier:self; 335, identifier:_get_agent_by_id; 336, argument_list; 336, 337; 336, 338; 337, identifier:enzyme_id; 338, identifier:event_id; 339, expression_statement; 339, 340; 340, assignment; 340, 341; 340, 342; 341, identifier:affected_event_tag; 342, call; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:event; 345, identifier:find; 346, argument_list; 346, 347; 347, string:".//*[@role=':AFFECTED']"; 348, if_statement; 348, 349; 348, 352; 349, comparison_operator:is; 349, 350; 349, 351; 350, identifier:affected_event_tag; 351, None; 352, block; 352, 353; 352, 362; 353, expression_statement; 353, 354; 354, assignment; 354, 355; 354, 356; 355, identifier:affected_event_tag; 356, call; 356, 357; 356, 360; 357, attribute; 357, 358; 357, 359; 358, identifier:event; 359, identifier:find; 360, argument_list; 360, 361; 361, string:".//*[@role=':OUTCOME']"; 362, if_statement; 362, 363; 362, 366; 363, comparison_operator:is; 363, 364; 363, 365; 364, identifier:affected_event_tag; 365, None; 366, block; 366, 367; 367, return_statement; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 371; 370, identifier:affected_id; 371, call; 371, 372; 371, 377; 372, attribute; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:affected_event_tag; 375, identifier:attrib; 376, identifier:get; 377, argument_list; 377, 378; 378, string:'id'; 379, if_statement; 379, 380; 379, 382; 380, not_operator; 380, 381; 381, identifier:affected_id; 382, block; 382, 383; 383, return_statement; 384, expression_statement; 384, 385; 385, assignment; 385, 386; 385, 387; 386, identifier:affected_event; 387, call; 387, 388; 387, 393; 388, attribute; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:self; 391, identifier:tree; 392, identifier:find; 393, argument_list; 393, 394; 394, binary_operator:%; 394, 395; 394, 396; 395, string:"EVENT/[@id='%s']"; 396, identifier:affected_id; 397, if_statement; 397, 398; 397, 401; 398, comparison_operator:is; 398, 399; 398, 400; 399, identifier:affected_event; 400, None; 401, block; 401, 402; 402, return_statement; 403, comment; 404, for_statement; 404, 405; 404, 406; 404, 411; 404, 412; 404, 413; 405, identifier:enz_t; 406, call; 406, 407; 406, 408; 407, identifier:_agent_list_product; 408, argument_list; 408, 409; 409, tuple; 409, 410; 410, identifier:enzyme_agent; 411, comment; 412, comment; 413, block; 413, 414; 413, 420; 413, 421; 413, 422; 413, 423; 413, 424; 413, 433; 413, 437; 413, 466; 414, expression_statement; 414, 415; 415, assignment; 415, 416; 415, 417; 416, identifier:enz; 417, subscript; 417, 418; 417, 419; 418, identifier:enz_t; 419, integer:0; 420, comment; 421, comment; 422, comment; 423, comment; 424, expression_statement; 424, 425; 425, assignment; 425, 426; 425, 427; 426, identifier:stmts; 427, call; 427, 428; 427, 431; 428, attribute; 428, 429; 428, 430; 429, identifier:self; 430, identifier:_get_modification_event; 431, argument_list; 431, 432; 432, identifier:affected_event; 433, expression_statement; 433, 434; 434, assignment; 434, 435; 434, 436; 435, identifier:stmts_to_make; 436, list:[]; 437, if_statement; 437, 438; 437, 439; 438, identifier:stmts; 439, block; 439, 440; 440, for_statement; 440, 441; 440, 442; 440, 443; 440, 444; 440, 445; 441, identifier:stmt; 442, identifier:stmts; 443, comment; 444, comment; 445, block; 445, 446; 446, if_statement; 446, 447; 446, 458; 447, boolean_operator:and; 447, 448; 447, 453; 448, comparison_operator:is; 448, 449; 448, 452; 449, attribute; 449, 450; 449, 451; 450, identifier:stmt; 451, identifier:enz; 452, None; 453, comparison_operator:is; 453, 454; 453, 457; 454, attribute; 454, 455; 454, 456; 455, identifier:stmt; 456, identifier:sub; 457, None; 458, block; 458, 459; 459, expression_statement; 459, 460; 460, call; 460, 461; 460, 464; 461, attribute; 461, 462; 461, 463; 462, identifier:stmts_to_make; 463, identifier:append; 464, argument_list; 464, 465; 465, identifier:stmt; 466, for_statement; 466, 467; 466, 468; 466, 469; 467, identifier:stmt; 468, identifier:stmts_to_make; 469, block; 469, 470; 469, 476; 469, 490; 470, expression_statement; 470, 471; 471, assignment; 471, 472; 471, 475; 472, attribute; 472, 473; 472, 474; 473, identifier:stmt; 474, identifier:enz; 475, identifier:enz; 476, for_statement; 476, 477; 476, 478; 476, 481; 477, identifier:ev; 478, attribute; 478, 479; 478, 480; 479, identifier:stmt; 480, identifier:evidence; 481, block; 481, 482; 482, expression_statement; 482, 483; 483, assignment; 483, 484; 483, 489; 484, subscript; 484, 485; 484, 488; 485, attribute; 485, 486; 485, 487; 486, identifier:ev; 487, identifier:epistemics; 488, string:'direct'; 489, False; 490, expression_statement; 490, 491; 491, call; 491, 492; 491, 497; 492, attribute; 492, 493; 492, 496; 493, attribute; 493, 494; 493, 495; 494, identifier:self; 495, identifier:statements; 496, identifier:append; 497, argument_list; 497, 498; 498, identifier:stmt; 499, expression_statement; 499, 500; 500, call; 500, 501; 500, 504; 501, attribute; 501, 502; 501, 503; 502, identifier:self; 503, identifier:_add_extracted; 504, argument_list; 504, 505; 504, 506; 505, identifier:event_type; 506, subscript; 506, 507; 506, 510; 507, attribute; 507, 508; 507, 509; 508, identifier:event; 509, identifier:attrib; 510, string:'id'; 511, expression_statement; 511, 512; 512, call; 512, 513; 512, 516; 513, attribute; 513, 514; 513, 515; 514, identifier:self; 515, identifier:_add_extracted; 516, argument_list; 516, 517; 516, 525; 517, attribute; 517, 518; 517, 524; 518, call; 518, 519; 518, 522; 519, attribute; 519, 520; 519, 521; 520, identifier:affected_event; 521, identifier:find; 522, argument_list; 522, 523; 523, string:'type'; 524, identifier:text; 525, identifier:affected_id | def get_modifications_indirect(self):
"""Extract indirect Modification INDRA Statements."""
# Get all the specific mod types
mod_event_types = list(ont_to_mod_type.keys())
# Add ONT::PTMs as a special case
mod_event_types += ['ONT::PTM']
def get_increase_events(mod_event_types):
mod_events = []
events = self.tree.findall("EVENT/[type='ONT::INCREASE']")
for event in events:
affected = event.find(".//*[@role=':AFFECTED']")
if affected is None:
continue
affected_id = affected.attrib.get('id')
if not affected_id:
continue
pattern = "EVENT/[@id='%s']" % affected_id
affected_event = self.tree.find(pattern)
if affected_event is not None:
affected_type = affected_event.find('type')
if affected_type is not None and \
affected_type.text in mod_event_types:
mod_events.append(event)
return mod_events
def get_cause_events(mod_event_types):
mod_events = []
ccs = self.tree.findall("CC/[type='ONT::CAUSE']")
for cc in ccs:
outcome = cc.find(".//*[@role=':OUTCOME']")
if outcome is None:
continue
outcome_id = outcome.attrib.get('id')
if not outcome_id:
continue
pattern = "EVENT/[@id='%s']" % outcome_id
outcome_event = self.tree.find(pattern)
if outcome_event is not None:
outcome_type = outcome_event.find('type')
if outcome_type is not None and \
outcome_type.text in mod_event_types:
mod_events.append(cc)
return mod_events
mod_events = get_increase_events(mod_event_types)
mod_events += get_cause_events(mod_event_types)
# Iterate over all modification events
for event in mod_events:
event_id = event.attrib['id']
if event_id in self._static_events:
continue
event_type = _get_type(event)
# Get enzyme Agent
enzyme = event.find(".//*[@role=':AGENT']")
if enzyme is None:
enzyme = event.find(".//*[@role=':FACTOR']")
if enzyme is None:
return
enzyme_id = enzyme.attrib.get('id')
if enzyme_id is None:
continue
enzyme_agent = self._get_agent_by_id(enzyme_id, event_id)
affected_event_tag = event.find(".//*[@role=':AFFECTED']")
if affected_event_tag is None:
affected_event_tag = event.find(".//*[@role=':OUTCOME']")
if affected_event_tag is None:
return
affected_id = affected_event_tag.attrib.get('id')
if not affected_id:
return
affected_event = self.tree.find("EVENT/[@id='%s']" % affected_id)
if affected_event is None:
return
# Iterate over all enzyme agents if there are multiple ones
for enz_t in _agent_list_product((enzyme_agent, )):
# enz_t comes out as a tuple so we need to take the first
# element here
enz = enz_t[0]
# Note that we re-run the extraction code here potentially
# multiple times. This is mainly to make sure each Statement
# object created here is independent (i.e. has different UUIDs)
# without having to manipulate it after creation.
stmts = self._get_modification_event(affected_event)
stmts_to_make = []
if stmts:
for stmt in stmts:
# The affected event should have no enzyme but should
# have a substrate
if stmt.enz is None and stmt.sub is not None:
stmts_to_make.append(stmt)
for stmt in stmts_to_make:
stmt.enz = enz
for ev in stmt.evidence:
ev.epistemics['direct'] = False
self.statements.append(stmt)
self._add_extracted(event_type, event.attrib['id'])
self._add_extracted(affected_event.find('type').text, affected_id) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:replace_activities; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 15; 5, 16; 5, 20; 5, 52; 5, 53; 5, 59; 5, 60; 5, 297; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:logger; 12, identifier:debug; 13, argument_list; 13, 14; 14, string:'Running PySB Preassembler replace activities'; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:new_stmts; 19, list:[]; 20, function_definition; 20, 21; 20, 22; 20, 24; 21, function_name:has_agent_activity; 22, parameters; 22, 23; 23, identifier:stmt; 24, block; 24, 25; 24, 27; 24, 50; 25, expression_statement; 25, 26; 26, comment; 27, for_statement; 27, 28; 27, 29; 27, 34; 28, identifier:agent; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:stmt; 32, identifier:agent_list; 33, argument_list; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 47; 36, boolean_operator:and; 36, 37; 36, 42; 37, call; 37, 38; 37, 39; 38, identifier:isinstance; 39, argument_list; 39, 40; 39, 41; 40, identifier:agent; 41, identifier:Agent; 42, comparison_operator:is; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:agent; 45, identifier:activity; 46, None; 47, block; 47, 48; 48, return_statement; 48, 49; 49, True; 50, return_statement; 50, 51; 51, False; 52, comment; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_gather_active_forms; 58, argument_list; 59, comment; 60, for_statement; 60, 61; 60, 64; 60, 70; 61, pattern_list; 61, 62; 61, 63; 62, identifier:j; 63, identifier:stmt; 64, call; 64, 65; 64, 66; 65, identifier:enumerate; 66, argument_list; 66, 67; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:statements; 70, block; 70, 71; 70, 90; 70, 91; 70, 92; 70, 107; 70, 115; 70, 122; 70, 123; 70, 124; 70, 132; 70, 260; 70, 261; 70, 262; 70, 272; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:logger; 75, identifier:debug; 76, argument_list; 76, 77; 77, binary_operator:%; 77, 78; 77, 79; 78, string:'%d/%d %s'; 79, tuple; 79, 80; 79, 83; 79, 89; 80, binary_operator:+; 80, 81; 80, 82; 81, identifier:j; 82, integer:1; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, attribute; 86, 87; 86, 88; 87, identifier:self; 88, identifier:statements; 89, identifier:stmt; 90, comment; 91, comment; 92, if_statement; 92, 93; 92, 98; 93, not_operator; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:has_agent_activity; 96, argument_list; 96, 97; 97, identifier:stmt; 98, block; 98, 99; 98, 106; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:new_stmts; 103, identifier:append; 104, argument_list; 104, 105; 105, identifier:stmt; 106, continue_statement; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:stmt_agents; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:stmt; 113, identifier:agent_list; 114, argument_list; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:num_agents; 118, call; 118, 119; 118, 120; 119, identifier:len; 120, argument_list; 120, 121; 121, identifier:stmt_agents; 122, comment; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:agent_forms; 127, list_comprehension; 127, 128; 127, 129; 128, list:[]; 129, for_in_clause; 129, 130; 129, 131; 130, identifier:a; 131, identifier:stmt_agents; 132, for_statement; 132, 133; 132, 136; 132, 140; 132, 141; 132, 142; 132, 143; 133, pattern_list; 133, 134; 133, 135; 134, identifier:i; 135, identifier:agent; 136, call; 136, 137; 136, 138; 137, identifier:enumerate; 138, argument_list; 138, 139; 139, identifier:stmt_agents; 140, comment; 141, comment; 142, comment; 143, block; 143, 144; 144, if_statement; 144, 145; 144, 161; 144, 248; 144, 249; 145, boolean_operator:and; 145, 146; 145, 155; 145, 156; 146, boolean_operator:and; 146, 147; 146, 150; 147, comparison_operator:is; 147, 148; 147, 149; 148, identifier:agent; 149, None; 150, call; 150, 151; 150, 152; 151, identifier:isinstance; 152, argument_list; 152, 153; 152, 154; 153, identifier:agent; 154, identifier:Agent; 155, line_continuation:\; 156, comparison_operator:is; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:agent; 159, identifier:activity; 160, None; 161, block; 161, 162; 161, 173; 161, 174; 161, 218; 161, 219; 161, 220; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:base_agent; 165, call; 165, 166; 165, 171; 166, attribute; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:self; 169, identifier:agent_set; 170, identifier:get_create_base_agent; 171, argument_list; 171, 172; 172, identifier:agent; 173, comment; 174, if_statement; 174, 175; 174, 180; 174, 198; 174, 199; 175, attribute; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:agent; 178, identifier:activity; 179, identifier:is_active; 180, block; 180, 181; 180, 187; 180, 188; 180, 189; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:active_forms; 184, attribute; 184, 185; 184, 186; 185, identifier:base_agent; 186, identifier:active_forms; 187, comment; 188, comment; 189, if_statement; 189, 190; 189, 192; 190, not_operator; 190, 191; 191, identifier:active_forms; 192, block; 192, 193; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:active_forms; 196, list:[agent]; 196, 197; 197, identifier:agent; 198, comment; 199, else_clause; 199, 200; 200, block; 200, 201; 200, 207; 200, 208; 200, 209; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 204; 203, identifier:active_forms; 204, attribute; 204, 205; 204, 206; 205, identifier:base_agent; 206, identifier:inactive_forms; 207, comment; 208, comment; 209, if_statement; 209, 210; 209, 212; 210, not_operator; 210, 211; 211, identifier:active_forms; 212, block; 212, 213; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:active_forms; 216, list:[agent]; 216, 217; 217, identifier:agent; 218, comment; 219, comment; 220, for_statement; 220, 221; 220, 222; 220, 223; 221, identifier:af; 222, identifier:active_forms; 223, block; 223, 224; 223, 231; 223, 239; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 227; 226, identifier:new_agent; 227, call; 227, 228; 227, 229; 228, identifier:fast_deepcopy; 229, argument_list; 229, 230; 230, identifier:agent; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:_set_agent_context; 236, argument_list; 236, 237; 236, 238; 237, identifier:af; 238, identifier:new_agent; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, subscript; 242, 243; 242, 244; 243, identifier:agent_forms; 244, identifier:i; 245, identifier:append; 246, argument_list; 246, 247; 247, identifier:new_agent; 248, comment; 249, else_clause; 249, 250; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, subscript; 254, 255; 254, 256; 255, identifier:agent_forms; 256, identifier:i; 257, identifier:append; 258, argument_list; 258, 259; 259, identifier:agent; 260, comment; 261, comment; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:agent_combs; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:itertools; 268, identifier:product; 269, argument_list; 269, 270; 270, list_splat; 270, 271; 271, identifier:agent_forms; 272, for_statement; 272, 273; 272, 274; 272, 275; 273, identifier:agent_comb; 274, identifier:agent_combs; 275, block; 275, 276; 275, 283; 275, 290; 276, expression_statement; 276, 277; 277, assignment; 277, 278; 277, 279; 278, identifier:new_stmt; 279, call; 279, 280; 279, 281; 280, identifier:fast_deepcopy; 281, argument_list; 281, 282; 282, identifier:stmt; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 288; 285, attribute; 285, 286; 285, 287; 286, identifier:new_stmt; 287, identifier:set_agent_list; 288, argument_list; 288, 289; 289, identifier:agent_comb; 290, expression_statement; 290, 291; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, identifier:new_stmts; 294, identifier:append; 295, argument_list; 295, 296; 296, identifier:new_stmt; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:self; 301, identifier:statements; 302, identifier:new_stmts | def replace_activities(self):
"""Replace ative flags with Agent states when possible."""
logger.debug('Running PySB Preassembler replace activities')
# TODO: handle activity hierarchies
new_stmts = []
def has_agent_activity(stmt):
"""Return True if any agents in the Statement have activity."""
for agent in stmt.agent_list():
if isinstance(agent, Agent) and agent.activity is not None:
return True
return False
# First collect all explicit active forms
self._gather_active_forms()
# Iterate over all statements
for j, stmt in enumerate(self.statements):
logger.debug('%d/%d %s' % (j + 1, len(self.statements), stmt))
# If the Statement doesn't have any activities, we can just
# keep it and move on
if not has_agent_activity(stmt):
new_stmts.append(stmt)
continue
stmt_agents = stmt.agent_list()
num_agents = len(stmt_agents)
# Make a list with an empty list for each Agent so that later
# we can build combinations of Agent forms
agent_forms = [[] for a in stmt_agents]
for i, agent in enumerate(stmt_agents):
# This is the case where there is an activity flag on an
# Agent which we will attempt to replace with an explicit
# active form
if agent is not None and isinstance(agent, Agent) and \
agent.activity is not None:
base_agent = self.agent_set.get_create_base_agent(agent)
# If it is an "active" state
if agent.activity.is_active:
active_forms = base_agent.active_forms
# If no explicit active forms are known then we use
# the generic one
if not active_forms:
active_forms = [agent]
# If it is an "inactive" state
else:
active_forms = base_agent.inactive_forms
# If no explicit inactive forms are known then we use
# the generic one
if not active_forms:
active_forms = [agent]
# We now iterate over the active agent forms and create
# new agents
for af in active_forms:
new_agent = fast_deepcopy(agent)
self._set_agent_context(af, new_agent)
agent_forms[i].append(new_agent)
# Otherwise we just copy over the agent as is
else:
agent_forms[i].append(agent)
# Now create all possible combinations of the agents and create new
# statements as needed
agent_combs = itertools.product(*agent_forms)
for agent_comb in agent_combs:
new_stmt = fast_deepcopy(stmt)
new_stmt.set_agent_list(agent_comb)
new_stmts.append(new_stmt)
self.statements = new_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:add_reverse_effects; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 17; 5, 21; 5, 25; 5, 211; 5, 215; 5, 279; 5, 306; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:pos_mod_sites; 12, dictionary; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:neg_mod_sites; 16, dictionary; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:syntheses; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:degradations; 24, list:[]; 25, for_statement; 25, 26; 25, 27; 25, 30; 26, identifier:stmt; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:statements; 30, block; 30, 31; 31, if_statement; 31, 32; 31, 37; 31, 79; 31, 127; 31, 175; 31, 193; 32, call; 32, 33; 32, 34; 33, identifier:isinstance; 34, argument_list; 34, 35; 34, 36; 35, identifier:stmt; 36, identifier:Phosphorylation; 37, block; 37, 38; 37, 46; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:agent; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:stmt; 44, identifier:sub; 45, identifier:name; 46, try_statement; 46, 47; 46, 63; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 55; 50, attribute; 50, 51; 50, 54; 51, subscript; 51, 52; 51, 53; 52, identifier:pos_mod_sites; 53, identifier:agent; 54, identifier:append; 55, argument_list; 55, 56; 56, tuple; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:stmt; 59, identifier:residue; 60, attribute; 60, 61; 60, 62; 61, identifier:stmt; 62, identifier:position; 63, except_clause; 63, 64; 63, 65; 64, identifier:KeyError; 65, block; 65, 66; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:pos_mod_sites; 70, identifier:agent; 71, list:[(stmt.residue, stmt.position)]; 71, 72; 72, tuple; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:stmt; 75, identifier:residue; 76, attribute; 76, 77; 76, 78; 77, identifier:stmt; 78, identifier:position; 79, elif_clause; 79, 80; 79, 85; 80, call; 80, 81; 80, 82; 81, identifier:isinstance; 82, argument_list; 82, 83; 82, 84; 83, identifier:stmt; 84, identifier:Dephosphorylation; 85, block; 85, 86; 85, 94; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:agent; 89, attribute; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:stmt; 92, identifier:sub; 93, identifier:name; 94, try_statement; 94, 95; 94, 111; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 103; 98, attribute; 98, 99; 98, 102; 99, subscript; 99, 100; 99, 101; 100, identifier:neg_mod_sites; 101, identifier:agent; 102, identifier:append; 103, argument_list; 103, 104; 104, tuple; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:stmt; 107, identifier:residue; 108, attribute; 108, 109; 108, 110; 109, identifier:stmt; 110, identifier:position; 111, except_clause; 111, 112; 111, 113; 112, identifier:KeyError; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:neg_mod_sites; 118, identifier:agent; 119, list:[(stmt.residue, stmt.position)]; 119, 120; 120, tuple; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:stmt; 123, identifier:residue; 124, attribute; 124, 125; 124, 126; 125, identifier:stmt; 126, identifier:position; 127, elif_clause; 127, 128; 127, 133; 128, call; 128, 129; 128, 130; 129, identifier:isinstance; 130, argument_list; 130, 131; 130, 132; 131, identifier:stmt; 132, identifier:Influence; 133, block; 133, 134; 134, if_statement; 134, 135; 134, 142; 134, 154; 135, comparison_operator:==; 135, 136; 135, 141; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:stmt; 139, identifier:overall_polarity; 140, argument_list; 141, integer:1; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, call; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:syntheses; 147, identifier:append; 148, argument_list; 148, 149; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:stmt; 152, identifier:obj; 153, identifier:name; 154, elif_clause; 154, 155; 154, 163; 155, comparison_operator:==; 155, 156; 155, 161; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:stmt; 159, identifier:overall_polarity; 160, argument_list; 161, unary_operator:-; 161, 162; 162, integer:1; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:degradations; 168, identifier:append; 169, argument_list; 169, 170; 170, attribute; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:stmt; 173, identifier:obj; 174, identifier:name; 175, elif_clause; 175, 176; 175, 181; 176, call; 176, 177; 176, 178; 177, identifier:isinstance; 178, argument_list; 178, 179; 178, 180; 179, identifier:stmt; 180, identifier:IncreaseAmount; 181, block; 181, 182; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:syntheses; 186, identifier:append; 187, argument_list; 187, 188; 188, attribute; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:stmt; 191, identifier:obj; 192, identifier:name; 193, elif_clause; 193, 194; 193, 199; 194, call; 194, 195; 194, 196; 195, identifier:isinstance; 196, argument_list; 196, 197; 196, 198; 197, identifier:stmt; 198, identifier:DecreaseAmount; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:degradations; 204, identifier:append; 205, argument_list; 205, 206; 206, attribute; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:stmt; 209, identifier:obj; 210, identifier:name; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:new_stmts; 214, list:[]; 215, for_statement; 215, 216; 215, 219; 215, 224; 216, pattern_list; 216, 217; 216, 218; 217, identifier:agent_name; 218, identifier:pos_sites; 219, call; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:pos_mod_sites; 222, identifier:items; 223, argument_list; 224, block; 224, 225; 224, 235; 224, 250; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:neg_sites; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:neg_mod_sites; 231, identifier:get; 232, argument_list; 232, 233; 232, 234; 233, identifier:agent_name; 234, list:[]; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 238; 237, identifier:no_neg_site; 238, call; 238, 239; 238, 245; 239, attribute; 239, 240; 239, 244; 240, call; 240, 241; 240, 242; 241, identifier:set; 242, argument_list; 242, 243; 243, identifier:pos_sites; 244, identifier:difference; 245, argument_list; 245, 246; 246, call; 246, 247; 246, 248; 247, identifier:set; 248, argument_list; 248, 249; 249, identifier:neg_sites; 250, for_statement; 250, 251; 250, 254; 250, 255; 251, pattern_list; 251, 252; 251, 253; 252, identifier:residue; 253, identifier:position; 254, identifier:no_neg_site; 255, block; 255, 256; 255, 272; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:st; 259, call; 259, 260; 259, 261; 260, identifier:Dephosphorylation; 261, argument_list; 261, 262; 261, 266; 261, 270; 261, 271; 262, call; 262, 263; 262, 264; 263, identifier:Agent; 264, argument_list; 264, 265; 265, string:'phosphatase'; 266, call; 266, 267; 266, 268; 267, identifier:Agent; 268, argument_list; 268, 269; 269, identifier:agent_name; 270, identifier:residue; 271, identifier:position; 272, expression_statement; 272, 273; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:new_stmts; 276, identifier:append; 277, argument_list; 277, 278; 278, identifier:st; 279, for_statement; 279, 280; 279, 281; 279, 282; 280, identifier:agent_name; 281, identifier:syntheses; 282, block; 282, 283; 283, if_statement; 283, 284; 283, 287; 284, comparison_operator:not; 284, 285; 284, 286; 285, identifier:agent_name; 286, identifier:degradations; 287, block; 287, 288; 287, 299; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:st; 291, call; 291, 292; 291, 293; 292, identifier:DecreaseAmount; 293, argument_list; 293, 294; 293, 295; 294, None; 295, call; 295, 296; 295, 297; 296, identifier:Agent; 297, argument_list; 297, 298; 298, identifier:agent_name; 299, expression_statement; 299, 300; 300, call; 300, 301; 300, 304; 301, attribute; 301, 302; 301, 303; 302, identifier:new_stmts; 303, identifier:append; 304, argument_list; 304, 305; 305, identifier:st; 306, expression_statement; 306, 307; 307, augmented_assignment:+=; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:statements; 311, identifier:new_stmts | def add_reverse_effects(self):
"""Add Statements for the reverse effects of some Statements.
For instance, if a protein is phosphorylated but never dephosphorylated
in the model, we add a generic dephosphorylation here. This step is
usually optional in the assembly process.
"""
# TODO: generalize to other modification sites
pos_mod_sites = {}
neg_mod_sites = {}
syntheses = []
degradations = []
for stmt in self.statements:
if isinstance(stmt, Phosphorylation):
agent = stmt.sub.name
try:
pos_mod_sites[agent].append((stmt.residue, stmt.position))
except KeyError:
pos_mod_sites[agent] = [(stmt.residue, stmt.position)]
elif isinstance(stmt, Dephosphorylation):
agent = stmt.sub.name
try:
neg_mod_sites[agent].append((stmt.residue, stmt.position))
except KeyError:
neg_mod_sites[agent] = [(stmt.residue, stmt.position)]
elif isinstance(stmt, Influence):
if stmt.overall_polarity() == 1:
syntheses.append(stmt.obj.name)
elif stmt.overall_polarity() == -1:
degradations.append(stmt.obj.name)
elif isinstance(stmt, IncreaseAmount):
syntheses.append(stmt.obj.name)
elif isinstance(stmt, DecreaseAmount):
degradations.append(stmt.obj.name)
new_stmts = []
for agent_name, pos_sites in pos_mod_sites.items():
neg_sites = neg_mod_sites.get(agent_name, [])
no_neg_site = set(pos_sites).difference(set(neg_sites))
for residue, position in no_neg_site:
st = Dephosphorylation(Agent('phosphatase'),
Agent(agent_name),
residue, position)
new_stmts.append(st)
for agent_name in syntheses:
if agent_name not in degradations:
st = DecreaseAmount(None, Agent(agent_name))
new_stmts.append(st)
self.statements += new_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:map_sites; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:stmts; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 54; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:valid_statements; 12, list:[]; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:mapped_statements; 16, list:[]; 17, for_statement; 17, 18; 17, 19; 17, 20; 18, identifier:stmt; 19, identifier:stmts; 20, block; 20, 21; 20, 30; 20, 31; 20, 32; 20, 33; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:mapped_stmt; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:map_stmt_sites; 28, argument_list; 28, 29; 29, identifier:stmt; 30, comment; 31, comment; 32, comment; 33, if_statement; 33, 34; 33, 37; 33, 45; 34, comparison_operator:is; 34, 35; 34, 36; 35, identifier:mapped_stmt; 36, None; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:mapped_statements; 42, identifier:append; 43, argument_list; 43, 44; 44, identifier:mapped_stmt; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:valid_statements; 51, identifier:append; 52, argument_list; 52, 53; 53, identifier:stmt; 54, return_statement; 54, 55; 55, expression_list; 55, 56; 55, 57; 56, identifier:valid_statements; 57, identifier:mapped_statements | def map_sites(self, stmts):
"""Check a set of statements for invalid modification sites.
Statements are checked against Uniprot reference sequences to determine
if residues referred to by post-translational modifications exist at
the given positions.
If there is nothing amiss with a statement (modifications on any of the
agents, modifications made in the statement, etc.), then the statement
goes into the list of valid statements. If there is a problem with the
statement, the offending modifications are looked up in the site map
(:py:attr:`site_map`), and an instance of :py:class:`MappedStatement`
is added to the list of mapped statements.
Parameters
----------
stmts : list of :py:class:`indra.statement.Statement`
The statements to check for site errors.
Returns
-------
tuple
2-tuple containing (valid_statements, mapped_statements). The first
element of the tuple is a list of valid statements
(:py:class:`indra.statement.Statement`) that were not found to
contain any site errors. The second element of the tuple is a list
of mapped statements (:py:class:`MappedStatement`) with information
on the incorrect sites and corresponding statements with correctly
mapped sites.
"""
valid_statements = []
mapped_statements = []
for stmt in stmts:
mapped_stmt = self.map_stmt_sites(stmt)
# If we got a MappedStatement as a return value, we add that to the
# list of mapped statements, otherwise, the original Statement is
# not invalid so we add it to the other list directly.
if mapped_stmt is not None:
mapped_statements.append(mapped_stmt)
else:
valid_statements.append(stmt)
return valid_statements, mapped_statements |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_map_agent_sites; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:agent; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 25; 6, 32; 6, 36; 6, 37; 6, 117; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, if_statement; 11, 12; 11, 20; 12, boolean_operator:or; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:agent; 15, None; 16, not_operator; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:agent; 19, identifier:mods; 20, block; 20, 21; 21, return_statement; 21, 22; 22, expression_list; 22, 23; 22, 24; 23, list:[]; 24, identifier:agent; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:new_agent; 28, call; 28, 29; 28, 30; 29, identifier:deepcopy; 30, argument_list; 30, 31; 31, identifier:agent; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:mapped_sites; 35, list:[]; 36, comment; 37, for_statement; 37, 38; 37, 41; 37, 47; 38, pattern_list; 38, 39; 38, 40; 39, identifier:idx; 40, identifier:mod_condition; 41, call; 41, 42; 41, 43; 42, identifier:enumerate; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:agent; 46, identifier:mods; 47, block; 47, 48; 47, 59; 47, 60; 47, 61; 47, 72; 47, 73; 47, 74; 47, 75; 47, 108; 47, 109; 47, 110; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 49, 52; 50, identifier:mapped_site; 51, line_continuation:\; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:_map_agent_mod; 56, argument_list; 56, 57; 56, 58; 57, identifier:agent; 58, identifier:mod_condition; 59, comment; 60, comment; 61, if_statement; 61, 62; 61, 70; 62, boolean_operator:or; 62, 63; 62, 65; 63, not_operator; 63, 64; 64, identifier:mapped_site; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:mapped_site; 68, identifier:not_invalid; 69, argument_list; 70, block; 70, 71; 71, continue_statement; 72, comment; 73, comment; 74, comment; 75, if_statement; 75, 76; 75, 81; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:mapped_site; 79, identifier:has_mapping; 80, argument_list; 81, block; 81, 82; 81, 100; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:mc; 85, call; 85, 86; 85, 87; 86, identifier:ModCondition; 87, argument_list; 87, 88; 87, 91; 87, 94; 87, 97; 88, attribute; 88, 89; 88, 90; 89, identifier:mod_condition; 90, identifier:mod_type; 91, attribute; 91, 92; 91, 93; 92, identifier:mapped_site; 93, identifier:mapped_res; 94, attribute; 94, 95; 94, 96; 95, identifier:mapped_site; 96, identifier:mapped_pos; 97, attribute; 97, 98; 97, 99; 98, identifier:mod_condition; 99, identifier:is_modified; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 107; 102, subscript; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:new_agent; 105, identifier:mods; 106, identifier:idx; 107, identifier:mc; 108, comment; 109, comment; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:mapped_sites; 114, identifier:append; 115, argument_list; 115, 116; 116, identifier:mapped_site; 117, return_statement; 117, 118; 118, expression_list; 118, 119; 118, 120; 119, identifier:mapped_sites; 120, identifier:new_agent | def _map_agent_sites(self, agent):
"""Check an agent for invalid sites and update if necessary.
Parameters
----------
agent : :py:class:`indra.statements.Agent`
Agent to check for invalid modification sites.
Returns
-------
tuple
The first element is a list of MappedSite objects, the second
element is either the original Agent, if unchanged, or a copy
of it.
"""
# If there are no modifications on this agent, then we can return the
# copy of the agent
if agent is None or not agent.mods:
return [], agent
new_agent = deepcopy(agent)
mapped_sites = []
# Now iterate over all the modifications and map each one
for idx, mod_condition in enumerate(agent.mods):
mapped_site = \
self._map_agent_mod(agent, mod_condition)
# If we couldn't do the mapping or the mapped site isn't invalid
# then we don't need to change the existing ModCondition
if not mapped_site or mapped_site.not_invalid():
continue
# Otherwise, if there is a mapping, we replace the old ModCondition
# with the new one where only the residue and position are updated,
# the mod type and the is modified flag are kept.
if mapped_site.has_mapping():
mc = ModCondition(mod_condition.mod_type,
mapped_site.mapped_res,
mapped_site.mapped_pos,
mod_condition.is_modified)
new_agent.mods[idx] = mc
# Finally, whether or not we have a mapping, we keep track of mapped
# sites and make them available to the caller
mapped_sites.append(mapped_site)
return mapped_sites, new_agent |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_map_agent_mod; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:agent; 6, identifier:mod_condition; 7, block; 7, 8; 7, 10; 7, 11; 7, 18; 7, 35; 7, 36; 7, 51; 7, 52; 7, 84; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:up_id; 14, call; 14, 15; 14, 16; 15, identifier:_get_uniprot_id; 16, argument_list; 16, 17; 17, identifier:agent; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:up_id; 21, block; 21, 22; 21, 33; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:logger; 26, identifier:debug; 27, argument_list; 27, 28; 28, binary_operator:%; 28, 29; 28, 30; 29, string:"No uniprot ID for %s"; 30, attribute; 30, 31; 30, 32; 31, identifier:agent; 32, identifier:name; 33, return_statement; 33, 34; 34, None; 35, comment; 36, if_statement; 36, 37; 36, 48; 37, boolean_operator:or; 37, 38; 37, 43; 38, comparison_operator:is; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:mod_condition; 41, identifier:position; 42, None; 43, comparison_operator:is; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:mod_condition; 46, identifier:residue; 47, None; 48, block; 48, 49; 49, return_statement; 49, 50; 50, None; 51, comment; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 53, 56; 54, identifier:mapped_site; 55, line_continuation:\; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:map_to_human_ref; 60, argument_list; 60, 61; 60, 62; 60, 63; 60, 66; 60, 69; 60, 74; 60, 79; 61, identifier:up_id; 62, string:'uniprot'; 63, attribute; 63, 64; 63, 65; 64, identifier:mod_condition; 65, identifier:residue; 66, attribute; 66, 67; 66, 68; 67, identifier:mod_condition; 68, identifier:position; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:do_methionine_offset; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:do_methionine_offset; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:do_orthology_mapping; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:do_orthology_mapping; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:do_isoform_mapping; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:do_isoform_mapping; 84, return_statement; 84, 85; 85, identifier:mapped_site | def _map_agent_mod(self, agent, mod_condition):
"""Map a single modification condition on an agent.
Parameters
----------
agent : :py:class:`indra.statements.Agent`
Agent to check for invalid modification sites.
mod_condition : :py:class:`indra.statements.ModCondition`
Modification to check for validity and map.
Returns
-------
protmapper.MappedSite or None
A MappedSite object is returned if a UniProt ID was found for the
agent, and if both the position and residue for the modification
condition were available. Otherwise None is returned.
"""
# Get the UniProt ID of the agent, if not found, return
up_id = _get_uniprot_id(agent)
if not up_id:
logger.debug("No uniprot ID for %s" % agent.name)
return None
# If no site information for this residue, skip
if mod_condition.position is None or mod_condition.residue is None:
return None
# Otherwise, try to map it and return the mapped site
mapped_site = \
self.map_to_human_ref(up_id, 'uniprot',
mod_condition.residue,
mod_condition.position,
do_methionine_offset=self.do_methionine_offset,
do_orthology_mapping=self.do_orthology_mapping,
do_isoform_mapping=self.do_isoform_mapping)
return mapped_site |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_get_graph_reductions; 3, parameters; 3, 4; 4, identifier:graph; 5, block; 5, 6; 5, 8; 5, 64; 5, 68; 5, 84; 5, 96; 5, 97; 5, 98; 5, 99; 5, 138; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 13; 9, function_name:frontier; 10, parameters; 10, 11; 10, 12; 11, identifier:g; 12, identifier:nd; 13, block; 13, 14; 13, 16; 14, expression_statement; 14, 15; 15, comment; 16, if_statement; 16, 17; 16, 25; 16, 32; 17, comparison_operator:==; 17, 18; 17, 24; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:g; 21, identifier:out_degree; 22, argument_list; 22, 23; 23, identifier:nd; 24, integer:0; 25, block; 25, 26; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:set; 29, argument_list; 29, 30; 30, list:[nd]; 30, 31; 31, identifier:nd; 32, else_clause; 32, 33; 33, block; 33, 34; 33, 40; 33, 62; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:frontiers; 37, call; 37, 38; 37, 39; 38, identifier:set; 39, argument_list; 40, for_statement; 40, 41; 40, 42; 40, 48; 41, identifier:n; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:g; 45, identifier:successors; 46, argument_list; 46, 47; 47, identifier:nd; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:frontiers; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:frontiers; 55, identifier:union; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:frontier; 59, argument_list; 59, 60; 59, 61; 60, identifier:graph; 61, identifier:n; 62, return_statement; 62, 63; 63, identifier:frontiers; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:reductions; 67, dictionary; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:nodes_sort; 71, call; 71, 72; 71, 73; 72, identifier:list; 73, argument_list; 73, 74; 74, call; 74, 75; 74, 82; 75, attribute; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:networkx; 79, identifier:algorithms; 80, identifier:dag; 81, identifier:topological_sort; 82, argument_list; 82, 83; 83, identifier:graph; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:frontiers; 87, list_comprehension; 87, 88; 87, 93; 88, call; 88, 89; 88, 90; 89, identifier:frontier; 90, argument_list; 90, 91; 90, 92; 91, identifier:graph; 92, identifier:n; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:n; 95, identifier:nodes_sort; 96, comment; 97, comment; 98, comment; 99, for_statement; 99, 100; 99, 103; 99, 107; 100, pattern_list; 100, 101; 100, 102; 101, identifier:i; 102, identifier:n1; 103, call; 103, 104; 103, 105; 104, identifier:enumerate; 105, argument_list; 105, 106; 106, identifier:nodes_sort; 107, block; 107, 108; 108, for_statement; 108, 109; 108, 112; 108, 116; 109, pattern_list; 109, 110; 109, 111; 110, identifier:j; 111, identifier:n2; 112, call; 112, 113; 112, 114; 113, identifier:enumerate; 114, argument_list; 114, 115; 115, identifier:nodes_sort; 116, block; 116, 117; 116, 123; 117, if_statement; 117, 118; 117, 121; 118, comparison_operator:>; 118, 119; 118, 120; 119, identifier:i; 120, identifier:j; 121, block; 121, 122; 122, continue_statement; 123, if_statement; 123, 124; 123, 131; 124, comparison_operator:==; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:frontiers; 127, identifier:i; 128, subscript; 128, 129; 128, 130; 129, identifier:frontiers; 130, identifier:j; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:reductions; 136, identifier:n1; 137, identifier:n2; 138, return_statement; 138, 139; 139, identifier:reductions | def _get_graph_reductions(graph):
"""Return transitive reductions on a DAG.
This is used to reduce the set of activities of a BaseAgent to the most
specific one(s) possible. For instance, if a BaseAgent is know to have
'activity', 'catalytic' and 'kinase' activity, then this function will
return {'activity': 'kinase', 'catalytic': 'kinase', 'kinase': 'kinase'}
as the set of reductions.
"""
def frontier(g, nd):
"""Return the nodes after nd in the topological sort that are at the
lowest possible level of the topological sort."""
if g.out_degree(nd) == 0:
return set([nd])
else:
frontiers = set()
for n in g.successors(nd):
frontiers = frontiers.union(frontier(graph, n))
return frontiers
reductions = {}
nodes_sort = list(networkx.algorithms.dag.topological_sort(graph))
frontiers = [frontier(graph, n) for n in nodes_sort]
# This loop ensures that if a node n2 comes after node n1 in the topological
# sort, and their frontiers are identical then n1 can be reduced to n2.
# If their frontiers aren't identical, the reduction cannot be done.
for i, n1 in enumerate(nodes_sort):
for j, n2 in enumerate(nodes_sort):
if i > j:
continue
if frontiers[i] == frontiers[j]:
reductions[n1] = n2
return reductions |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:gather_implicit_activities; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 13; 9, identifier:stmt; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:statements; 13, block; 13, 14; 14, if_statement; 14, 15; 14, 34; 14, 72; 14, 116; 14, 160; 14, 204; 14, 273; 14, 342; 15, boolean_operator:or; 15, 16; 15, 28; 15, 29; 16, boolean_operator:or; 16, 17; 16, 22; 16, 23; 17, call; 17, 18; 17, 19; 18, identifier:isinstance; 19, argument_list; 19, 20; 19, 21; 20, identifier:stmt; 21, identifier:Phosphorylation; 22, line_continuation:\; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:stmt; 27, identifier:Transphosphorylation; 28, line_continuation:\; 29, call; 29, 30; 29, 31; 30, identifier:isinstance; 31, argument_list; 31, 32; 31, 33; 32, identifier:stmt; 33, identifier:Autophosphorylation; 34, block; 34, 35; 35, if_statement; 35, 36; 35, 41; 36, comparison_operator:is; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:stmt; 39, identifier:enz; 40, None; 41, block; 41, 42; 41, 53; 41, 60; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:enz_base; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_get_base; 49, argument_list; 49, 50; 50, attribute; 50, 51; 50, 52; 51, identifier:stmt; 52, identifier:enz; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:enz_base; 57, identifier:add_activity; 58, argument_list; 58, 59; 59, string:'kinase'; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:enz_base; 64, identifier:add_active_state; 65, argument_list; 65, 66; 65, 67; 66, string:'kinase'; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:stmt; 70, identifier:enz; 71, identifier:mods; 72, elif_clause; 72, 73; 72, 78; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:stmt; 77, identifier:Dephosphorylation; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:is; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:stmt; 83, identifier:enz; 84, None; 85, block; 85, 86; 85, 97; 85, 104; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:enz_base; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_get_base; 93, argument_list; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:stmt; 96, identifier:enz; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:enz_base; 101, identifier:add_activity; 102, argument_list; 102, 103; 103, string:'phosphatase'; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:enz_base; 108, identifier:add_active_state; 109, argument_list; 109, 110; 109, 111; 110, string:'phosphatase'; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:stmt; 114, identifier:enz; 115, identifier:mods; 116, elif_clause; 116, 117; 116, 122; 117, call; 117, 118; 117, 119; 118, identifier:isinstance; 119, argument_list; 119, 120; 119, 121; 120, identifier:stmt; 121, identifier:Modification; 122, block; 122, 123; 123, if_statement; 123, 124; 123, 129; 124, comparison_operator:is; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:stmt; 127, identifier:enz; 128, None; 129, block; 129, 130; 129, 141; 129, 148; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:enz_base; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_get_base; 137, argument_list; 137, 138; 138, attribute; 138, 139; 138, 140; 139, identifier:stmt; 140, identifier:enz; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:enz_base; 145, identifier:add_activity; 146, argument_list; 146, 147; 147, string:'catalytic'; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:enz_base; 152, identifier:add_active_state; 153, argument_list; 153, 154; 153, 155; 154, string:'catalytic'; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:stmt; 158, identifier:enz; 159, identifier:mods; 160, elif_clause; 160, 161; 160, 166; 161, call; 161, 162; 161, 163; 162, identifier:isinstance; 163, argument_list; 163, 164; 163, 165; 164, identifier:stmt; 165, identifier:SelfModification; 166, block; 166, 167; 167, if_statement; 167, 168; 167, 173; 168, comparison_operator:is; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:stmt; 171, identifier:enz; 172, None; 173, block; 173, 174; 173, 185; 173, 192; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:enz_base; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:self; 180, identifier:_get_base; 181, argument_list; 181, 182; 182, attribute; 182, 183; 182, 184; 183, identifier:stmt; 184, identifier:enz; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:enz_base; 189, identifier:add_activity; 190, argument_list; 190, 191; 191, string:'catalytic'; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:enz_base; 196, identifier:add_active_state; 197, argument_list; 197, 198; 197, 199; 198, string:'catalytic'; 199, attribute; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:stmt; 202, identifier:enz; 203, identifier:mods; 204, elif_clause; 204, 205; 204, 210; 205, call; 205, 206; 205, 207; 206, identifier:isinstance; 207, argument_list; 207, 208; 207, 209; 208, identifier:stmt; 209, identifier:Gef; 210, block; 210, 211; 211, if_statement; 211, 212; 211, 217; 212, comparison_operator:is; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:stmt; 215, identifier:gef; 216, None; 217, block; 217, 218; 217, 229; 217, 236; 217, 261; 218, expression_statement; 218, 219; 219, assignment; 219, 220; 219, 221; 220, identifier:gef_base; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:self; 224, identifier:_get_base; 225, argument_list; 225, 226; 226, attribute; 226, 227; 226, 228; 227, identifier:stmt; 228, identifier:gef; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:gef_base; 233, identifier:add_activity; 234, argument_list; 234, 235; 235, string:'gef'; 236, if_statement; 236, 237; 236, 244; 236, 255; 237, comparison_operator:is; 237, 238; 237, 243; 238, attribute; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:stmt; 241, identifier:gef; 242, identifier:activity; 243, None; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:act; 248, attribute; 248, 249; 248, 254; 249, attribute; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:stmt; 252, identifier:gef; 253, identifier:activity; 254, identifier:activity_type; 255, else_clause; 255, 256; 256, block; 256, 257; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:act; 260, string:'activity'; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:gef_base; 265, identifier:add_active_state; 266, argument_list; 266, 267; 266, 268; 267, identifier:act; 268, attribute; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:stmt; 271, identifier:gef; 272, identifier:mods; 273, elif_clause; 273, 274; 273, 279; 274, call; 274, 275; 274, 276; 275, identifier:isinstance; 276, argument_list; 276, 277; 276, 278; 277, identifier:stmt; 278, identifier:Gap; 279, block; 279, 280; 280, if_statement; 280, 281; 280, 286; 281, comparison_operator:is; 281, 282; 281, 285; 282, attribute; 282, 283; 282, 284; 283, identifier:stmt; 284, identifier:gap; 285, None; 286, block; 286, 287; 286, 298; 286, 305; 286, 330; 287, expression_statement; 287, 288; 288, assignment; 288, 289; 288, 290; 289, identifier:gap_base; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:self; 293, identifier:_get_base; 294, argument_list; 294, 295; 295, attribute; 295, 296; 295, 297; 296, identifier:stmt; 297, identifier:gap; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:gap_base; 302, identifier:add_activity; 303, argument_list; 303, 304; 304, string:'gap'; 305, if_statement; 305, 306; 305, 313; 305, 324; 306, comparison_operator:is; 306, 307; 306, 312; 307, attribute; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:stmt; 310, identifier:gap; 311, identifier:activity; 312, None; 313, block; 313, 314; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:act; 317, attribute; 317, 318; 317, 323; 318, attribute; 318, 319; 318, 322; 319, attribute; 319, 320; 319, 321; 320, identifier:stmt; 321, identifier:gap; 322, identifier:activity; 323, identifier:activity_type; 324, else_clause; 324, 325; 325, block; 325, 326; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:act; 329, string:'activity'; 330, expression_statement; 330, 331; 331, call; 331, 332; 331, 335; 332, attribute; 332, 333; 332, 334; 333, identifier:gap_base; 334, identifier:add_active_state; 335, argument_list; 335, 336; 335, 337; 336, string:'act'; 337, attribute; 337, 338; 337, 341; 338, attribute; 338, 339; 338, 340; 339, identifier:stmt; 340, identifier:gap; 341, identifier:mods; 342, elif_clause; 342, 343; 342, 348; 343, call; 343, 344; 343, 345; 344, identifier:isinstance; 345, argument_list; 345, 346; 345, 347; 346, identifier:stmt; 347, identifier:RegulateActivity; 348, block; 348, 349; 349, if_statement; 349, 350; 349, 355; 350, comparison_operator:is; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:stmt; 353, identifier:subj; 354, None; 355, block; 355, 356; 355, 367; 356, expression_statement; 356, 357; 357, assignment; 357, 358; 357, 359; 358, identifier:subj_base; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:self; 362, identifier:_get_base; 363, argument_list; 363, 364; 364, attribute; 364, 365; 364, 366; 365, identifier:stmt; 366, identifier:subj; 367, expression_statement; 367, 368; 368, call; 368, 369; 368, 372; 369, attribute; 369, 370; 369, 371; 370, identifier:subj_base; 371, identifier:add_activity; 372, argument_list; 372, 373; 373, attribute; 373, 374; 373, 375; 374, identifier:stmt; 375, identifier:j | def gather_implicit_activities(self):
"""Aggregate all implicit activities and active forms of Agents.
Iterate over self.statements and collect the implied activities
and active forms of Agents that appear in the Statements.
Note that using this function to collect implied Agent activities can
be risky. Assume, for instance, that a Statement from a reading
system states that EGF bound to EGFR phosphorylates ERK. This would
be interpreted as implicit evidence for the EGFR-bound form of EGF
to have 'kinase' activity, which is clearly incorrect.
In contrast the alternative pair of this function:
gather_explicit_activities collects only explicitly stated activities.
"""
for stmt in self.statements:
if isinstance(stmt, Phosphorylation) or \
isinstance(stmt, Transphosphorylation) or \
isinstance(stmt, Autophosphorylation):
if stmt.enz is not None:
enz_base = self._get_base(stmt.enz)
enz_base.add_activity('kinase')
enz_base.add_active_state('kinase', stmt.enz.mods)
elif isinstance(stmt, Dephosphorylation):
if stmt.enz is not None:
enz_base = self._get_base(stmt.enz)
enz_base.add_activity('phosphatase')
enz_base.add_active_state('phosphatase', stmt.enz.mods)
elif isinstance(stmt, Modification):
if stmt.enz is not None:
enz_base = self._get_base(stmt.enz)
enz_base.add_activity('catalytic')
enz_base.add_active_state('catalytic', stmt.enz.mods)
elif isinstance(stmt, SelfModification):
if stmt.enz is not None:
enz_base = self._get_base(stmt.enz)
enz_base.add_activity('catalytic')
enz_base.add_active_state('catalytic', stmt.enz.mods)
elif isinstance(stmt, Gef):
if stmt.gef is not None:
gef_base = self._get_base(stmt.gef)
gef_base.add_activity('gef')
if stmt.gef.activity is not None:
act = stmt.gef.activity.activity_type
else:
act = 'activity'
gef_base.add_active_state(act, stmt.gef.mods)
elif isinstance(stmt, Gap):
if stmt.gap is not None:
gap_base = self._get_base(stmt.gap)
gap_base.add_activity('gap')
if stmt.gap.activity is not None:
act = stmt.gap.activity.activity_type
else:
act = 'activity'
gap_base.add_active_state('act', stmt.gap.mods)
elif isinstance(stmt, RegulateActivity):
if stmt.subj is not None:
subj_base = self._get_base(stmt.subj)
subj_base.add_activity(stmt.j) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:reduce_activities; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 13; 9, identifier:stmt; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:statements; 13, block; 13, 14; 13, 22; 13, 72; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:agents; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:stmt; 20, identifier:agent_list; 21, argument_list; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:agent; 24, identifier:agents; 25, block; 25, 26; 26, if_statement; 26, 27; 26, 36; 27, boolean_operator:and; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:agent; 30, None; 31, comparison_operator:is; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:agent; 34, identifier:activity; 35, None; 36, block; 36, 37; 36, 46; 36, 59; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:agent_base; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_get_base; 44, argument_list; 44, 45; 45, identifier:agent; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:act_red; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:agent_base; 52, identifier:get_activity_reduction; 53, argument_list; 53, 54; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:agent; 57, identifier:activity; 58, identifier:activity_type; 59, if_statement; 59, 60; 59, 63; 60, comparison_operator:is; 60, 61; 60, 62; 61, identifier:act_red; 62, None; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:agent; 69, identifier:activity; 70, identifier:activity_type; 71, identifier:act_red; 72, if_statement; 72, 73; 72, 78; 72, 120; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 77; 76, identifier:stmt; 77, identifier:RegulateActivity; 78, block; 78, 79; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:is; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:stmt; 83, identifier:obj; 84, None; 85, block; 85, 86; 85, 97; 85, 109; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:obj_base; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:_get_base; 93, argument_list; 93, 94; 94, attribute; 94, 95; 94, 96; 95, identifier:stmt; 96, identifier:obj; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 98, 101; 99, identifier:act_red; 100, line_continuation:\; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:obj_base; 104, identifier:get_activity_reduction; 105, argument_list; 105, 106; 106, attribute; 106, 107; 106, 108; 107, identifier:stmt; 108, identifier:obj_activity; 109, if_statement; 109, 110; 109, 113; 110, comparison_operator:is; 110, 111; 110, 112; 111, identifier:act_red; 112, None; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:stmt; 118, identifier:obj_activity; 119, identifier:act_red; 120, elif_clause; 120, 121; 120, 126; 121, call; 121, 122; 121, 123; 122, identifier:isinstance; 123, argument_list; 123, 124; 123, 125; 124, identifier:stmt; 125, identifier:ActiveForm; 126, block; 126, 127; 126, 138; 126, 149; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:agent_base; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_get_base; 134, argument_list; 134, 135; 135, attribute; 135, 136; 135, 137; 136, identifier:stmt; 137, identifier:agent; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 141; 140, identifier:act_red; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:agent_base; 144, identifier:get_activity_reduction; 145, argument_list; 145, 146; 146, attribute; 146, 147; 146, 148; 147, identifier:stmt; 148, identifier:activity; 149, if_statement; 149, 150; 149, 153; 150, comparison_operator:is; 150, 151; 150, 152; 151, identifier:act_red; 152, None; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:stmt; 158, identifier:activity; 159, identifier:act_red | def reduce_activities(self):
"""Rewrite the activity types referenced in Statements for consistency.
Activity types are reduced to the most specific form whenever possible.
For instance, if 'kinase' is the only specific activity type known
for the BaseAgent of BRAF, its generic 'activity' forms are rewritten
to 'kinase'.
"""
for stmt in self.statements:
agents = stmt.agent_list()
for agent in agents:
if agent is not None and agent.activity is not None:
agent_base = self._get_base(agent)
act_red = agent_base.get_activity_reduction(
agent.activity.activity_type)
if act_red is not None:
agent.activity.activity_type = act_red
if isinstance(stmt, RegulateActivity):
if stmt.obj is not None:
obj_base = self._get_base(stmt.obj)
act_red = \
obj_base.get_activity_reduction(stmt.obj_activity)
if act_red is not None:
stmt.obj_activity = act_red
elif isinstance(stmt, ActiveForm):
agent_base = self._get_base(stmt.agent)
act_red = agent_base.get_activity_reduction(stmt.activity)
if act_red is not None:
stmt.activity = act_red |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:infer_activations; 3, parameters; 3, 4; 4, identifier:stmts; 5, block; 5, 6; 5, 8; 5, 12; 5, 20; 5, 28; 5, 190; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:linked_stmts; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:af_stmts; 15, call; 15, 16; 15, 17; 16, identifier:_get_statements_by_type; 17, argument_list; 17, 18; 17, 19; 18, identifier:stmts; 19, identifier:ActiveForm; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:mod_stmts; 23, call; 23, 24; 23, 25; 24, identifier:_get_statements_by_type; 25, argument_list; 25, 26; 25, 27; 26, identifier:stmts; 27, identifier:Modification; 28, for_statement; 28, 29; 28, 32; 28, 41; 28, 42; 28, 43; 29, pattern_list; 29, 30; 29, 31; 30, identifier:af_stmt; 31, identifier:mod_stmt; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:itertools; 35, identifier:product; 36, argument_list; 36, 37; 37, list_splat; 37, 38; 38, tuple; 38, 39; 38, 40; 39, identifier:af_stmts; 40, identifier:mod_stmts; 41, comment; 42, comment; 43, block; 43, 44; 43, 66; 43, 67; 43, 76; 43, 80; 43, 121; 43, 126; 43, 127; 43, 133; 43, 134; 43, 177; 44, if_statement; 44, 45; 44, 64; 45, boolean_operator:or; 45, 46; 45, 51; 45, 52; 46, comparison_operator:is; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:mod_stmt; 49, identifier:enz; 50, None; 51, line_continuation:\; 52, parenthesized_expression; 52, 53; 53, not_operator; 53, 54; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:af_stmt; 58, identifier:agent; 59, identifier:entity_matches; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:mod_stmt; 63, identifier:sub; 64, block; 64, 65; 65, continue_statement; 66, comment; 67, if_statement; 67, 68; 67, 74; 68, not_operator; 68, 69; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:af_stmt; 72, identifier:agent; 73, identifier:mods; 74, block; 74, 75; 75, continue_statement; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:found; 79, False; 80, for_statement; 80, 81; 80, 82; 80, 87; 81, identifier:mc; 82, attribute; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:af_stmt; 85, identifier:agent; 86, identifier:mods; 87, block; 87, 88; 88, if_statement; 88, 89; 88, 116; 89, boolean_operator:and; 89, 90; 89, 108; 89, 109; 90, boolean_operator:and; 90, 91; 90, 100; 90, 101; 91, comparison_operator:==; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:mc; 94, identifier:mod_type; 95, subscript; 95, 96; 95, 97; 96, identifier:modclass_to_modtype; 97, attribute; 97, 98; 97, 99; 98, identifier:mod_stmt; 99, identifier:__class__; 100, line_continuation:\; 101, comparison_operator:==; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:mc; 104, identifier:residue; 105, attribute; 105, 106; 105, 107; 106, identifier:mod_stmt; 107, identifier:residue; 108, line_continuation:\; 109, comparison_operator:==; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:mc; 112, identifier:position; 113, attribute; 113, 114; 113, 115; 114, identifier:mod_stmt; 115, identifier:position; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:found; 120, True; 121, if_statement; 121, 122; 121, 124; 122, not_operator; 122, 123; 123, identifier:found; 124, block; 124, 125; 125, continue_statement; 126, comment; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:ev; 130, attribute; 130, 131; 130, 132; 131, identifier:mod_stmt; 132, identifier:evidence; 133, comment; 134, if_statement; 134, 135; 134, 138; 134, 157; 135, attribute; 135, 136; 135, 137; 136, identifier:af_stmt; 137, identifier:is_active; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:st; 142, call; 142, 143; 142, 144; 143, identifier:Activation; 144, argument_list; 144, 145; 144, 148; 144, 151; 144, 154; 145, attribute; 145, 146; 145, 147; 146, identifier:mod_stmt; 147, identifier:enz; 148, attribute; 148, 149; 148, 150; 149, identifier:mod_stmt; 150, identifier:sub; 151, attribute; 151, 152; 151, 153; 152, identifier:af_stmt; 153, identifier:activity; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:evidence; 156, identifier:ev; 157, else_clause; 157, 158; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:st; 162, call; 162, 163; 162, 164; 163, identifier:Inhibition; 164, argument_list; 164, 165; 164, 168; 164, 171; 164, 174; 165, attribute; 165, 166; 165, 167; 166, identifier:mod_stmt; 167, identifier:enz; 168, attribute; 168, 169; 168, 170; 169, identifier:mod_stmt; 170, identifier:sub; 171, attribute; 171, 172; 171, 173; 172, identifier:af_stmt; 173, identifier:activity; 174, keyword_argument; 174, 175; 174, 176; 175, identifier:evidence; 176, identifier:ev; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:linked_stmts; 181, identifier:append; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:LinkedStatement; 185, argument_list; 185, 186; 185, 189; 186, list:[af_stmt, mod_stmt]; 186, 187; 186, 188; 187, identifier:af_stmt; 188, identifier:mod_stmt; 189, identifier:st; 190, return_statement; 190, 191; 191, identifier:linked_stmts | def infer_activations(stmts):
"""Return inferred RegulateActivity from Modification + ActiveForm.
This function looks for combinations of Modification and ActiveForm
Statements and infers Activation/Inhibition Statements from them.
For example, if we know that A phosphorylates B, and the
phosphorylated form of B is active, then we can infer that
A activates B. This can also be viewed as having "explained" a given
Activation/Inhibition Statement with a combination of more mechanistic
Modification + ActiveForm Statements.
Parameters
----------
stmts : list[indra.statements.Statement]
A list of Statements to infer RegulateActivity from.
Returns
-------
linked_stmts : list[indra.mechlinker.LinkedStatement]
A list of LinkedStatements representing the inferred Statements.
"""
linked_stmts = []
af_stmts = _get_statements_by_type(stmts, ActiveForm)
mod_stmts = _get_statements_by_type(stmts, Modification)
for af_stmt, mod_stmt in itertools.product(*(af_stmts, mod_stmts)):
# There has to be an enzyme and the substrate and the
# agent of the active form have to match
if mod_stmt.enz is None or \
(not af_stmt.agent.entity_matches(mod_stmt.sub)):
continue
# We now check the modifications to make sure they are consistent
if not af_stmt.agent.mods:
continue
found = False
for mc in af_stmt.agent.mods:
if mc.mod_type == modclass_to_modtype[mod_stmt.__class__] and \
mc.residue == mod_stmt.residue and \
mc.position == mod_stmt.position:
found = True
if not found:
continue
# Collect evidence
ev = mod_stmt.evidence
# Finally, check the polarity of the ActiveForm
if af_stmt.is_active:
st = Activation(mod_stmt.enz, mod_stmt.sub, af_stmt.activity,
evidence=ev)
else:
st = Inhibition(mod_stmt.enz, mod_stmt.sub, af_stmt.activity,
evidence=ev)
linked_stmts.append(LinkedStatement([af_stmt, mod_stmt], st))
return linked_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:infer_active_forms; 3, parameters; 3, 4; 4, identifier:stmts; 5, block; 5, 6; 5, 8; 5, 12; 5, 251; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:linked_stmts; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 19; 12, 20; 13, identifier:act_stmt; 14, call; 14, 15; 14, 16; 15, identifier:_get_statements_by_type; 16, argument_list; 16, 17; 16, 18; 17, identifier:stmts; 18, identifier:RegulateActivity; 19, comment; 20, block; 20, 21; 20, 51; 20, 55; 20, 61; 20, 116; 20, 121; 20, 125; 20, 187; 20, 198; 20, 231; 20, 242; 21, if_statement; 21, 22; 21, 49; 22, not_operator; 22, 23; 23, parenthesized_expression; 23, 24; 24, boolean_operator:and; 24, 25; 24, 42; 25, boolean_operator:and; 25, 26; 25, 33; 26, comparison_operator:is; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:act_stmt; 30, identifier:subj; 31, identifier:activity; 32, None; 33, comparison_operator:==; 33, 34; 33, 41; 34, attribute; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:act_stmt; 38, identifier:subj; 39, identifier:activity; 40, identifier:activity_type; 41, string:'kinase'; 42, attribute; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:act_stmt; 46, identifier:subj; 47, identifier:activity; 48, identifier:is_active; 49, block; 49, 50; 50, continue_statement; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:matching; 54, list:[]; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:ev; 58, attribute; 58, 59; 58, 60; 59, identifier:act_stmt; 60, identifier:evidence; 61, for_statement; 61, 62; 61, 63; 61, 68; 62, identifier:mod_stmt; 63, call; 63, 64; 63, 65; 64, identifier:_get_statements_by_type; 65, argument_list; 65, 66; 65, 67; 66, identifier:stmts; 67, identifier:Modification; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 75; 70, comparison_operator:is; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:mod_stmt; 73, identifier:enz; 74, None; 75, block; 75, 76; 76, if_statement; 76, 77; 76, 99; 77, boolean_operator:and; 77, 78; 77, 88; 77, 89; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:mod_stmt; 82, identifier:enz; 83, identifier:entity_matches; 84, argument_list; 84, 85; 85, attribute; 85, 86; 85, 87; 86, identifier:act_stmt; 87, identifier:subj; 88, line_continuation:\; 89, call; 89, 90; 89, 95; 90, attribute; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:mod_stmt; 93, identifier:sub; 94, identifier:entity_matches; 95, argument_list; 95, 96; 96, attribute; 96, 97; 96, 98; 97, identifier:act_stmt; 98, identifier:obj; 99, block; 99, 100; 99, 107; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:matching; 104, identifier:append; 105, argument_list; 105, 106; 106, identifier:mod_stmt; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:ev; 111, identifier:extend; 112, argument_list; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:mod_stmt; 115, identifier:evidence; 116, if_statement; 116, 117; 116, 119; 117, not_operator; 117, 118; 118, identifier:matching; 119, block; 119, 120; 120, continue_statement; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:mods; 124, list:[]; 125, for_statement; 125, 126; 125, 127; 125, 128; 126, identifier:mod_stmt; 127, identifier:matching; 128, block; 128, 129; 128, 141; 128, 166; 128, 180; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:mod_type_name; 132, call; 132, 133; 132, 140; 133, attribute; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:mod_stmt; 137, identifier:__class__; 138, identifier:__name__; 139, identifier:lower; 140, argument_list; 141, if_statement; 141, 142; 141, 147; 141, 152; 142, call; 142, 143; 142, 144; 143, identifier:isinstance; 144, argument_list; 144, 145; 144, 146; 145, identifier:mod_stmt; 146, identifier:AddModification; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:is_modified; 151, True; 152, else_clause; 152, 153; 153, block; 153, 154; 153, 158; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:is_modified; 157, False; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:mod_type_name; 161, subscript; 161, 162; 161, 163; 162, identifier:mod_type_name; 163, slice; 163, 164; 163, 165; 164, integer:2; 165, colon; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:mc; 169, call; 169, 170; 169, 171; 170, identifier:ModCondition; 171, argument_list; 171, 172; 171, 173; 171, 176; 171, 179; 172, identifier:mod_type_name; 173, attribute; 173, 174; 173, 175; 174, identifier:mod_stmt; 175, identifier:residue; 176, attribute; 176, 177; 176, 178; 177, identifier:mod_stmt; 178, identifier:position; 179, identifier:is_modified; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:mods; 184, identifier:append; 185, argument_list; 185, 186; 186, identifier:mc; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:source_stmts; 190, binary_operator:+; 190, 191; 190, 193; 191, list:[act_stmt]; 191, 192; 192, identifier:act_stmt; 193, list_comprehension; 193, 194; 193, 195; 194, identifier:m; 195, for_in_clause; 195, 196; 195, 197; 196, identifier:m; 197, identifier:matching; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:st; 201, call; 201, 202; 201, 203; 202, identifier:ActiveForm; 203, argument_list; 203, 204; 203, 222; 203, 225; 203, 228; 204, call; 204, 205; 204, 206; 205, identifier:Agent; 206, argument_list; 206, 207; 206, 212; 206, 215; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:act_stmt; 210, identifier:obj; 211, identifier:name; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:mods; 214, identifier:mods; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:db_refs; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:act_stmt; 220, identifier:obj; 221, identifier:db_refs; 222, attribute; 222, 223; 222, 224; 223, identifier:act_stmt; 224, identifier:obj_activity; 225, attribute; 225, 226; 225, 227; 226, identifier:act_stmt; 227, identifier:is_activation; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:evidence; 230, identifier:ev; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:linked_stmts; 235, identifier:append; 236, argument_list; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:LinkedStatement; 239, argument_list; 239, 240; 239, 241; 240, identifier:source_stmts; 241, identifier:st; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 247; 244, attribute; 244, 245; 244, 246; 245, identifier:logger; 246, identifier:info; 247, argument_list; 247, 248; 248, binary_operator:%; 248, 249; 248, 250; 249, string:'inferred: %s'; 250, identifier:st; 251, return_statement; 251, 252; 252, identifier:linked_stmts | def infer_active_forms(stmts):
"""Return inferred ActiveForm from RegulateActivity + Modification.
This function looks for combinations of Activation/Inhibition
Statements and Modification Statements, and infers an ActiveForm
from them. For example, if we know that A activates B and
A phosphorylates B, then we can infer that the phosphorylated form
of B is active.
Parameters
----------
stmts : list[indra.statements.Statement]
A list of Statements to infer ActiveForms from.
Returns
-------
linked_stmts : list[indra.mechlinker.LinkedStatement]
A list of LinkedStatements representing the inferred Statements.
"""
linked_stmts = []
for act_stmt in _get_statements_by_type(stmts, RegulateActivity):
# TODO: revise the conditions here
if not (act_stmt.subj.activity is not None and
act_stmt.subj.activity.activity_type == 'kinase' and
act_stmt.subj.activity.is_active):
continue
matching = []
ev = act_stmt.evidence
for mod_stmt in _get_statements_by_type(stmts, Modification):
if mod_stmt.enz is not None:
if mod_stmt.enz.entity_matches(act_stmt.subj) and \
mod_stmt.sub.entity_matches(act_stmt.obj):
matching.append(mod_stmt)
ev.extend(mod_stmt.evidence)
if not matching:
continue
mods = []
for mod_stmt in matching:
mod_type_name = mod_stmt.__class__.__name__.lower()
if isinstance(mod_stmt, AddModification):
is_modified = True
else:
is_modified = False
mod_type_name = mod_type_name[2:]
mc = ModCondition(mod_type_name, mod_stmt.residue,
mod_stmt.position, is_modified)
mods.append(mc)
source_stmts = [act_stmt] + [m for m in matching]
st = ActiveForm(Agent(act_stmt.obj.name, mods=mods,
db_refs=act_stmt.obj.db_refs),
act_stmt.obj_activity, act_stmt.is_activation,
evidence=ev)
linked_stmts.append(LinkedStatement(source_stmts, st))
logger.info('inferred: %s' % st)
return linked_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:infer_modifications; 3, parameters; 3, 4; 4, identifier:stmts; 5, block; 5, 6; 5, 8; 5, 12; 5, 191; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:linked_stmts; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 19; 13, identifier:act_stmt; 14, call; 14, 15; 14, 16; 15, identifier:_get_statements_by_type; 16, argument_list; 16, 17; 16, 18; 17, identifier:stmts; 18, identifier:RegulateActivity; 19, block; 19, 20; 20, for_statement; 20, 21; 20, 22; 20, 27; 21, identifier:af_stmt; 22, call; 22, 23; 22, 24; 23, identifier:_get_statements_by_type; 24, argument_list; 24, 25; 24, 26; 25, identifier:stmts; 26, identifier:ActiveForm; 27, block; 27, 28; 27, 42; 27, 50; 27, 51; 27, 73; 27, 82; 28, if_statement; 28, 29; 28, 40; 29, not_operator; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:af_stmt; 34, identifier:agent; 35, identifier:entity_matches; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:act_stmt; 39, identifier:obj; 40, block; 40, 41; 41, continue_statement; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:mods; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:af_stmt; 48, identifier:agent; 49, identifier:mods; 50, comment; 51, if_statement; 51, 52; 51, 71; 52, boolean_operator:or; 52, 53; 52, 65; 52, 66; 53, boolean_operator:or; 53, 54; 53, 59; 53, 60; 54, attribute; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:af_stmt; 57, identifier:agent; 58, identifier:mutations; 59, line_continuation:\; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:af_stmt; 63, identifier:agent; 64, identifier:bound_conditions; 65, line_continuation:\; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:af_stmt; 69, identifier:agent; 70, identifier:location; 71, block; 71, 72; 72, continue_statement; 73, if_statement; 73, 74; 73, 80; 74, not_operator; 74, 75; 75, attribute; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:af_stmt; 78, identifier:agent; 79, identifier:mods; 80, block; 80, 81; 81, continue_statement; 82, for_statement; 82, 83; 82, 84; 82, 89; 83, identifier:mod; 84, attribute; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:af_stmt; 87, identifier:agent; 88, identifier:mods; 89, block; 89, 90; 89, 100; 89, 112; 89, 133; 89, 139; 89, 144; 89, 165; 89, 175; 89, 182; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:evs; 93, binary_operator:+; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:act_stmt; 96, identifier:evidence; 97, attribute; 97, 98; 97, 99; 98, identifier:af_stmt; 99, identifier:evidence; 100, for_statement; 100, 101; 100, 102; 100, 103; 101, identifier:ev; 102, identifier:evs; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 111; 106, subscript; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:ev; 109, identifier:epistemics; 110, string:'direct'; 111, False; 112, if_statement; 112, 113; 112, 116; 112, 123; 113, attribute; 113, 114; 113, 115; 114, identifier:mod; 115, identifier:is_modified; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:mod_type_name; 120, attribute; 120, 121; 120, 122; 121, identifier:mod; 122, identifier:mod_type; 123, else_clause; 123, 124; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:mod_type_name; 128, subscript; 128, 129; 128, 130; 129, identifier:modtype_to_inverse; 130, attribute; 130, 131; 130, 132; 131, identifier:mod; 132, identifier:mod_type; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:mod_class; 136, subscript; 136, 137; 136, 138; 137, identifier:modtype_to_modclass; 138, identifier:mod_type_name; 139, if_statement; 139, 140; 139, 142; 140, not_operator; 140, 141; 141, identifier:mod_class; 142, block; 142, 143; 143, continue_statement; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:st; 147, call; 147, 148; 147, 149; 148, identifier:mod_class; 149, argument_list; 149, 150; 149, 153; 149, 156; 149, 159; 149, 162; 150, attribute; 150, 151; 150, 152; 151, identifier:act_stmt; 152, identifier:subj; 153, attribute; 153, 154; 153, 155; 154, identifier:act_stmt; 155, identifier:obj; 156, attribute; 156, 157; 156, 158; 157, identifier:mod; 158, identifier:residue; 159, attribute; 159, 160; 159, 161; 160, identifier:mod; 161, identifier:position; 162, keyword_argument; 162, 163; 162, 164; 163, identifier:evidence; 164, identifier:evs; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:ls; 168, call; 168, 169; 168, 170; 169, identifier:LinkedStatement; 170, argument_list; 170, 171; 170, 174; 171, list:[act_stmt, af_stmt]; 171, 172; 171, 173; 172, identifier:act_stmt; 173, identifier:af_stmt; 174, identifier:st; 175, expression_statement; 175, 176; 176, call; 176, 177; 176, 180; 177, attribute; 177, 178; 177, 179; 178, identifier:linked_stmts; 179, identifier:append; 180, argument_list; 180, 181; 181, identifier:ls; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:logger; 186, identifier:info; 187, argument_list; 187, 188; 188, binary_operator:%; 188, 189; 188, 190; 189, string:'inferred: %s'; 190, identifier:st; 191, return_statement; 191, 192; 192, identifier:linked_stmts | def infer_modifications(stmts):
"""Return inferred Modification from RegulateActivity + ActiveForm.
This function looks for combinations of Activation/Inhibition Statements
and ActiveForm Statements that imply a Modification Statement.
For example, if we know that A activates B, and phosphorylated B is
active, then we can infer that A leads to the phosphorylation of B.
An additional requirement when making this assumption is that the
activity of B should only be dependent on the modified state and not
other context - otherwise the inferred Modification is not necessarily
warranted.
Parameters
----------
stmts : list[indra.statements.Statement]
A list of Statements to infer Modifications from.
Returns
-------
linked_stmts : list[indra.mechlinker.LinkedStatement]
A list of LinkedStatements representing the inferred Statements.
"""
linked_stmts = []
for act_stmt in _get_statements_by_type(stmts, RegulateActivity):
for af_stmt in _get_statements_by_type(stmts, ActiveForm):
if not af_stmt.agent.entity_matches(act_stmt.obj):
continue
mods = af_stmt.agent.mods
# Make sure the ActiveForm only involves modified sites
if af_stmt.agent.mutations or \
af_stmt.agent.bound_conditions or \
af_stmt.agent.location:
continue
if not af_stmt.agent.mods:
continue
for mod in af_stmt.agent.mods:
evs = act_stmt.evidence + af_stmt.evidence
for ev in evs:
ev.epistemics['direct'] = False
if mod.is_modified:
mod_type_name = mod.mod_type
else:
mod_type_name = modtype_to_inverse[mod.mod_type]
mod_class = modtype_to_modclass[mod_type_name]
if not mod_class:
continue
st = mod_class(act_stmt.subj,
act_stmt.obj,
mod.residue, mod.position,
evidence=evs)
ls = LinkedStatement([act_stmt, af_stmt], st)
linked_stmts.append(ls)
logger.info('inferred: %s' % st)
return linked_stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:score_evidence_list; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:evidences; 6, block; 6, 7; 6, 9; 6, 129; 6, 147; 6, 164; 6, 171; 6, 178; 6, 179; 6, 180; 6, 181; 6, 182; 6, 183; 6, 184; 6, 185; 6, 186; 6, 187; 6, 196; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:_score; 11, parameters; 11, 12; 12, identifier:evidences; 13, block; 13, 14; 13, 20; 13, 21; 13, 31; 13, 40; 13, 41; 13, 57; 13, 58; 13, 68; 13, 94; 13, 95; 13, 96; 13, 100; 13, 120; 13, 121; 13, 127; 14, if_statement; 14, 15; 14, 17; 15, not_operator; 15, 16; 16, identifier:evidences; 17, block; 17, 18; 18, return_statement; 18, 19; 19, integer:0; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:sources; 24, list_comprehension; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:ev; 27, identifier:source_api; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:ev; 30, identifier:evidences; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:uniq_sources; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:numpy; 37, identifier:unique; 38, argument_list; 38, 39; 39, identifier:sources; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:syst_factors; 44, dictionary_comprehension; 44, 45; 44, 54; 45, pair; 45, 46; 45, 47; 46, identifier:s; 47, subscript; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:prior_probs; 52, string:'syst'; 53, identifier:s; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:s; 56, identifier:uniq_sources; 57, comment; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:rand_factors; 61, dictionary_comprehension; 61, 62; 61, 65; 62, pair; 62, 63; 62, 64; 63, identifier:k; 64, list:[]; 65, for_in_clause; 65, 66; 65, 67; 66, identifier:k; 67, identifier:uniq_sources; 68, for_statement; 68, 69; 68, 70; 68, 71; 69, identifier:ev; 70, identifier:evidences; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 81; 74, attribute; 74, 75; 74, 80; 75, subscript; 75, 76; 75, 77; 76, identifier:rand_factors; 77, attribute; 77, 78; 77, 79; 78, identifier:ev; 79, identifier:source_api; 80, identifier:append; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:evidence_random_noise_prior; 84, argument_list; 84, 85; 84, 86; 84, 91; 85, identifier:ev; 86, subscript; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:prior_probs; 90, string:'rand'; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:subtype_probs; 94, comment; 95, comment; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:neg_prob_prior; 99, integer:1; 100, for_statement; 100, 101; 100, 102; 100, 103; 101, identifier:s; 102, identifier:uniq_sources; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, augmented_assignment:*=; 105, 106; 105, 107; 106, identifier:neg_prob_prior; 107, parenthesized_expression; 107, 108; 108, binary_operator:+; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:syst_factors; 111, identifier:s; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:numpy; 115, identifier:prod; 116, argument_list; 116, 117; 117, subscript; 117, 118; 117, 119; 118, identifier:rand_factors; 119, identifier:s; 120, comment; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:prob_prior; 124, binary_operator:-; 124, 125; 124, 126; 125, integer:1; 126, identifier:neg_prob_prior; 127, return_statement; 127, 128; 128, identifier:prob_prior; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:pos_evidence; 132, list_comprehension; 132, 133; 132, 134; 132, 137; 133, identifier:ev; 134, for_in_clause; 134, 135; 134, 136; 135, identifier:ev; 136, identifier:evidences; 137, if_clause; 137, 138; 138, not_operator; 138, 139; 139, call; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:ev; 143, identifier:epistemics; 144, identifier:get; 145, argument_list; 145, 146; 146, string:'negated'; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:neg_evidence; 150, list_comprehension; 150, 151; 150, 152; 150, 155; 151, identifier:ev; 152, for_in_clause; 152, 153; 152, 154; 153, identifier:ev; 154, identifier:evidences; 155, if_clause; 155, 156; 156, call; 156, 157; 156, 162; 157, attribute; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:ev; 160, identifier:epistemics; 161, identifier:get; 162, argument_list; 162, 163; 163, string:'negated'; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:pp; 167, call; 167, 168; 167, 169; 168, identifier:_score; 169, argument_list; 169, 170; 170, identifier:pos_evidence; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:np; 174, call; 174, 175; 174, 176; 175, identifier:_score; 176, argument_list; 176, 177; 177, identifier:neg_evidence; 178, comment; 179, comment; 180, comment; 181, comment; 182, comment; 183, comment; 184, comment; 185, comment; 186, comment; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:score; 190, binary_operator:*; 190, 191; 190, 192; 191, identifier:pp; 192, parenthesized_expression; 192, 193; 193, binary_operator:-; 193, 194; 193, 195; 194, integer:1; 195, identifier:np; 196, return_statement; 196, 197; 197, identifier:score | def score_evidence_list(self, evidences):
"""Return belief score given a list of supporting evidences."""
def _score(evidences):
if not evidences:
return 0
# Collect all unique sources
sources = [ev.source_api for ev in evidences]
uniq_sources = numpy.unique(sources)
# Calculate the systematic error factors given unique sources
syst_factors = {s: self.prior_probs['syst'][s]
for s in uniq_sources}
# Calculate the radom error factors for each source
rand_factors = {k: [] for k in uniq_sources}
for ev in evidences:
rand_factors[ev.source_api].append(
evidence_random_noise_prior(
ev,
self.prior_probs['rand'],
self.subtype_probs))
# The probability of incorrectness is the product of the
# source-specific probabilities
neg_prob_prior = 1
for s in uniq_sources:
neg_prob_prior *= (syst_factors[s] +
numpy.prod(rand_factors[s]))
# Finally, the probability of correctness is one minus incorrect
prob_prior = 1 - neg_prob_prior
return prob_prior
pos_evidence = [ev for ev in evidences if
not ev.epistemics.get('negated')]
neg_evidence = [ev for ev in evidences if
ev.epistemics.get('negated')]
pp = _score(pos_evidence)
np = _score(neg_evidence)
# The basic assumption is that the positive and negative evidence
# can't simultaneously be correct.
# There are two cases to consider. (1) If the positive evidence is
# incorrect then there is no Statement and the belief should be 0,
# irrespective of the negative evidence.
# (2) If the positive evidence is correct and the negative evidence
# is incorrect.
# This amounts to the following formula:
# 0 * (1-pp) + 1 * (pp * (1-np)) which we simplify below
score = pp * (1 - np)
return score |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:update_probs; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 10; 5, 14; 5, 24; 5, 83; 5, 84; 5, 88; 5, 160; 5, 161; 5, 162; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:syst_error; 13, float:0.05; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:prior_probs; 17, dictionary; 17, 18; 17, 21; 18, pair; 18, 19; 18, 20; 19, string:'syst'; 20, dictionary; 21, pair; 21, 22; 21, 23; 22, string:'rand'; 23, dictionary; 24, for_statement; 24, 25; 24, 30; 24, 37; 24, 38; 25, pattern_list; 25, 26; 25, 27; 26, identifier:source; 27, tuple_pattern; 27, 28; 27, 29; 28, identifier:p; 29, identifier:n; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:prior_counts; 35, identifier:items; 36, argument_list; 37, comment; 38, block; 38, 39; 38, 47; 38, 55; 39, if_statement; 39, 40; 39, 45; 40, comparison_operator:==; 40, 41; 40, 44; 41, binary_operator:+; 41, 42; 41, 43; 42, identifier:n; 43, identifier:p; 44, integer:0; 45, block; 45, 46; 46, continue_statement; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 54; 49, subscript; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:prior_probs; 52, string:'syst'; 53, identifier:source; 54, identifier:syst_error; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 62; 56, 63; 57, subscript; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:prior_probs; 60, string:'rand'; 61, identifier:source; 62, line_continuation:\; 63, binary_operator:-; 63, 64; 63, 82; 64, binary_operator:-; 64, 65; 64, 66; 65, integer:1; 66, call; 66, 67; 66, 68; 67, identifier:min; 68, argument_list; 68, 69; 69, tuple; 69, 70; 69, 79; 70, binary_operator:/; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:float; 73, argument_list; 73, 74; 74, identifier:p; 75, parenthesized_expression; 75, 76; 76, binary_operator:+; 76, 77; 76, 78; 77, identifier:n; 78, identifier:p; 79, binary_operator:-; 79, 80; 79, 81; 80, integer:1; 81, identifier:syst_error; 82, identifier:syst_error; 83, comment; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:subtype_probs; 87, dictionary; 88, for_statement; 88, 89; 88, 92; 88, 99; 89, pattern_list; 89, 90; 89, 91; 90, identifier:source; 91, identifier:entry; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:subtype_counts; 97, identifier:items; 98, argument_list; 99, block; 99, 100; 100, for_statement; 100, 101; 100, 106; 100, 111; 100, 112; 101, pattern_list; 101, 102; 101, 103; 102, identifier:rule; 103, tuple_pattern; 103, 104; 103, 105; 104, identifier:p; 105, identifier:n; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:entry; 109, identifier:items; 110, argument_list; 111, comment; 112, block; 112, 113; 112, 121; 112, 132; 113, if_statement; 113, 114; 113, 119; 114, comparison_operator:==; 114, 115; 114, 118; 115, binary_operator:+; 115, 116; 115, 117; 116, identifier:n; 117, identifier:p; 118, integer:0; 119, block; 119, 120; 120, continue_statement; 121, if_statement; 121, 122; 121, 125; 122, comparison_operator:not; 122, 123; 122, 124; 123, identifier:source; 124, identifier:subtype_probs; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:subtype_probs; 130, identifier:source; 131, dictionary; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 139; 133, 140; 134, subscript; 134, 135; 134, 138; 135, subscript; 135, 136; 135, 137; 136, identifier:subtype_probs; 137, identifier:source; 138, identifier:rule; 139, line_continuation:\; 140, binary_operator:-; 140, 141; 140, 159; 141, binary_operator:-; 141, 142; 141, 143; 142, integer:1; 143, call; 143, 144; 143, 145; 144, identifier:min; 145, argument_list; 145, 146; 146, tuple; 146, 147; 146, 156; 147, binary_operator:/; 147, 148; 147, 152; 148, call; 148, 149; 148, 150; 149, identifier:float; 150, argument_list; 150, 151; 151, identifier:p; 152, parenthesized_expression; 152, 153; 153, binary_operator:+; 153, 154; 153, 155; 154, identifier:n; 155, identifier:p; 156, binary_operator:-; 156, 157; 156, 158; 157, integer:1; 158, identifier:syst_error; 159, identifier:syst_error; 160, comment; 161, comment; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 171; 164, attribute; 164, 165; 164, 170; 165, call; 165, 166; 165, 167; 166, identifier:super; 167, argument_list; 167, 168; 167, 169; 168, identifier:BayesianScorer; 169, identifier:self; 170, identifier:update_probs; 171, argument_list; 171, 172; 171, 173; 172, identifier:prior_probs; 173, identifier:subtype_probs | def update_probs(self):
"""Update the internal probability values given the counts."""
# We deal with the prior probsfirst
# This is a fixed assumed value for systematic error
syst_error = 0.05
prior_probs = {'syst': {}, 'rand': {}}
for source, (p, n) in self.prior_counts.items():
# Skip if there are no actual counts
if n + p == 0:
continue
prior_probs['syst'][source] = syst_error
prior_probs['rand'][source] = \
1 - min((float(p) / (n + p), 1-syst_error)) - syst_error
# Next we deal with subtype probs based on counts
subtype_probs = {}
for source, entry in self.subtype_counts.items():
for rule, (p, n) in entry.items():
# Skip if there are no actual counts
if n + p == 0:
continue
if source not in subtype_probs:
subtype_probs[source] = {}
subtype_probs[source][rule] = \
1 - min((float(p) / (n + p), 1-syst_error)) - syst_error
# Finally we propagate this into the full probability
# data structures of the parent class
super(BayesianScorer, self).update_probs(prior_probs, subtype_probs) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:set_hierarchy_probs; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:statements; 6, block; 6, 7; 6, 9; 6, 80; 6, 126; 6, 165; 6, 172; 6, 177; 6, 184; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:build_hierarchy_graph; 11, parameters; 11, 12; 12, identifier:stmts; 13, block; 13, 14; 13, 16; 13, 24; 13, 78; 14, expression_statement; 14, 15; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:g; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:networkx; 22, identifier:DiGraph; 23, argument_list; 24, for_statement; 24, 25; 24, 26; 24, 27; 25, identifier:st1; 26, identifier:stmts; 27, block; 27, 28; 27, 42; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:g; 32, identifier:add_node; 33, argument_list; 33, 34; 33, 39; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:st1; 37, identifier:matches_key; 38, argument_list; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:stmt; 41, identifier:st1; 42, for_statement; 42, 43; 42, 44; 42, 47; 43, identifier:st2; 44, attribute; 44, 45; 44, 46; 45, identifier:st1; 46, identifier:supported_by; 47, block; 47, 48; 47, 62; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:g; 52, identifier:add_node; 53, argument_list; 53, 54; 53, 59; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:st2; 57, identifier:matches_key; 58, argument_list; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:stmt; 61, identifier:st2; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:g; 66, identifier:add_edge; 67, argument_list; 67, 68; 67, 73; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:st2; 71, identifier:matches_key; 72, argument_list; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:st1; 76, identifier:matches_key; 77, argument_list; 78, return_statement; 78, 79; 79, identifier:g; 80, function_definition; 80, 81; 80, 82; 80, 84; 81, function_name:get_ranked_stmts; 82, parameters; 82, 83; 83, identifier:g; 84, block; 84, 85; 84, 87; 84, 100; 84, 110; 84, 124; 85, expression_statement; 85, 86; 86, comment; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:node_ranks; 90, call; 90, 91; 90, 98; 91, attribute; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:networkx; 95, identifier:algorithms; 96, identifier:dag; 97, identifier:topological_sort; 98, argument_list; 98, 99; 99, identifier:g; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:node_ranks; 103, call; 103, 104; 103, 105; 104, identifier:reversed; 105, argument_list; 105, 106; 106, call; 106, 107; 106, 108; 107, identifier:list; 108, argument_list; 108, 109; 109, identifier:node_ranks; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:stmts; 113, list_comprehension; 113, 114; 113, 121; 114, subscript; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:g; 118, identifier:node; 119, identifier:n; 120, string:'stmt'; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:n; 123, identifier:node_ranks; 124, return_statement; 124, 125; 125, identifier:stmts; 126, function_definition; 126, 127; 126, 128; 126, 130; 127, function_name:assert_no_cycle; 128, parameters; 128, 129; 129, identifier:g; 130, block; 130, 131; 130, 133; 130, 156; 130, 162; 131, expression_statement; 131, 132; 132, comment; 133, try_statement; 133, 134; 133, 148; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:cyc; 138, call; 138, 139; 138, 146; 139, attribute; 139, 140; 139, 145; 140, attribute; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:networkx; 143, identifier:algorithms; 144, identifier:cycles; 145, identifier:find_cycle; 146, argument_list; 146, 147; 147, identifier:g; 148, except_clause; 148, 149; 148, 154; 149, attribute; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:networkx; 152, identifier:exception; 153, identifier:NetworkXNoCycle; 154, block; 154, 155; 155, return_statement; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:msg; 159, binary_operator:%; 159, 160; 159, 161; 160, string:'Cycle found in hierarchy graph: %s'; 161, identifier:cyc; 162, assert_statement; 162, 163; 162, 164; 163, False; 164, identifier:msg; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:g; 168, call; 168, 169; 168, 170; 169, identifier:build_hierarchy_graph; 170, argument_list; 170, 171; 171, identifier:statements; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:assert_no_cycle; 175, argument_list; 175, 176; 176, identifier:g; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:ranked_stmts; 180, call; 180, 181; 180, 182; 181, identifier:get_ranked_stmts; 182, argument_list; 182, 183; 183, identifier:g; 184, for_statement; 184, 185; 184, 186; 184, 187; 185, identifier:st; 186, identifier:ranked_stmts; 187, block; 187, 188; 187, 195; 187, 199; 187, 200; 187, 235; 187, 236; 187, 237; 187, 249; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:bps; 191, call; 191, 192; 191, 193; 192, identifier:_get_belief_package; 193, argument_list; 193, 194; 194, identifier:st; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:supporting_evidences; 198, list:[]; 199, comment; 200, for_statement; 200, 201; 200, 202; 200, 208; 200, 209; 200, 210; 201, identifier:bp; 202, subscript; 202, 203; 202, 204; 203, identifier:bps; 204, slice; 204, 205; 204, 206; 205, colon; 206, unary_operator:-; 206, 207; 207, integer:1; 208, comment; 209, comment; 210, block; 210, 211; 211, for_statement; 211, 212; 211, 213; 211, 216; 212, identifier:ev; 213, attribute; 213, 214; 213, 215; 214, identifier:bp; 215, identifier:evidences; 216, block; 216, 217; 217, if_statement; 217, 218; 217, 227; 218, not_operator; 218, 219; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:ev; 223, identifier:epistemics; 224, identifier:get; 225, argument_list; 225, 226; 226, string:'negated'; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:supporting_evidences; 232, identifier:append; 233, argument_list; 233, 234; 234, identifier:ev; 235, comment; 236, comment; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 240; 239, identifier:belief; 240, call; 240, 241; 240, 246; 241, attribute; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:self; 244, identifier:scorer; 245, identifier:score_statement; 246, argument_list; 246, 247; 246, 248; 247, identifier:st; 248, identifier:supporting_evidences; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:st; 253, identifier:belief; 254, identifier:belief | def set_hierarchy_probs(self, statements):
"""Sets hierarchical belief probabilities for INDRA Statements.
The Statements are assumed to be in a hierarchical relation graph with
the supports and supported_by attribute of each Statement object having
been set.
The hierarchical belief probability of each Statement is calculated
based on its prior probability and the probabilities propagated from
Statements supporting it in the hierarchy graph.
Parameters
----------
statements : list[indra.statements.Statement]
A list of INDRA Statements whose belief scores are to
be calculated. Each Statement object's belief attribute is updated
by this function.
"""
def build_hierarchy_graph(stmts):
"""Return a DiGraph based on matches keys and Statement supports"""
g = networkx.DiGraph()
for st1 in stmts:
g.add_node(st1.matches_key(), stmt=st1)
for st2 in st1.supported_by:
g.add_node(st2.matches_key(), stmt=st2)
g.add_edge(st2.matches_key(), st1.matches_key())
return g
def get_ranked_stmts(g):
"""Return a topological sort of statement matches keys from a graph.
"""
node_ranks = networkx.algorithms.dag.topological_sort(g)
node_ranks = reversed(list(node_ranks))
stmts = [g.node[n]['stmt'] for n in node_ranks]
return stmts
def assert_no_cycle(g):
"""If the graph has cycles, throws AssertionError."""
try:
cyc = networkx.algorithms.cycles.find_cycle(g)
except networkx.exception.NetworkXNoCycle:
return
msg = 'Cycle found in hierarchy graph: %s' % cyc
assert False, msg
g = build_hierarchy_graph(statements)
assert_no_cycle(g)
ranked_stmts = get_ranked_stmts(g)
for st in ranked_stmts:
bps = _get_belief_package(st)
supporting_evidences = []
# NOTE: the last belief package in the list is this statement's own
for bp in bps[:-1]:
# Iterate over all the parent evidences and add only
# non-negated ones
for ev in bp.evidences:
if not ev.epistemics.get('negated'):
supporting_evidences.append(ev)
# Now add the Statement's own evidence
# Now score all the evidences
belief = self.scorer.score_statement(st, supporting_evidences)
st.belief = belief |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_statements; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:reprocess; 7, False; 8, block; 8, 9; 8, 11; 8, 226; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 19; 11, 20; 11, 215; 12, boolean_operator:or; 12, 13; 12, 18; 13, comparison_operator:is; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:_statements; 17, None; 18, identifier:reprocess; 19, comment; 20, block; 20, 21; 20, 36; 20, 37; 20, 173; 20, 174; 20, 206; 21, if_statement; 21, 22; 21, 27; 22, comparison_operator:is; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:content; 26, None; 27, block; 27, 28; 27, 34; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_statements; 33, list:[]; 34, return_statement; 34, 35; 35, list:[]; 36, comment; 37, if_statement; 37, 38; 37, 45; 37, 87; 37, 142; 37, 162; 38, comparison_operator:==; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:reader; 42, attribute; 42, 43; 42, 44; 43, identifier:ReachReader; 44, identifier:name; 45, block; 45, 46; 46, if_statement; 46, 47; 46, 54; 46, 55; 46, 76; 47, comparison_operator:==; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:format; 51, attribute; 51, 52; 51, 53; 52, identifier:formats; 53, identifier:JSON; 54, comment; 55, block; 55, 56; 55, 67; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:json_str; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:json; 62, identifier:dumps; 63, argument_list; 63, 64; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:content; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:processor; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:reach; 73, identifier:process_json_str; 74, argument_list; 74, 75; 75, identifier:json_str; 76, else_clause; 76, 77; 77, block; 77, 78; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:ReadingError; 81, argument_list; 81, 82; 82, binary_operator:%; 82, 83; 82, 84; 83, string:"Incorrect format for Reach output: %s."; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:format; 87, elif_clause; 87, 88; 87, 95; 88, comparison_operator:==; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:self; 91, identifier:reader; 92, attribute; 92, 93; 92, 94; 93, identifier:SparserReader; 94, identifier:name; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 104; 96, 105; 96, 129; 97, comparison_operator:==; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:format; 101, attribute; 101, 102; 101, 103; 102, identifier:formats; 103, identifier:JSON; 104, comment; 105, block; 105, 106; 105, 117; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:processor; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:sparser; 112, identifier:process_json_dict; 113, argument_list; 113, 114; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:content; 117, if_statement; 117, 118; 117, 121; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:processor; 120, None; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:processor; 126, identifier:set_statements_pmid; 127, argument_list; 127, 128; 128, None; 129, else_clause; 129, 130; 130, block; 130, 131; 131, raise_statement; 131, 132; 132, call; 132, 133; 132, 134; 133, identifier:ReadingError; 134, argument_list; 134, 135; 135, binary_operator:%; 135, 136; 135, 139; 136, concatenated_string; 136, 137; 136, 138; 137, string:"Sparser should only ever be JSON, not "; 138, string:"%s."; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:format; 142, elif_clause; 142, 143; 142, 150; 143, comparison_operator:==; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:reader; 147, attribute; 147, 148; 147, 149; 148, identifier:TripsReader; 149, identifier:name; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:processor; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:trips; 157, identifier:process_xml; 158, argument_list; 158, 159; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:content; 162, else_clause; 162, 163; 163, block; 163, 164; 164, raise_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:ReadingError; 167, argument_list; 167, 168; 168, binary_operator:%; 168, 169; 168, 170; 169, string:"Unknown reader: %s."; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:reader; 173, comment; 174, if_statement; 174, 175; 174, 178; 174, 198; 175, comparison_operator:is; 175, 176; 175, 177; 176, identifier:processor; 177, None; 178, block; 178, 179; 178, 194; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:logger; 183, identifier:error; 184, argument_list; 184, 185; 185, binary_operator:%; 185, 186; 185, 187; 186, string:"Production of statements from %s failed for %s."; 187, tuple; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:reader; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:content_id; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:stmts; 197, list:[]; 198, else_clause; 198, 199; 199, block; 199, 200; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:stmts; 203, attribute; 203, 204; 203, 205; 204, identifier:processor; 205, identifier:statements; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:self; 210, identifier:_statements; 211, subscript; 211, 212; 211, 213; 212, identifier:stmts; 213, slice; 213, 214; 214, colon; 215, else_clause; 215, 216; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:stmts; 220, subscript; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:_statements; 224, slice; 224, 225; 225, colon; 226, return_statement; 226, 227; 227, identifier:stmts | def get_statements(self, reprocess=False):
"""General method to create statements."""
if self._statements is None or reprocess:
# Handle the case that there is no content.
if self.content is None:
self._statements = []
return []
# Map to the different processors.
if self.reader == ReachReader.name:
if self.format == formats.JSON:
# Process the reach json into statements.
json_str = json.dumps(self.content)
processor = reach.process_json_str(json_str)
else:
raise ReadingError("Incorrect format for Reach output: %s."
% self.format)
elif self.reader == SparserReader.name:
if self.format == formats.JSON:
# Process the sparser content into statements
processor = sparser.process_json_dict(self.content)
if processor is not None:
processor.set_statements_pmid(None)
else:
raise ReadingError("Sparser should only ever be JSON, not "
"%s." % self.format)
elif self.reader == TripsReader.name:
processor = trips.process_xml(self.content)
else:
raise ReadingError("Unknown reader: %s." % self.reader)
# Get the statements from the processor, if it was resolved.
if processor is None:
logger.error("Production of statements from %s failed for %s."
% (self.reader, self.content_id))
stmts = []
else:
stmts = processor.statements
self._statements = stmts[:]
else:
stmts = self._statements[:]
return stmts |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:read; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:read_list; 6, default_parameter; 6, 7; 6, 8; 7, identifier:verbose; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:log; 11, False; 12, default_parameter; 12, 13; 12, 14; 13, identifier:n_per_proc; 14, None; 15, block; 15, 16; 15, 18; 15, 22; 15, 29; 15, 38; 15, 45; 15, 52; 15, 56; 15, 81; 15, 357; 15, 366; 16, expression_statement; 16, 17; 17, string:"Perform the actual reading."; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:ret; 21, list:[]; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:prep_input; 27, argument_list; 27, 28; 28, identifier:read_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:L; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:file_list; 38, if_statement; 38, 39; 38, 42; 39, comparison_operator:==; 39, 40; 39, 41; 40, identifier:L; 41, integer:0; 42, block; 42, 43; 43, return_statement; 43, 44; 44, identifier:ret; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:logger; 49, identifier:info; 50, argument_list; 50, 51; 51, string:"Beginning to run sparser."; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:output_file_list; 55, list:[]; 56, if_statement; 56, 57; 56, 58; 56, 75; 57, identifier:log; 58, block; 58, 59; 58, 67; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:log_name; 62, binary_operator:%; 62, 63; 62, 64; 63, string:'sparser_run_%s.log'; 64, call; 64, 65; 64, 66; 65, identifier:_time_stamp; 66, argument_list; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:outbuf; 70, call; 70, 71; 70, 72; 71, identifier:open; 72, argument_list; 72, 73; 72, 74; 73, identifier:log_name; 74, string:'wb'; 75, else_clause; 75, 76; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:outbuf; 80, None; 81, try_statement; 81, 82; 81, 334; 82, block; 82, 83; 83, if_statement; 83, 84; 83, 89; 83, 121; 84, comparison_operator:==; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:n_proc; 88, integer:1; 89, block; 89, 90; 90, for_statement; 90, 91; 90, 92; 90, 95; 91, identifier:fpath; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:file_list; 95, block; 95, 96; 95, 109; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, pattern_list; 98, 99; 98, 100; 99, identifier:outpath; 100, identifier:_; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:read_one; 105, argument_list; 105, 106; 105, 107; 105, 108; 106, identifier:fpath; 107, identifier:outbuf; 108, identifier:verbose; 109, if_statement; 109, 110; 109, 113; 110, comparison_operator:is; 110, 111; 110, 112; 111, identifier:outpath; 112, None; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:output_file_list; 118, identifier:append; 119, argument_list; 119, 120; 120, identifier:outpath; 121, else_clause; 121, 122; 122, block; 122, 123; 122, 146; 122, 150; 122, 255; 123, if_statement; 123, 124; 123, 127; 124, comparison_operator:is; 124, 125; 124, 126; 125, identifier:n_per_proc; 126, None; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 131; 130, identifier:n_per_proc; 131, call; 131, 132; 131, 133; 132, identifier:max; 133, argument_list; 133, 134; 133, 135; 134, integer:1; 135, call; 135, 136; 135, 137; 136, identifier:min; 137, argument_list; 137, 138; 137, 139; 138, integer:1000; 139, binary_operator://; 139, 140; 139, 145; 140, binary_operator://; 140, 141; 140, 142; 141, identifier:L; 142, attribute; 142, 143; 142, 144; 143, identifier:self; 144, identifier:n_proc; 145, integer:2; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:pool; 149, None; 150, try_statement; 150, 151; 150, 236; 151, block; 151, 152; 151, 161; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:pool; 155, call; 155, 156; 155, 157; 156, identifier:Pool; 157, argument_list; 157, 158; 158, attribute; 158, 159; 158, 160; 159, identifier:self; 160, identifier:n_proc; 161, if_statement; 161, 162; 161, 165; 161, 207; 162, comparison_operator:is; 162, 163; 162, 164; 163, identifier:n_per_proc; 164, integer:1; 165, block; 165, 166; 165, 195; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:batches; 169, list_comprehension; 169, 170; 169, 185; 170, subscript; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:file_list; 174, slice; 174, 175; 174, 178; 174, 179; 175, binary_operator:*; 175, 176; 175, 177; 176, identifier:n; 177, identifier:n_per_proc; 178, colon; 179, binary_operator:*; 179, 180; 179, 184; 180, parenthesized_expression; 180, 181; 181, binary_operator:+; 181, 182; 181, 183; 182, identifier:n; 183, integer:1; 184, identifier:n_per_proc; 185, for_in_clause; 185, 186; 185, 187; 186, identifier:n; 187, call; 187, 188; 187, 189; 188, identifier:range; 189, argument_list; 189, 190; 190, binary_operator:+; 190, 191; 190, 194; 191, binary_operator://; 191, 192; 191, 193; 192, identifier:L; 193, identifier:n_per_proc; 194, integer:1; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:out_lists_and_buffs; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:pool; 201, identifier:map; 202, argument_list; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:read_some; 206, identifier:batches; 207, else_clause; 207, 208; 208, block; 208, 209; 208, 223; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:out_files_and_buffs; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, identifier:pool; 215, identifier:map; 216, argument_list; 216, 217; 216, 220; 217, attribute; 217, 218; 217, 219; 218, identifier:self; 219, identifier:read_one; 220, attribute; 220, 221; 220, 222; 221, identifier:self; 222, identifier:file_list; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:out_lists_and_buffs; 226, list_comprehension; 226, 227; 226, 231; 227, tuple; 227, 228; 227, 230; 228, list:[out_files]; 228, 229; 229, identifier:out_files; 230, identifier:buffs; 231, for_in_clause; 231, 232; 231, 235; 232, pattern_list; 232, 233; 232, 234; 233, identifier:out_files; 234, identifier:buffs; 235, identifier:out_files_and_buffs; 236, finally_clause; 236, 237; 237, block; 237, 238; 238, if_statement; 238, 239; 238, 242; 239, comparison_operator:is; 239, 240; 239, 241; 240, identifier:pool; 241, None; 242, block; 242, 243; 242, 249; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:pool; 247, identifier:close; 248, argument_list; 249, expression_statement; 249, 250; 250, call; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:pool; 253, identifier:join; 254, argument_list; 255, for_statement; 255, 256; 255, 261; 255, 265; 256, pattern_list; 256, 257; 256, 258; 257, identifier:i; 258, tuple_pattern; 258, 259; 258, 260; 259, identifier:out_list; 260, identifier:buff; 261, call; 261, 262; 261, 263; 262, identifier:enumerate; 263, argument_list; 263, 264; 264, identifier:out_lists_and_buffs; 265, block; 265, 266; 265, 275; 266, if_statement; 266, 267; 266, 270; 267, comparison_operator:is; 267, 268; 267, 269; 268, identifier:out_list; 269, None; 270, block; 270, 271; 271, expression_statement; 271, 272; 272, augmented_assignment:+=; 272, 273; 272, 274; 273, identifier:output_file_list; 274, identifier:out_list; 275, if_statement; 275, 276; 275, 277; 276, identifier:log; 277, block; 277, 278; 277, 292; 277, 328; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:outbuf; 282, identifier:write; 283, argument_list; 283, 284; 284, binary_operator:%; 284, 285; 284, 286; 285, string:b'Log for producing output %d/%d.\n'; 286, tuple; 286, 287; 286, 288; 287, identifier:i; 288, call; 288, 289; 288, 290; 289, identifier:len; 290, argument_list; 290, 291; 291, identifier:out_lists_and_buffs; 292, if_statement; 292, 293; 292, 296; 292, 317; 293, comparison_operator:is; 293, 294; 293, 295; 294, identifier:buff; 295, None; 296, block; 296, 297; 296, 304; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:buff; 301, identifier:seek; 302, argument_list; 302, 303; 303, integer:0; 304, expression_statement; 304, 305; 305, call; 305, 306; 305, 309; 306, attribute; 306, 307; 306, 308; 307, identifier:outbuf; 308, identifier:write; 309, argument_list; 309, 310; 310, binary_operator:+; 310, 311; 310, 316; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:buff; 314, identifier:read; 315, argument_list; 316, string:b'\n'; 317, else_clause; 317, 318; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, call; 320, 321; 320, 324; 321, attribute; 321, 322; 321, 323; 322, identifier:outbuf; 323, identifier:write; 324, argument_list; 324, 325; 325, concatenated_string; 325, 326; 325, 327; 326, string:b'ERROR: no buffer was None. '; 327, string:b'No logs available.\n'; 328, expression_statement; 328, 329; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:outbuf; 332, identifier:flush; 333, argument_list; 334, finally_clause; 334, 335; 335, block; 335, 336; 336, if_statement; 336, 337; 336, 338; 337, identifier:log; 338, block; 338, 339; 338, 345; 339, expression_statement; 339, 340; 340, call; 340, 341; 340, 344; 341, attribute; 341, 342; 341, 343; 342, identifier:outbuf; 343, identifier:close; 344, argument_list; 345, if_statement; 345, 346; 345, 347; 346, identifier:verbose; 347, block; 347, 348; 348, expression_statement; 348, 349; 349, call; 349, 350; 349, 353; 350, attribute; 350, 351; 350, 352; 351, identifier:logger; 352, identifier:info; 353, argument_list; 353, 354; 354, binary_operator:%; 354, 355; 354, 356; 355, string:"Sparser logs may be found at %s."; 356, identifier:log_name; 357, expression_statement; 357, 358; 358, assignment; 358, 359; 358, 360; 359, identifier:ret; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:self; 363, identifier:get_output; 364, argument_list; 364, 365; 365, identifier:output_file_list; 366, return_statement; 366, 367; 367, identifier:ret | def read(self, read_list, verbose=False, log=False, n_per_proc=None):
"Perform the actual reading."
ret = []
self.prep_input(read_list)
L = len(self.file_list)
if L == 0:
return ret
logger.info("Beginning to run sparser.")
output_file_list = []
if log:
log_name = 'sparser_run_%s.log' % _time_stamp()
outbuf = open(log_name, 'wb')
else:
outbuf = None
try:
if self.n_proc == 1:
for fpath in self.file_list:
outpath, _ = self.read_one(fpath, outbuf, verbose)
if outpath is not None:
output_file_list.append(outpath)
else:
if n_per_proc is None:
n_per_proc = max(1, min(1000, L//self.n_proc//2))
pool = None
try:
pool = Pool(self.n_proc)
if n_per_proc is not 1:
batches = [self.file_list[n*n_per_proc:(n+1)*n_per_proc]
for n in range(L//n_per_proc + 1)]
out_lists_and_buffs = pool.map(self.read_some,
batches)
else:
out_files_and_buffs = pool.map(self.read_one,
self.file_list)
out_lists_and_buffs = [([out_files], buffs)
for out_files, buffs
in out_files_and_buffs]
finally:
if pool is not None:
pool.close()
pool.join()
for i, (out_list, buff) in enumerate(out_lists_and_buffs):
if out_list is not None:
output_file_list += out_list
if log:
outbuf.write(b'Log for producing output %d/%d.\n'
% (i, len(out_lists_and_buffs)))
if buff is not None:
buff.seek(0)
outbuf.write(buff.read() + b'\n')
else:
outbuf.write(b'ERROR: no buffer was None. '
b'No logs available.\n')
outbuf.flush()
finally:
if log:
outbuf.close()
if verbose:
logger.info("Sparser logs may be found at %s." %
log_name)
ret = self.get_output(output_file_list)
return ret |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:read_pmid_sentences; 3, parameters; 3, 4; 3, 5; 4, identifier:pmid_sentences; 5, dictionary_splat_pattern; 5, 6; 6, identifier:drum_args; 7, block; 7, 8; 7, 10; 7, 32; 7, 33; 7, 43; 7, 47; 7, 51; 7, 52; 7, 238; 7, 239; 7, 252; 8, expression_statement; 8, 9; 9, comment; 10, function_definition; 10, 11; 10, 12; 10, 15; 11, function_name:_set_pmid; 12, parameters; 12, 13; 12, 14; 13, identifier:statements; 14, identifier:pmid; 15, block; 15, 16; 16, for_statement; 16, 17; 16, 18; 16, 19; 17, identifier:stmt; 18, identifier:statements; 19, block; 19, 20; 20, for_statement; 20, 21; 20, 22; 20, 25; 21, identifier:evidence; 22, attribute; 22, 23; 22, 24; 23, identifier:stmt; 24, identifier:evidence; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:evidence; 30, identifier:pmid; 31, identifier:pmid; 32, comment; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:run_drum; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:drum_args; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, string:'run_drum'; 42, False; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:drum_process; 46, None; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:all_statements; 50, dictionary; 51, comment; 52, for_statement; 52, 53; 52, 56; 52, 61; 53, pattern_list; 53, 54; 53, 55; 54, identifier:pmid; 55, identifier:sentences; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:pmid_sentences; 59, identifier:items; 60, argument_list; 61, block; 61, 62; 61, 69; 61, 83; 61, 91; 61, 92; 61, 100; 61, 108; 61, 115; 61, 116; 61, 117; 61, 146; 61, 147; 61, 158; 61, 159; 61, 171; 61, 175; 61, 176; 61, 201; 61, 202; 61, 208; 61, 216; 61, 232; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:logger; 66, identifier:info; 67, argument_list; 67, 68; 68, string:'================================'; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:logger; 73, identifier:info; 74, argument_list; 74, 75; 75, binary_operator:%; 75, 76; 75, 77; 76, string:'Processing %d sentences for %s'; 77, tuple; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:sentences; 82, identifier:pmid; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:ts; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:time; 89, identifier:time; 90, argument_list; 91, comment; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:drum_args; 96, string:'name'; 97, binary_operator:%; 97, 98; 97, 99; 98, string:'DrumReader%s'; 99, identifier:pmid; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:dr; 103, call; 103, 104; 103, 105; 104, identifier:DrumReader; 105, argument_list; 105, 106; 106, dictionary_splat; 106, 107; 107, identifier:drum_args; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:time; 112, identifier:sleep; 113, argument_list; 113, 114; 114, integer:3; 115, comment; 116, comment; 117, if_statement; 117, 118; 117, 123; 118, boolean_operator:and; 118, 119; 118, 120; 119, identifier:run_drum; 120, comparison_operator:is; 120, 121; 120, 122; 121, identifier:drum_process; 122, None; 123, block; 123, 124; 123, 132; 123, 138; 123, 139; 123, 140; 124, expression_statement; 124, 125; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:drum_args; 128, identifier:pop; 129, argument_list; 129, 130; 129, 131; 130, string:'run_drum'; 131, None; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:drum_process; 135, attribute; 135, 136; 135, 137; 136, identifier:dr; 137, identifier:drum_system; 138, comment; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:drum_args; 144, string:'drum_system'; 145, identifier:drum_process; 146, comment; 147, for_statement; 147, 148; 147, 149; 147, 150; 148, identifier:sentence; 149, identifier:sentences; 150, block; 150, 151; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:dr; 155, identifier:read_text; 156, argument_list; 156, 157; 157, identifier:sentence; 158, comment; 159, try_statement; 159, 160; 159, 167; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:dr; 165, identifier:start; 166, argument_list; 167, except_clause; 167, 168; 167, 169; 168, identifier:SystemExit; 169, block; 169, 170; 170, pass_statement; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:statements; 174, list:[]; 175, comment; 176, for_statement; 176, 177; 176, 178; 176, 181; 176, 182; 177, identifier:extraction; 178, attribute; 178, 179; 178, 180; 179, identifier:dr; 180, identifier:extractions; 181, comment; 182, block; 182, 183; 182, 188; 182, 195; 183, if_statement; 183, 184; 183, 186; 184, not_operator; 184, 185; 185, identifier:extraction; 186, block; 186, 187; 187, continue_statement; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:tp; 191, call; 191, 192; 191, 193; 192, identifier:process_xml; 193, argument_list; 193, 194; 194, identifier:extraction; 195, expression_statement; 195, 196; 196, augmented_assignment:+=; 196, 197; 196, 198; 197, identifier:statements; 198, attribute; 198, 199; 198, 200; 199, identifier:tp; 200, identifier:statements; 201, comment; 202, expression_statement; 202, 203; 203, call; 203, 204; 203, 205; 204, identifier:_set_pmid; 205, argument_list; 205, 206; 205, 207; 206, identifier:statements; 207, identifier:pmid; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:te; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:time; 214, identifier:time; 215, argument_list; 216, expression_statement; 216, 217; 217, call; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:logger; 220, identifier:info; 221, argument_list; 221, 222; 222, binary_operator:%; 222, 223; 222, 224; 223, string:'Reading took %d seconds and produced %d Statements.'; 224, tuple; 224, 225; 224, 228; 225, binary_operator:-; 225, 226; 225, 227; 226, identifier:te; 227, identifier:ts; 228, call; 228, 229; 228, 230; 229, identifier:len; 230, argument_list; 230, 231; 231, identifier:statements; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 237; 234, subscript; 234, 235; 234, 236; 235, identifier:all_statements; 236, identifier:pmid; 237, identifier:statements; 238, comment; 239, if_statement; 239, 240; 239, 245; 240, boolean_operator:and; 240, 241; 240, 242; 241, identifier:drum_process; 242, attribute; 242, 243; 242, 244; 243, identifier:dr; 244, identifier:drum_system; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:dr; 250, identifier:_kill_drum; 251, argument_list; 252, return_statement; 252, 253; 253, identifier:all_statements | def read_pmid_sentences(pmid_sentences, **drum_args):
"""Read sentences from a PMID-keyed dictonary and return all Statements
Parameters
----------
pmid_sentences : dict[str, list[str]]
A dictonary where each key is a PMID pointing to a list of sentences
to be read.
**drum_args
Keyword arguments passed directly to the DrumReader. Typical
things to specify are `host` and `port`. If `run_drum` is specified
as True, this process will internally run the DRUM reading system
as a subprocess. Otherwise, DRUM is expected to be running
independently.
Returns
-------
all_statements : list[indra.statement.Statement]
A list of INDRA Statements resulting from the reading
"""
def _set_pmid(statements, pmid):
for stmt in statements:
for evidence in stmt.evidence:
evidence.pmid = pmid
# See if we need to start DRUM as a subprocess
run_drum = drum_args.get('run_drum', False)
drum_process = None
all_statements = {}
# Iterate over all the keys and sentences to read
for pmid, sentences in pmid_sentences.items():
logger.info('================================')
logger.info('Processing %d sentences for %s' % (len(sentences), pmid))
ts = time.time()
# Make a DrumReader instance
drum_args['name'] = 'DrumReader%s' % pmid
dr = DrumReader(**drum_args)
time.sleep(3)
# If there is no DRUM process set yet, we get the one that was
# just started by the DrumReader
if run_drum and drum_process is None:
drum_args.pop('run_drum', None)
drum_process = dr.drum_system
# By setting this, we ensuer that the reference to the
# process is passed in to all future DrumReaders
drum_args['drum_system'] = drum_process
# Now read each sentence for this key
for sentence in sentences:
dr.read_text(sentence)
# Start receiving results and exit when done
try:
dr.start()
except SystemExit:
pass
statements = []
# Process all the extractions into INDRA Statements
for extraction in dr.extractions:
# Sometimes we get nothing back
if not extraction:
continue
tp = process_xml(extraction)
statements += tp.statements
# Set the PMIDs for the evidences of the Statements
_set_pmid(statements, pmid)
te = time.time()
logger.info('Reading took %d seconds and produced %d Statements.' %
(te-ts, len(statements)))
all_statements[pmid] = statements
# If we were running a DRUM process, we should kill it
if drum_process and dr.drum_system:
dr._kill_drum()
return all_statements |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:graph_query; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:kind; 5, identifier:source; 6, default_parameter; 6, 7; 6, 8; 7, identifier:target; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:neighbor_limit; 11, integer:1; 12, default_parameter; 12, 13; 12, 14; 13, identifier:database_filter; 14, None; 15, block; 15, 16; 15, 18; 15, 40; 15, 54; 15, 55; 15, 59; 15, 65; 15, 71; 15, 77; 15, 78; 15, 86; 15, 105; 15, 111; 15, 112; 15, 134; 15, 140; 15, 171; 15, 204; 15, 211; 15, 232; 15, 239; 15, 253; 15, 290; 15, 291; 15, 292; 15, 301; 15, 313; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:default_databases; 21, list:['wp', 'smpdb', 'reconx', 'reactome', 'psp', 'pid',
'panther', 'netpath', 'msigdb', 'mirtarbase', 'kegg',
'intact', 'inoh', 'humancyc', 'hprd',
'drugbank', 'dip', 'corum']; 21, 22; 21, 23; 21, 24; 21, 25; 21, 26; 21, 27; 21, 28; 21, 29; 21, 30; 21, 31; 21, 32; 21, 33; 21, 34; 21, 35; 21, 36; 21, 37; 21, 38; 21, 39; 22, string:'wp'; 23, string:'smpdb'; 24, string:'reconx'; 25, string:'reactome'; 26, string:'psp'; 27, string:'pid'; 28, string:'panther'; 29, string:'netpath'; 30, string:'msigdb'; 31, string:'mirtarbase'; 32, string:'kegg'; 33, string:'intact'; 34, string:'inoh'; 35, string:'humancyc'; 36, string:'hprd'; 37, string:'drugbank'; 38, string:'dip'; 39, string:'corum'; 40, if_statement; 40, 41; 40, 43; 40, 48; 41, not_operator; 41, 42; 42, identifier:database_filter; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:query_databases; 47, identifier:default_databases; 48, else_clause; 48, 49; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:query_databases; 53, identifier:database_filter; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:params; 58, dictionary; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 64; 61, subscript; 61, 62; 61, 63; 62, identifier:params; 63, string:'format'; 64, string:'BIOPAX'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:params; 69, string:'organism'; 70, string:'9606'; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:params; 75, string:'datasource'; 76, identifier:query_databases; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:kind_str; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:kind; 84, identifier:lower; 85, argument_list; 86, if_statement; 86, 87; 86, 93; 87, comparison_operator:not; 87, 88; 87, 89; 88, identifier:kind; 89, list:['neighborhood', 'pathsbetween', 'pathsfromto']; 89, 90; 89, 91; 89, 92; 90, string:'neighborhood'; 91, string:'pathsbetween'; 92, string:'pathsfromto'; 93, block; 93, 94; 93, 103; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:logger; 98, identifier:warn; 99, argument_list; 99, 100; 100, binary_operator:%; 100, 101; 100, 102; 101, string:'Invalid query type %s'; 102, identifier:kind_str; 103, return_statement; 103, 104; 104, None; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:params; 109, string:'kind'; 110, identifier:kind_str; 111, comment; 112, if_statement; 112, 113; 112, 118; 112, 123; 113, call; 113, 114; 113, 115; 114, identifier:isinstance; 115, argument_list; 115, 116; 115, 117; 116, identifier:source; 117, identifier:basestring; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:source_str; 122, identifier:source; 123, else_clause; 123, 124; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:source_str; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, string:','; 131, identifier:join; 132, argument_list; 132, 133; 133, identifier:source; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:params; 138, string:'source'; 139, identifier:source_str; 140, try_statement; 140, 141; 140, 155; 141, block; 141, 142; 141, 149; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:neighbor_limit; 145, call; 145, 146; 145, 147; 146, identifier:int; 147, argument_list; 147, 148; 148, identifier:neighbor_limit; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 154; 151, subscript; 151, 152; 151, 153; 152, identifier:params; 153, string:'limit'; 154, identifier:neighbor_limit; 155, except_clause; 155, 156; 155, 159; 156, tuple; 156, 157; 156, 158; 157, identifier:TypeError; 158, identifier:ValueError; 159, block; 159, 160; 159, 169; 160, expression_statement; 160, 161; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:logger; 164, identifier:warn; 165, argument_list; 165, 166; 166, binary_operator:%; 166, 167; 166, 168; 167, string:'Invalid neighborhood limit %s'; 168, identifier:neighbor_limit; 169, return_statement; 169, 170; 170, None; 171, if_statement; 171, 172; 171, 175; 172, comparison_operator:is; 172, 173; 172, 174; 173, identifier:target; 174, None; 175, block; 175, 176; 175, 198; 176, if_statement; 176, 177; 176, 182; 176, 187; 177, call; 177, 178; 177, 179; 178, identifier:isinstance; 179, argument_list; 179, 180; 179, 181; 180, identifier:target; 181, identifier:basestring; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:target_str; 186, identifier:target; 187, else_clause; 187, 188; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:target_str; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, string:','; 195, identifier:join; 196, argument_list; 196, 197; 197, identifier:target; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:params; 202, string:'target'; 203, identifier:target_str; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:logger; 208, identifier:info; 209, argument_list; 209, 210; 210, string:'Sending Pathway Commons query with parameters: '; 211, for_statement; 211, 212; 211, 215; 211, 220; 212, pattern_list; 212, 213; 212, 214; 213, identifier:k; 214, identifier:v; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:params; 218, identifier:items; 219, argument_list; 220, block; 220, 221; 221, expression_statement; 221, 222; 222, call; 222, 223; 222, 226; 223, attribute; 223, 224; 223, 225; 224, identifier:logger; 225, identifier:info; 226, argument_list; 226, 227; 227, binary_operator:%; 227, 228; 227, 229; 228, string:' %s: %s'; 229, tuple; 229, 230; 229, 231; 230, identifier:k; 231, identifier:v; 232, expression_statement; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:logger; 236, identifier:info; 237, argument_list; 237, 238; 238, string:'Sending Pathway Commons query...'; 239, expression_statement; 239, 240; 240, assignment; 240, 241; 240, 242; 241, identifier:res; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:requests; 245, identifier:get; 246, argument_list; 246, 247; 246, 250; 247, binary_operator:+; 247, 248; 247, 249; 248, identifier:pc2_url; 249, string:'graph'; 250, keyword_argument; 250, 251; 250, 252; 251, identifier:params; 252, identifier:params; 253, if_statement; 253, 254; 253, 260; 254, not_operator; 254, 255; 255, comparison_operator:==; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:res; 258, identifier:status_code; 259, integer:200; 260, block; 260, 261; 260, 272; 260, 288; 261, expression_statement; 261, 262; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:logger; 265, identifier:error; 266, argument_list; 266, 267; 267, binary_operator:%; 267, 268; 267, 269; 268, string:'Response is HTTP code %d.'; 269, attribute; 269, 270; 269, 271; 270, identifier:res; 271, identifier:status_code; 272, if_statement; 272, 273; 272, 278; 273, comparison_operator:==; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:res; 276, identifier:status_code; 277, integer:500; 278, block; 278, 279; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:logger; 283, identifier:error; 284, argument_list; 284, 285; 285, concatenated_string; 285, 286; 285, 287; 286, string:'Note: HTTP code 500 can mean empty '; 287, string:'results for a valid query.'; 288, return_statement; 288, 289; 289, None; 290, comment; 291, comment; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:model; 295, call; 295, 296; 295, 297; 296, identifier:owl_str_to_model; 297, argument_list; 297, 298; 298, attribute; 298, 299; 298, 300; 299, identifier:res; 300, identifier:content; 301, if_statement; 301, 302; 301, 305; 302, comparison_operator:is; 302, 303; 302, 304; 303, identifier:model; 304, None; 305, block; 305, 306; 306, expression_statement; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:logger; 310, identifier:info; 311, argument_list; 311, 312; 312, string:'Pathway Commons query returned a model...'; 313, return_statement; 313, 314; 314, identifier:model | def graph_query(kind, source, target=None, neighbor_limit=1,
database_filter=None):
"""Perform a graph query on PathwayCommons.
For more information on these queries, see
http://www.pathwaycommons.org/pc2/#graph
Parameters
----------
kind : str
The kind of graph query to perform. Currently 3 options are
implemented, 'neighborhood', 'pathsbetween' and 'pathsfromto'.
source : list[str]
A list of gene names which are the source set for the graph query.
target : Optional[list[str]]
A list of gene names which are the target set for the graph query.
Only needed for 'pathsfromto' queries.
neighbor_limit : Optional[int]
This limits the length of the longest path considered in
the graph query. Default: 1
Returns
-------
model : org.biopax.paxtools.model.Model
A BioPAX model (java object).
"""
default_databases = ['wp', 'smpdb', 'reconx', 'reactome', 'psp', 'pid',
'panther', 'netpath', 'msigdb', 'mirtarbase', 'kegg',
'intact', 'inoh', 'humancyc', 'hprd',
'drugbank', 'dip', 'corum']
if not database_filter:
query_databases = default_databases
else:
query_databases = database_filter
# excluded: ctd
params = {}
params['format'] = 'BIOPAX'
params['organism'] = '9606'
params['datasource'] = query_databases
# Get the "kind" string
kind_str = kind.lower()
if kind not in ['neighborhood', 'pathsbetween', 'pathsfromto']:
logger.warn('Invalid query type %s' % kind_str)
return None
params['kind'] = kind_str
# Get the source string
if isinstance(source, basestring):
source_str = source
else:
source_str = ','.join(source)
params['source'] = source_str
try:
neighbor_limit = int(neighbor_limit)
params['limit'] = neighbor_limit
except (TypeError, ValueError):
logger.warn('Invalid neighborhood limit %s' % neighbor_limit)
return None
if target is not None:
if isinstance(target, basestring):
target_str = target
else:
target_str = ','.join(target)
params['target'] = target_str
logger.info('Sending Pathway Commons query with parameters: ')
for k, v in params.items():
logger.info(' %s: %s' % (k, v))
logger.info('Sending Pathway Commons query...')
res = requests.get(pc2_url + 'graph', params=params)
if not res.status_code == 200:
logger.error('Response is HTTP code %d.' % res.status_code)
if res.status_code == 500:
logger.error('Note: HTTP code 500 can mean empty '
'results for a valid query.')
return None
# We don't decode to Unicode here because owl_str_to_model expects
# a byte stream
model = owl_str_to_model(res.content)
if model is not None:
logger.info('Pathway Commons query returned a model...')
return model |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:set_CCLE_context; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:cell_types; 6, block; 6, 7; 6, 9; 6, 15; 6, 16; 6, 29; 6, 42; 6, 43; 6, 53; 6, 99; 6, 100; 6, 101; 6, 102; 6, 288; 6, 295; 6, 305; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:get_gene_names; 14, argument_list; 15, comment; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 17, 20; 18, identifier:exp_values; 19, line_continuation:\; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:context_client; 23, identifier:get_protein_expression; 24, argument_list; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:_gene_names; 28, identifier:cell_types; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 30, 33; 31, identifier:mut_values; 32, line_continuation:\; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:context_client; 36, identifier:get_mutations; 37, argument_list; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:_gene_names; 41, identifier:cell_types; 42, comment; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:muts; 46, dictionary_comprehension; 46, 47; 46, 50; 47, pair; 47, 48; 47, 49; 48, identifier:cell_line; 49, dictionary; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:cell_line; 52, identifier:cell_types; 53, for_statement; 53, 54; 53, 57; 53, 62; 54, pattern_list; 54, 55; 54, 56; 55, identifier:cell_line; 56, identifier:entries; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:mut_values; 60, identifier:items; 61, argument_list; 62, block; 62, 63; 63, if_statement; 63, 64; 63, 67; 64, comparison_operator:is; 64, 65; 64, 66; 65, identifier:entries; 66, None; 67, block; 67, 68; 68, for_statement; 68, 69; 68, 72; 68, 77; 69, pattern_list; 69, 70; 69, 71; 70, identifier:gene; 71, identifier:mutations; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:entries; 75, identifier:items; 76, argument_list; 77, block; 77, 78; 78, if_statement; 78, 79; 78, 80; 78, 89; 79, identifier:mutations; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:muts; 86, identifier:cell_line; 87, identifier:gene; 88, integer:1; 89, else_clause; 89, 90; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 98; 93, subscript; 93, 94; 93, 97; 94, subscript; 94, 95; 94, 96; 95, identifier:muts; 96, identifier:cell_line; 97, identifier:gene; 98, integer:0; 99, comment; 100, comment; 101, comment; 102, function_definition; 102, 103; 102, 104; 102, 106; 103, function_name:bin_exp; 104, parameters; 104, 105; 105, identifier:expression_dict; 106, block; 106, 107; 106, 111; 106, 115; 106, 145; 106, 149; 106, 184; 106, 185; 106, 202; 106, 286; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:d; 110, identifier:expression_dict; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:exp_values; 114, list:[]; 115, for_statement; 115, 116; 115, 117; 115, 118; 116, identifier:line; 117, identifier:d; 118, block; 118, 119; 119, for_statement; 119, 120; 119, 121; 119, 124; 120, identifier:gene; 121, subscript; 121, 122; 121, 123; 122, identifier:d; 123, identifier:line; 124, block; 124, 125; 124, 133; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:val; 128, subscript; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:d; 131, identifier:line; 132, identifier:gene; 133, if_statement; 133, 134; 133, 137; 134, comparison_operator:is; 134, 135; 134, 136; 135, identifier:val; 136, None; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:exp_values; 142, identifier:append; 143, argument_list; 143, 144; 144, identifier:val; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:thr_dict; 148, dictionary; 149, for_statement; 149, 150; 149, 151; 149, 156; 150, identifier:n_bins; 151, call; 151, 152; 151, 153; 152, identifier:range; 153, argument_list; 153, 154; 153, 155; 154, integer:3; 155, integer:10; 156, block; 156, 157; 156, 178; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:bin_thr; 160, subscript; 160, 161; 160, 175; 161, subscript; 161, 162; 161, 174; 162, call; 162, 163; 162, 166; 163, attribute; 163, 164; 163, 165; 164, identifier:np; 165, identifier:histogram; 166, argument_list; 166, 167; 166, 173; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:log10; 171, argument_list; 171, 172; 172, identifier:exp_values; 173, identifier:n_bins; 174, integer:1; 175, slice; 175, 176; 175, 177; 176, integer:1; 177, colon; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 183; 180, subscript; 180, 181; 180, 182; 181, identifier:thr_dict; 182, identifier:n_bins; 183, identifier:bin_thr; 184, comment; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:binned_dict; 188, dictionary_comprehension; 188, 189; 188, 195; 189, pair; 189, 190; 189, 191; 190, identifier:x; 191, call; 191, 192; 191, 193; 192, identifier:deepcopy; 193, argument_list; 193, 194; 194, identifier:expression_dict; 195, for_in_clause; 195, 196; 195, 197; 196, identifier:x; 197, call; 197, 198; 197, 199; 198, identifier:range; 199, argument_list; 199, 200; 199, 201; 200, integer:3; 201, integer:10; 202, for_statement; 202, 203; 202, 204; 202, 205; 203, identifier:n_bins; 204, identifier:binned_dict; 205, block; 205, 206; 206, for_statement; 206, 207; 206, 208; 206, 211; 207, identifier:line; 208, subscript; 208, 209; 208, 210; 209, identifier:binned_dict; 210, identifier:n_bins; 211, block; 211, 212; 212, for_statement; 212, 213; 212, 214; 212, 219; 212, 220; 213, identifier:gene; 214, subscript; 214, 215; 214, 218; 215, subscript; 215, 216; 215, 217; 216, identifier:binned_dict; 217, identifier:n_bins; 218, identifier:line; 219, comment; 220, block; 220, 221; 221, if_statement; 221, 222; 221, 231; 221, 242; 222, comparison_operator:is; 222, 223; 222, 230; 223, subscript; 223, 224; 223, 229; 224, subscript; 224, 225; 224, 228; 225, subscript; 225, 226; 225, 227; 226, identifier:binned_dict; 227, identifier:n_bins; 228, identifier:line; 229, identifier:gene; 230, None; 231, block; 231, 232; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 241; 234, subscript; 234, 235; 234, 240; 235, subscript; 235, 236; 235, 239; 236, subscript; 236, 237; 236, 238; 237, identifier:binned_dict; 238, identifier:n_bins; 239, identifier:line; 240, identifier:gene; 241, identifier:n_bins; 242, else_clause; 242, 243; 243, block; 243, 244; 243, 259; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:val; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:np; 250, identifier:log10; 251, argument_list; 251, 252; 252, subscript; 252, 253; 252, 258; 253, subscript; 253, 254; 253, 257; 254, subscript; 254, 255; 254, 256; 255, identifier:binned_dict; 256, identifier:n_bins; 257, identifier:line; 258, identifier:gene; 259, for_statement; 259, 260; 259, 263; 259, 269; 260, pattern_list; 260, 261; 260, 262; 261, identifier:thr_idx; 262, identifier:thr; 263, call; 263, 264; 263, 265; 264, identifier:enumerate; 265, argument_list; 265, 266; 266, subscript; 266, 267; 266, 268; 267, identifier:thr_dict; 268, identifier:n_bins; 269, block; 269, 270; 270, if_statement; 270, 271; 270, 274; 271, comparison_operator:<=; 271, 272; 271, 273; 272, identifier:val; 273, identifier:thr; 274, block; 274, 275; 274, 285; 275, expression_statement; 275, 276; 276, assignment; 276, 277; 276, 284; 277, subscript; 277, 278; 277, 283; 278, subscript; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:binned_dict; 281, identifier:n_bins; 282, identifier:line; 283, identifier:gene; 284, identifier:thr_idx; 285, break_statement; 286, return_statement; 286, 287; 287, identifier:binned_dict; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:binned_exp; 291, call; 291, 292; 291, 293; 292, identifier:bin_exp; 293, argument_list; 293, 294; 294, identifier:exp_values; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:context; 298, dictionary; 298, 299; 298, 302; 299, pair; 299, 300; 299, 301; 300, string:'bin_expression'; 301, identifier:binned_exp; 302, pair; 302, 303; 302, 304; 303, string:'mutation'; 304, identifier:muts; 305, expression_statement; 305, 306; 306, assignment; 306, 307; 306, 312; 307, subscript; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:self; 310, identifier:_context; 311, string:'CCLE'; 312, identifier:context | def set_CCLE_context(self, cell_types):
"""Set context of all nodes and node members from CCLE."""
self.get_gene_names()
# Get expression and mutations from context client
exp_values = \
context_client.get_protein_expression(self._gene_names, cell_types)
mut_values = \
context_client.get_mutations(self._gene_names, cell_types)
# Make a dict of presence/absence of mutations
muts = {cell_line: {} for cell_line in cell_types}
for cell_line, entries in mut_values.items():
if entries is not None:
for gene, mutations in entries.items():
if mutations:
muts[cell_line][gene] = 1
else:
muts[cell_line][gene] = 0
# Create bins for the exp values
# because colorbrewer only does 3-9 bins and I don't feel like
# reinventing color scheme theory, this will only bin 3-9 bins
def bin_exp(expression_dict):
d = expression_dict
exp_values = []
for line in d:
for gene in d[line]:
val = d[line][gene]
if val is not None:
exp_values.append(val)
thr_dict = {}
for n_bins in range(3, 10):
bin_thr = np.histogram(np.log10(exp_values), n_bins)[1][1:]
thr_dict[n_bins] = bin_thr
# this dict isn't yet binned, that happens in the loop
binned_dict = {x: deepcopy(expression_dict) for x in range(3, 10)}
for n_bins in binned_dict:
for line in binned_dict[n_bins]:
for gene in binned_dict[n_bins][line]:
# last bin is reserved for None
if binned_dict[n_bins][line][gene] is None:
binned_dict[n_bins][line][gene] = n_bins
else:
val = np.log10(binned_dict[n_bins][line][gene])
for thr_idx, thr in enumerate(thr_dict[n_bins]):
if val <= thr:
binned_dict[n_bins][line][gene] = thr_idx
break
return binned_dict
binned_exp = bin_exp(exp_values)
context = {'bin_expression': binned_exp,
'mutation': muts}
self._context['CCLE'] = context |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_node_key; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:node_dict_item; 6, block; 6, 7; 6, 9; 6, 21; 6, 33; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:s; 12, call; 12, 13; 12, 14; 13, identifier:tuple; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:sorted; 17, argument_list; 17, 18; 18, subscript; 18, 19; 18, 20; 19, identifier:node_dict_item; 20, string:'sources'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:t; 24, call; 24, 25; 24, 26; 25, identifier:tuple; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:sorted; 29, argument_list; 29, 30; 30, subscript; 30, 31; 30, 32; 31, identifier:node_dict_item; 32, string:'targets'; 33, return_statement; 33, 34; 34, tuple; 34, 35; 34, 36; 35, identifier:s; 36, identifier:t | def _get_node_key(self, node_dict_item):
"""Return a tuple of sorted sources and targets given a node dict."""
s = tuple(sorted(node_dict_item['sources']))
t = tuple(sorted(node_dict_item['targets']))
return (s, t) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_get_node_groups; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 30; 5, 106; 5, 107; 5, 117; 5, 145; 5, 146; 5, 166; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:node_dict; 11, dictionary_comprehension; 11, 12; 11, 25; 12, pair; 12, 13; 12, 18; 13, subscript; 13, 14; 13, 17; 14, subscript; 14, 15; 14, 16; 15, identifier:node; 16, string:'data'; 17, string:'id'; 18, dictionary; 18, 19; 18, 22; 19, pair; 19, 20; 19, 21; 20, string:'sources'; 21, list:[]; 22, pair; 22, 23; 22, 24; 23, string:'targets'; 24, list:[]; 25, for_in_clause; 25, 26; 25, 27; 26, identifier:node; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_nodes; 30, for_statement; 30, 31; 30, 32; 30, 35; 30, 36; 31, identifier:edge; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_edges; 35, comment; 36, block; 36, 37; 36, 56; 36, 71; 36, 72; 36, 91; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:edge_data; 40, tuple; 40, 41; 40, 46; 40, 51; 41, subscript; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:edge; 44, string:'data'; 45, string:'i'; 46, subscript; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:edge; 49, string:'data'; 50, string:'polarity'; 51, subscript; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:edge; 54, string:'data'; 55, string:'source'; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 69; 58, attribute; 58, 59; 58, 68; 59, subscript; 59, 60; 59, 67; 60, subscript; 60, 61; 60, 62; 61, identifier:node_dict; 62, subscript; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:edge; 65, string:'data'; 66, string:'target'; 67, string:'sources'; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:edge_data; 71, comment; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:edge_data; 75, tuple; 75, 76; 75, 81; 75, 86; 76, subscript; 76, 77; 76, 80; 77, subscript; 77, 78; 77, 79; 78, identifier:edge; 79, string:'data'; 80, string:'i'; 81, subscript; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:edge; 84, string:'data'; 85, string:'polarity'; 86, subscript; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:edge; 89, string:'data'; 90, string:'target'; 91, expression_statement; 91, 92; 92, call; 92, 93; 92, 104; 93, attribute; 93, 94; 93, 103; 94, subscript; 94, 95; 94, 102; 95, subscript; 95, 96; 95, 97; 96, identifier:node_dict; 97, subscript; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:edge; 100, string:'data'; 101, string:'source'; 102, string:'targets'; 103, identifier:append; 104, argument_list; 104, 105; 105, identifier:edge_data; 106, comment; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:node_key_dict; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:collections; 113, identifier:defaultdict; 114, argument_list; 114, 115; 115, lambda; 115, 116; 116, list:[]; 117, for_statement; 117, 118; 117, 121; 117, 126; 118, pattern_list; 118, 119; 118, 120; 119, identifier:node_id; 120, identifier:node_d; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:node_dict; 124, identifier:items; 125, argument_list; 126, block; 126, 127; 126, 136; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:key; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:self; 133, identifier:_get_node_key; 134, argument_list; 134, 135; 135, identifier:node_d; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:node_key_dict; 141, identifier:key; 142, identifier:append; 143, argument_list; 143, 144; 144, identifier:node_id; 145, comment; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:node_groups; 149, list_comprehension; 149, 150; 149, 151; 149, 158; 150, identifier:g; 151, for_in_clause; 151, 152; 151, 153; 152, identifier:g; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:node_key_dict; 156, identifier:values; 157, argument_list; 158, if_clause; 158, 159; 159, parenthesized_expression; 159, 160; 160, comparison_operator:>; 160, 161; 160, 165; 161, call; 161, 162; 161, 163; 162, identifier:len; 163, argument_list; 163, 164; 164, identifier:g; 165, integer:1; 166, return_statement; 166, 167; 167, identifier:node_groups | def _get_node_groups(self):
"""Return a list of node id lists that are topologically identical.
First construct a node_dict which is keyed to the node id and
has a value which is a dict with keys 'sources' and 'targets'.
The 'sources' and 'targets' each contain a list of tuples
(i, polarity, source) edge of the node. node_dict is then processed
by _get_node_key() which returns a tuple of (s,t) where s,t are
sorted tuples of the ids for the source and target nodes. (s,t) is
then used as a key in node_key_dict where the values are the node
ids. node_groups is restricted to groups greater than 1 node.
"""
node_dict = {node['data']['id']: {'sources': [], 'targets': []}
for node in self._nodes}
for edge in self._edges:
# Add edge as a source for its target node
edge_data = (edge['data']['i'], edge['data']['polarity'],
edge['data']['source'])
node_dict[edge['data']['target']]['sources'].append(edge_data)
# Add edge as target for its source node
edge_data = (edge['data']['i'], edge['data']['polarity'],
edge['data']['target'])
node_dict[edge['data']['source']]['targets'].append(edge_data)
# Make a dictionary of nodes based on source/target as a key
node_key_dict = collections.defaultdict(lambda: [])
for node_id, node_d in node_dict.items():
key = self._get_node_key(node_d)
node_key_dict[key].append(node_id)
# Constrain the groups to ones that have more than 1 member
node_groups = [g for g in node_key_dict.values() if (len(g) > 1)]
return node_groups |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_group_edges; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 15; 5, 16; 5, 245; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:edges_to_add; 12, list:[[], []]; 12, 13; 12, 14; 13, list:[]; 14, list:[]; 15, comment; 16, for_statement; 16, 17; 16, 18; 16, 21; 17, identifier:e; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_edges; 21, block; 21, 22; 21, 29; 21, 39; 21, 51; 21, 52; 21, 53; 21, 54; 21, 62; 21, 70; 21, 90; 21, 110; 21, 111; 21, 112; 21, 141; 21, 142; 21, 143; 21, 172; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:new_edge; 25, call; 25, 26; 25, 27; 26, identifier:deepcopy; 27, argument_list; 27, 28; 28, identifier:e; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:new_edge; 34, string:'data'; 35, identifier:pop; 36, argument_list; 36, 37; 36, 38; 37, string:'id'; 38, None; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:uuid_list; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:new_edge; 46, string:'data'; 47, identifier:pop; 48, argument_list; 48, 49; 48, 50; 49, string:'uuid_list'; 50, list:[]; 51, comment; 52, comment; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:source; 57, subscript; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:e; 60, string:'data'; 61, string:'source'; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:target; 65, subscript; 65, 66; 65, 69; 66, subscript; 66, 67; 66, 68; 67, identifier:e; 68, string:'data'; 69, string:'target'; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:source_node; 73, subscript; 73, 74; 73, 89; 74, list_comprehension; 74, 75; 74, 76; 74, 81; 75, identifier:x; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:x; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_nodes; 81, if_clause; 81, 82; 82, comparison_operator:==; 82, 83; 82, 88; 83, subscript; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:x; 86, string:'data'; 87, string:'id'; 88, identifier:source; 89, integer:0; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:target_node; 93, subscript; 93, 94; 93, 109; 94, list_comprehension; 94, 95; 94, 96; 94, 101; 95, identifier:x; 96, for_in_clause; 96, 97; 96, 98; 97, identifier:x; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_nodes; 101, if_clause; 101, 102; 102, comparison_operator:==; 102, 103; 102, 108; 103, subscript; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:x; 106, string:'data'; 107, string:'id'; 108, identifier:target; 109, integer:0; 110, comment; 111, comment; 112, if_statement; 112, 113; 112, 120; 113, comparison_operator:!=; 113, 114; 113, 119; 114, subscript; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:source_node; 117, string:'data'; 118, string:'parent'; 119, string:''; 120, block; 120, 121; 120, 133; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 128; 123, subscript; 123, 124; 123, 127; 124, subscript; 124, 125; 124, 126; 125, identifier:new_edge; 126, string:'data'; 127, string:'source'; 128, subscript; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:source_node; 131, string:'data'; 132, string:'parent'; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 140; 135, subscript; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:e; 138, string:'data'; 139, string:'i'; 140, string:'Virtual'; 141, comment; 142, comment; 143, if_statement; 143, 144; 143, 151; 144, comparison_operator:!=; 144, 145; 144, 150; 145, subscript; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:target_node; 148, string:'data'; 149, string:'parent'; 150, string:''; 151, block; 151, 152; 151, 164; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 159; 154, subscript; 154, 155; 154, 158; 155, subscript; 155, 156; 155, 157; 156, identifier:new_edge; 157, string:'data'; 158, string:'target'; 159, subscript; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:target_node; 162, string:'data'; 163, string:'parent'; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 171; 166, subscript; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:e; 169, string:'data'; 170, string:'i'; 171, string:'Virtual'; 172, if_statement; 172, 173; 172, 180; 173, comparison_operator:==; 173, 174; 173, 179; 174, subscript; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:e; 177, string:'data'; 178, string:'i'; 179, string:'Virtual'; 180, block; 180, 181; 181, if_statement; 181, 182; 181, 187; 181, 206; 182, comparison_operator:not; 182, 183; 182, 184; 183, identifier:new_edge; 184, subscript; 184, 185; 184, 186; 185, identifier:edges_to_add; 186, integer:0; 187, block; 187, 188; 187, 197; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 195; 190, attribute; 190, 191; 190, 194; 191, subscript; 191, 192; 191, 193; 192, identifier:edges_to_add; 193, integer:0; 194, identifier:append; 195, argument_list; 195, 196; 196, identifier:new_edge; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 204; 199, attribute; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:edges_to_add; 202, integer:1; 203, identifier:append; 204, argument_list; 204, 205; 205, identifier:uuid_list; 206, else_clause; 206, 207; 207, block; 207, 208; 207, 219; 207, 227; 208, expression_statement; 208, 209; 209, assignment; 209, 210; 209, 211; 210, identifier:idx; 211, call; 211, 212; 211, 217; 212, attribute; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:edges_to_add; 215, integer:0; 216, identifier:index; 217, argument_list; 217, 218; 218, identifier:new_edge; 219, expression_statement; 219, 220; 220, augmented_assignment:+=; 220, 221; 220, 226; 221, subscript; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:edges_to_add; 224, integer:1; 225, identifier:idx; 226, identifier:uuid_list; 227, expression_statement; 227, 228; 228, assignment; 228, 229; 228, 234; 229, subscript; 229, 230; 229, 233; 230, subscript; 230, 231; 230, 232; 231, identifier:edges_to_add; 232, integer:1; 233, identifier:idx; 234, call; 234, 235; 234, 236; 235, identifier:list; 236, argument_list; 236, 237; 237, call; 237, 238; 237, 239; 238, identifier:set; 239, argument_list; 239, 240; 240, subscript; 240, 241; 240, 244; 241, subscript; 241, 242; 241, 243; 242, identifier:edges_to_add; 243, integer:1; 244, identifier:idx; 245, for_statement; 245, 246; 245, 247; 245, 252; 246, identifier:ze; 247, call; 247, 248; 247, 249; 248, identifier:zip; 249, argument_list; 249, 250; 250, list_splat; 250, 251; 251, identifier:edges_to_add; 252, block; 252, 253; 252, 259; 252, 271; 252, 281; 253, expression_statement; 253, 254; 254, assignment; 254, 255; 254, 256; 255, identifier:edge; 256, subscript; 256, 257; 256, 258; 257, identifier:ze; 258, integer:0; 259, expression_statement; 259, 260; 260, assignment; 260, 261; 260, 266; 261, subscript; 261, 262; 261, 265; 262, subscript; 262, 263; 262, 264; 263, identifier:edge; 264, string:'data'; 265, string:'id'; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:self; 269, identifier:_get_new_id; 270, argument_list; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 278; 273, subscript; 273, 274; 273, 277; 274, subscript; 274, 275; 274, 276; 275, identifier:edge; 276, string:'data'; 277, string:'uuid_list'; 278, subscript; 278, 279; 278, 280; 279, identifier:ze; 280, integer:1; 281, expression_statement; 281, 282; 282, call; 282, 283; 282, 288; 283, attribute; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:self; 286, identifier:_edges; 287, identifier:append; 288, argument_list; 288, 289; 289, identifier:edge | def _group_edges(self):
"""Group all edges that are topologically identical.
This means that (i, source, target, polarity) are the same, then sets
edges on parent (i.e. - group) nodes to 'Virtual' and creates a new
edge to represent all of them.
"""
# edit edges on parent nodes and make new edges for them
edges_to_add = [[], []] # [group_edges, uuid_lists]
for e in self._edges:
new_edge = deepcopy(e)
new_edge['data'].pop('id', None)
uuid_list = new_edge['data'].pop('uuid_list', [])
# Check if edge source or target are contained in a parent
# If source or target in parent edit edge
# Nodes may only point within their container
source = e['data']['source']
target = e['data']['target']
source_node = [x for x in self._nodes if
x['data']['id'] == source][0]
target_node = [x for x in self._nodes if
x['data']['id'] == target][0]
# If the source node is in a group, we change the source of this
# edge to the group
if source_node['data']['parent'] != '':
new_edge['data']['source'] = source_node['data']['parent']
e['data']['i'] = 'Virtual'
# If the targete node is in a group, we change the target of this
# edge to the group
if target_node['data']['parent'] != '':
new_edge['data']['target'] = target_node['data']['parent']
e['data']['i'] = 'Virtual'
if e['data']['i'] == 'Virtual':
if new_edge not in edges_to_add[0]:
edges_to_add[0].append(new_edge)
edges_to_add[1].append(uuid_list)
else:
idx = edges_to_add[0].index(new_edge)
edges_to_add[1][idx] += uuid_list
edges_to_add[1][idx] = list(set(edges_to_add[1][idx]))
for ze in zip(*edges_to_add):
edge = ze[0]
edge['data']['id'] = self._get_new_id()
edge['data']['uuid_list'] = ze[1]
self._edges.append(edge) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 40; 2, function_name:get_statements; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 3, 34; 3, 37; 4, default_parameter; 4, 5; 4, 6; 5, identifier:subject; 6, None; 7, default_parameter; 7, 8; 7, 9; 8, identifier:object; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:agents; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:stmt_type; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:use_exact_type; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:persist; 21, True; 22, default_parameter; 22, 23; 22, 24; 23, identifier:timeout; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:simple_response; 27, False; 28, default_parameter; 28, 29; 28, 30; 29, identifier:ev_limit; 30, integer:10; 31, default_parameter; 31, 32; 31, 33; 32, identifier:best_first; 33, True; 34, default_parameter; 34, 35; 34, 36; 35, identifier:tries; 36, integer:2; 37, default_parameter; 37, 38; 37, 39; 38, identifier:max_stmts; 39, None; 40, block; 40, 41; 40, 43; 40, 60; 40, 61; 40, 76; 41, expression_statement; 41, 42; 42, comment; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:processor; 46, call; 46, 47; 46, 48; 47, identifier:IndraDBRestProcessor; 48, argument_list; 48, 49; 48, 50; 48, 51; 48, 52; 48, 53; 48, 54; 48, 55; 48, 56; 48, 57; 48, 58; 48, 59; 49, identifier:subject; 50, identifier:object; 51, identifier:agents; 52, identifier:stmt_type; 53, identifier:use_exact_type; 54, identifier:persist; 55, identifier:timeout; 56, identifier:ev_limit; 57, identifier:best_first; 58, identifier:tries; 59, identifier:max_stmts; 60, comment; 61, if_statement; 61, 62; 61, 63; 61, 70; 62, identifier:simple_response; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:ret; 67, attribute; 67, 68; 67, 69; 68, identifier:processor; 69, identifier:statements; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:ret; 75, identifier:processor; 76, return_statement; 76, 77; 77, identifier:ret | def get_statements(subject=None, object=None, agents=None, stmt_type=None,
use_exact_type=False, persist=True, timeout=None,
simple_response=False, ev_limit=10, best_first=True, tries=2,
max_stmts=None):
"""Get a processor for the INDRA DB web API matching given agents and type.
There are two types of responses available. You can just get a list of
INDRA Statements, or you can get an IndraDBRestProcessor object, which allow
Statements to be loaded in a background thread, providing a sample of the
best* content available promptly in the sample_statements attribute, and
populates the statements attribute when the paged load is complete.
The latter should be used in all new code, and where convenient the prior
should be converted to use the processor, as this option may be removed in
the future.
* In the sense of having the most supporting evidence.
Parameters
----------
subject/object : str
Optionally specify the subject and/or object of the statements in
you wish to get from the database. By default, the namespace is assumed
to be HGNC gene names, however you may specify another namespace by
including `@<namespace>` at the end of the name string. For example, if
you want to specify an agent by chebi, you could use `CHEBI:6801@CHEBI`,
or if you wanted to use the HGNC id, you could use `6871@HGNC`.
agents : list[str]
A list of agents, specified in the same manner as subject and object,
but without specifying their grammatical position.
stmt_type : str
Specify the types of interactions you are interested in, as indicated
by the sub-classes of INDRA's Statements. This argument is *not* case
sensitive. If the statement class given has sub-classes
(e.g. RegulateAmount has IncreaseAmount and DecreaseAmount), then both
the class itself, and its subclasses, will be queried, by default. If
you do not want this behavior, set use_exact_type=True. Note that if
max_stmts is set, it is possible only the exact statement type will
be returned, as this is the first searched. The processor then cycles
through the types, getting a page of results for each type and adding it
to the quota, until the max number of statements is reached.
use_exact_type : bool
If stmt_type is given, and you only want to search for that specific
statement type, set this to True. Default is False.
persist : bool
Default is True. When False, if a query comes back limited (not all
results returned), just give up and pass along what was returned.
Otherwise, make further queries to get the rest of the data (which may
take some time).
timeout : positive int or None
If an int, block until the work is done and statements are retrieved, or
until the timeout has expired, in which case the results so far will be
returned in the response object, and further results will be added in
a separate thread as they become available. If simple_response is True,
all statements available will be returned. Otherwise (if None), block
indefinitely until all statements are retrieved. Default is None.
simple_response : bool
If True, a simple list of statements is returned (thus block should also
be True). If block is False, only the original sample will be returned
(as though persist was False), until the statements are done loading, in
which case the rest should appear in the list. This behavior is not
encouraged. Default is False (which breaks backwards compatibility with
usage of INDRA versions from before 1/22/2019). WE ENCOURAGE ALL NEW
USE-CASES TO USE THE PROCESSOR, AS THIS FEATURE MAY BE REMOVED AT A
LATER DATE.
ev_limit : int or None
Limit the amount of evidence returned per Statement. Default is 10.
best_first : bool
If True, the preassembled statements will be sorted by the amount of
evidence they have, and those with the most evidence will be
prioritized. When using `max_stmts`, this means you will get the "best"
statements. If False, statements will be queried in arbitrary order.
tries : int > 0
Set the number of times to try the query. The database often caches
results, so if a query times out the first time, trying again after a
timeout will often succeed fast enough to avoid a timeout. This can also
help gracefully handle an unreliable connection, if you're willing to
wait. Default is 2.
max_stmts : int or None
Select the maximum number of statements to return. When set less than
1000 the effect is much the same as setting persist to false, and will
guarantee a faster response. Default is None.
Returns
-------
processor : :py:class:`IndraDBRestProcessor`
An instance of the IndraDBRestProcessor, which has an attribute
`statements` which will be populated when the query/queries are done.
This is the default behavior, and is encouraged in all future cases,
however a simple list of statements may be returned using the
`simple_response` option described above.
"""
processor = IndraDBRestProcessor(subject, object, agents, stmt_type,
use_exact_type, persist, timeout,
ev_limit, best_first, tries, max_stmts)
# Format the result appropriately.
if simple_response:
ret = processor.statements
else:
ret = processor
return ret |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_statements_by_hash; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:hash_list; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ev_limit; 7, integer:100; 8, default_parameter; 8, 9; 8, 10; 9, identifier:best_first; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:tries; 13, integer:2; 14, block; 14, 15; 14, 17; 14, 35; 14, 41; 14, 61; 14, 83; 14, 106; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 24; 18, not_operator; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:isinstance; 21, argument_list; 21, 22; 21, 23; 22, identifier:hash_list; 23, identifier:list; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:ValueError; 28, argument_list; 28, 29; 29, binary_operator:%; 29, 30; 29, 31; 30, string:"The `hash_list` input is a list, not %s."; 31, call; 31, 32; 31, 33; 32, identifier:type; 33, argument_list; 33, 34; 34, identifier:hash_list; 35, if_statement; 35, 36; 35, 38; 36, not_operator; 36, 37; 37, identifier:hash_list; 38, block; 38, 39; 39, return_statement; 39, 40; 40, list:[]; 41, if_statement; 41, 42; 41, 49; 42, call; 42, 43; 42, 44; 43, identifier:isinstance; 44, argument_list; 44, 45; 44, 48; 45, subscript; 45, 46; 45, 47; 46, identifier:hash_list; 47, integer:0; 48, identifier:str; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:hash_list; 53, list_comprehension; 53, 54; 53, 58; 54, call; 54, 55; 54, 56; 55, identifier:int; 56, argument_list; 56, 57; 57, identifier:h; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:h; 60, identifier:hash_list; 61, if_statement; 61, 62; 61, 75; 62, not_operator; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:all; 65, argument_list; 65, 66; 66, list_comprehension; 66, 67; 66, 72; 67, call; 67, 68; 67, 69; 68, identifier:isinstance; 69, argument_list; 69, 70; 69, 71; 70, identifier:h; 71, identifier:int; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:h; 74, identifier:hash_list; 75, block; 75, 76; 76, raise_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:ValueError; 79, argument_list; 79, 80; 80, concatenated_string; 80, 81; 80, 82; 81, string:"Hashes must be ints or strings that can be "; 82, string:"converted into ints."; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:resp; 86, call; 86, 87; 86, 88; 87, identifier:submit_statement_request; 88, argument_list; 88, 89; 88, 90; 88, 91; 88, 94; 88, 100; 88, 103; 89, string:'post'; 90, string:'from_hashes'; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:ev_limit; 93, identifier:ev_limit; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:data; 96, dictionary; 96, 97; 97, pair; 97, 98; 97, 99; 98, string:'hashes'; 99, identifier:hash_list; 100, keyword_argument; 100, 101; 100, 102; 101, identifier:best_first; 102, identifier:best_first; 103, keyword_argument; 103, 104; 103, 105; 104, identifier:tries; 105, identifier:tries; 106, return_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:stmts_from_json; 109, argument_list; 109, 110; 110, call; 110, 111; 110, 120; 111, attribute; 111, 112; 111, 119; 112, subscript; 112, 113; 112, 118; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:resp; 116, identifier:json; 117, argument_list; 118, string:'statements'; 119, identifier:values; 120, argument_list | def get_statements_by_hash(hash_list, ev_limit=100, best_first=True, tries=2):
"""Get fully formed statements from a list of hashes.
Parameters
----------
hash_list : list[int or str]
A list of statement hashes.
ev_limit : int or None
Limit the amount of evidence returned per Statement. Default is 100.
best_first : bool
If True, the preassembled statements will be sorted by the amount of
evidence they have, and those with the most evidence will be
prioritized. When using `max_stmts`, this means you will get the "best"
statements. If False, statements will be queried in arbitrary order.
tries : int > 0
Set the number of times to try the query. The database often caches
results, so if a query times out the first time, trying again after a
timeout will often succeed fast enough to avoid a timeout. This can
also help gracefully handle an unreliable connection, if you're
willing to wait. Default is 2.
"""
if not isinstance(hash_list, list):
raise ValueError("The `hash_list` input is a list, not %s."
% type(hash_list))
if not hash_list:
return []
if isinstance(hash_list[0], str):
hash_list = [int(h) for h in hash_list]
if not all([isinstance(h, int) for h in hash_list]):
raise ValueError("Hashes must be ints or strings that can be "
"converted into ints.")
resp = submit_statement_request('post', 'from_hashes', ev_limit=ev_limit,
data={'hashes': hash_list},
best_first=best_first, tries=tries)
return stmts_from_json(resp.json()['statements'].values()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:get_statements_for_paper; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:ids; 5, default_parameter; 5, 6; 5, 7; 6, identifier:ev_limit; 7, integer:10; 8, default_parameter; 8, 9; 8, 10; 9, identifier:best_first; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:tries; 13, integer:2; 14, default_parameter; 14, 15; 14, 16; 15, identifier:max_stmts; 16, None; 17, block; 17, 18; 17, 20; 17, 36; 17, 62; 17, 72; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:id_l; 23, list_comprehension; 23, 24; 23, 31; 24, dictionary; 24, 25; 24, 28; 25, pair; 25, 26; 25, 27; 26, string:'id'; 27, identifier:id_val; 28, pair; 28, 29; 28, 30; 29, string:'type'; 30, identifier:id_type; 31, for_in_clause; 31, 32; 31, 35; 32, pattern_list; 32, 33; 32, 34; 33, identifier:id_type; 34, identifier:id_val; 35, identifier:ids; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:resp; 39, call; 39, 40; 39, 41; 40, identifier:submit_statement_request; 41, argument_list; 41, 42; 41, 43; 41, 44; 41, 50; 41, 53; 41, 56; 41, 59; 42, string:'post'; 43, string:'from_papers'; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:data; 46, dictionary; 46, 47; 47, pair; 47, 48; 47, 49; 48, string:'ids'; 49, identifier:id_l; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:ev_limit; 52, identifier:ev_limit; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:best_first; 55, identifier:best_first; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:tries; 58, identifier:tries; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:max_stmts; 61, identifier:max_stmts; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:stmts_json; 65, subscript; 65, 66; 65, 71; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:resp; 69, identifier:json; 70, argument_list; 71, string:'statements'; 72, return_statement; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:stmts_from_json; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:stmts_json; 79, identifier:values; 80, argument_list | def get_statements_for_paper(ids, ev_limit=10, best_first=True, tries=2,
max_stmts=None):
"""Get the set of raw Statements extracted from a paper given by the id.
Parameters
----------
ids : list[(<id type>, <id value>)]
A list of tuples with ids and their type. The type can be any one of
'pmid', 'pmcid', 'doi', 'pii', 'manuscript id', or 'trid', which is the
primary key id of the text references in the database.
ev_limit : int or None
Limit the amount of evidence returned per Statement. Default is 10.
best_first : bool
If True, the preassembled statements will be sorted by the amount of
evidence they have, and those with the most evidence will be
prioritized. When using `max_stmts`, this means you will get the "best"
statements. If False, statements will be queried in arbitrary order.
tries : int > 0
Set the number of times to try the query. The database often caches
results, so if a query times out the first time, trying again after a
timeout will often succeed fast enough to avoid a timeout. This can also
help gracefully handle an unreliable connection, if you're willing to
wait. Default is 2.
max_stmts : int or None
Select a maximum number of statements to be returned. Default is None.
Returns
-------
stmts : list[:py:class:`indra.statements.Statement`]
A list of INDRA Statement instances.
"""
id_l = [{'id': id_val, 'type': id_type} for id_type, id_val in ids]
resp = submit_statement_request('post', 'from_papers', data={'ids': id_l},
ev_limit=ev_limit, best_first=best_first,
tries=tries, max_stmts=max_stmts)
stmts_json = resp.json()['statements']
return stmts_from_json(stmts_json.values()) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_statement_queries; 3, parameters; 3, 4; 3, 5; 4, identifier:stmts; 5, dictionary_splat_pattern; 5, 6; 6, identifier:params; 7, block; 7, 8; 7, 10; 7, 63; 7, 67; 7, 74; 7, 81; 7, 213; 8, expression_statement; 8, 9; 9, comment; 10, function_definition; 10, 11; 10, 12; 10, 14; 11, function_name:pick_ns; 12, parameters; 12, 13; 13, identifier:ag; 14, block; 14, 15; 14, 57; 15, for_statement; 15, 16; 15, 17; 15, 24; 15, 45; 16, identifier:ns; 17, list:['HGNC', 'FPLX', 'CHEMBL', 'CHEBI', 'GO', 'MESH']; 17, 18; 17, 19; 17, 20; 17, 21; 17, 22; 17, 23; 18, string:'HGNC'; 19, string:'FPLX'; 20, string:'CHEMBL'; 21, string:'CHEBI'; 22, string:'GO'; 23, string:'MESH'; 24, block; 24, 25; 25, if_statement; 25, 26; 25, 35; 26, comparison_operator:in; 26, 27; 26, 28; 27, identifier:ns; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:ag; 32, identifier:db_refs; 33, identifier:keys; 34, argument_list; 35, block; 35, 36; 35, 44; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:dbid; 39, subscript; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:ag; 42, identifier:db_refs; 43, identifier:ns; 44, break_statement; 45, else_clause; 45, 46; 46, block; 46, 47; 46, 51; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:ns; 50, string:'TEXT'; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:dbid; 54, attribute; 54, 55; 54, 56; 55, identifier:ag; 56, identifier:name; 57, return_statement; 57, 58; 58, binary_operator:%; 58, 59; 58, 60; 59, string:'%s@%s'; 60, tuple; 60, 61; 60, 62; 61, identifier:dbid; 62, identifier:ns; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:queries; 66, list:[]; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:url_base; 70, call; 70, 71; 70, 72; 71, identifier:get_url_base; 72, argument_list; 72, 73; 73, string:'statements/from_agents'; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:non_binary_statements; 77, list:[Complex, SelfModification, ActiveForm]; 77, 78; 77, 79; 77, 80; 78, identifier:Complex; 79, identifier:SelfModification; 80, identifier:ActiveForm; 81, for_statement; 81, 82; 81, 83; 81, 84; 82, identifier:stmt; 83, identifier:stmts; 84, block; 84, 85; 84, 89; 84, 158; 84, 168; 84, 175; 84, 204; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:kwargs; 88, dictionary; 89, if_statement; 89, 90; 89, 96; 89, 127; 90, comparison_operator:not; 90, 91; 90, 95; 91, call; 91, 92; 91, 93; 92, identifier:type; 93, argument_list; 93, 94; 94, identifier:stmt; 95, identifier:non_binary_statements; 96, block; 96, 97; 97, for_statement; 97, 98; 97, 101; 97, 112; 98, pattern_list; 98, 99; 98, 100; 99, identifier:pos; 100, identifier:ag; 101, call; 101, 102; 101, 103; 102, identifier:zip; 103, argument_list; 103, 104; 103, 107; 104, list:['subject', 'object']; 104, 105; 104, 106; 105, string:'subject'; 106, string:'object'; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:stmt; 110, identifier:agent_list; 111, argument_list; 112, block; 112, 113; 113, if_statement; 113, 114; 113, 117; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:ag; 116, None; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:kwargs; 122, identifier:pos; 123, call; 123, 124; 123, 125; 124, identifier:pick_ns; 125, argument_list; 125, 126; 126, identifier:ag; 127, else_clause; 127, 128; 128, block; 128, 129; 129, for_statement; 129, 130; 129, 133; 129, 141; 130, pattern_list; 130, 131; 130, 132; 131, identifier:i; 132, identifier:ag; 133, call; 133, 134; 133, 135; 134, identifier:enumerate; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:stmt; 139, identifier:agent_list; 140, argument_list; 141, block; 141, 142; 142, if_statement; 142, 143; 142, 146; 143, comparison_operator:is; 143, 144; 143, 145; 144, identifier:ag; 145, None; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 154; 149, subscript; 149, 150; 149, 151; 150, identifier:kwargs; 151, binary_operator:%; 151, 152; 151, 153; 152, string:'agent%d'; 153, identifier:i; 154, call; 154, 155; 154, 156; 155, identifier:pick_ns; 156, argument_list; 156, 157; 157, identifier:ag; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 163; 160, subscript; 160, 161; 160, 162; 161, identifier:kwargs; 162, string:'type'; 163, attribute; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:stmt; 166, identifier:__class__; 167, identifier:__name__; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:kwargs; 172, identifier:update; 173, argument_list; 173, 174; 174, identifier:params; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:query_str; 178, binary_operator:+; 178, 179; 178, 180; 179, string:'?'; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, string:'&'; 183, identifier:join; 184, argument_list; 184, 185; 185, list_comprehension; 185, 186; 185, 191; 185, 200; 186, binary_operator:%; 186, 187; 186, 188; 187, string:'%s=%s'; 188, tuple; 188, 189; 188, 190; 189, identifier:k; 190, identifier:v; 191, for_in_clause; 191, 192; 191, 195; 192, pattern_list; 192, 193; 192, 194; 193, identifier:k; 194, identifier:v; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:kwargs; 198, identifier:items; 199, argument_list; 200, if_clause; 200, 201; 201, comparison_operator:is; 201, 202; 201, 203; 202, identifier:v; 203, None; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:queries; 208, identifier:append; 209, argument_list; 209, 210; 210, binary_operator:+; 210, 211; 210, 212; 211, identifier:url_base; 212, identifier:query_str; 213, return_statement; 213, 214; 214, identifier:queries | def get_statement_queries(stmts, **params):
"""Get queries used to search based on a statement.
In addition to the stmts, you can enter any parameters standard to the
query. See https://github.com/indralab/indra_db/rest_api for a full list.
Parameters
----------
stmts : list[Statement]
A list of INDRA statements.
"""
def pick_ns(ag):
for ns in ['HGNC', 'FPLX', 'CHEMBL', 'CHEBI', 'GO', 'MESH']:
if ns in ag.db_refs.keys():
dbid = ag.db_refs[ns]
break
else:
ns = 'TEXT'
dbid = ag.name
return '%s@%s' % (dbid, ns)
queries = []
url_base = get_url_base('statements/from_agents')
non_binary_statements = [Complex, SelfModification, ActiveForm]
for stmt in stmts:
kwargs = {}
if type(stmt) not in non_binary_statements:
for pos, ag in zip(['subject', 'object'], stmt.agent_list()):
if ag is not None:
kwargs[pos] = pick_ns(ag)
else:
for i, ag in enumerate(stmt.agent_list()):
if ag is not None:
kwargs['agent%d' % i] = pick_ns(ag)
kwargs['type'] = stmt.__class__.__name__
kwargs.update(params)
query_str = '?' + '&'.join(['%s=%s' % (k, v) for k, v in kwargs.items()
if v is not None])
queries.append(url_base + query_str)
return queries |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:make_statement; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:action; 6, identifier:mention; 7, block; 7, 8; 7, 10; 7, 25; 7, 33; 7, 34; 7, 35; 7, 36; 7, 37; 7, 38; 7, 39; 7, 43; 7, 111; 7, 112; 7, 118; 7, 124; 7, 132; 7, 140; 7, 141; 7, 169; 7, 170; 7, 171; 7, 172; 7, 173; 7, 185; 7, 197; 7, 198; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 11, 16; 12, tuple_pattern; 12, 13; 12, 14; 13, identifier:statement_generator; 14, identifier:is_direct; 15, line_continuation:\; 16, call; 16, 17; 16, 18; 17, identifier:geneways_action_to_indra_statement_type; 18, argument_list; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:mention; 21, identifier:actiontype; 22, attribute; 22, 23; 22, 24; 23, identifier:action; 24, identifier:plo; 25, if_statement; 25, 26; 25, 29; 25, 30; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:statement_generator; 28, None; 29, comment; 30, block; 30, 31; 31, return_statement; 31, 32; 32, None; 33, comment; 34, comment; 35, comment; 36, comment; 37, comment; 38, comment; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:text; 42, None; 43, if_statement; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:get_ft_mention; 47, block; 47, 48; 48, try_statement; 48, 49; 48, 97; 49, block; 49, 50; 49, 62; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 55; 52, pattern_list; 52, 53; 52, 54; 53, identifier:content; 54, identifier:content_type; 55, call; 55, 56; 55, 57; 56, identifier:get_full_text; 57, argument_list; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:mention; 60, identifier:pmid; 61, string:'pmid'; 62, if_statement; 62, 63; 62, 66; 63, comparison_operator:is; 63, 64; 63, 65; 64, identifier:content; 65, None; 66, block; 66, 67; 66, 75; 66, 83; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:ftm; 70, call; 70, 71; 70, 72; 71, identifier:FullTextMention; 72, argument_list; 72, 73; 72, 74; 73, identifier:mention; 74, identifier:content; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:sentences; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:ftm; 81, identifier:find_matching_sentences; 82, argument_list; 83, if_statement; 83, 84; 83, 90; 84, comparison_operator:==; 84, 85; 84, 89; 85, call; 85, 86; 85, 87; 86, identifier:len; 87, argument_list; 87, 88; 88, identifier:sentences; 89, integer:1; 90, block; 90, 91; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:text; 94, subscript; 94, 95; 94, 96; 95, identifier:sentences; 96, integer:0; 97, except_clause; 97, 98; 97, 99; 98, identifier:Exception; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:logger; 104, identifier:warning; 105, argument_list; 105, 106; 106, binary_operator:+; 106, 107; 106, 108; 107, string:'Could not fetch full text for PMID '; 108, attribute; 108, 109; 108, 110; 109, identifier:mention; 110, identifier:pmid; 111, comment; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:epistemics; 115, call; 115, 116; 115, 117; 116, identifier:dict; 117, argument_list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:epistemics; 122, string:'direct'; 123, identifier:is_direct; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:annotations; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:mention; 130, identifier:make_annotation; 131, argument_list; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 137; 134, subscript; 134, 135; 134, 136; 135, identifier:annotations; 136, string:'plo'; 137, attribute; 137, 138; 137, 139; 138, identifier:action; 139, identifier:plo; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:evidence; 144, call; 144, 145; 144, 146; 145, identifier:Evidence; 146, argument_list; 146, 147; 146, 150; 146, 155; 146, 160; 146, 163; 146, 166; 147, keyword_argument; 147, 148; 147, 149; 148, identifier:source_api; 149, string:'geneways'; 150, keyword_argument; 150, 151; 150, 152; 151, identifier:source_id; 152, attribute; 152, 153; 152, 154; 153, identifier:mention; 154, identifier:actionmentionid; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:pmid; 157, attribute; 157, 158; 157, 159; 158, identifier:mention; 159, identifier:pmid; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:text; 162, identifier:text; 163, keyword_argument; 163, 164; 163, 165; 164, identifier:epistemics; 165, identifier:epistemics; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:annotations; 168, identifier:annotations; 169, comment; 170, comment; 171, comment; 172, comment; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 176; 175, identifier:upstream_agent; 176, call; 176, 177; 176, 178; 177, identifier:get_agent; 178, argument_list; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:mention; 181, identifier:upstream; 182, attribute; 182, 183; 182, 184; 183, identifier:action; 184, identifier:up; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:downstream_agent; 188, call; 188, 189; 188, 190; 189, identifier:get_agent; 190, argument_list; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:mention; 193, identifier:downstream; 194, attribute; 194, 195; 194, 196; 195, identifier:action; 196, identifier:dn; 197, comment; 198, return_statement; 198, 199; 199, call; 199, 200; 199, 201; 200, identifier:statement_generator; 201, argument_list; 201, 202; 201, 203; 201, 204; 202, identifier:upstream_agent; 203, identifier:downstream_agent; 204, identifier:evidence | def make_statement(self, action, mention):
"""Makes an INDRA statement from a Geneways action and action mention.
Parameters
----------
action : GenewaysAction
The mechanism that the Geneways mention maps to. Note that
several text mentions can correspond to the same action if they are
referring to the same relationship - there may be multiple
Geneways action mentions corresponding to each action.
mention : GenewaysActionMention
The Geneways action mention object corresponding to a single
mention of a mechanism in a specific text. We make a new INDRA
statement corresponding to each action mention.
Returns
-------
statement : indra.statements.Statement
An INDRA statement corresponding to the provided Geneways action
mention, or None if the action mention's type does not map onto
any INDRA statement type in geneways_action_type_mapper.
"""
(statement_generator, is_direct) = \
geneways_action_to_indra_statement_type(mention.actiontype,
action.plo)
if statement_generator is None:
# Geneways statement does not map onto an indra statement
return None
# Try to find the full-text sentence
# Unfortunately, the sentence numbers in the Geneways dataset
# don't correspond to an obvious sentence segmentation.
# This code looks for sentences with the subject, object, and verb
# listed by the Geneways action mention table and only includes
# it in the evidence if there is exactly one such sentence
text = None
if self.get_ft_mention:
try:
content, content_type = get_full_text(mention.pmid, 'pmid')
if content is not None:
ftm = FullTextMention(mention, content)
sentences = ftm.find_matching_sentences()
if len(sentences) == 1:
text = sentences[0]
except Exception:
logger.warning('Could not fetch full text for PMID ' +
mention.pmid)
# Make an evidence object
epistemics = dict()
epistemics['direct'] = is_direct
annotations = mention.make_annotation()
annotations['plo'] = action.plo # plo only in action table
evidence = Evidence(source_api='geneways',
source_id=mention.actionmentionid,
pmid=mention.pmid, text=text,
epistemics=epistemics,
annotations=annotations)
# Construct the grounded and name standardized agents
# Note that this involves grounding the agent by
# converting the Entrez ID listed in the Geneways data with
# HGNC and UniProt
upstream_agent = get_agent(mention.upstream, action.up)
downstream_agent = get_agent(mention.downstream, action.dn)
# Make the statement
return statement_generator(upstream_agent, downstream_agent, evidence) |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:directly_or_indirectly_related; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, identifier:self; 5, identifier:ns1; 6, identifier:id1; 7, identifier:ns2; 8, identifier:id2; 9, identifier:closure_dict; 10, identifier:relation_func; 11, block; 11, 12; 11, 14; 11, 15; 11, 39; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, if_statement; 15, 16; 15, 28; 15, 31; 15, 32; 16, boolean_operator:or; 16, 17; 16, 20; 17, comparison_operator:is; 17, 18; 17, 19; 18, identifier:id2; 19, None; 20, parenthesized_expression; 20, 21; 21, boolean_operator:and; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:id2; 24, None; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:id1; 27, None; 28, block; 28, 29; 29, return_statement; 29, 30; 30, True; 31, comment; 32, elif_clause; 32, 33; 32, 36; 33, comparison_operator:is; 33, 34; 33, 35; 34, identifier:id1; 35, None; 36, block; 36, 37; 37, return_statement; 37, 38; 38, False; 39, if_statement; 39, 40; 39, 41; 39, 86; 40, identifier:closure_dict; 41, block; 41, 42; 41, 52; 41, 62; 41, 71; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:term1; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:get_uri; 49, argument_list; 49, 50; 49, 51; 50, identifier:ns1; 51, identifier:id1; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:term2; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:get_uri; 59, argument_list; 59, 60; 59, 61; 60, identifier:ns2; 61, identifier:id2; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:ec; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:closure_dict; 68, identifier:get; 69, argument_list; 69, 70; 70, identifier:term1; 71, if_statement; 71, 72; 71, 79; 71, 82; 72, boolean_operator:and; 72, 73; 72, 76; 73, comparison_operator:is; 73, 74; 73, 75; 74, identifier:ec; 75, None; 76, comparison_operator:in; 76, 77; 76, 78; 77, identifier:term2; 78, identifier:ec; 79, block; 79, 80; 80, return_statement; 80, 81; 81, True; 82, else_clause; 82, 83; 83, block; 83, 84; 84, return_statement; 84, 85; 85, False; 86, else_clause; 86, 87; 87, block; 87, 88; 87, 189; 87, 201; 88, if_statement; 88, 89; 88, 93; 88, 145; 89, not_operator; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:self; 92, identifier:uri_as_name; 93, block; 93, 94; 93, 103; 93, 112; 93, 123; 93, 134; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:e1; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:find_entity; 101, argument_list; 101, 102; 102, identifier:id1; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:e2; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:find_entity; 110, argument_list; 110, 111; 111, identifier:id2; 112, if_statement; 112, 113; 112, 120; 113, boolean_operator:or; 113, 114; 113, 117; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:e1; 116, None; 117, comparison_operator:is; 117, 118; 117, 119; 118, identifier:e2; 119, None; 120, block; 120, 121; 121, return_statement; 121, 122; 122, False; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:t1; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:rdflib; 130, identifier:term; 131, identifier:URIRef; 132, argument_list; 132, 133; 133, identifier:e1; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:t2; 137, call; 137, 138; 137, 143; 138, attribute; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:rdflib; 141, identifier:term; 142, identifier:URIRef; 143, argument_list; 143, 144; 144, identifier:e2; 145, else_clause; 145, 146; 146, block; 146, 147; 146, 157; 146, 167; 146, 178; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:u1; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:self; 153, identifier:get_uri; 154, argument_list; 154, 155; 154, 156; 155, identifier:ns1; 156, identifier:id1; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:u2; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:get_uri; 164, argument_list; 164, 165; 164, 166; 165, identifier:ns2; 166, identifier:id2; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:t1; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:rdflib; 174, identifier:term; 175, identifier:URIRef; 176, argument_list; 176, 177; 177, identifier:u1; 178, expression_statement; 178, 179; 179, assignment; 179, 180; 179, 181; 180, identifier:t2; 181, call; 181, 182; 181, 187; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:rdflib; 185, identifier:term; 186, identifier:URIRef; 187, argument_list; 187, 188; 188, identifier:u2; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:to; 192, call; 192, 193; 192, 198; 193, attribute; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:self; 196, identifier:graph; 197, identifier:transitiveClosure; 198, argument_list; 198, 199; 198, 200; 199, identifier:relation_func; 200, identifier:t1; 201, if_statement; 201, 202; 201, 205; 201, 208; 202, comparison_operator:in; 202, 203; 202, 204; 203, identifier:t2; 204, identifier:to; 205, block; 205, 206; 206, return_statement; 206, 207; 207, True; 208, else_clause; 208, 209; 209, block; 209, 210; 210, return_statement; 210, 211; 211, False | def directly_or_indirectly_related(self, ns1, id1, ns2, id2, closure_dict,
relation_func):
"""Return True if two entities have the speicified relationship.
This relation is constructed possibly through multiple links connecting
the two entities directly or indirectly.
Parameters
----------
ns1 : str
Namespace code for an entity.
id1 : str
URI for an entity.
ns2 : str
Namespace code for an entity.
id2 : str
URI for an entity.
closure_dict: dict
A dictionary mapping node names to nodes that have the
specified relationship, directly or indirectly. Empty if this
has not been precomputed.
relation_func: function
Function with arguments (node, graph) that generates objects
with some relationship with node on the given graph.
Returns
-------
bool
True if t1 has the specified relationship with t2, either
directly or through a series of intermediates; False otherwise.
"""
# if id2 is None, or both are None, then it's by definition isa:
if id2 is None or (id2 is None and id1 is None):
return True
# If only id1 is None, then it cannot be isa
elif id1 is None:
return False
if closure_dict:
term1 = self.get_uri(ns1, id1)
term2 = self.get_uri(ns2, id2)
ec = closure_dict.get(term1)
if ec is not None and term2 in ec:
return True
else:
return False
else:
if not self.uri_as_name:
e1 = self.find_entity(id1)
e2 = self.find_entity(id2)
if e1 is None or e2 is None:
return False
t1 = rdflib.term.URIRef(e1)
t2 = rdflib.term.URIRef(e2)
else:
u1 = self.get_uri(ns1, id1)
u2 = self.get_uri(ns2, id2)
t1 = rdflib.term.URIRef(u1)
t2 = rdflib.term.URIRef(u2)
to = self.graph.transitiveClosure(relation_func, t1)
if t2 in to:
return True
else:
return False |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:analyze_db_reading; 3, parameters; 3, 4; 3, 5; 4, identifier:job_prefix; 5, default_parameter; 5, 6; 5, 7; 6, identifier:reading_queue; 7, string:'run_db_reading_queue'; 8, block; 8, 9; 8, 11; 8, 12; 8, 20; 8, 24; 8, 28; 8, 32; 8, 69; 8, 70; 8, 84; 8, 102; 8, 112; 8, 122; 8, 123; 8, 244; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:log_strs; 15, call; 15, 16; 15, 17; 16, identifier:get_logs_from_db_reading; 17, argument_list; 17, 18; 17, 19; 18, identifier:job_prefix; 19, identifier:reading_queue; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:indra_log_strs; 23, list:[]; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:all_reach_logs; 27, list:[]; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:log_stats; 31, list:[]; 32, for_statement; 32, 33; 32, 34; 32, 35; 33, identifier:log_str; 34, identifier:log_strs; 35, block; 35, 36; 35, 45; 35, 52; 35, 59; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 41; 38, pattern_list; 38, 39; 38, 40; 39, identifier:log_str; 40, identifier:reach_logs; 41, call; 41, 42; 41, 43; 42, identifier:separate_reach_logs; 43, argument_list; 43, 44; 44, identifier:log_str; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:all_reach_logs; 49, identifier:extend; 50, argument_list; 50, 51; 51, identifier:reach_logs; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:indra_log_strs; 56, identifier:append; 57, argument_list; 57, 58; 58, identifier:log_str; 59, expression_statement; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:log_stats; 63, identifier:append; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:get_reading_stats; 67, argument_list; 67, 68; 68, identifier:log_str; 69, comment; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:failed_reach_logs; 73, list_comprehension; 73, 74; 73, 75; 73, 80; 74, identifier:reach_log_str; 75, for_in_clause; 75, 76; 75, 79; 76, pattern_list; 76, 77; 76, 78; 77, identifier:result; 78, identifier:reach_log_str; 79, identifier:all_reach_logs; 80, if_clause; 80, 81; 81, comparison_operator:==; 81, 82; 81, 83; 82, identifier:result; 83, string:'FAILURE'; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:failed_id_dicts; 87, list_comprehension; 87, 88; 87, 94; 87, 97; 88, call; 88, 89; 88, 90; 89, identifier:analyze_reach_log; 90, argument_list; 90, 91; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:log_str; 93, identifier:reach_log; 94, for_in_clause; 94, 95; 94, 96; 95, identifier:reach_log; 96, identifier:failed_reach_logs; 97, if_clause; 97, 98; 98, call; 98, 99; 98, 100; 99, identifier:bool; 100, argument_list; 100, 101; 101, identifier:reach_log; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:tcids_unfinished; 105, set_comprehension; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:id_dict; 108, string:'not_done'; 109, for_in_clause; 109, 110; 109, 111; 110, identifier:id_dict; 111, identifier:failed_id_dicts; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:print; 115, argument_list; 115, 116; 116, binary_operator:%; 116, 117; 116, 118; 117, string:"Found %d unfinished tcids."; 118, call; 118, 119; 118, 120; 119, identifier:len; 120, argument_list; 120, 121; 121, identifier:tcids_unfinished; 122, comment; 123, if_statement; 123, 124; 123, 125; 123, 238; 124, identifier:log_stats; 125, block; 125, 126; 125, 141; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:sum_dict; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:dict; 132, identifier:fromkeys; 133, argument_list; 133, 134; 134, call; 134, 135; 134, 140; 135, attribute; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:log_stats; 138, integer:0; 139, identifier:keys; 140, argument_list; 141, for_statement; 141, 142; 141, 143; 141, 144; 142, identifier:log_stat; 143, identifier:log_stats; 144, block; 144, 145; 145, for_statement; 145, 146; 145, 147; 145, 152; 146, identifier:k; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:log_stat; 150, identifier:keys; 151, argument_list; 152, block; 152, 153; 153, if_statement; 153, 154; 153, 161; 153, 213; 154, call; 154, 155; 154, 156; 155, identifier:isinstance; 156, argument_list; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:log_stat; 159, identifier:k; 160, identifier:list; 161, block; 161, 162; 161, 185; 162, if_statement; 162, 163; 162, 170; 163, comparison_operator:not; 163, 164; 163, 165; 164, identifier:k; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:sum_dict; 168, identifier:keys; 169, argument_list; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:sum_dict; 175, identifier:k; 176, binary_operator:*; 176, 177; 176, 179; 177, list:[0]; 177, 178; 178, integer:0; 179, call; 179, 180; 179, 181; 180, identifier:len; 181, argument_list; 181, 182; 182, subscript; 182, 183; 182, 184; 183, identifier:log_stat; 184, identifier:k; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:sum_dict; 189, identifier:k; 190, list_comprehension; 190, 191; 190, 202; 191, binary_operator:+; 191, 192; 191, 197; 192, subscript; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:sum_dict; 195, identifier:k; 196, identifier:i; 197, subscript; 197, 198; 197, 201; 198, subscript; 198, 199; 198, 200; 199, identifier:log_stat; 200, identifier:k; 201, identifier:i; 202, for_in_clause; 202, 203; 202, 204; 203, identifier:i; 204, call; 204, 205; 204, 206; 205, identifier:range; 206, argument_list; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:len; 209, argument_list; 209, 210; 210, subscript; 210, 211; 210, 212; 211, identifier:log_stat; 212, identifier:k; 213, else_clause; 213, 214; 214, block; 214, 215; 214, 230; 215, if_statement; 215, 216; 215, 223; 216, comparison_operator:not; 216, 217; 216, 218; 217, identifier:k; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:sum_dict; 221, identifier:keys; 222, argument_list; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, assignment; 225, 226; 225, 229; 226, subscript; 226, 227; 226, 228; 227, identifier:sum_dict; 228, identifier:k; 229, integer:0; 230, expression_statement; 230, 231; 231, augmented_assignment:+=; 231, 232; 231, 235; 232, subscript; 232, 233; 232, 234; 233, identifier:sum_dict; 234, identifier:k; 235, subscript; 235, 236; 235, 237; 236, identifier:log_stat; 237, identifier:k; 238, else_clause; 238, 239; 239, block; 239, 240; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 243; 242, identifier:sum_dict; 243, dictionary; 244, return_statement; 244, 245; 245, expression_list; 245, 246; 245, 247; 245, 248; 246, identifier:tcids_unfinished; 247, identifier:sum_dict; 248, identifier:log_stats | def analyze_db_reading(job_prefix, reading_queue='run_db_reading_queue'):
"""Run various analysis on a particular reading job."""
# Analyze reach failures
log_strs = get_logs_from_db_reading(job_prefix, reading_queue)
indra_log_strs = []
all_reach_logs = []
log_stats = []
for log_str in log_strs:
log_str, reach_logs = separate_reach_logs(log_str)
all_reach_logs.extend(reach_logs)
indra_log_strs.append(log_str)
log_stats.append(get_reading_stats(log_str))
# Analayze the reach failures.
failed_reach_logs = [reach_log_str
for result, reach_log_str in all_reach_logs
if result == 'FAILURE']
failed_id_dicts = [analyze_reach_log(log_str=reach_log)
for reach_log in failed_reach_logs if bool(reach_log)]
tcids_unfinished = {id_dict['not_done'] for id_dict in failed_id_dicts}
print("Found %d unfinished tcids." % len(tcids_unfinished))
# Summarize the global stats
if log_stats:
sum_dict = dict.fromkeys(log_stats[0].keys())
for log_stat in log_stats:
for k in log_stat.keys():
if isinstance(log_stat[k], list):
if k not in sum_dict.keys():
sum_dict[k] = [0]*len(log_stat[k])
sum_dict[k] = [sum_dict[k][i] + log_stat[k][i]
for i in range(len(log_stat[k]))]
else:
if k not in sum_dict.keys():
sum_dict[k] = 0
sum_dict[k] += log_stat[k]
else:
sum_dict = {}
return tcids_unfinished, sum_dict, log_stats |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_create_base_agent; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:agent; 6, block; 6, 7; 6, 9; 6, 52; 6, 53; 6, 224; 6, 225; 6, 245; 7, expression_statement; 7, 8; 8, comment; 9, try_statement; 9, 10; 9, 24; 10, block; 10, 11; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:base_agent; 14, subscript; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:agents; 18, call; 18, 19; 18, 20; 19, identifier:_n; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:agent; 23, identifier:name; 24, except_clause; 24, 25; 24, 26; 25, identifier:KeyError; 26, block; 26, 27; 26, 39; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:base_agent; 30, call; 30, 31; 30, 32; 31, identifier:BaseAgent; 32, argument_list; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:_n; 35, argument_list; 35, 36; 36, attribute; 36, 37; 36, 38; 37, identifier:agent; 38, identifier:name; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 51; 41, subscript; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:agents; 45, call; 45, 46; 45, 47; 46, identifier:_n; 47, argument_list; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:agent; 50, identifier:name; 51, identifier:base_agent; 52, comment; 53, if_statement; 53, 54; 53, 59; 53, 60; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:agent; 58, identifier:Agent; 59, comment; 60, block; 60, 61; 60, 100; 60, 101; 60, 114; 60, 115; 60, 176; 60, 177; 60, 198; 60, 199; 61, for_statement; 61, 62; 61, 63; 61, 66; 62, identifier:bc; 63, attribute; 63, 64; 63, 65; 64, identifier:agent; 65, identifier:bound_conditions; 66, block; 66, 67; 66, 78; 66, 88; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:bound_base_agent; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:get_create_base_agent; 74, argument_list; 74, 75; 75, attribute; 75, 76; 75, 77; 76, identifier:bc; 77, identifier:agent; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:bound_base_agent; 82, identifier:create_site; 83, argument_list; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:get_binding_site_name; 86, argument_list; 86, 87; 87, identifier:agent; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:base_agent; 92, identifier:create_site; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:get_binding_site_name; 96, argument_list; 96, 97; 97, attribute; 97, 98; 97, 99; 98, identifier:bc; 99, identifier:agent; 100, comment; 101, for_statement; 101, 102; 101, 103; 101, 106; 102, identifier:mc; 103, attribute; 103, 104; 103, 105; 104, identifier:agent; 105, identifier:mods; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:base_agent; 111, identifier:create_mod_site; 112, argument_list; 112, 113; 113, identifier:mc; 114, comment; 115, for_statement; 115, 116; 115, 117; 115, 120; 116, identifier:mc; 117, attribute; 117, 118; 117, 119; 118, identifier:agent; 119, identifier:mutations; 120, block; 120, 121; 120, 132; 120, 143; 120, 164; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:res_from; 124, conditional_expression:if; 124, 125; 124, 128; 124, 131; 125, attribute; 125, 126; 125, 127; 126, identifier:mc; 127, identifier:residue_from; 128, attribute; 128, 129; 128, 130; 129, identifier:mc; 130, identifier:residue_from; 131, string:'mut'; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:res_to; 135, conditional_expression:if; 135, 136; 135, 139; 135, 142; 136, attribute; 136, 137; 136, 138; 137, identifier:mc; 138, identifier:residue_to; 139, attribute; 139, 140; 139, 141; 140, identifier:mc; 141, identifier:residue_to; 142, string:'X'; 143, if_statement; 143, 144; 143, 149; 143, 154; 144, comparison_operator:is; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:mc; 147, identifier:position; 148, None; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:mut_site_name; 153, identifier:res_from; 154, else_clause; 154, 155; 155, block; 155, 156; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:mut_site_name; 159, binary_operator:+; 159, 160; 159, 161; 160, identifier:res_from; 161, attribute; 161, 162; 161, 163; 162, identifier:mc; 163, identifier:position; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 169; 166, attribute; 166, 167; 166, 168; 167, identifier:base_agent; 168, identifier:create_site; 169, argument_list; 169, 170; 169, 171; 170, identifier:mut_site_name; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:states; 173, list:['WT', res_to]; 173, 174; 173, 175; 174, string:'WT'; 175, identifier:res_to; 176, comment; 177, if_statement; 177, 178; 177, 183; 178, comparison_operator:is; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:agent; 181, identifier:location; 182, None; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:base_agent; 188, identifier:create_site; 189, argument_list; 189, 190; 189, 191; 190, string:'loc'; 191, list:[_n(agent.location)]; 191, 192; 192, call; 192, 193; 192, 194; 193, identifier:_n; 194, argument_list; 194, 195; 195, attribute; 195, 196; 195, 197; 196, identifier:agent; 197, identifier:location; 198, comment; 199, if_statement; 199, 200; 199, 205; 200, comparison_operator:is; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:agent; 203, identifier:activity; 204, None; 205, block; 205, 206; 205, 214; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:site_name; 209, attribute; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:agent; 212, identifier:activity; 213, identifier:activity_type; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:base_agent; 218, identifier:create_site; 219, argument_list; 219, 220; 219, 221; 220, identifier:site_name; 221, list:['inactive', 'active']; 221, 222; 221, 223; 222, string:'inactive'; 223, string:'active'; 224, comment; 225, for_statement; 225, 226; 225, 229; 225, 236; 226, pattern_list; 226, 227; 226, 228; 227, identifier:db_name; 228, identifier:db_ref; 229, call; 229, 230; 229, 235; 230, attribute; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:agent; 233, identifier:db_refs; 234, identifier:items; 235, argument_list; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, assignment; 238, 239; 238, 244; 239, subscript; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:base_agent; 242, identifier:db_refs; 243, identifier:db_name; 244, identifier:db_ref; 245, return_statement; 245, 246; 246, identifier:base_agent | def get_create_base_agent(self, agent):
"""Return base agent with given name, creating it if needed."""
try:
base_agent = self.agents[_n(agent.name)]
except KeyError:
base_agent = BaseAgent(_n(agent.name))
self.agents[_n(agent.name)] = base_agent
# If it's a molecular agent
if isinstance(agent, Agent):
# Handle bound conditions
for bc in agent.bound_conditions:
bound_base_agent = self.get_create_base_agent(bc.agent)
bound_base_agent.create_site(get_binding_site_name(agent))
base_agent.create_site(get_binding_site_name(bc.agent))
# Handle modification conditions
for mc in agent.mods:
base_agent.create_mod_site(mc)
# Handle mutation conditions
for mc in agent.mutations:
res_from = mc.residue_from if mc.residue_from else 'mut'
res_to = mc.residue_to if mc.residue_to else 'X'
if mc.position is None:
mut_site_name = res_from
else:
mut_site_name = res_from + mc.position
base_agent.create_site(mut_site_name, states=['WT', res_to])
# Handle location condition
if agent.location is not None:
base_agent.create_site('loc', [_n(agent.location)])
# Handle activity
if agent.activity is not None:
site_name = agent.activity.activity_type
base_agent.create_site(site_name, ['inactive', 'active'])
# There might be overwrites here
for db_name, db_ref in agent.db_refs.items():
base_agent.db_refs[db_name] = db_ref
return base_agent |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:classify_nodes; 3, parameters; 3, 4; 3, 5; 4, identifier:graph; 5, identifier:hub; 6, block; 6, 7; 6, 9; 6, 20; 6, 144; 6, 148; 6, 234; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:node_stats; 12, call; 12, 13; 12, 14; 13, identifier:defaultdict; 14, argument_list; 14, 15; 15, lambda; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:defaultdict; 18, argument_list; 18, 19; 19, identifier:list; 20, for_statement; 20, 21; 20, 25; 20, 33; 20, 34; 21, pattern_list; 21, 22; 21, 23; 21, 24; 22, identifier:u; 23, identifier:v; 24, identifier:data; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:graph; 28, identifier:edges; 29, argument_list; 29, 30; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:data; 32, True; 33, comment; 34, block; 34, 35; 34, 128; 35, if_statement; 35, 36; 35, 39; 35, 80; 35, 81; 35, 125; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:hub; 38, identifier:u; 39, block; 39, 40; 39, 48; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 45; 42, pattern_list; 42, 43; 42, 44; 43, identifier:h; 44, identifier:o; 45, expression_list; 45, 46; 45, 47; 46, identifier:u; 47, identifier:v; 48, if_statement; 48, 49; 48, 54; 48, 67; 49, comparison_operator:!=; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:data; 52, string:'i'; 53, string:'Complex'; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 64; 57, attribute; 57, 58; 57, 63; 58, subscript; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:node_stats; 61, identifier:o; 62, string:'up'; 63, identifier:append; 64, argument_list; 64, 65; 65, unary_operator:-; 65, 66; 66, integer:1; 67, else_clause; 67, 68; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 78; 71, attribute; 71, 72; 71, 77; 72, subscript; 72, 73; 72, 76; 73, subscript; 73, 74; 73, 75; 74, identifier:node_stats; 75, identifier:o; 76, string:'up'; 77, identifier:append; 78, argument_list; 78, 79; 79, integer:0; 80, comment; 81, elif_clause; 81, 82; 81, 85; 82, comparison_operator:==; 82, 83; 82, 84; 83, identifier:hub; 84, identifier:v; 85, block; 85, 86; 85, 94; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, pattern_list; 88, 89; 88, 90; 89, identifier:h; 90, identifier:o; 91, expression_list; 91, 92; 91, 93; 92, identifier:v; 93, identifier:u; 94, if_statement; 94, 95; 94, 100; 94, 112; 95, comparison_operator:!=; 95, 96; 95, 99; 96, subscript; 96, 97; 96, 98; 97, identifier:data; 98, string:'i'; 99, string:'Complex'; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 110; 103, attribute; 103, 104; 103, 109; 104, subscript; 104, 105; 104, 108; 105, subscript; 105, 106; 105, 107; 106, identifier:node_stats; 107, identifier:o; 108, string:'up'; 109, identifier:append; 110, argument_list; 110, 111; 111, integer:1; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 123; 116, attribute; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 121; 118, subscript; 118, 119; 118, 120; 119, identifier:node_stats; 120, identifier:o; 121, string:'up'; 122, identifier:append; 123, argument_list; 123, 124; 124, integer:0; 125, else_clause; 125, 126; 126, block; 126, 127; 127, continue_statement; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 137; 130, attribute; 130, 131; 130, 136; 131, subscript; 131, 132; 131, 135; 132, subscript; 132, 133; 132, 134; 133, identifier:node_stats; 134, identifier:o; 135, string:'interaction'; 136, identifier:append; 137, argument_list; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:edge_type_to_class; 140, argument_list; 140, 141; 141, subscript; 141, 142; 141, 143; 142, identifier:data; 143, string:'i'; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:node_classes; 147, dictionary; 148, for_statement; 148, 149; 148, 152; 148, 157; 149, pattern_list; 149, 150; 149, 151; 150, identifier:node_id; 151, identifier:stats; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:node_stats; 155, identifier:items; 156, argument_list; 157, block; 157, 158; 157, 177; 157, 178; 157, 179; 157, 180; 157, 200; 157, 215; 157, 225; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:up; 161, call; 161, 162; 161, 163; 162, identifier:max; 163, argument_list; 163, 164; 163, 170; 164, call; 164, 165; 164, 166; 165, identifier:set; 166, argument_list; 166, 167; 167, subscript; 167, 168; 167, 169; 168, identifier:stats; 169, string:'up'; 170, keyword_argument; 170, 171; 170, 172; 171, identifier:key; 172, attribute; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:stats; 175, string:'up'; 176, identifier:count; 177, comment; 178, comment; 179, comment; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:interactions; 183, list_comprehension; 183, 184; 183, 185; 183, 190; 184, identifier:i; 185, for_in_clause; 185, 186; 185, 187; 186, identifier:i; 187, subscript; 187, 188; 187, 189; 188, identifier:stats; 189, string:'interaction'; 190, if_clause; 190, 191; 191, not_operator; 191, 192; 192, parenthesized_expression; 192, 193; 193, boolean_operator:and; 193, 194; 193, 197; 194, comparison_operator:!=; 194, 195; 194, 196; 195, identifier:up; 196, integer:0; 197, comparison_operator:==; 197, 198; 197, 199; 198, identifier:i; 199, string:'complex'; 200, expression_statement; 200, 201; 201, assignment; 201, 202; 201, 203; 202, identifier:edge_type; 203, call; 203, 204; 203, 205; 204, identifier:max; 205, argument_list; 205, 206; 205, 210; 206, call; 206, 207; 206, 208; 207, identifier:set; 208, argument_list; 208, 209; 209, identifier:interactions; 210, keyword_argument; 210, 211; 210, 212; 211, identifier:key; 212, attribute; 212, 213; 212, 214; 213, identifier:interactions; 214, identifier:count; 215, expression_statement; 215, 216; 216, assignment; 216, 217; 216, 218; 217, identifier:node_type; 218, subscript; 218, 219; 218, 224; 219, subscript; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:graph; 222, identifier:nodes; 223, identifier:node_id; 224, string:'type'; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 230; 227, subscript; 227, 228; 227, 229; 228, identifier:node_classes; 229, identifier:node_id; 230, tuple; 230, 231; 230, 232; 230, 233; 231, identifier:up; 232, identifier:edge_type; 233, identifier:node_type; 234, return_statement; 234, 235; 235, identifier:node_classes | def classify_nodes(graph, hub):
"""Classify each node based on its type and relationship to the hub."""
node_stats = defaultdict(lambda: defaultdict(list))
for u, v, data in graph.edges(data=True):
# This means the node is downstream of the hub
if hub == u:
h, o = u, v
if data['i'] != 'Complex':
node_stats[o]['up'].append(-1)
else:
node_stats[o]['up'].append(0)
# This means the node is upstream of the hub
elif hub == v:
h, o = v, u
if data['i'] != 'Complex':
node_stats[o]['up'].append(1)
else:
node_stats[o]['up'].append(0)
else:
continue
node_stats[o]['interaction'].append(edge_type_to_class(data['i']))
node_classes = {}
for node_id, stats in node_stats.items():
up = max(set(stats['up']), key=stats['up'].count)
# Special case: if up is not 0 then we should exclude complexes
# from the edge_type states so that we don't end up with
# (-1, complex, ...) or (1, complex, ...) as the node class
interactions = [i for i in stats['interaction'] if
not (up != 0 and i == 'complex')]
edge_type = max(set(interactions), key=interactions.count)
node_type = graph.nodes[node_id]['type']
node_classes[node_id] = (up, edge_type, node_type)
return node_classes |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:doi_query; 3, parameters; 3, 4; 3, 5; 4, identifier:pmid; 5, default_parameter; 5, 6; 5, 7; 6, identifier:search_limit; 7, integer:10; 8, block; 8, 9; 8, 11; 8, 12; 8, 25; 8, 50; 8, 51; 8, 57; 8, 58; 8, 73; 8, 74; 8, 83; 8, 99; 8, 100; 8, 109; 8, 124; 8, 125; 8, 134; 8, 149; 8, 150; 8, 154; 8, 164; 8, 222; 8, 244; 8, 252; 8, 256; 8, 257; 8, 460; 8, 461; 8, 462; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:pubmed_meta_dict; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:pubmed_client; 18, identifier:get_metadata_for_ids; 19, argument_list; 19, 20; 19, 22; 20, list:[pmid]; 20, 21; 21, identifier:pmid; 22, keyword_argument; 22, 23; 22, 24; 23, identifier:get_issns_from_nlm; 24, True; 25, if_statement; 25, 26; 25, 38; 26, boolean_operator:or; 26, 27; 26, 30; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:pubmed_meta_dict; 29, None; 30, comparison_operator:is; 30, 31; 30, 37; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:pubmed_meta_dict; 34, identifier:get; 35, argument_list; 35, 36; 36, identifier:pmid; 37, None; 38, block; 38, 39; 38, 48; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:logger; 43, identifier:warning; 44, argument_list; 44, 45; 45, binary_operator:%; 45, 46; 45, 47; 46, string:'No metadata found in Pubmed for PMID%s'; 47, identifier:pmid; 48, return_statement; 48, 49; 49, None; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:pubmed_meta; 54, subscript; 54, 55; 54, 56; 55, identifier:pubmed_meta_dict; 56, identifier:pmid; 57, comment; 58, if_statement; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:pubmed_meta; 62, identifier:get; 63, argument_list; 63, 64; 64, string:'doi'; 65, block; 65, 66; 66, return_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:pubmed_meta; 70, identifier:get; 71, argument_list; 71, 72; 72, string:'doi'; 73, comment; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:pm_article_title; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:pubmed_meta; 80, identifier:get; 81, argument_list; 81, 82; 82, string:'title'; 83, if_statement; 83, 84; 83, 87; 84, comparison_operator:is; 84, 85; 84, 86; 85, identifier:pm_article_title; 86, None; 87, block; 87, 88; 87, 97; 88, expression_statement; 88, 89; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:logger; 92, identifier:warning; 93, argument_list; 93, 94; 94, binary_operator:%; 94, 95; 94, 96; 95, string:'No article title found in Pubmed for PMID%s'; 96, identifier:pmid; 97, return_statement; 97, 98; 98, None; 99, comment; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:pm_issn_list; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:pubmed_meta; 106, identifier:get; 107, argument_list; 107, 108; 108, string:'issn_list'; 109, if_statement; 109, 110; 109, 112; 110, not_operator; 110, 111; 111, identifier:pm_issn_list; 112, block; 112, 113; 112, 122; 113, expression_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:logger; 117, identifier:warning; 118, argument_list; 118, 119; 119, binary_operator:%; 119, 120; 119, 121; 120, string:'No ISSNs found in Pubmed for PMID%s'; 121, identifier:pmid; 122, return_statement; 122, 123; 123, None; 124, comment; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:pm_page; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:pubmed_meta; 131, identifier:get; 132, argument_list; 132, 133; 133, string:'page'; 134, if_statement; 134, 135; 134, 137; 135, not_operator; 135, 136; 136, identifier:pm_page; 137, block; 137, 138; 137, 147; 138, expression_statement; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:logger; 142, identifier:debug; 143, argument_list; 143, 144; 144, binary_operator:%; 144, 145; 144, 146; 145, string:'No page number found in Pubmed for PMID%s'; 146, identifier:pmid; 147, return_statement; 147, 148; 148, None; 149, comment; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:url; 153, identifier:crossref_search_url; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:params; 157, dictionary; 157, 158; 157, 161; 158, pair; 158, 159; 158, 160; 159, string:'q'; 160, identifier:pm_article_title; 161, pair; 161, 162; 161, 163; 162, string:'sort'; 163, string:'score'; 164, try_statement; 164, 165; 164, 176; 164, 202; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:res; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:requests; 172, identifier:get; 173, argument_list; 173, 174; 173, 175; 174, identifier:crossref_search_url; 175, identifier:params; 176, except_clause; 176, 177; 176, 185; 177, as_pattern; 177, 178; 177, 183; 178, attribute; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:requests; 181, identifier:exceptions; 182, identifier:ConnectionError; 183, as_pattern_target; 183, 184; 184, identifier:e; 185, block; 185, 186; 185, 193; 185, 200; 186, expression_statement; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:logger; 190, identifier:error; 191, argument_list; 191, 192; 192, string:'CrossRef service could not be reached.'; 193, expression_statement; 193, 194; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:logger; 197, identifier:error; 198, argument_list; 198, 199; 199, identifier:e; 200, return_statement; 200, 201; 201, None; 202, except_clause; 202, 203; 202, 207; 203, as_pattern; 203, 204; 203, 205; 204, identifier:Exception; 205, as_pattern_target; 205, 206; 206, identifier:e; 207, block; 207, 208; 207, 220; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:logger; 212, identifier:error; 213, argument_list; 213, 214; 214, binary_operator:%; 214, 215; 214, 216; 215, string:'Error accessing CrossRef service: %s'; 216, call; 216, 217; 216, 218; 217, identifier:str; 218, argument_list; 218, 219; 219, identifier:e; 220, return_statement; 220, 221; 221, None; 222, if_statement; 222, 223; 222, 228; 223, comparison_operator:!=; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:res; 226, identifier:status_code; 227, integer:200; 228, block; 228, 229; 228, 242; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:logger; 233, identifier:info; 234, argument_list; 234, 235; 235, binary_operator:%; 235, 236; 235, 237; 236, string:'PMID%s: no search results from CrossRef, code %d'; 237, tuple; 237, 238; 237, 239; 238, identifier:pmid; 239, attribute; 239, 240; 239, 241; 240, identifier:res; 241, identifier:status_code; 242, return_statement; 242, 243; 243, None; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:raw_message; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:res; 250, identifier:json; 251, argument_list; 252, expression_statement; 252, 253; 253, assignment; 253, 254; 253, 255; 254, identifier:mapped_doi; 255, None; 256, comment; 257, for_statement; 257, 258; 257, 261; 257, 265; 258, pattern_list; 258, 259; 258, 260; 259, identifier:result_ix; 260, identifier:result; 261, call; 261, 262; 261, 263; 262, identifier:enumerate; 263, argument_list; 263, 264; 264, identifier:raw_message; 265, block; 265, 266; 265, 285; 265, 291; 265, 292; 265, 302; 265, 312; 265, 313; 265, 320; 265, 326; 265, 335; 265, 344; 265, 345; 265, 359; 265, 360; 265, 374; 265, 375; 265, 376; 265, 391; 265, 392; 265, 393; 265, 394; 265, 395; 265, 396; 265, 411; 265, 426; 265, 445; 265, 446; 265, 459; 266, if_statement; 266, 267; 266, 270; 267, comparison_operator:>; 267, 268; 267, 269; 268, identifier:result_ix; 269, identifier:search_limit; 270, block; 270, 271; 270, 284; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:logger; 275, identifier:info; 276, argument_list; 276, 277; 277, binary_operator:%; 277, 278; 277, 281; 278, concatenated_string; 278, 279; 278, 280; 279, string:'PMID%s: No match found within first %s results, '; 280, string:'giving up!'; 281, tuple; 281, 282; 281, 283; 282, identifier:pmid; 283, identifier:search_limit; 284, break_statement; 285, expression_statement; 285, 286; 286, assignment; 286, 287; 286, 288; 287, identifier:xref_doi_url; 288, subscript; 288, 289; 288, 290; 289, identifier:result; 290, string:'doi'; 291, comment; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:m; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:re; 298, identifier:match; 299, argument_list; 299, 300; 299, 301; 300, string:'^http://dx.doi.org/(.*)$'; 301, identifier:xref_doi_url; 302, expression_statement; 302, 303; 303, assignment; 303, 304; 303, 305; 304, identifier:xref_doi; 305, subscript; 305, 306; 305, 311; 306, call; 306, 307; 306, 310; 307, attribute; 307, 308; 307, 309; 308, identifier:m; 309, identifier:groups; 310, argument_list; 311, integer:0; 312, comment; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:xref_meta; 316, call; 316, 317; 316, 318; 317, identifier:get_metadata; 318, argument_list; 318, 319; 319, identifier:xref_doi; 320, if_statement; 320, 321; 320, 324; 321, comparison_operator:is; 321, 322; 321, 323; 322, identifier:xref_meta; 323, None; 324, block; 324, 325; 325, continue_statement; 326, expression_statement; 326, 327; 327, assignment; 327, 328; 327, 329; 328, identifier:xref_issn_list; 329, call; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:xref_meta; 332, identifier:get; 333, argument_list; 333, 334; 334, string:'ISSN'; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:xref_page; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:xref_meta; 341, identifier:get; 342, argument_list; 342, 343; 343, string:'page'; 344, comment; 345, if_statement; 345, 346; 345, 348; 346, not_operator; 346, 347; 347, identifier:xref_issn_list; 348, block; 348, 349; 348, 358; 349, expression_statement; 349, 350; 350, call; 350, 351; 350, 354; 351, attribute; 351, 352; 351, 353; 352, identifier:logger; 353, identifier:debug; 354, argument_list; 354, 355; 355, binary_operator:%; 355, 356; 355, 357; 356, string:'No ISSN found for DOI %s, skipping'; 357, identifier:xref_doi_url; 358, continue_statement; 359, comment; 360, if_statement; 360, 361; 360, 363; 361, not_operator; 361, 362; 362, identifier:xref_page; 363, block; 363, 364; 363, 373; 364, expression_statement; 364, 365; 365, call; 365, 366; 365, 369; 366, attribute; 366, 367; 366, 368; 367, identifier:logger; 368, identifier:debug; 369, argument_list; 369, 370; 370, binary_operator:%; 370, 371; 370, 372; 371, string:'No page number found for DOI %s, skipping'; 372, identifier:xref_doi_url; 373, continue_statement; 374, comment; 375, comment; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 379; 378, identifier:matching_issns; 379, call; 379, 380; 379, 386; 380, attribute; 380, 381; 380, 385; 381, call; 381, 382; 381, 383; 382, identifier:set; 383, argument_list; 383, 384; 384, identifier:pm_issn_list; 385, identifier:intersection; 386, argument_list; 386, 387; 387, call; 387, 388; 387, 389; 388, identifier:set; 389, argument_list; 389, 390; 390, identifier:xref_issn_list; 391, comment; 392, comment; 393, comment; 394, comment; 395, comment; 396, expression_statement; 396, 397; 397, assignment; 397, 398; 397, 399; 398, identifier:pm_start_page; 399, call; 399, 400; 399, 410; 400, attribute; 400, 401; 400, 409; 401, subscript; 401, 402; 401, 408; 402, call; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:pm_page; 405, identifier:split; 406, argument_list; 406, 407; 407, string:'-'; 408, integer:0; 409, identifier:upper; 410, argument_list; 411, expression_statement; 411, 412; 412, assignment; 412, 413; 412, 414; 413, identifier:xr_start_page; 414, call; 414, 415; 414, 425; 415, attribute; 415, 416; 415, 424; 416, subscript; 416, 417; 416, 423; 417, call; 417, 418; 417, 421; 418, attribute; 418, 419; 418, 420; 419, identifier:xref_page; 420, identifier:split; 421, argument_list; 421, 422; 422, string:'-'; 423, integer:0; 424, identifier:upper; 425, argument_list; 426, if_statement; 426, 427; 426, 433; 427, call; 427, 428; 427, 431; 428, attribute; 428, 429; 428, 430; 429, identifier:xr_start_page; 430, identifier:endswith; 431, argument_list; 431, 432; 432, string:'E'; 433, block; 433, 434; 434, expression_statement; 434, 435; 435, assignment; 435, 436; 435, 437; 436, identifier:xr_start_page; 437, binary_operator:+; 437, 438; 437, 439; 438, string:'E'; 439, subscript; 439, 440; 439, 441; 440, identifier:xr_start_page; 441, slice; 441, 442; 441, 443; 442, colon; 443, unary_operator:-; 443, 444; 444, integer:1; 445, comment; 446, if_statement; 446, 447; 446, 452; 446, 453; 447, boolean_operator:and; 447, 448; 447, 449; 448, identifier:matching_issns; 449, comparison_operator:==; 449, 450; 449, 451; 450, identifier:pm_start_page; 451, identifier:xr_start_page; 452, comment; 453, block; 453, 454; 453, 458; 454, expression_statement; 454, 455; 455, assignment; 455, 456; 455, 457; 456, identifier:mapped_doi; 457, identifier:xref_doi; 458, break_statement; 459, comment; 460, comment; 461, comment; 462, return_statement; 462, 463; 463, identifier:mapped_doi | def doi_query(pmid, search_limit=10):
"""Get the DOI for a PMID by matching CrossRef and Pubmed metadata.
Searches CrossRef using the article title and then accepts search hits only
if they have a matching journal ISSN and page number with what is obtained
from the Pubmed database.
"""
# Get article metadata from PubMed
pubmed_meta_dict = pubmed_client.get_metadata_for_ids([pmid],
get_issns_from_nlm=True)
if pubmed_meta_dict is None or pubmed_meta_dict.get(pmid) is None:
logger.warning('No metadata found in Pubmed for PMID%s' % pmid)
return None
# The test above ensures we've got this now
pubmed_meta = pubmed_meta_dict[pmid]
# Check if we already got a DOI from Pubmed itself!
if pubmed_meta.get('doi'):
return pubmed_meta.get('doi')
# Check for the title, which we'll need for the CrossRef search
pm_article_title = pubmed_meta.get('title')
if pm_article_title is None:
logger.warning('No article title found in Pubmed for PMID%s' % pmid)
return None
# Get the ISSN list
pm_issn_list = pubmed_meta.get('issn_list')
if not pm_issn_list:
logger.warning('No ISSNs found in Pubmed for PMID%s' % pmid)
return None
# Get the page number
pm_page = pubmed_meta.get('page')
if not pm_page:
logger.debug('No page number found in Pubmed for PMID%s' % pmid)
return None
# Now query CrossRef using the title we've got
url = crossref_search_url
params = {'q': pm_article_title, 'sort': 'score'}
try:
res = requests.get(crossref_search_url, params)
except requests.exceptions.ConnectionError as e:
logger.error('CrossRef service could not be reached.')
logger.error(e)
return None
except Exception as e:
logger.error('Error accessing CrossRef service: %s' % str(e))
return None
if res.status_code != 200:
logger.info('PMID%s: no search results from CrossRef, code %d' %
(pmid, res.status_code))
return None
raw_message = res.json()
mapped_doi = None
# Iterate over the search results, looking up XREF metadata
for result_ix, result in enumerate(raw_message):
if result_ix > search_limit:
logger.info('PMID%s: No match found within first %s results, '
'giving up!' % (pmid, search_limit))
break
xref_doi_url = result['doi']
# Strip the URL prefix off of the DOI
m = re.match('^http://dx.doi.org/(.*)$', xref_doi_url)
xref_doi = m.groups()[0]
# Get the XREF metadata using the DOI
xref_meta = get_metadata(xref_doi)
if xref_meta is None:
continue
xref_issn_list = xref_meta.get('ISSN')
xref_page = xref_meta.get('page')
# If there's no ISSN info for this article, skip to the next result
if not xref_issn_list:
logger.debug('No ISSN found for DOI %s, skipping' % xref_doi_url)
continue
# If there's no page info for this article, skip to the next result
if not xref_page:
logger.debug('No page number found for DOI %s, skipping' %
xref_doi_url)
continue
# Now check for an ISSN match by looking for the set intersection
# between the Pubmed ISSN list and the CrossRef ISSN list.
matching_issns = set(pm_issn_list).intersection(set(xref_issn_list))
# Before comparing page numbers, regularize the page numbers a bit.
# Note that we only compare the first page number, since frequently
# the final page number will simply be missing in one of the data
# sources. We also canonicalize page numbers of the form '14E' to
# 'E14' (which is the format used by Pubmed).
pm_start_page = pm_page.split('-')[0].upper()
xr_start_page = xref_page.split('-')[0].upper()
if xr_start_page.endswith('E'):
xr_start_page = 'E' + xr_start_page[:-1]
# Now compare the ISSN list and page numbers
if matching_issns and pm_start_page == xr_start_page:
# We found a match!
mapped_doi = xref_doi
break
# Otherwise, keep looking through the results...
# Return a DOI, or None if we didn't find one that met our matching
# criteria
return mapped_doi |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_agent_rule_str; 3, parameters; 3, 4; 4, identifier:agent; 5, block; 5, 6; 5, 8; 5, 18; 5, 19; 5, 254; 5, 263; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:rule_str_list; 11, list:[_n(agent.name)]; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:_n; 14, argument_list; 14, 15; 15, attribute; 15, 16; 15, 17; 16, identifier:agent; 17, identifier:name; 18, comment; 19, if_statement; 19, 20; 19, 27; 20, call; 20, 21; 20, 22; 21, identifier:isinstance; 22, argument_list; 22, 23; 22, 24; 23, identifier:agent; 24, attribute; 24, 25; 24, 26; 25, identifier:ist; 26, identifier:Agent; 27, block; 27, 28; 27, 77; 27, 139; 27, 187; 27, 206; 28, for_statement; 28, 29; 28, 30; 28, 33; 29, identifier:mod; 30, attribute; 30, 31; 30, 32; 31, identifier:agent; 32, identifier:mods; 33, block; 33, 34; 33, 42; 33, 55; 33, 68; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:mstr; 37, subscript; 37, 38; 37, 39; 38, identifier:abbrevs; 39, attribute; 39, 40; 39, 41; 40, identifier:mod; 41, identifier:mod_type; 42, if_statement; 42, 43; 42, 48; 43, comparison_operator:is; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:mod; 46, identifier:residue; 47, None; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, augmented_assignment:+=; 50, 51; 50, 52; 51, identifier:mstr; 52, attribute; 52, 53; 52, 54; 53, identifier:mod; 54, identifier:residue; 55, if_statement; 55, 56; 55, 61; 56, comparison_operator:is; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:mod; 59, identifier:position; 60, None; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, augmented_assignment:+=; 63, 64; 63, 65; 64, identifier:mstr; 65, attribute; 65, 66; 65, 67; 66, identifier:mod; 67, identifier:position; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:rule_str_list; 72, identifier:append; 73, argument_list; 73, 74; 74, binary_operator:%; 74, 75; 74, 76; 75, string:'%s'; 76, identifier:mstr; 77, for_statement; 77, 78; 77, 79; 77, 82; 78, identifier:mut; 79, attribute; 79, 80; 79, 81; 80, identifier:agent; 81, identifier:mutations; 82, block; 82, 83; 82, 94; 82, 105; 82, 126; 82, 132; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:res_from; 86, conditional_expression:if; 86, 87; 86, 90; 86, 93; 87, attribute; 87, 88; 87, 89; 88, identifier:mut; 89, identifier:residue_from; 90, attribute; 90, 91; 90, 92; 91, identifier:mut; 92, identifier:residue_from; 93, string:'mut'; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:res_to; 97, conditional_expression:if; 97, 98; 97, 101; 97, 104; 98, attribute; 98, 99; 98, 100; 99, identifier:mut; 100, identifier:residue_to; 101, attribute; 101, 102; 101, 103; 102, identifier:mut; 103, identifier:residue_to; 104, string:'X'; 105, if_statement; 105, 106; 105, 111; 105, 116; 106, comparison_operator:is; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:mut; 109, identifier:position; 110, None; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:mut_site_name; 115, identifier:res_from; 116, else_clause; 116, 117; 117, block; 117, 118; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:mut_site_name; 121, binary_operator:+; 121, 122; 121, 123; 122, identifier:res_from; 123, attribute; 123, 124; 123, 125; 124, identifier:mut; 125, identifier:position; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:mstr; 129, binary_operator:+; 129, 130; 129, 131; 130, identifier:mut_site_name; 131, identifier:res_to; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:rule_str_list; 136, identifier:append; 137, argument_list; 137, 138; 138, identifier:mstr; 139, if_statement; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, identifier:agent; 142, identifier:bound_conditions; 143, block; 143, 144; 144, for_statement; 144, 145; 144, 146; 144, 149; 145, identifier:b; 146, attribute; 146, 147; 146, 148; 147, identifier:agent; 148, identifier:bound_conditions; 149, block; 149, 150; 150, if_statement; 150, 151; 150, 154; 150, 169; 151, attribute; 151, 152; 151, 153; 152, identifier:b; 153, identifier:is_bound; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:rule_str_list; 159, identifier:append; 160, argument_list; 160, 161; 161, call; 161, 162; 161, 163; 162, identifier:_n; 163, argument_list; 163, 164; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:b; 167, identifier:agent; 168, identifier:name; 169, else_clause; 169, 170; 170, block; 170, 171; 171, expression_statement; 171, 172; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:rule_str_list; 175, identifier:append; 176, argument_list; 176, 177; 177, binary_operator:+; 177, 178; 177, 179; 178, string:'n'; 179, call; 179, 180; 179, 181; 180, identifier:_n; 181, argument_list; 181, 182; 182, attribute; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:b; 185, identifier:agent; 186, identifier:name; 187, if_statement; 187, 188; 187, 193; 188, comparison_operator:is; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:agent; 191, identifier:location; 192, None; 193, block; 193, 194; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:rule_str_list; 198, identifier:append; 199, argument_list; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:_n; 202, argument_list; 202, 203; 203, attribute; 203, 204; 203, 205; 204, identifier:agent; 205, identifier:location; 206, if_statement; 206, 207; 206, 212; 207, comparison_operator:is; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:agent; 210, identifier:activity; 211, None; 212, block; 212, 213; 213, if_statement; 213, 214; 213, 219; 213, 235; 214, attribute; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:agent; 217, identifier:activity; 218, identifier:is_active; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:rule_str_list; 224, identifier:append; 225, argument_list; 225, 226; 226, subscript; 226, 227; 226, 232; 227, attribute; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:agent; 230, identifier:activity; 231, identifier:activity_type; 232, slice; 232, 233; 232, 234; 233, colon; 234, integer:3; 235, else_clause; 235, 236; 236, block; 236, 237; 237, expression_statement; 237, 238; 238, call; 238, 239; 238, 242; 239, attribute; 239, 240; 239, 241; 240, identifier:rule_str_list; 241, identifier:append; 242, argument_list; 242, 243; 243, binary_operator:+; 243, 244; 243, 253; 244, subscript; 244, 245; 244, 250; 245, attribute; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:agent; 248, identifier:activity; 249, identifier:activity_type; 250, slice; 250, 251; 250, 252; 251, colon; 252, integer:3; 253, string:'_inact'; 254, expression_statement; 254, 255; 255, assignment; 255, 256; 255, 257; 256, identifier:rule_str; 257, call; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, string:'_'; 260, identifier:join; 261, argument_list; 261, 262; 262, identifier:rule_str_list; 263, return_statement; 263, 264; 264, identifier:rule_str | def get_agent_rule_str(agent):
"""Construct a string from an Agent as part of a PySB rule name."""
rule_str_list = [_n(agent.name)]
# If it's a molecular agent
if isinstance(agent, ist.Agent):
for mod in agent.mods:
mstr = abbrevs[mod.mod_type]
if mod.residue is not None:
mstr += mod.residue
if mod.position is not None:
mstr += mod.position
rule_str_list.append('%s' % mstr)
for mut in agent.mutations:
res_from = mut.residue_from if mut.residue_from else 'mut'
res_to = mut.residue_to if mut.residue_to else 'X'
if mut.position is None:
mut_site_name = res_from
else:
mut_site_name = res_from + mut.position
mstr = mut_site_name + res_to
rule_str_list.append(mstr)
if agent.bound_conditions:
for b in agent.bound_conditions:
if b.is_bound:
rule_str_list.append(_n(b.agent.name))
else:
rule_str_list.append('n' + _n(b.agent.name))
if agent.location is not None:
rule_str_list.append(_n(agent.location))
if agent.activity is not None:
if agent.activity.is_active:
rule_str_list.append(agent.activity.activity_type[:3])
else:
rule_str_list.append(agent.activity.activity_type[:3] + '_inact')
rule_str = '_'.join(rule_str_list)
return rule_str |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:grounded_monomer_patterns; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:model; 5, identifier:agent; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ignore_activities; 8, False; 9, block; 9, 10; 9, 12; 9, 13; 9, 46; 9, 47; 9, 48; 9, 52; 9, 138; 9, 139; 9, 140; 9, 155; 9, 156; 9, 157; 9, 158; 9, 159; 9, 160; 9, 161; 9, 162; 9, 163; 9, 164; 9, 165; 9, 166; 9, 170; 9, 384; 9, 385; 9, 449; 9, 450; 9, 485; 10, expression_statement; 10, 11; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 22; 14, not_operator; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:agent; 19, attribute; 19, 20; 19, 21; 20, identifier:ist; 21, identifier:Agent; 22, block; 22, 23; 22, 36; 22, 41; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:monomer; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:model; 30, identifier:monomers; 31, identifier:get; 32, argument_list; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:agent; 35, identifier:name; 36, if_statement; 36, 37; 36, 39; 37, not_operator; 37, 38; 38, identifier:monomer; 39, block; 39, 40; 40, return_statement; 41, expression_statement; 41, 42; 42, yield; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:monomer; 45, argument_list; 46, comment; 47, comment; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:monomer; 51, None; 52, for_statement; 52, 53; 52, 54; 52, 57; 53, identifier:ann; 54, attribute; 54, 55; 54, 56; 55, identifier:model; 56, identifier:annotations; 57, block; 57, 58; 57, 62; 57, 71; 57, 82; 57, 93; 57, 103; 57, 104; 57, 105; 57, 106; 58, if_statement; 58, 59; 58, 60; 59, identifier:monomer; 60, block; 60, 61; 61, break_statement; 62, if_statement; 62, 63; 62, 69; 63, not_operator; 63, 64; 64, comparison_operator:==; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:ann; 67, identifier:predicate; 68, string:'is'; 69, block; 69, 70; 70, continue_statement; 71, if_statement; 71, 72; 71, 80; 72, not_operator; 72, 73; 73, call; 73, 74; 73, 75; 74, identifier:isinstance; 75, argument_list; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:ann; 78, identifier:subject; 79, identifier:Monomer; 80, block; 80, 81; 81, continue_statement; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, tuple_pattern; 84, 85; 84, 86; 85, identifier:ns; 86, identifier:id; 87, call; 87, 88; 87, 89; 88, identifier:parse_identifiers_url; 89, argument_list; 89, 90; 90, attribute; 90, 91; 90, 92; 91, identifier:ann; 92, identifier:object; 93, if_statement; 93, 94; 93, 101; 94, boolean_operator:and; 94, 95; 94, 98; 95, comparison_operator:is; 95, 96; 95, 97; 96, identifier:ns; 97, None; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:id; 100, None; 101, block; 101, 102; 102, continue_statement; 103, comment; 104, comment; 105, comment; 106, for_statement; 106, 107; 106, 110; 106, 117; 106, 118; 106, 119; 106, 120; 106, 121; 107, pattern_list; 107, 108; 107, 109; 108, identifier:db_ns; 109, identifier:db_id; 110, call; 110, 111; 110, 116; 111, attribute; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:agent; 114, identifier:db_refs; 115, identifier:items; 116, argument_list; 117, comment; 118, comment; 119, comment; 120, comment; 121, block; 121, 122; 122, if_statement; 122, 123; 122, 130; 123, boolean_operator:and; 123, 124; 123, 127; 124, comparison_operator:==; 124, 125; 124, 126; 125, identifier:db_ns; 126, identifier:ns; 127, comparison_operator:==; 127, 128; 127, 129; 128, identifier:db_id; 129, identifier:id; 130, block; 130, 131; 130, 137; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:monomer; 134, attribute; 134, 135; 134, 136; 135, identifier:ann; 136, identifier:subject; 137, break_statement; 138, comment; 139, comment; 140, if_statement; 140, 141; 140, 144; 141, comparison_operator:is; 141, 142; 141, 143; 142, identifier:monomer; 143, None; 144, block; 144, 145; 144, 154; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:logger; 149, identifier:info; 150, argument_list; 150, 151; 151, binary_operator:%; 151, 152; 151, 153; 152, string:'No monomer found corresponding to agent %s'; 153, identifier:agent; 154, return_statement; 155, comment; 156, comment; 157, comment; 158, comment; 159, comment; 160, comment; 161, comment; 162, comment; 163, comment; 164, comment; 165, comment; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:sc_list; 169, list:[]; 170, for_statement; 170, 171; 170, 172; 170, 175; 170, 176; 170, 177; 170, 178; 170, 179; 171, identifier:mod; 172, attribute; 172, 173; 172, 174; 173, identifier:agent; 174, identifier:mods; 175, comment; 176, comment; 177, comment; 178, comment; 179, block; 179, 180; 179, 184; 179, 191; 179, 198; 179, 300; 179, 301; 179, 312; 179, 328; 179, 344; 179, 345; 179, 346; 179, 347; 179, 352; 179, 353; 179, 354; 179, 355; 179, 359; 179, 377; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:mod_sites; 183, dictionary; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:res_sites; 187, call; 187, 188; 187, 189; 188, identifier:set; 189, argument_list; 189, 190; 190, list:[]; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:pos_sites; 194, call; 194, 195; 194, 196; 195, identifier:set; 196, argument_list; 196, 197; 197, list:[]; 198, for_statement; 198, 199; 198, 200; 198, 203; 198, 204; 199, identifier:ann; 200, attribute; 200, 201; 200, 202; 201, identifier:monomer; 202, identifier:site_annotations; 203, comment; 204, block; 204, 205; 205, if_statement; 205, 206; 205, 220; 205, 250; 205, 275; 206, boolean_operator:and; 206, 207; 206, 212; 206, 213; 207, comparison_operator:==; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:ann; 210, identifier:predicate; 211, string:'is_modification'; 212, line_continuation:\; 213, comparison_operator:==; 213, 214; 213, 217; 214, attribute; 214, 215; 214, 216; 215, identifier:ann; 216, identifier:object; 217, attribute; 217, 218; 217, 219; 218, identifier:mod; 219, identifier:mod_type; 220, block; 220, 221; 220, 227; 220, 233; 220, 240; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:site_state; 224, attribute; 224, 225; 224, 226; 225, identifier:ann; 226, identifier:subject; 227, assert_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:isinstance; 230, argument_list; 230, 231; 230, 232; 231, identifier:site_state; 232, identifier:tuple; 233, assert_statement; 233, 234; 234, comparison_operator:==; 234, 235; 234, 239; 235, call; 235, 236; 235, 237; 236, identifier:len; 237, argument_list; 237, 238; 238, identifier:site_state; 239, integer:2; 240, expression_statement; 240, 241; 241, assignment; 241, 242; 241, 247; 242, subscript; 242, 243; 242, 244; 243, identifier:mod_sites; 244, subscript; 244, 245; 244, 246; 245, identifier:site_state; 246, integer:0; 247, subscript; 247, 248; 247, 249; 248, identifier:site_state; 249, integer:1; 250, elif_clause; 250, 251; 250, 265; 251, boolean_operator:and; 251, 252; 251, 257; 251, 258; 252, comparison_operator:==; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:ann; 255, identifier:predicate; 256, string:'is_residue'; 257, line_continuation:\; 258, comparison_operator:==; 258, 259; 258, 262; 259, attribute; 259, 260; 259, 261; 260, identifier:ann; 261, identifier:object; 262, attribute; 262, 263; 262, 264; 263, identifier:mod; 264, identifier:residue; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:res_sites; 270, identifier:add; 271, argument_list; 271, 272; 272, attribute; 272, 273; 272, 274; 273, identifier:ann; 274, identifier:subject; 275, elif_clause; 275, 276; 275, 290; 276, boolean_operator:and; 276, 277; 276, 282; 276, 283; 277, comparison_operator:==; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:ann; 280, identifier:predicate; 281, string:'is_position'; 282, line_continuation:\; 283, comparison_operator:==; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:ann; 286, identifier:object; 287, attribute; 287, 288; 287, 289; 288, identifier:mod; 289, identifier:position; 290, block; 290, 291; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:pos_sites; 295, identifier:add; 296, argument_list; 296, 297; 297, attribute; 297, 298; 297, 299; 298, identifier:ann; 299, identifier:subject; 300, comment; 301, expression_statement; 301, 302; 302, assignment; 302, 303; 302, 304; 303, identifier:viable_sites; 304, call; 304, 305; 304, 306; 305, identifier:set; 306, argument_list; 306, 307; 307, call; 307, 308; 307, 311; 308, attribute; 308, 309; 308, 310; 309, identifier:mod_sites; 310, identifier:keys; 311, argument_list; 312, if_statement; 312, 313; 312, 318; 313, comparison_operator:is; 313, 314; 313, 317; 314, attribute; 314, 315; 314, 316; 315, identifier:mod; 316, identifier:residue; 317, None; 318, block; 318, 319; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:viable_sites; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:viable_sites; 325, identifier:intersection; 326, argument_list; 326, 327; 327, identifier:res_sites; 328, if_statement; 328, 329; 328, 334; 329, comparison_operator:is; 329, 330; 329, 333; 330, attribute; 330, 331; 330, 332; 331, identifier:mod; 332, identifier:position; 333, None; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, assignment; 336, 337; 336, 338; 337, identifier:viable_sites; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:viable_sites; 341, identifier:intersection; 342, argument_list; 342, 343; 343, identifier:pos_sites; 344, comment; 345, comment; 346, comment; 347, if_statement; 347, 348; 347, 350; 348, not_operator; 348, 349; 349, identifier:viable_sites; 350, block; 350, 351; 351, return_statement; 352, comment; 353, comment; 354, comment; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:pattern_list; 358, list:[]; 359, for_statement; 359, 360; 359, 361; 359, 362; 360, identifier:site_name; 361, identifier:viable_sites; 362, block; 362, 363; 363, expression_statement; 363, 364; 364, call; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:pattern_list; 367, identifier:append; 368, argument_list; 368, 369; 369, dictionary; 369, 370; 370, pair; 370, 371; 370, 372; 371, identifier:site_name; 372, tuple; 372, 373; 372, 376; 373, subscript; 373, 374; 373, 375; 374, identifier:mod_sites; 375, identifier:site_name; 376, identifier:WILD; 377, expression_statement; 377, 378; 378, call; 378, 379; 378, 382; 379, attribute; 379, 380; 379, 381; 380, identifier:sc_list; 381, identifier:append; 382, argument_list; 382, 383; 383, identifier:pattern_list; 384, comment; 385, if_statement; 385, 386; 385, 392; 385, 393; 385, 394; 385, 395; 385, 396; 386, boolean_operator:and; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:agent; 389, identifier:activity; 390, not_operator; 390, 391; 391, identifier:ignore_activities; 392, comment; 393, comment; 394, comment; 395, comment; 396, block; 396, 397; 396, 409; 396, 413; 396, 442; 397, expression_statement; 397, 398; 398, assignment; 398, 399; 398, 400; 399, identifier:rel_type; 400, conditional_expression:if; 400, 401; 400, 402; 400, 407; 400, 408; 401, string:'has_active_pattern'; 402, attribute; 402, 403; 402, 406; 403, attribute; 403, 404; 403, 405; 404, identifier:agent; 405, identifier:activity; 406, identifier:is_active; 407, line_continuation:\; 408, string:'has_inactive_pattern'; 409, expression_statement; 409, 410; 410, assignment; 410, 411; 410, 412; 411, identifier:active_form_list; 412, list:[]; 413, for_statement; 413, 414; 413, 415; 413, 418; 414, identifier:ann; 415, attribute; 415, 416; 415, 417; 416, identifier:model; 417, identifier:annotations; 418, block; 418, 419; 419, if_statement; 419, 420; 419, 431; 419, 432; 420, boolean_operator:and; 420, 421; 420, 426; 421, comparison_operator:==; 421, 422; 421, 425; 422, attribute; 422, 423; 422, 424; 423, identifier:ann; 424, identifier:subject; 425, identifier:monomer; 426, comparison_operator:==; 426, 427; 426, 430; 427, attribute; 427, 428; 427, 429; 428, identifier:ann; 429, identifier:predicate; 430, identifier:rel_type; 431, comment; 432, block; 432, 433; 433, expression_statement; 433, 434; 434, call; 434, 435; 434, 438; 435, attribute; 435, 436; 435, 437; 436, identifier:active_form_list; 437, identifier:append; 438, argument_list; 438, 439; 439, attribute; 439, 440; 439, 441; 440, identifier:ann; 441, identifier:object; 442, expression_statement; 442, 443; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, identifier:sc_list; 446, identifier:append; 447, argument_list; 447, 448; 448, identifier:active_form_list; 449, comment; 450, for_statement; 450, 451; 450, 452; 450, 459; 451, identifier:pattern_combo; 452, call; 452, 453; 452, 456; 453, attribute; 453, 454; 453, 455; 454, identifier:itertools; 455, identifier:product; 456, argument_list; 456, 457; 457, list_splat; 457, 458; 458, identifier:sc_list; 459, block; 459, 460; 459, 464; 459, 475; 460, expression_statement; 460, 461; 461, assignment; 461, 462; 461, 463; 462, identifier:mp_sc; 463, dictionary; 464, for_statement; 464, 465; 464, 466; 464, 467; 465, identifier:pattern; 466, identifier:pattern_combo; 467, block; 467, 468; 468, expression_statement; 468, 469; 469, call; 469, 470; 469, 473; 470, attribute; 470, 471; 470, 472; 471, identifier:mp_sc; 472, identifier:update; 473, argument_list; 473, 474; 474, identifier:pattern; 475, if_statement; 475, 476; 475, 477; 476, identifier:mp_sc; 477, block; 477, 478; 478, expression_statement; 478, 479; 479, yield; 479, 480; 480, call; 480, 481; 480, 482; 481, identifier:monomer; 482, argument_list; 482, 483; 483, dictionary_splat; 483, 484; 484, identifier:mp_sc; 485, if_statement; 485, 486; 485, 488; 486, not_operator; 486, 487; 487, identifier:sc_list; 488, block; 488, 489; 489, expression_statement; 489, 490; 490, yield; 490, 491; 491, call; 491, 492; 491, 493; 492, identifier:monomer; 493, argument_list | def grounded_monomer_patterns(model, agent, ignore_activities=False):
"""Get monomer patterns for the agent accounting for grounding information.
Parameters
----------
model : pysb.core.Model
The model to search for MonomerPatterns matching the given Agent.
agent : indra.statements.Agent
The Agent to find matching MonomerPatterns for.
ignore_activites : bool
Whether to ignore any ActivityConditions on the agent when determining
the required site conditions for the MonomerPattern. For example, if
set to True, will find a match for the agent `MAPK1(activity=kinase)`
even if the corresponding MAPK1 Monomer in the model has no site
named `kinase`. Default is False (more stringent matching).
Returns
-------
generator of MonomerPatterns
"""
# If it's not a molecular agent
if not isinstance(agent, ist.Agent):
monomer = model.monomers.get(agent.name)
if not monomer:
return
yield monomer()
# Iterate over all model annotations to identify the monomer associated
# with this agent
monomer = None
for ann in model.annotations:
if monomer:
break
if not ann.predicate == 'is':
continue
if not isinstance(ann.subject, Monomer):
continue
(ns, id) = parse_identifiers_url(ann.object)
if ns is None and id is None:
continue
# We now have an identifiers.org namespace/ID for a given monomer;
# we check to see if there is a matching identifier in the db_refs
# for this agent
for db_ns, db_id in agent.db_refs.items():
# We've found a match! Return first match
# FIXME Could also update this to check for alternative
# FIXME matches, or make sure that all grounding IDs match,
# FIXME etc.
if db_ns == ns and db_id == id:
monomer = ann.subject
break
# We looked at all the annotations in the model and didn't find a
# match
if monomer is None:
logger.info('No monomer found corresponding to agent %s' % agent)
return
# Now that we have a monomer for the agent, look for site/state
# combinations corresponding to the state of the agent. For every one of
# the modifications specified in the agent signature, check to see if it
# can be satisfied based on the agent's annotations. For every one we find
# that is consistent, we yield it--there may be more than one.
# FIXME
# Create a list of tuples, each one representing the site conditions
# that can satisfy a particular agent condition. Each entry in the list
# will contain a list of dicts associated with a particular mod/activity
# condition. Each dict will represent a site/state combination satisfying
# the constraints imposed by that mod/activity condition.
sc_list = []
for mod in agent.mods:
# Find all site/state combinations that have the appropriate
# modification type
# As we iterate, build up a dict identifying the annotations of
# particular sites
mod_sites = {}
res_sites = set([])
pos_sites = set([])
for ann in monomer.site_annotations:
# Don't forget to handle Nones!
if ann.predicate == 'is_modification' and \
ann.object == mod.mod_type:
site_state = ann.subject
assert isinstance(site_state, tuple)
assert len(site_state) == 2
mod_sites[site_state[0]] = site_state[1]
elif ann.predicate == 'is_residue' and \
ann.object == mod.residue:
res_sites.add(ann.subject)
elif ann.predicate == 'is_position' and \
ann.object == mod.position:
pos_sites.add(ann.subject)
# If the residue field of the agent is specified,
viable_sites = set(mod_sites.keys())
if mod.residue is not None:
viable_sites = viable_sites.intersection(res_sites)
if mod.position is not None:
viable_sites = viable_sites.intersection(pos_sites)
# If there are no viable sites annotated in the model matching the
# available info in the mod condition, then we won't be able to
# satisfy the conditions on this agent
if not viable_sites:
return
# Otherwise, update the
# If there are any sites left after we subject them to residue
# and position constraints, then return the relevant monomer patterns!
pattern_list = []
for site_name in viable_sites:
pattern_list.append({site_name: (mod_sites[site_name], WILD)})
sc_list.append(pattern_list)
# Now check for monomer patterns satisfying the agent's activity condition
if agent.activity and not ignore_activities:
# Iterate through annotations with this monomer as the subject
# and a has_active_pattern or has_inactive_pattern relationship
# FIXME: Currently activity type is not annotated/checked
# FIXME act_type = agent.activity.activity_type
rel_type = 'has_active_pattern' if agent.activity.is_active \
else 'has_inactive_pattern'
active_form_list = []
for ann in model.annotations:
if ann.subject == monomer and ann.predicate == rel_type:
# The annotation object contains the active/inactive pattern
active_form_list.append(ann.object)
sc_list.append(active_form_list)
# Now that we've got a list of conditions
for pattern_combo in itertools.product(*sc_list):
mp_sc = {}
for pattern in pattern_combo:
mp_sc.update(pattern)
if mp_sc:
yield monomer(**mp_sc)
if not sc_list:
yield monomer() |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_site_pattern; 3, parameters; 3, 4; 4, identifier:agent; 5, block; 5, 6; 5, 8; 5, 20; 5, 24; 5, 25; 5, 62; 5, 63; 5, 147; 5, 148; 5, 203; 5, 204; 5, 222; 5, 223; 5, 261; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 17; 9, not_operator; 9, 10; 10, call; 10, 11; 10, 12; 11, identifier:isinstance; 12, argument_list; 12, 13; 12, 14; 13, identifier:agent; 14, attribute; 14, 15; 14, 16; 15, identifier:ist; 16, identifier:Agent; 17, block; 17, 18; 18, return_statement; 18, 19; 19, dictionary; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:pattern; 23, dictionary; 24, comment; 25, for_statement; 25, 26; 25, 27; 25, 30; 25, 31; 25, 32; 26, identifier:bc; 27, attribute; 27, 28; 27, 29; 28, identifier:agent; 29, identifier:bound_conditions; 30, comment; 31, comment; 32, block; 32, 33; 33, if_statement; 33, 34; 33, 37; 33, 49; 34, attribute; 34, 35; 34, 36; 35, identifier:bc; 36, identifier:is_bound; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 48; 40, subscript; 40, 41; 40, 42; 41, identifier:pattern; 42, call; 42, 43; 42, 44; 43, identifier:get_binding_site_name; 44, argument_list; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:bc; 47, identifier:agent; 48, identifier:ANY; 49, else_clause; 49, 50; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 61; 53, subscript; 53, 54; 53, 55; 54, identifier:pattern; 55, call; 55, 56; 55, 57; 56, identifier:get_binding_site_name; 57, argument_list; 57, 58; 58, attribute; 58, 59; 58, 60; 59, identifier:bc; 60, identifier:agent; 61, None; 62, comment; 63, for_statement; 63, 64; 63, 65; 63, 68; 64, identifier:mod; 65, attribute; 65, 66; 65, 67; 66, identifier:agent; 67, identifier:mods; 68, block; 68, 69; 68, 77; 68, 90; 68, 103; 68, 112; 68, 120; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:mod_site_str; 72, subscript; 72, 73; 72, 74; 73, identifier:abbrevs; 74, attribute; 74, 75; 74, 76; 75, identifier:mod; 76, identifier:mod_type; 77, if_statement; 77, 78; 77, 83; 78, comparison_operator:is; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:mod; 81, identifier:residue; 82, None; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:mod_site_str; 87, attribute; 87, 88; 87, 89; 88, identifier:mod; 89, identifier:residue; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:mod_pos_str; 93, conditional_expression:if; 93, 94; 93, 97; 93, 102; 94, attribute; 94, 95; 94, 96; 95, identifier:mod; 96, identifier:position; 97, comparison_operator:is; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:mod; 100, identifier:position; 101, None; 102, string:''; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:mod_site; 106, parenthesized_expression; 106, 107; 107, binary_operator:%; 107, 108; 107, 109; 108, string:'%s%s'; 109, tuple; 109, 110; 109, 111; 110, identifier:mod_site_str; 111, identifier:mod_pos_str; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:site_states; 115, subscript; 115, 116; 115, 117; 116, identifier:states; 117, attribute; 117, 118; 117, 119; 118, identifier:mod; 119, identifier:mod_type; 120, if_statement; 120, 121; 120, 124; 120, 135; 121, attribute; 121, 122; 121, 123; 122, identifier:mod; 123, identifier:is_modified; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, subscript; 127, 128; 127, 129; 128, identifier:pattern; 129, identifier:mod_site; 130, tuple; 130, 131; 130, 134; 131, subscript; 131, 132; 131, 133; 132, identifier:site_states; 133, integer:1; 134, identifier:WILD; 135, else_clause; 135, 136; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 142; 139, subscript; 139, 140; 139, 141; 140, identifier:pattern; 141, identifier:mod_site; 142, tuple; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:site_states; 145, integer:0; 146, identifier:WILD; 147, comment; 148, for_statement; 148, 149; 148, 150; 148, 153; 149, identifier:mc; 150, attribute; 150, 151; 150, 152; 151, identifier:agent; 152, identifier:mutations; 153, block; 153, 154; 153, 165; 153, 176; 153, 197; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:res_from; 157, conditional_expression:if; 157, 158; 157, 161; 157, 164; 158, attribute; 158, 159; 158, 160; 159, identifier:mc; 160, identifier:residue_from; 161, attribute; 161, 162; 161, 163; 162, identifier:mc; 163, identifier:residue_from; 164, string:'mut'; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:res_to; 168, conditional_expression:if; 168, 169; 168, 172; 168, 175; 169, attribute; 169, 170; 169, 171; 170, identifier:mc; 171, identifier:residue_to; 172, attribute; 172, 173; 172, 174; 173, identifier:mc; 174, identifier:residue_to; 175, string:'X'; 176, if_statement; 176, 177; 176, 182; 176, 187; 177, comparison_operator:is; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:mc; 180, identifier:position; 181, None; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:mut_site_name; 186, identifier:res_from; 187, else_clause; 187, 188; 188, block; 188, 189; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:mut_site_name; 192, binary_operator:+; 192, 193; 192, 194; 193, identifier:res_from; 194, attribute; 194, 195; 194, 196; 195, identifier:mc; 196, identifier:position; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 202; 199, subscript; 199, 200; 199, 201; 200, identifier:pattern; 201, identifier:mut_site_name; 202, identifier:res_to; 203, comment; 204, if_statement; 204, 205; 204, 210; 205, comparison_operator:is; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:agent; 208, identifier:location; 209, None; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 216; 213, subscript; 213, 214; 213, 215; 214, identifier:pattern; 215, string:'loc'; 216, call; 216, 217; 216, 218; 217, identifier:_n; 218, argument_list; 218, 219; 219, attribute; 219, 220; 219, 221; 220, identifier:agent; 221, identifier:location; 222, comment; 223, if_statement; 223, 224; 223, 229; 224, comparison_operator:is; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:agent; 227, identifier:activity; 228, None; 229, block; 229, 230; 229, 238; 229, 255; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:active_site_name; 233, attribute; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:agent; 236, identifier:activity; 237, identifier:activity_type; 238, if_statement; 238, 239; 238, 244; 238, 249; 239, attribute; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:agent; 242, identifier:activity; 243, identifier:is_active; 244, block; 244, 245; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:active_site_state; 248, string:'active'; 249, else_clause; 249, 250; 250, block; 250, 251; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:active_site_state; 254, string:'inactive'; 255, expression_statement; 255, 256; 256, assignment; 256, 257; 256, 260; 257, subscript; 257, 258; 257, 259; 258, identifier:pattern; 259, identifier:active_site_name; 260, identifier:active_site_state; 261, return_statement; 261, 262; 262, identifier:pattern | def get_site_pattern(agent):
"""Construct a dictionary of Monomer site states from an Agent.
This crates the mapping to the associated PySB monomer from an
INDRA Agent object."""
if not isinstance(agent, ist.Agent):
return {}
pattern = {}
# Handle bound conditions
for bc in agent.bound_conditions:
# Here we make the assumption that the binding site
# is simply named after the binding partner
if bc.is_bound:
pattern[get_binding_site_name(bc.agent)] = ANY
else:
pattern[get_binding_site_name(bc.agent)] = None
# Handle modifications
for mod in agent.mods:
mod_site_str = abbrevs[mod.mod_type]
if mod.residue is not None:
mod_site_str = mod.residue
mod_pos_str = mod.position if mod.position is not None else ''
mod_site = ('%s%s' % (mod_site_str, mod_pos_str))
site_states = states[mod.mod_type]
if mod.is_modified:
pattern[mod_site] = (site_states[1], WILD)
else:
pattern[mod_site] = (site_states[0], WILD)
# Handle mutations
for mc in agent.mutations:
res_from = mc.residue_from if mc.residue_from else 'mut'
res_to = mc.residue_to if mc.residue_to else 'X'
if mc.position is None:
mut_site_name = res_from
else:
mut_site_name = res_from + mc.position
pattern[mut_site_name] = res_to
# Handle location
if agent.location is not None:
pattern['loc'] = _n(agent.location)
# Handle activity
if agent.activity is not None:
active_site_name = agent.activity.activity_type
if agent.activity.is_active:
active_site_state = 'active'
else:
active_site_state = 'inactive'
pattern[active_site_name] = active_site_state
return pattern |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:make_model; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:policies; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:initial_conditions; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:reverse_effects; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:model_name; 16, string:'indra_model'; 17, block; 17, 18; 17, 20; 17, 29; 17, 40; 17, 46; 17, 55; 17, 63; 17, 71; 17, 79; 17, 87; 17, 88; 17, 89; 17, 95; 17, 96; 17, 265; 17, 266; 17, 272; 17, 273; 17, 282; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:ppa; 23, call; 23, 24; 23, 25; 24, identifier:PysbPreassembler; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:statements; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:processed_policies; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:process_policies; 38, argument_list; 38, 39; 39, identifier:policies; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:ppa; 44, identifier:replace_activities; 45, argument_list; 46, if_statement; 46, 47; 46, 48; 47, identifier:reverse_effects; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:ppa; 53, identifier:add_reverse_effects; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:statements; 60, attribute; 60, 61; 60, 62; 61, identifier:ppa; 62, identifier:statements; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:model; 68, call; 68, 69; 68, 70; 69, identifier:Model; 70, argument_list; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 78; 73, attribute; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:model; 77, identifier:name; 78, identifier:model_name; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:agent_set; 84, call; 84, 85; 84, 86; 85, identifier:BaseAgentSet; 86, argument_list; 87, comment; 88, comment; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:self; 93, identifier:_monomers; 94, argument_list; 95, comment; 96, for_statement; 96, 97; 96, 100; 96, 107; 97, pattern_list; 97, 98; 97, 99; 98, identifier:agent_name; 99, identifier:agent; 100, call; 100, 101; 100, 106; 101, attribute; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:agent_set; 105, identifier:items; 106, argument_list; 107, block; 107, 108; 107, 124; 107, 132; 107, 141; 107, 176; 107, 177; 107, 197; 107, 217; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:m; 111, call; 111, 112; 111, 113; 112, identifier:Monomer; 113, argument_list; 113, 114; 113, 118; 113, 121; 114, call; 114, 115; 114, 116; 115, identifier:_n; 116, argument_list; 116, 117; 117, identifier:agent_name; 118, attribute; 118, 119; 118, 120; 119, identifier:agent; 120, identifier:sites; 121, attribute; 121, 122; 121, 123; 122, identifier:agent; 123, identifier:site_states; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:m; 128, identifier:site_annotations; 129, attribute; 129, 130; 129, 131; 130, identifier:agent; 131, identifier:site_annotations; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 139; 134, attribute; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:self; 137, identifier:model; 138, identifier:add_component; 139, argument_list; 139, 140; 140, identifier:m; 141, for_statement; 141, 142; 141, 145; 141, 152; 142, pattern_list; 142, 143; 142, 144; 143, identifier:db_name; 144, identifier:db_ref; 145, call; 145, 146; 145, 151; 146, attribute; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:agent; 149, identifier:db_refs; 150, identifier:items; 151, argument_list; 152, block; 152, 153; 152, 162; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:a; 156, call; 156, 157; 156, 158; 157, identifier:get_annotation; 158, argument_list; 158, 159; 158, 160; 158, 161; 159, identifier:m; 160, identifier:db_name; 161, identifier:db_ref; 162, if_statement; 162, 163; 162, 166; 163, comparison_operator:is; 163, 164; 163, 165; 164, identifier:a; 165, None; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 174; 169, attribute; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:self; 172, identifier:model; 173, identifier:add_annotation; 174, argument_list; 174, 175; 175, identifier:a; 176, comment; 177, for_statement; 177, 178; 177, 179; 177, 182; 178, identifier:af; 179, attribute; 179, 180; 179, 181; 180, identifier:agent; 181, identifier:active_forms; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, call; 184, 185; 184, 190; 185, attribute; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:self; 188, identifier:model; 189, identifier:add_annotation; 190, argument_list; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:Annotation; 193, argument_list; 193, 194; 193, 195; 193, 196; 194, identifier:m; 195, identifier:af; 196, string:'has_active_pattern'; 197, for_statement; 197, 198; 197, 199; 197, 202; 198, identifier:iaf; 199, attribute; 199, 200; 199, 201; 200, identifier:agent; 201, identifier:inactive_forms; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, call; 204, 205; 204, 210; 205, attribute; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:model; 209, identifier:add_annotation; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:Annotation; 213, argument_list; 213, 214; 213, 215; 213, 216; 214, identifier:m; 215, identifier:iaf; 216, string:'has_inactive_pattern'; 217, for_statement; 217, 218; 217, 219; 217, 222; 218, identifier:at; 219, attribute; 219, 220; 219, 221; 220, identifier:agent; 221, identifier:activity_types; 222, block; 222, 223; 222, 230; 222, 244; 222, 251; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 226; 225, identifier:act_site_cond; 226, dictionary; 226, 227; 227, pair; 227, 228; 227, 229; 228, identifier:at; 229, string:'active'; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:model; 236, identifier:add_annotation; 237, argument_list; 237, 238; 238, call; 238, 239; 238, 240; 239, identifier:Annotation; 240, argument_list; 240, 241; 240, 242; 240, 243; 241, identifier:m; 242, identifier:act_site_cond; 243, string:'has_active_pattern'; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:inact_site_cond; 247, dictionary; 247, 248; 248, pair; 248, 249; 248, 250; 249, identifier:at; 250, string:'inactive'; 251, expression_statement; 251, 252; 252, call; 252, 253; 252, 258; 253, attribute; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:self; 256, identifier:model; 257, identifier:add_annotation; 258, argument_list; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:Annotation; 261, argument_list; 261, 262; 261, 263; 261, 264; 262, identifier:m; 263, identifier:inact_site_cond; 264, string:'has_inactive_pattern'; 265, comment; 266, expression_statement; 266, 267; 267, call; 267, 268; 267, 271; 268, attribute; 268, 269; 268, 270; 269, identifier:self; 270, identifier:_assemble; 271, argument_list; 272, comment; 273, if_statement; 273, 274; 273, 275; 274, identifier:initial_conditions; 275, block; 275, 276; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:self; 280, identifier:add_default_initial_conditions; 281, argument_list; 282, return_statement; 282, 283; 283, attribute; 283, 284; 283, 285; 284, identifier:self; 285, identifier:model | def make_model(self, policies=None, initial_conditions=True,
reverse_effects=False, model_name='indra_model'):
"""Assemble the PySB model from the collected INDRA Statements.
This method assembles a PySB model from the set of INDRA Statements.
The assembled model is both returned and set as the assembler's
model argument.
Parameters
----------
policies : Optional[Union[str, dict]]
A string or dictionary that defines one or more assembly policies.
If policies is a string, it defines a global assembly policy
that applies to all Statement types.
Example: one_step, interactions_only
A dictionary of policies has keys corresponding to Statement types
and values to the policy to be applied to that type of Statement.
For Statement types whose policy is undefined, the 'default'
policy is applied.
Example: {'Phosphorylation': 'two_step'}
initial_conditions : Optional[bool]
If True, default initial conditions are generated for the
Monomers in the model. Default: True
reverse_effects : Optional[bool]
If True, reverse rules are added to the model for activity,
modification and amount regulations that have no corresponding
reverse effects. Default: False
model_name : Optional[str]
The name attribute assigned to the PySB Model object.
Default: "indra_model"
Returns
-------
model : pysb.Model
The assembled PySB model object.
"""
ppa = PysbPreassembler(self.statements)
self.processed_policies = self.process_policies(policies)
ppa.replace_activities()
if reverse_effects:
ppa.add_reverse_effects()
self.statements = ppa.statements
self.model = Model()
self.model.name = model_name
self.agent_set = BaseAgentSet()
# Collect information about the monomers/self.agent_set from the
# statements
self._monomers()
# Add the monomers to the model based on our BaseAgentSet
for agent_name, agent in self.agent_set.items():
m = Monomer(_n(agent_name), agent.sites, agent.site_states)
m.site_annotations = agent.site_annotations
self.model.add_component(m)
for db_name, db_ref in agent.db_refs.items():
a = get_annotation(m, db_name, db_ref)
if a is not None:
self.model.add_annotation(a)
# Iterate over the active_forms
for af in agent.active_forms:
self.model.add_annotation(Annotation(m, af,
'has_active_pattern'))
for iaf in agent.inactive_forms:
self.model.add_annotation(Annotation(m, iaf,
'has_inactive_pattern'))
for at in agent.activity_types:
act_site_cond = {at: 'active'}
self.model.add_annotation(Annotation(m, act_site_cond,
'has_active_pattern'))
inact_site_cond = {at: 'inactive'}
self.model.add_annotation(Annotation(m, inact_site_cond,
'has_inactive_pattern'))
# Iterate over the statements to generate rules
self._assemble()
# Add initial conditions
if initial_conditions:
self.add_default_initial_conditions()
return self.model |
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_modifications; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 9; 5, 13; 5, 24; 5, 30; 5, 31; 6, expression_statement; 6, 7; 7, comment; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:qstr; 12, string:"$.events.frames[(@.type is 'protein-modification')]"; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:res; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:tree; 21, identifier:execute; 22, argument_list; 22, 23; 23, identifier:qstr; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:res; 27, None; 28, block; 28, 29; 29, return_statement; 30, comment; 31, for_statement; 31, 32; 31, 33; 31, 34; 31, 35; 32, identifier:r; 33, identifier:res; 34, comment; 35, block; 35, 36; 35, 45; 35, 46; 35, 55; 35, 64; 35, 75; 35, 81; 35, 87; 35, 91; 35, 95; 35, 96; 35, 97; 35, 133; 35, 144; 35, 167; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:modification_type; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:r; 42, identifier:get; 43, argument_list; 43, 44; 44, string:'subtype'; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:epistemics; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:_get_epistemics; 53, argument_list; 53, 54; 54, identifier:r; 55, if_statement; 55, 56; 55, 62; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:epistemics; 59, identifier:get; 60, argument_list; 60, 61; 61, string:'negated'; 62, block; 62, 63; 63, continue_statement; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, pattern_list; 66, 67; 66, 68; 67, identifier:annotations; 68, identifier:context; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:_get_annot_context; 73, argument_list; 73, 74; 74, identifier:r; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:frame_id; 78, subscript; 78, 79; 78, 80; 79, identifier:r; 80, string:'frame_id'; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:args; 84, subscript; 84, 85; 84, 86; 85, identifier:r; 86, string:'arguments'; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:site; 90, None; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:theme; 94, None; 95, comment; 96, comment; 97, for_statement; 97, 98; 97, 99; 97, 100; 98, identifier:a; 99, identifier:args; 100, block; 100, 101; 101, if_statement; 101, 102; 101, 110; 101, 117; 102, comparison_operator:==; 102, 103; 102, 109; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:self; 106, identifier:_get_arg_type; 107, argument_list; 107, 108; 108, identifier:a; 109, string:'theme'; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:theme; 114, subscript; 114, 115; 114, 116; 115, identifier:a; 116, string:'arg'; 117, elif_clause; 117, 118; 117, 126; 118, comparison_operator:==; 118, 119; 118, 125; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:self; 122, identifier:_get_arg_type; 123, argument_list; 123, 124; 124, identifier:a; 125, string:'site'; 126, block; 126, 127; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:site; 130, subscript; 130, 131; 130, 132; 131, identifier:a; 132, string:'text'; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 138; 135, pattern_list; 135, 136; 135, 137; 136, identifier:theme_agent; 137, identifier:theme_coords; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:_get_agent_from_entity; 142, argument_list; 142, 143; 143, identifier:theme; 144, if_statement; 144, 145; 144, 148; 144, 158; 145, comparison_operator:is; 145, 146; 145, 147; 146, identifier:site; 147, None; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:mods; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:self; 155, identifier:_parse_site_text; 156, argument_list; 156, 157; 157, identifier:site; 158, else_clause; 158, 159; 159, block; 159, 160; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:mods; 163, list:[(None, None)]; 163, 164; 164, tuple; 164, 165; 164, 166; 165, None; 166, None; 167, for_statement; 167, 168; 167, 169; 167, 170; 167, 171; 168, identifier:mod; 169, identifier:mods; 170, comment; 171, block; 171, 172; 171, 178; 171, 179; 171, 180; 171, 188; 171, 199; 171, 206; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 177; 174, pattern_list; 174, 175; 174, 176; 175, identifier:residue; 176, identifier:pos; 177, identifier:mod; 178, comment; 179, comment; 180, expression_statement; 180, 181; 181, assignment; 181, 182; 181, 183; 182, identifier:qstr; 183, binary_operator:+; 183, 184; 183, 185; 184, string:"$.events.frames[(@.type is 'regulation') and "; 185, binary_operator:%; 185, 186; 185, 187; 186, string:"(@.arguments[0].arg is '%s')]"; 187, identifier:frame_id; 188, expression_statement; 188, 189; 189, assignment; 189, 190; 189, 191; 190, identifier:reg_res; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:tree; 196, identifier:execute; 197, argument_list; 197, 198; 198, identifier:qstr; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:reg_res; 202, call; 202, 203; 202, 204; 203, identifier:list; 204, argument_list; 204, 205; 205, identifier:reg_res; 206, for_statement; 206, 207; 206, 208; 206, 209; 207, identifier:reg; 208, identifier:reg_res; 209, block; 209, 210; 209, 218; 209, 261; 209, 262; 209, 263; 209, 264; 209, 265; 209, 274; 209, 303; 209, 309; 209, 319; 209, 345; 209, 354; 209, 355; 209, 364; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, pattern_list; 212, 213; 212, 214; 213, identifier:controller_agent; 214, identifier:controller_coords; 215, expression_list; 215, 216; 215, 217; 216, None; 217, None; 218, for_statement; 218, 219; 218, 220; 218, 223; 219, identifier:a; 220, subscript; 220, 221; 220, 222; 221, identifier:reg; 222, string:'arguments'; 223, block; 223, 224; 224, if_statement; 224, 225; 224, 233; 225, comparison_operator:==; 225, 226; 225, 232; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:_get_arg_type; 230, argument_list; 230, 231; 231, identifier:a; 232, string:'controller'; 233, block; 233, 234; 233, 243; 234, expression_statement; 234, 235; 235, assignment; 235, 236; 235, 237; 236, identifier:controller; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:a; 240, identifier:get; 241, argument_list; 241, 242; 242, string:'arg'; 243, if_statement; 243, 244; 243, 247; 244, comparison_operator:is; 244, 245; 244, 246; 245, identifier:controller; 246, None; 247, block; 247, 248; 247, 260; 248, expression_statement; 248, 249; 249, assignment; 249, 250; 249, 253; 249, 254; 250, pattern_list; 250, 251; 250, 252; 251, identifier:controller_agent; 252, identifier:controller_coords; 253, line_continuation:\; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:self; 257, identifier:_get_agent_from_entity; 258, argument_list; 258, 259; 259, identifier:controller; 260, break_statement; 261, comment; 262, comment; 263, comment; 264, comment; 265, expression_statement; 265, 266; 266, assignment; 266, 267; 266, 268; 267, identifier:reg_subtype; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:reg; 271, identifier:get; 272, argument_list; 272, 273; 273, string:'subtype'; 274, if_statement; 274, 275; 274, 278; 275, comparison_operator:==; 275, 276; 275, 277; 276, identifier:reg_subtype; 277, string:'negative-regulation'; 278, block; 278, 279; 278, 289; 279, expression_statement; 279, 280; 280, assignment; 280, 281; 280, 282; 280, 283; 281, identifier:modification_type; 282, line_continuation:\; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:modtype_to_inverse; 286, identifier:get; 287, argument_list; 287, 288; 288, identifier:modification_type; 289, if_statement; 289, 290; 289, 292; 290, not_operator; 290, 291; 291, identifier:modification_type; 292, block; 292, 293; 292, 302; 293, expression_statement; 293, 294; 294, call; 294, 295; 294, 298; 295, attribute; 295, 296; 295, 297; 296, identifier:logger; 297, identifier:warning; 298, argument_list; 298, 299; 299, binary_operator:%; 299, 300; 299, 301; 300, string:'Unhandled modification type: %s'; 301, identifier:modification_type; 302, continue_statement; 303, expression_statement; 303, 304; 304, assignment; 304, 305; 304, 306; 305, identifier:sentence; 306, subscript; 306, 307; 306, 308; 307, identifier:reg; 308, string:'verbose-text'; 309, expression_statement; 309, 310; 310, assignment; 310, 311; 310, 316; 311, subscript; 311, 312; 311, 315; 312, subscript; 312, 313; 312, 314; 313, identifier:annotations; 314, string:'agents'; 315, string:'coords'; 316, list:[controller_coords,
theme_coords]; 316, 317; 316, 318; 317, identifier:controller_coords; 318, identifier:theme_coords; 319, expression_statement; 319, 320; 320, assignment; 320, 321; 320, 322; 321, identifier:ev; 322, call; 322, 323; 322, 324; 323, identifier:Evidence; 324, argument_list; 324, 325; 324, 328; 324, 331; 324, 334; 324, 339; 324, 342; 325, keyword_argument; 325, 326; 325, 327; 326, identifier:source_api; 327, string:'reach'; 328, keyword_argument; 328, 329; 328, 330; 329, identifier:text; 330, identifier:sentence; 331, keyword_argument; 331, 332; 331, 333; 332, identifier:annotations; 333, identifier:annotations; 334, keyword_argument; 334, 335; 334, 336; 335, identifier:pmid; 336, attribute; 336, 337; 336, 338; 337, identifier:self; 338, identifier:citation; 339, keyword_argument; 339, 340; 339, 341; 340, identifier:context; 341, identifier:context; 342, keyword_argument; 342, 343; 342, 344; 343, identifier:epistemics; 344, identifier:epistemics; 345, expression_statement; 345, 346; 346, assignment; 346, 347; 346, 348; 347, identifier:args; 348, list:[controller_agent, theme_agent, residue, pos, ev]; 348, 349; 348, 350; 348, 351; 348, 352; 348, 353; 349, identifier:controller_agent; 350, identifier:theme_agent; 351, identifier:residue; 352, identifier:pos; 353, identifier:ev; 354, comment; 355, expression_statement; 355, 356; 356, assignment; 356, 357; 356, 358; 357, identifier:ModStmt; 358, call; 358, 359; 358, 362; 359, attribute; 359, 360; 359, 361; 360, identifier:modtype_to_modclass; 361, identifier:get; 362, argument_list; 362, 363; 363, identifier:modification_type; 364, if_statement; 364, 365; 364, 368; 364, 378; 365, comparison_operator:is; 365, 366; 365, 367; 366, identifier:ModStmt; 367, None; 368, block; 368, 369; 369, expression_statement; 369, 370; 370, call; 370, 371; 370, 374; 371, attribute; 371, 372; 371, 373; 372, identifier:logger; 373, identifier:warning; 374, argument_list; 374, 375; 375, binary_operator:%; 375, 376; 375, 377; 376, string:'Unhandled modification type: %s'; 377, identifier:modification_type; 378, else_clause; 378, 379; 378, 380; 378, 381; 379, comment; 380, comment; 381, block; 381, 382; 381, 395; 382, if_statement; 382, 383; 382, 386; 383, comparison_operator:==; 383, 384; 383, 385; 384, identifier:modification_type; 385, string:'autophosphorylation'; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, assignment; 388, 389; 388, 390; 389, identifier:args; 390, list:[theme_agent, residue, pos, ev]; 390, 391; 390, 392; 390, 393; 390, 394; 391, identifier:theme_agent; 392, identifier:residue; 393, identifier:pos; 394, identifier:ev; 395, expression_statement; 395, 396; 396, call; 396, 397; 396, 402; 397, attribute; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:self; 400, identifier:statements; 401, identifier:append; 402, argument_list; 402, 403; 403, call; 403, 404; 403, 405; 404, identifier:ModStmt; 405, argument_list; 405, 406; 406, list_splat; 406, 407; 407, identifier:args | def get_modifications(self):
"""Extract Modification INDRA Statements."""
# Find all event frames that are a type of protein modification
qstr = "$.events.frames[(@.type is 'protein-modification')]"
res = self.tree.execute(qstr)
if res is None:
return
# Extract each of the results when possible
for r in res:
# The subtype of the modification
modification_type = r.get('subtype')
# Skip negated events (i.e. something doesn't happen)
epistemics = self._get_epistemics(r)
if epistemics.get('negated'):
continue
annotations, context = self._get_annot_context(r)
frame_id = r['frame_id']
args = r['arguments']
site = None
theme = None
# Find the substrate (the "theme" agent here) and the
# site and position it is modified on
for a in args:
if self._get_arg_type(a) == 'theme':
theme = a['arg']
elif self._get_arg_type(a) == 'site':
site = a['text']
theme_agent, theme_coords = self._get_agent_from_entity(theme)
if site is not None:
mods = self._parse_site_text(site)
else:
mods = [(None, None)]
for mod in mods:
# Add up to one statement for each site
residue, pos = mod
# Now we need to look for all regulation event to get to the
# enzymes (the "controller" here)
qstr = "$.events.frames[(@.type is 'regulation') and " + \
"(@.arguments[0].arg is '%s')]" % frame_id
reg_res = self.tree.execute(qstr)
reg_res = list(reg_res)
for reg in reg_res:
controller_agent, controller_coords = None, None
for a in reg['arguments']:
if self._get_arg_type(a) == 'controller':
controller = a.get('arg')
if controller is not None:
controller_agent, controller_coords = \
self._get_agent_from_entity(controller)
break
# Check the polarity of the regulation and if negative,
# flip the modification type.
# For instance, negative-regulation of a phosphorylation
# will become an (indirect) dephosphorylation
reg_subtype = reg.get('subtype')
if reg_subtype == 'negative-regulation':
modification_type = \
modtype_to_inverse.get(modification_type)
if not modification_type:
logger.warning('Unhandled modification type: %s' %
modification_type)
continue
sentence = reg['verbose-text']
annotations['agents']['coords'] = [controller_coords,
theme_coords]
ev = Evidence(source_api='reach', text=sentence,
annotations=annotations, pmid=self.citation,
context=context, epistemics=epistemics)
args = [controller_agent, theme_agent, residue, pos, ev]
# Here ModStmt is a sub-class of Modification
ModStmt = modtype_to_modclass.get(modification_type)
if ModStmt is None:
logger.warning('Unhandled modification type: %s' %
modification_type)
else:
# Handle this special case here because only
# enzyme argument is needed
if modification_type == 'autophosphorylation':
args = [theme_agent, residue, pos, ev]
self.statements.append(ModStmt(*args)) |
Subsets and Splits